#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
double pow(double, int);
double logarithm(double x, int n) {
  double ans = ((x - 1) / x);
  if (n == 1) {
    return ans;
  } else {
    for (int i = 1; i < n; i++) {
      ans += (1.0 / (i + 1)) * pow(((x - 1) / x), i + 1);
    }
    return ans;
  }
}


x86 assembly (clang, -O0):
<logarithm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xed3(%rip),%xmm1
subsd  %xmm1,%xmm0
divsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    1154 <logarithm+0x44>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11d5 <logarithm+0xc5>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11cb <logarithm+0xbb>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cvtsi2sd %eax,%xmm1
movsd  0xe87(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe71(%rip),%xmm1
subsd  %xmm1,%xmm0
divsd  -0x10(%rbp),%xmm0
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1030 <pow@plt>
movaps %xmm0,%xmm2
movsd  -0x30(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    115b <logarithm+0x4b>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logarithm>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movsd  0xee0(%rip),%xmm1
addsd  %xmm0,%xmm1
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
cmp    $0x2,%edi
jl     114e <logarithm+0x3e>
lea    -0x1(%rdi),%r14d
cmp    $0x2,%edi
movsd  %xmm0,0x18(%rsp)
jne    1157 <logarithm+0x47>
mov    $0x2,%edi
movapd %xmm0,%xmm1
jmp    11fb <logarithm+0xeb>
movapd %xmm0,%xmm1
jmp    1207 <logarithm+0xf7>
mov    %r14d,%ebp
and    $0xfffffffe,%ebp
neg    %ebp
mov    $0x2,%ebx
movapd %xmm0,%xmm1
nopl   0x0(%rax,%rax,1)
movsd  %xmm1,0x10(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
movsd  0xe83(%rip),%xmm2
divsd  %xmm0,%xmm2
movsd  %xmm2,0x8(%rsp)
movsd  0x18(%rsp),%xmm0
mov    %ebx,%edi
call   1030 <pow@plt>
mulsd  0x8(%rsp),%xmm0
addsd  0x10(%rsp),%xmm0
movsd  %xmm0,0x10(%rsp)
lea    0x1(%rbx),%edi
xorps  %xmm0,%xmm0
cvtsi2sd %edi,%xmm0
movsd  0xe48(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x18(%rsp),%xmm0
call   1030 <pow@plt>
movapd %xmm0,%xmm1
mulsd  0x8(%rsp),%xmm1
addsd  0x10(%rsp),%xmm1
mov    %ebx,%edi
add    $0x2,%edi
lea    (%rbx,%rbp,1),%eax
add    $0x2,%eax
mov    %edi,%ebx
cmp    $0x2,%eax
jne    1170 <logarithm+0x60>
movsd  0x18(%rsp),%xmm0
test   $0x1,%r14b
jne    120d <logarithm+0xfd>
movapd %xmm1,%xmm0
jmp    123d <logarithm+0x12d>
movsd  %xmm1,0x10(%rsp)
xorps  %xmm1,%xmm1
cvtsi2sd %edi,%xmm1
movsd  0xde6(%rip),%xmm2
divsd  %xmm1,%xmm2
movsd  %xmm2,0x8(%rsp)
call   1030 <pow@plt>
mulsd  0x8(%rsp),%xmm0
addsd  0x10(%rsp),%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define double @logarithm(double %0, i32 %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca i32
%6 = alloca double
%7 = alloca i32
store double %0, double* %4
store i32 %1, i32* %5
%8 = load double, double* %4
%9 = fsub double %8, 1.000000e+00
%10 = load double, double* %4
%11 = fdiv double %9, %10
store double %11, double* %6
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load double, double* %6
store double %15, double* %3
br label %40
16: ; preds = %2
store i32 1, i32* %7
br label %17
17: ; preds = %35, %16
%18 = load i32, i32* %7
%19 = load i32, i32* %5
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %38
21: ; preds = %17
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 1
%24 = sitofp i32 %23 to double
%25 = fdiv double 1.000000e+00, %24
%26 = load double, double* %4
%27 = fsub double %26, 1.000000e+00
%28 = load double, double* %4
%29 = fdiv double %27, %28
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
%32 = call double @pow(double %29, i32 %31)
%33 = load double, double* %6
%34 = call double @llvm.fmuladd.f64(double %25, double %32, double %33)
store double %34, double* %6
br label %35
35: ; preds = %21
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
store i32 %37, i32* %7
br label %17
38: ; preds = %17
%39 = load double, double* %6
store double %39, double* %3
br label %40
40: ; preds = %38, %14
%41 = load double, double* %3
ret double %41
}

LLVM IR (-O2):
define double @logarithm(double %0, i32 %1) {
%3 = fadd double %0, -1.000000e+00
%4 = fdiv double %3, %0
%5 = icmp sgt i32 %1, 1
br i1 %5, label %6, label %40
6: ; preds = %2
%7 = add i32 %1, -1
%8 = and i32 %7, 1
%9 = icmp eq i32 %1, 2
br i1 %9, label %30, label %10
10: ; preds = %6
%11 = and i32 %7, -2
br label %12
12: ; preds = %12, %10
%13 = phi i32 [ 1, %10 ], [ %21, %12 ]
%14 = phi double [ %4, %10 ], [ %25, %12 ]
%15 = phi i32 [ 0, %10 ], [ %26, %12 ]
%16 = add nuw nsw i32 %13, 1
%17 = sitofp i32 %16 to double
%18 = fdiv double 1.000000e+00, %17
%19 = tail call double @pow(double %4, i32 %16)
%20 = tail call double @llvm.fmuladd.f64(double %18, double %19, double %14)
%21 = add nuw nsw i32 %13, 2
%22 = sitofp i32 %21 to double
%23 = fdiv double 1.000000e+00, %22
%24 = tail call double @pow(double %4, i32 %21)
%25 = tail call double @llvm.fmuladd.f64(double %23, double %24, double %20)
%26 = add i32 %15, 2
%27 = icmp eq i32 %26, %11
br i1 %27, label %28, label %12
28: ; preds = %12
%29 = add nuw i32 %13, 3
br label %30
30: ; preds = %28, %6
%31 = phi double [ undef, %6 ], [ %25, %28 ]
%32 = phi i32 [ 2, %6 ], [ %29, %28 ]
%33 = phi double [ %4, %6 ], [ %25, %28 ]
%34 = icmp eq i32 %8, 0
br i1 %34, label %40, label %35
35: ; preds = %30
%36 = sitofp i32 %32 to double
%37 = fdiv double 1.000000e+00, %36
%38 = tail call double @pow(double %4, i32 %32)
%39 = tail call double @llvm.fmuladd.f64(double %37, double %38, double %33)
br label %40
40: ; preds = %35, %30, %2
%41 = phi double [ %4, %2 ], [ %31, %30 ], [ %39, %35 ]
ret double %41
}