double varprofile(double x1, double x2,
                  double (*rho_func)(double, double, double *), double *ps,
                  void (*lenseq_func)(double, double, double *, double *,
                                      double *),
                  double *pl, double (*ld_func)(int, double *, double), int n,
                  double gam[]) {
  double y1, y2;
  double rho;
  double I0, I1;
  (*lenseq_func)(x1, x2, &y1, &y2, pl);
  rho = rho_func(y1, y2, ps);
  I0 = (*ld_func)(n, gam, 0.0);
  if (rho == 1.0) {
    I1 = (*ld_func)(n, gam, 1.0);
    return (I0 - I1);
  } else if (rho < 1.0) {
    I1 = (*ld_func)(n, gam, 1.0);
    return ((*ld_func)(n, gam, rho) + I0 - 2.0 * I1);
  } else
    return (I0 - (*ld_func)(n, gam, 1.0 / rho));
}


x86 assembly (clang, -O0):
<varprofile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    0x10(%rbp),%rax
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %rdi,-0x20(%rbp)
mov    %rsi,-0x28(%rbp)
mov    %rdx,-0x30(%rbp)
mov    %rcx,-0x38(%rbp)
mov    %r8,-0x40(%rbp)
mov    %r9d,-0x44(%rbp)
mov    -0x30(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
mov    -0x38(%rbp),%rdx
lea    -0x50(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   *%rax
mov    -0x20(%rbp),%rax
movsd  -0x50(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm1
mov    -0x28(%rbp),%rdi
call   *%rax
movsd  %xmm0,-0x60(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%edi
mov    0x10(%rbp),%rsi
xorps  %xmm0,%xmm0
call   *%rax
movsd  %xmm0,-0x68(%rbp)
movsd  -0x60(%rbp),%xmm0
movsd  0xe78(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    11c6 <varprofile+0xc6>
jp     11c6 <varprofile+0xc6>
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%edi
mov    0x10(%rbp),%rsi
movsd  0xe55(%rip),%xmm0
call   *%rax
movsd  %xmm0,-0x70(%rbp)
movsd  -0x68(%rbp),%xmm0
subsd  -0x70(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1256 <varprofile+0x156>
movsd  0xe32(%rip),%xmm0
ucomisd -0x60(%rbp),%xmm0
jbe    1221 <varprofile+0x121>
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%edi
mov    0x10(%rbp),%rsi
movsd  0xe14(%rip),%xmm0
call   *%rax
movsd  %xmm0,-0x70(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%edi
mov    0x10(%rbp),%rsi
movsd  -0x60(%rbp),%xmm0
call   *%rax
addsd  -0x68(%rbp),%xmm0
movsd  -0x70(%rbp),%xmm1
addsd  %xmm1,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1256 <varprofile+0x156>
movsd  -0x68(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%edi
mov    0x10(%rbp),%rsi
movsd  0xdc2(%rip),%xmm0
divsd  -0x60(%rbp),%xmm0
call   *%rax
movaps %xmm0,%xmm1
movsd  -0x78(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<varprofile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %r9d,%r12d
mov    %r8,%r15
mov    %rdx,%rax
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    0x60(%rsp),%r14
lea    0x28(%rsp),%rdi
lea    0x20(%rsp),%rsi
mov    %rcx,%rdx
call   *%rax
movsd  0x28(%rsp),%xmm0
movsd  0x20(%rsp),%xmm1
mov    %rbx,%rdi
call   *%rbp
movsd  %xmm0,0x10(%rsp)
xorps  %xmm0,%xmm0
mov    %r12d,%edi
mov    %r14,%rsi
call   *%r15
movsd  0x10(%rsp),%xmm1
ucomisd 0xea0(%rip),%xmm1
movsd  %xmm0,0x8(%rsp)
movsd  0xe92(%rip),%xmm0
jne    1172 <varprofile+0x72>
jnp    11c0 <varprofile+0xc0>
ucomisd %xmm1,%xmm0
jbe    11bc <varprofile+0xbc>
movsd  0xe80(%rip),%xmm0
mov    %r12d,%edi
mov    %r14,%rsi
call   *%r15
movsd  %xmm0,0x18(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
movsd  0x10(%rsp),%xmm0
call   *%r15
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
movsd  0x18(%rsp),%xmm1
addsd  %xmm1,%xmm1
subsd  %xmm1,%xmm0
jmp    11d7 <varprofile+0xd7>
divsd  %xmm1,%xmm0
mov    %r12d,%edi
mov    %r14,%rsi
call   *%r15
movsd  0x8(%rsp),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @varprofile(double %0, double %1, double (double, double, double*)* %2, double* %3, void (double, double, double*, double*, double*)* %4, double* %5, double (i32, double*, double)* %6, i32 %7, double* %8) {
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double (double, double, double*)*
%14 = alloca double*
%15 = alloca void (double, double, double*, double*, double*)*
%16 = alloca double*
%17 = alloca double (i32, double*, double)*
%18 = alloca i32
%19 = alloca double*
%20 = alloca double
%21 = alloca double
%22 = alloca double
%23 = alloca double
%24 = alloca double
store double %0, double* %11
store double %1, double* %12
store double (double, double, double*)* %2, double (double, double, double*)** %13
store double* %3, double** %14
store void (double, double, double*, double*, double*)* %4, void (double, double, double*, double*, double*)** %15
store double* %5, double** %16
store double (i32, double*, double)* %6, double (i32, double*, double)** %17
store i32 %7, i32* %18
store double* %8, double** %19
%25 = load void (double, double, double*, double*, double*)*, void (double, double, double*, double*, double*)** %15
%26 = load double, double* %11
%27 = load double, double* %12
%28 = load double*, double** %16
%29 = load double (double, double, double*)*, double (double, double, double*)** %13
%30 = load double, double* %20
%31 = load double, double* %21
%32 = load double*, double** %14
%33 = call double %29(double %30, double %31, double* %32)
store double %33, double* %22
%34 = load double (i32, double*, double)*, double (i32, double*, double)** %17
%35 = load i32, i32* %18
%36 = load double*, double** %19
%37 = call double %34(i32 %35, double* %36, double 0.000000e+00)
store double %37, double* %23
%38 = load double, double* %22
%39 = fcmp oeq double %38, 1.000000e+00
br i1 %39, label %40, label %48
40: ; preds = %9
%41 = load double (i32, double*, double)*, double (i32, double*, double)** %17
%42 = load i32, i32* %18
%43 = load double*, double** %19
%44 = call double %41(i32 %42, double* %43, double 1.000000e+00)
store double %44, double* %24
%45 = load double, double* %23
%46 = load double, double* %24
%47 = fsub double %45, %46
store double %47, double* %10
br label %74
48: ; preds = %9
%49 = load double, double* %22
%50 = fcmp olt double %49, 1.000000e+00
br i1 %50, label %51, label %65
51: ; preds = %48
%52 = load double (i32, double*, double)*, double (i32, double*, double)** %17
%53 = load i32, i32* %18
%54 = load double*, double** %19
%55 = call double %52(i32 %53, double* %54, double 1.000000e+00)
store double %55, double* %24
%56 = load double (i32, double*, double)*, double (i32, double*, double)** %17
%57 = load i32, i32* %18
%58 = load double*, double** %19
%59 = load double, double* %22
%60 = call double %56(i32 %57, double* %58, double %59)
%61 = load double, double* %23
%62 = fadd double %60, %61
%63 = load double, double* %24
%64 = call double @llvm.fmuladd.f64(double -2.000000e+00, double %63, double %62)
store double %64, double* %10
br label %74
65: ; preds = %48
%66 = load double, double* %23
%67 = load double (i32, double*, double)*, double (i32, double*, double)** %17
%68 = load i32, i32* %18
%69 = load double*, double** %19
%70 = load double, double* %22
%71 = fdiv double 1.000000e+00, %70
%72 = call double %67(i32 %68, double* %69, double %71)
%73 = fsub double %66, %72
store double %73, double* %10
br label %74
74: ; preds = %65, %51, %40
%75 = load double, double* %10
ret double %75
}

LLVM IR (-O2):
define double @varprofile(double %0, double %1, double (double, double, double*)* nocapture %2, double* %3, void (double, double, double*, double*, double*)* nocapture %4, double* %5, double (i32, double*, double)* nocapture %6, i32 %7, double* %8) {
%10 = alloca double
%11 = alloca double
%12 = bitcast double* %10 to i8*
%13 = bitcast double* %11 to i8*
%14 = load double, double* %10
%15 = load double, double* %11
%16 = call double %2(double %14, double %15, double* %3)
%17 = call double %6(i32 %7, double* %8, double 0.000000e+00)
%18 = fcmp oeq double %16, 1.000000e+00
br i1 %18, label %19, label %22
19: ; preds = %9
%20 = call double %6(i32 %7, double* %8, double 1.000000e+00)
%21 = fsub double %17, %20
br label %33
22: ; preds = %9
%23 = fcmp olt double %16, 1.000000e+00
br i1 %23, label %24, label %29
24: ; preds = %22
%25 = call double %6(i32 %7, double* %8, double 1.000000e+00)
%26 = call double %6(i32 %7, double* %8, double %16)
%27 = fadd double %17, %26
%28 = call double @llvm.fmuladd.f64(double %25, double -2.000000e+00, double %27)
br label %33
29: ; preds = %22
%30 = fdiv double 1.000000e+00, %16
%31 = call double %6(i32 %7, double* %8, double %30)
%32 = fsub double %17, %31
br label %33
33: ; preds = %29, %24, %19
%34 = phi double [ %21, %19 ], [ %28, %24 ], [ %32, %29 ]
ret double %34
}