double power(const double num, const int exp) {
  if (exp < 0)
    return 1.0 / power(num, -exp);
  double result;
  switch (exp) {
  case 0:
    return 1.0;
  case 1:
    return num;
  case 2:
    result = num * num;
    return result;
  case 3:
    result = num * num;
    result *= num;
    return result;
  case 4:
    result = num * num;
    result *= result;
    return result;
  case 5:
    result = num * num;
    result *= result;
    result *= num;
    return result;
  default:
    result = power(num, exp / 2);
    result *= result;
    if ((exp % 2) == 1)
      result *= num;
    return result;
  }
}


x86 assembly (clang, -O0):
<power>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1152 <power+0x42>
movsd  -0x10(%rbp),%xmm0
xor    %edi,%edi
sub    -0x14(%rbp),%edi
call   1030 <power@plt>
movaps %xmm0,%xmm1
movsd  0xebc(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a4 <power+0x194>
mov    -0x14(%rbp),%eax
mov    %rax,-0x28(%rbp)
sub    $0x5,%rax
ja     124c <power+0x13c>
mov    -0x28(%rbp),%rax
lea    0xe9a(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movsd  0xe81(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a4 <power+0x194>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a4 <power+0x194>
movsd  -0x10(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a4 <power+0x194>
movsd  -0x10(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a4 <power+0x194>
movsd  -0x10(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a4 <power+0x194>
movsd  -0x10(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a4 <power+0x194>
movsd  -0x10(%rbp),%xmm0
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1030 <power@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
jne    129a <power+0x18a>
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    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<power>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movapd %xmm0,%xmm1
test   %edi,%edi
js     1148 <power+0x38>
cmp    $0x5,%ebx
ja     1183 <power+0x73>
movsd  0xed4(%rip),%xmm0
mov    %ebx,%eax
lea    0xed3(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movapd %xmm1,%xmm0
mulsd  %xmm1,%xmm0
jmp    117d <power+0x6d>
neg    %ebx
movapd %xmm1,%xmm0
mov    %ebx,%edi
call   1030 <power@plt>
movapd %xmm0,%xmm1
movsd  0xe9f(%rip),%xmm0
divsd  %xmm1,%xmm0
jmp    11b7 <power+0xa7>
mulsd  %xmm1,%xmm1
mulsd  %xmm1,%xmm1
jmp    11b3 <power+0xa3>
movapd %xmm1,%xmm0
mulsd  %xmm1,%xmm0
mulsd  %xmm0,%xmm0
mulsd  %xmm1,%xmm0
jmp    11b7 <power+0xa7>
mov    %ebx,%edi
shr    %edi
movsd  %xmm1,0x8(%rsp)
movapd %xmm1,%xmm0
call   1030 <power@plt>
mulsd  %xmm0,%xmm0
test   $0x1,%bl
je     11a7 <power+0x97>
movsd  0x8(%rsp),%xmm1
jmp    11af <power+0x9f>
movsd  0xe51(%rip),%xmm1
mulsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define double @power(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 slt i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = load double, double* %4
%11 = load i32, i32* %5
%12 = sub nsw i32 0, %11
%13 = call double @power(double %10, i32 %12)
%14 = fdiv double 1.000000e+00, %13
store double %14, double* %3
br label %69
15: ; preds = %2
%16 = load i32, i32* %5
switch i32 %16, label %52 [
i32 0, label %17
i32 1, label %18
i32 2, label %20
i32 3, label %25
i32 4, label %33
i32 5, label %41
]
17: ; preds = %15
store double 1.000000e+00, double* %3
br label %69
18: ; preds = %15
%19 = load double, double* %4
store double %19, double* %3
br label %69
20: ; preds = %15
%21 = load double, double* %4
%22 = load double, double* %4
%23 = fmul double %21, %22
store double %23, double* %6
%24 = load double, double* %6
store double %24, double* %3
br label %69
25: ; preds = %15
%26 = load double, double* %4
%27 = load double, double* %4
%28 = fmul double %26, %27
store double %28, double* %6
%29 = load double, double* %4
%30 = load double, double* %6
%31 = fmul double %30, %29
store double %31, double* %6
%32 = load double, double* %6
store double %32, double* %3
br label %69
33: ; preds = %15
%34 = load double, double* %4
%35 = load double, double* %4
%36 = fmul double %34, %35
store double %36, double* %6
%37 = load double, double* %6
%38 = load double, double* %6
%39 = fmul double %38, %37
store double %39, double* %6
%40 = load double, double* %6
store double %40, double* %3
br label %69
41: ; preds = %15
%42 = load double, double* %4
%43 = load double, double* %4
%44 = fmul double %42, %43
store double %44, double* %6
%45 = load double, double* %6
%46 = load double, double* %6
%47 = fmul double %46, %45
store double %47, double* %6
%48 = load double, double* %4
%49 = load double, double* %6
%50 = fmul double %49, %48
store double %50, double* %6
%51 = load double, double* %6
store double %51, double* %3
br label %69
52: ; preds = %15
%53 = load double, double* %4
%54 = load i32, i32* %5
%55 = sdiv i32 %54, 2
%56 = call double @power(double %53, i32 %55)
store double %56, double* %6
%57 = load double, double* %6
%58 = load double, double* %6
%59 = fmul double %58, %57
store double %59, double* %6
%60 = load i32, i32* %5
%61 = srem i32 %60, 2
%62 = icmp eq i32 %61, 1
br i1 %62, label %63, label %67
63: ; preds = %52
%64 = load double, double* %4
%65 = load double, double* %6
%66 = fmul double %65, %64
store double %66, double* %6
br label %67
67: ; preds = %63, %52
%68 = load double, double* %6
store double %68, double* %3
br label %69
69: ; preds = %67, %41, %33, %25, %20, %18, %17, %9
%70 = load double, double* %3
ret double %70
}

LLVM IR (-O2):
define double @power(double %0, i32 %1) {
%3 = icmp slt i32 %1, 0
br i1 %3, label %6, label %10
4: ; preds = %10, %20, %17, %14, %12, %11, %24, %6
%5 = phi double [ %9, %6 ], [ %31, %24 ], [ %23, %20 ], [ %19, %17 ], [ %16, %14 ], [ %13, %12 ], [ %0, %11 ], [ 1.000000e+00, %10 ]
ret double %5
6: ; preds = %2
%7 = sub nsw i32 0, %1
%8 = tail call double @power(double %0, i32 %7)
%9 = fdiv double 1.000000e+00, %8
br label %4
10: ; preds = %2
switch i32 %1, label %24 [
i32 0, label %4
i32 1, label %11
i32 2, label %12
i32 3, label %14
i32 4, label %17
i32 5, label %20
]
11: ; preds = %10
br label %4
12: ; preds = %10
%13 = fmul double %0, %0
br label %4
14: ; preds = %10
%15 = fmul double %0, %0
%16 = fmul double %15, %0
br label %4
17: ; preds = %10
%18 = fmul double %0, %0
%19 = fmul double %18, %18
br label %4
20: ; preds = %10
%21 = fmul double %0, %0
%22 = fmul double %21, %21
%23 = fmul double %22, %0
br label %4
24: ; preds = %10
%25 = lshr i32 %1, 1
%26 = tail call double @power(double %0, i32 %25)
%27 = fmul double %26, %26
%28 = and i32 %1, 1
%29 = icmp eq i32 %28, 0
%30 = select i1 %29, double 1.000000e+00, double %0
%31 = fmul double %30, %27
br label %4
}