# a_0 / a_n is a0
# n* is a1
# temp is t0
# four is t1

myCollatz:        
                                # // Init n* with 0
    addi a1, x0, 0              # int n* = 0
    
                                # // Init four with 4. We do this so 
                                # // because it is not possible to do 
                                # // a Branch check an imidiate. 
    addi t1, x0, 4              # int four = 4
    
                                # // If a_n is all ready 4 then we 
                                # // return immediately.
    beq a0, t1, return          # if a_n == 4 { goto return } 
main_loop:                      
                                # // Do a_n mod 2 by anding the
                                # // laest significat bit.
    andi t0, a0, 1              # int temp = a_n % 2 
    
                                # // Check if a_n % 2 is != 0.
    bne t0, x0, else            # if temp != 0 { goto else }
    
                                # // Divide a_n by two.
                                # // This can be done by bit shifting
                                # // a_n one to the rigth.
    srli a0, a0, 1              # a_n = a_n / 2
    
                                # // Jump after the else Block.
    jal, x0, end                # goto end      
else: 

                                # // Perform a_n = 3 * a_n + 1
                                # // Adding a_n togther three times.
                                # // And adding one to the result.
    add t0, a0, a0              # temp = a_n + a_n
    add t0, t0, a0              # temp += a_n
    addi a0, t0, 1              # a_n = temp + 1
    
end:
                                # Incement n* by one.
    addi, a1, a1, 1             # n* += 1
return:                            
                                # // Check if we need to loop again.
    bne a0, t1, main_loop       # if a_n != 4 { goto main_loop }  
    
                                # // Else return n*.
    jalr, x0, ra, 0             # return n*
