double pow_by_squaring(double x, double n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x;
  if ((int)n % 2 == 0)
    return pow_by_squaring(x * x, n / 2);
  return x * pow_by_squaring(x * x, (n - 1) / 2);
}


x86 assembly (clang, -O0):
<pow_by_squaring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    114c <pow_by_squaring+0x3c>
jp     114c <pow_by_squaring+0x3c>
movsd  0xebe(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ff <pow_by_squaring+0xef>
movsd  -0x18(%rbp),%xmm0
movsd  0xea7(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1178 <pow_by_squaring+0x68>
jp     1178 <pow_by_squaring+0x68>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ff <pow_by_squaring+0xef>
cvttsd2si -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11b8 <pow_by_squaring+0xa8>
movsd  -0x10(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movsd  0xe63(%rip),%xmm2
divsd  %xmm2,%xmm1
call   1030 <pow_by_squaring@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    11ff <pow_by_squaring+0xef>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movsd  0xe27(%rip),%xmm2
subsd  %xmm2,%xmm1
movsd  0xe23(%rip),%xmm2
divsd  %xmm2,%xmm1
call   1030 <pow_by_squaring@plt>
movaps %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pow_by_squaring>:
push   %rax
xorpd  %xmm2,%xmm2
ucomisd %xmm2,%xmm1
jne    112b <pow_by_squaring+0x1b>
jp     112b <pow_by_squaring+0x1b>
movsd  0xedb(%rip),%xmm5
movapd %xmm5,%xmm0
pop    %rax
ret
movapd %xmm0,%xmm5
movsd  0xec9(%rip),%xmm3
movsd  0xed1(%rip),%xmm4
nop
ucomisd %xmm3,%xmm1
jne    1148 <pow_by_squaring+0x38>
jnp    1125 <pow_by_squaring+0x15>
cvttsd2si %xmm1,%eax
movapd %xmm5,%xmm0
mulsd  %xmm5,%xmm0
test   $0x1,%al
jne    116e <pow_by_squaring+0x5e>
mulsd  %xmm4,%xmm1
ucomisd %xmm2,%xmm1
movapd %xmm0,%xmm5
jne    1140 <pow_by_squaring+0x30>
jp     1140 <pow_by_squaring+0x30>
movapd %xmm3,%xmm5
jmp    1125 <pow_by_squaring+0x15>
addsd  0xe92(%rip),%xmm1
mulsd  0xe92(%rip),%xmm1
movsd  %xmm5,(%rsp)
call   1030 <pow_by_squaring@plt>
movsd  (%rsp),%xmm5
mulsd  %xmm0,%xmm5
jmp    1125 <pow_by_squaring+0x15>

LLVM IR (-O0):
define double @pow_by_squaring(double %0, double %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca double
store double %0, double* %4
store double %1, double* %5
%6 = load double, double* %5
%7 = fcmp oeq double %6, 0.000000e+00
br i1 %7, label %8, label %9
8: ; preds = %2
store double 1.000000e+00, double* %3
br label %36
9: ; preds = %2
%10 = load double, double* %5
%11 = fcmp oeq double %10, 1.000000e+00
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = load double, double* %4
store double %13, double* %3
br label %36
14: ; preds = %9
%15 = load double, double* %5
%16 = fptosi double %15 to i32
%17 = srem i32 %16, 2
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %14
%20 = load double, double* %4
%21 = load double, double* %4
%22 = fmul double %20, %21
%23 = load double, double* %5
%24 = fdiv double %23, 2.000000e+00
%25 = call double @pow_by_squaring(double %22, double %24)
store double %25, double* %3
br label %36
26: ; preds = %14
%27 = load double, double* %4
%28 = load double, double* %4
%29 = load double, double* %4
%30 = fmul double %28, %29
%31 = load double, double* %5
%32 = fsub double %31, 1.000000e+00
%33 = fdiv double %32, 2.000000e+00
%34 = call double @pow_by_squaring(double %30, double %33)
%35 = fmul double %27, %34
store double %35, double* %3
br label %36
36: ; preds = %26, %19, %12, %8
%37 = load double, double* %3
ret double %37
}

LLVM IR (-O2):
define double @pow_by_squaring(double %0, double %1) {
%3 = fcmp oeq double %1, 0.000000e+00
br i1 %3, label %16, label %4
4: ; preds = %2, %13
%5 = phi double [ %14, %13 ], [ %1, %2 ]
%6 = phi double [ %12, %13 ], [ %0, %2 ]
%7 = fcmp oeq double %5, 1.000000e+00
br i1 %7, label %16, label %8
8: ; preds = %4
%9 = fptosi double %5 to i32
%10 = and i32 %9, 1
%11 = icmp eq i32 %10, 0
%12 = fmul double %6, %6
br i1 %11, label %13, label %18
13: ; preds = %8
%14 = fmul double %5, 5.000000e-01
%15 = fcmp oeq double %14, 0.000000e+00
br i1 %15, label %16, label %4
16: ; preds = %4, %13, %2, %18
%17 = phi double [ %22, %18 ], [ 1.000000e+00, %2 ], [ %6, %4 ], [ 1.000000e+00, %13 ]
ret double %17
18: ; preds = %8
%19 = fadd double %5, -1.000000e+00
%20 = fmul double %19, 5.000000e-01
%21 = tail call double @pow_by_squaring(double %12, double %20)
%22 = fmul double %6, %21
br label %16
}