'Algorithm that was supposed to go into recurse does not go into recurse

I am pretty new to LLVM IR and I am trying to convert the following into LLVM IR

double sum(double *input, int n) {
  double result = input[0] + ... + input[n-1]
  return result
}

and this is what I have done:

    %free_func = type void (double*)*
    %list = type { %list*, double*, %free_func }



define double @sum(double* %ptr, i32 %n) {
entry:
    %var1 = alloca i32
    store i32 %n, i32* %var1
    %var2 = alloca double*
    store double* %ptr, double** %var2

    %conv1.1 = load i32, i32* %var1
    %tmp1 = icmp sle i32 %conv1.1, 0
    
    br i1 %tmp1, label %recurse, label %done

        
    recurse:
        
        %0 = bitcast double** %var2 to %list*       ; base field
        %1 = bitcast double** %var2 to %list**      ; next field
        %2 = bitcast double** %var2 to %list***     ; next next field

        %3 = bitcast %list* %0 to i16*
        %conv2.1 = load i16, i16* %3
        %4 = bitcast %list** %1 to i16*        
        %conv2.2 = load i16, i16* %4

        %tmp2 = add nsw i16 %conv2.2, %conv2.1    ; add base value & next value
        %tmp3 = alloca i16
        store i16 %tmp2, i16* %tmp3

        %5 = bitcast i16* %tmp3 to %list*         ; convert vector back to list
        store %list* %5, %list** %1               ; replace next value with sum
        %conv3.1 = load %list*, %list** %1        ; replace base with next
        %conv3.2 = load %list**, %list*** %2      ; replace next with next next
        %6 = bitcast %list* %conv3.1 to double*   ; convert list back to double

        %tmp5 = sub nsw i32 %conv1.1, 1

        %tmp6 = call double @sum(double* %6, i32 %tmp5) 

        ret double %tmp6

        
    done:
        
        %conv6.1 = load double, double* %ptr
        ret double %conv6.1

}

It seems to go straight to done without going through recurse and I am not sure what is wrong. May I ask for tips on how to make this work? Thank you and I am sorry if the code looks messy because I've only just started learning and am practising.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source