double rate_function(double *book, double cost, int life_years, int year,
                     double declining_rate, double freeze_rate) {
  double straight_line_rate;
  double rate;
  double depreciation;
  if (year == life_years + 1) {
    freeze_rate /= 2.0;
    depreciation = *book * freeze_rate;
    *book -= depreciation;
    return freeze_rate;
  } else if (freeze_rate) {
    depreciation = *book * freeze_rate;
    *book -= depreciation;
    return freeze_rate;
  }
  straight_line_rate = 1.0 / (double)life_years;
  depreciation = declining_rate * straight_line_rate * *book;
  if (year == 1)
    depreciation = depreciation * 0.5;
  rate = depreciation / cost;
  *book = *book - depreciation;
  return rate;
}


x86 assembly (clang, -O0):
<rate_function>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
mov    %edx,-0x20(%rbp)
movsd  %xmm1,-0x28(%rbp)
movsd  %xmm2,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jne    117a <rate_function+0x7a>
movsd  -0x30(%rbp),%xmm0
movsd  0xed5(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm1
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1249 <rate_function+0x149>
movsd  -0x30(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1197 <rate_function+0x97>
jp     1197 <rate_function+0x97>
jmp    11cd <rate_function+0xcd>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm1
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1249 <rate_function+0x149>
jmp    11d2 <rate_function+0xd2>
cvtsi2sdl -0x1c(%rbp),%xmm1
movsd  0xe21(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x28(%rbp),%xmm0
mulsd  -0x38(%rbp),%xmm0
mov    -0x10(%rbp),%rax
mulsd  (%rax),%xmm0
movsd  %xmm0,-0x48(%rbp)
cmpl   $0x1,-0x20(%rbp)
jne    121b <rate_function+0x11b>
movsd  0xdf7(%rip),%xmm0
mulsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
divsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
subsd  -0x48(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<rate_function>:
lea    0x1(%rsi),%eax
cmp    %edx,%eax
jne    1121 <rate_function+0x21>
mulsd  0xef9(%rip),%xmm2
movsd  (%rdi),%xmm1
movapd %xmm2,%xmm0
mulsd  %xmm1,%xmm0
subsd  %xmm0,%xmm1
jmp    1173 <rate_function+0x73>
xorpd  %xmm3,%xmm3
ucomisd %xmm3,%xmm2
jne    112d <rate_function+0x2d>
jnp    113f <rate_function+0x3f>
movsd  (%rdi),%xmm1
movapd %xmm1,%xmm0
mulsd  %xmm2,%xmm0
subsd  %xmm0,%xmm1
jmp    1173 <rate_function+0x73>
xorps  %xmm3,%xmm3
cvtsi2sd %esi,%xmm3
movsd  0xeb2(%rip),%xmm2
divsd  %xmm3,%xmm2
mulsd  %xmm1,%xmm2
movsd  (%rdi),%xmm1
mulsd  %xmm1,%xmm2
cmp    $0x1,%edx
jne    116b <rate_function+0x6b>
mulsd  0xe9d(%rip),%xmm2
subsd  %xmm2,%xmm1
divsd  %xmm0,%xmm2
movsd  %xmm1,(%rdi)
movapd %xmm2,%xmm0
ret

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

LLVM IR (-O2):
define double @rate_function(double* nocapture %0, double %1, i32 %2, i32 %3, double %4, double %5) {
%7 = add nsw i32 %2, 1
%8 = icmp eq i32 %7, %3
br i1 %8, label %9, label %14
9: ; preds = %6
%10 = fmul double %5, 5.000000e-01
%11 = load double, double* %0
%12 = fmul double %10, %11
%13 = fsub double %11, %12
br label %31
14: ; preds = %6
%15 = fcmp une double %5, 0.000000e+00
br i1 %15, label %16, label %20
16: ; preds = %14
%17 = load double, double* %0
%18 = fmul double %17, %5
%19 = fsub double %17, %18
br label %31
20: ; preds = %14
%21 = sitofp i32 %2 to double
%22 = fdiv double 1.000000e+00, %21
%23 = fmul double %22, %4
%24 = load double, double* %0
%25 = fmul double %23, %24
%26 = icmp eq i32 %3, 1
%27 = fmul double %25, 5.000000e-01
%28 = select i1 %26, double %27, double %25
%29 = fdiv double %28, %1
%30 = fsub double %24, %28
br label %31
31: ; preds = %20, %16, %9
%32 = phi double [ %30, %20 ], [ %19, %16 ], [ %13, %9 ]
%33 = phi double [ %29, %20 ], [ %5, %16 ], [ %10, %9 ]
store double %32, double* %0
ret double %33
}