double power(int base, int exponent) {
  double result = 1;
  int i;
  if (exponent > 0) {
    for (i = 1; i <= exponent; i++) {
      result *= result;
    }
    return result;
  } else if (exponent == 0) {
    return 1;
  } else {
    exponent = -exponent;
    for (i = 1; i <= exponent; i++) {
      result *= result;
    }
    return (1 / result);
  }
}


x86 assembly (clang, -O0):
<power>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movsd  0xeee(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1160 <power+0x60>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     1151 <power+0x51>
movsd  -0x18(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1128 <power+0x28>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c6 <power+0xc6>
cmpl   $0x0,-0x10(%rbp)
jne    117c <power+0x7c>
movsd  0xe8e(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c6 <power+0xc6>
xor    %eax,%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     11b4 <power+0xb4>
movsd  -0x18(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118b <power+0x8b>
movsd  0xe44(%rip),%xmm0
divsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<power>:
movsd  0xef8(%rip),%xmm0
ret

LLVM IR (-O0):
define double @power(i32 %0, i32 %1) {
%3 = alloca double
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store double 1.000000e+00, double* %6
%8 = load i32, i32* %5
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %2
store i32 1, i32* %7
br label %11
11: ; preds = %19, %10
%12 = load i32, i32* %7
%13 = load i32, i32* %5
%14 = icmp sle i32 %12, %13
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = load double, double* %6
%17 = load double, double* %6
%18 = fmul double %17, %16
store double %18, double* %6
br label %19
19: ; preds = %15
%20 = load i32, i32* %7
%21 = add nsw i32 %20, 1
store i32 %21, i32* %7
br label %11
22: ; preds = %11
%23 = load double, double* %6
store double %23, double* %3
br label %45
24: ; preds = %2
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
store double 1.000000e+00, double* %3
br label %45
28: ; preds = %24
%29 = load i32, i32* %5
%30 = sub nsw i32 0, %29
store i32 %30, i32* %5
store i32 1, i32* %7
br label %31
31: ; preds = %39, %28
%32 = load i32, i32* %7
%33 = load i32, i32* %5
%34 = icmp sle i32 %32, %33
br i1 %34, label %35, label %42
35: ; preds = %31
%36 = load double, double* %6
%37 = load double, double* %6
%38 = fmul double %37, %36
store double %38, double* %6
br label %39
39: ; preds = %35
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %31
42: ; preds = %31
%43 = load double, double* %6
%44 = fdiv double 1.000000e+00, %43
store double %44, double* %3
br label %45
45: ; preds = %42, %27, %22
%46 = load double, double* %3
ret double %46
}

LLVM IR (-O2):
define double @power(i32 %0, i32 %1) {
ret double 1.000000e+00
}