double powRecursion(double x, int n) {
  if (n == 1) {
    return x;
  } else if (n == 2) {
    return x * x;
  }
  double half = powRecursion(x, n / 2);
  half = half * half;
  if (n & 1) {
    half *= x;
  }
  return half;
}


x86 assembly (clang, -O0):
<powRecursion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    1139 <powRecursion+0x29>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11af <powRecursion+0x9f>
cmpl   $0x2,-0x14(%rbp)
jne    1157 <powRecursion+0x47>
movsd  -0x10(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11af <powRecursion+0x9f>
jmp    115c <powRecursion+0x4c>
movsd  -0x10(%rbp),%xmm0
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1030 <powRecursion@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11a5 <powRecursion+0x95>
movsd  -0x10(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<powRecursion>:
push   %rbx
sub    $0x10,%rsp
movapd %xmm0,%xmm1
cmp    $0x1,%edi
je     1160 <powRecursion+0x50>
mov    %edi,%ebx
cmp    $0x2,%edi
jne    112b <powRecursion+0x1b>
mulsd  %xmm1,%xmm1
jmp    1160 <powRecursion+0x50>
mov    %ebx,%edi
shr    $0x1f,%edi
add    %ebx,%edi
sar    %edi
movsd  %xmm1,0x8(%rsp)
movapd %xmm1,%xmm0
call   1030 <powRecursion@plt>
mulsd  %xmm0,%xmm0
test   $0x1,%bl
je     1154 <powRecursion+0x44>
movsd  0x8(%rsp),%xmm1
jmp    115c <powRecursion+0x4c>
movsd  0xea4(%rip),%xmm1
mulsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define double @powRecursion(double %0, i32 %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca i32
%6 = alloca double
store double %0, double* %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load double, double* %4
store double %10, double* %3
br label %36
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 2
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load double, double* %4
%16 = load double, double* %4
%17 = fmul double %15, %16
store double %17, double* %3
br label %36
18: ; preds = %11
br label %19
19: ; preds = %18
%20 = load double, double* %4
%21 = load i32, i32* %5
%22 = sdiv i32 %21, 2
%23 = call double @powRecursion(double %20, i32 %22)
store double %23, double* %6
%24 = load double, double* %6
%25 = load double, double* %6
%26 = fmul double %24, %25
store double %26, double* %6
%27 = load i32, i32* %5
%28 = and i32 %27, 1
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %19
%31 = load double, double* %4
%32 = load double, double* %6
%33 = fmul double %32, %31
store double %33, double* %6
br label %34
34: ; preds = %30, %19
%35 = load double, double* %6
store double %35, double* %3
br label %36
36: ; preds = %34, %14, %9
%37 = load double, double* %3
ret double %37
}

LLVM IR (-O2):
define double @powRecursion(double %0, i32 %1) {
switch i32 %1, label %7 [
i32 1, label %5
i32 2, label %3
]
3: ; preds = %2
%4 = fmul double %0, %0
br label %5
5: ; preds = %3, %2, %7
%6 = phi double [ %14, %7 ], [ %4, %3 ], [ %0, %2 ]
ret double %6
7: ; preds = %2
%8 = sdiv i32 %1, 2
%9 = tail call double @powRecursion(double %0, i32 %8)
%10 = fmul double %9, %9
%11 = and i32 %1, 1
%12 = icmp eq i32 %11, 0
%13 = select i1 %12, double 1.000000e+00, double %0
%14 = fmul double %13, %10
br label %5
}