long double exponentation_by_squaring(long double base, int power) {
  if (power < 0) {
    return exponentation_by_squaring(1 / base, -power);
  } else if (power == 0) {
    return 1;
  } else if (power == 1) {
    return base;
  } else if (power % 2 == 0) {
    return exponentation_by_squaring(base * base, power / 2);
  } else if (power % 2 == 1) {
    return base * exponentation_by_squaring(base * base, (power - 1) / 2);
  }
}


x86 assembly (clang, -O0):
<exponentation_by_squaring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
fldt   0x10(%rbp)
fstpt  -0x20(%rbp)
mov    %edi,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1149 <exponentation_by_squaring+0x39>
fldt   -0x20(%rbp)
fld1
fdivp  %st,%st(1)
mov    -0x24(%rbp),%edi
neg    %edi
mov    %rsp,%rax
fstpt  (%rax)
call   1030 <exponentation_by_squaring@plt>
fstpt  -0x10(%rbp)
jmp    1207 <exponentation_by_squaring+0xf7>
cmpl   $0x0,-0x24(%rbp)
jne    115d <exponentation_by_squaring+0x4d>
fld1
fstpt  -0x10(%rbp)
jmp    1207 <exponentation_by_squaring+0xf7>
cmpl   $0x1,-0x24(%rbp)
jne    1172 <exponentation_by_squaring+0x62>
fldt   -0x20(%rbp)
fstpt  -0x10(%rbp)
jmp    1207 <exponentation_by_squaring+0xf7>
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11a9 <exponentation_by_squaring+0x99>
fldt   -0x20(%rbp)
fmul   %st,%st(0)
mov    -0x24(%rbp),%edi
mov    %edi,%eax
shr    $0x1f,%eax
add    %eax,%edi
sar    %edi
mov    %rsp,%rax
fstpt  (%rax)
call   1030 <exponentation_by_squaring@plt>
fstpt  -0x10(%rbp)
jmp    1207 <exponentation_by_squaring+0xf7>
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
jne    11f3 <exponentation_by_squaring+0xe3>
fldt   -0x20(%rbp)
fld    %st(0)
fstpt  -0x30(%rbp)
fmul   %st,%st(0)
mov    -0x24(%rbp),%ecx
mov    %ecx,%edx
add    $0xffffffff,%edx
mov    %ecx,%eax
shr    $0x1f,%edx
mov    %edx,%ecx
lea    -0x1(%rax,%rcx,1),%edi
sar    %edi
mov    %rsp,%rax
fstpt  (%rax)
call   1030 <exponentation_by_squaring@plt>
fldt   -0x30(%rbp)
fmulp  %st,%st(1)
fstpt  -0x10(%rbp)
jmp    1207 <exponentation_by_squaring+0xf7>
jmp    11f8 <exponentation_by_squaring+0xe8>
jmp    11fd <exponentation_by_squaring+0xed>
jmp    1202 <exponentation_by_squaring+0xf2>
jmp    1207 <exponentation_by_squaring+0xf7>
fldt   -0x10(%rbp)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exponentation_by_squaring>:
sub    $0x28,%rsp
fldt   0x30(%rsp)
fld1
test   %edi,%edi
jns    1128 <exponentation_by_squaring+0x18>
xchg   %ax,%ax
fdiv   %st,%st(1)
neg    %edi
test   %edi,%edi
js     1120 <exponentation_by_squaring+0x10>
je     1143 <exponentation_by_squaring+0x33>
cmp    $0x1,%edi
je     114a <exponentation_by_squaring+0x3a>
fld    %st(1)
fmul   %st(2),%st
test   $0x1,%dil
jne    1151 <exponentation_by_squaring+0x41>
fstp   %st(2)
shr    %edi
test   %edi,%edi
jns    1128 <exponentation_by_squaring+0x18>
jmp    1120 <exponentation_by_squaring+0x10>
fstp   %st(1)
add    $0x28,%rsp
ret
fstp   %st(0)
add    $0x28,%rsp
ret
fstp   %st(1)
add    $0xffffffff,%edi
shr    %edi
fstpt  (%rsp)
fstpt  0x1c(%rsp)
call   1030 <exponentation_by_squaring@plt>
fldt   0x1c(%rsp)
fmulp  %st,%st(1)
add    $0x28,%rsp
ret

LLVM IR (-O0):
define x86_fp80 @exponentation_by_squaring(x86_fp80 %0, i32 %1) {
%3 = alloca x86_fp80
%4 = alloca x86_fp80
%5 = alloca i32
store x86_fp80 %0, x86_fp80* %4
store i32 %1, i32* %5
%6 = load i32, i32* %5
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = load x86_fp80, x86_fp80* %4
%10 = fdiv x86_fp80 0xK3FFF8000000000000000, %9
%11 = load i32, i32* %5
%12 = sub nsw i32 0, %11
%13 = call x86_fp80 @exponentation_by_squaring(x86_fp80 %10, i32 %12)
store x86_fp80 %13, x86_fp80* %3
br label %52
14: ; preds = %2
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14
store x86_fp80 0xK3FFF8000000000000000, x86_fp80* %3
br label %52
18: ; preds = %14
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = load x86_fp80, x86_fp80* %4
store x86_fp80 %22, x86_fp80* %3
br label %52
23: ; preds = %18
%24 = load i32, i32* %5
%25 = srem i32 %24, 2
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %23
%28 = load x86_fp80, x86_fp80* %4
%29 = load x86_fp80, x86_fp80* %4
%30 = fmul x86_fp80 %28, %29
%31 = load i32, i32* %5
%32 = sdiv i32 %31, 2
%33 = call x86_fp80 @exponentation_by_squaring(x86_fp80 %30, i32 %32)
store x86_fp80 %33, x86_fp80* %3
br label %52
34: ; preds = %23
%35 = load i32, i32* %5
%36 = srem i32 %35, 2
%37 = icmp eq i32 %36, 1
br i1 %37, label %38, label %48
38: ; preds = %34
%39 = load x86_fp80, x86_fp80* %4
%40 = load x86_fp80, x86_fp80* %4
%41 = load x86_fp80, x86_fp80* %4
%42 = fmul x86_fp80 %40, %41
%43 = load i32, i32* %5
%44 = sub nsw i32 %43, 1
%45 = sdiv i32 %44, 2
%46 = call x86_fp80 @exponentation_by_squaring(x86_fp80 %42, i32 %45)
%47 = fmul x86_fp80 %39, %46
store x86_fp80 %47, x86_fp80* %3
br label %52
48: ; preds = %34
br label %49
49: ; preds = %48
br label %50
50: ; preds = %49
br label %51
51: ; preds = %50
br label %52
52: ; preds = %8, %17, %21, %27, %38, %51
%53 = load x86_fp80, x86_fp80* %3
ret x86_fp80 %53
}

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