double fast_pow(double x, int n) {
  if (n == 0) {
    return 1.0;
  }
  if (n == 1) {
    return x;
  }
  double t = fast_pow(x, n / 2);
  return n & 1 ? t * t * x : t * t;
}


x86 assembly (clang, -O0):
<fast_pow>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    113c <fast_pow+0x2c>
movsd  0xece(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11b2 <fast_pow+0xa2>
cmpl   $0x1,-0x14(%rbp)
jne    1155 <fast_pow+0x45>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11b2 <fast_pow+0xa2>
movsd  -0x10(%rbp),%xmm0
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1030 <fast_pow@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1199 <fast_pow+0x89>
movsd  -0x20(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    11a8 <fast_pow+0x98>
movsd  -0x20(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fast_pow>:
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
je     114d <fast_pow+0x3d>
mov    %edi,%ebx
movapd %xmm0,%xmm1
cmp    $0x1,%edi
je     1163 <fast_pow+0x53>
mov    %ebx,%edi
shr    $0x1f,%edi
add    %ebx,%edi
sar    %edi
movsd  %xmm1,0x8(%rsp)
movapd %xmm1,%xmm0
call   1030 <fast_pow@plt>
mulsd  %xmm0,%xmm0
test   $0x1,%bl
je     1157 <fast_pow+0x47>
movsd  0x8(%rsp),%xmm1
jmp    115f <fast_pow+0x4f>
movsd  0xeab(%rip),%xmm1
jmp    1163 <fast_pow+0x53>
movsd  0xea1(%rip),%xmm1
mulsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define double @fast_pow(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, 0
br i1 %8, label %9, label %10
9: ; preds = %2
store double 1.000000e+00, double* %3
br label %35
10: ; preds = %2
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = load double, double* %4
store double %14, double* %3
br label %35
15: ; preds = %10
%16 = load double, double* %4
%17 = load i32, i32* %5
%18 = sdiv i32 %17, 2
%19 = call double @fast_pow(double %16, i32 %18)
store double %19, double* %6
%20 = load i32, i32* %5
%21 = and i32 %20, 1
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %15
%24 = load double, double* %6
%25 = load double, double* %6
%26 = fmul double %24, %25
%27 = load double, double* %4
%28 = fmul double %26, %27
br label %33
29: ; preds = %15
%30 = load double, double* %6
%31 = load double, double* %6
%32 = fmul double %30, %31
br label %33
33: ; preds = %29, %23
%34 = phi double [ %28, %23 ], [ %32, %29 ]
store double %34, double* %3
br label %35
35: ; preds = %33, %13, %9
%36 = load double, double* %3
ret double %36
}

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