#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 */
int pow(double, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, double *);
int main() {
  double x, n;
  printf("Enter the value of x : ");
  scanf("%lf", &x);
  printf("Enter the number of terms n : ");
  scanf("%lf", &n);
  double ln_x = 0;
  int i;
  for (i = 1; i <= n; i++)
    ln_x = ln_x + ((1 / (double)i) * pow(((x - 1) / x), i));
  printf("The value of log(%d) approximated to %d terms is : %lf\n", (int)x,
         (int)n, ln_x);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xec2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xecc(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <scanf@plt>
lea    0xec0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeae(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <scanf@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
cvtsi2sdl -0x24(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     1205 <main+0xd5>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
cvtsi2sdl -0x24(%rbp),%xmm1
movsd  0xe4b(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe35(%rip),%xmm1
subsd  %xmm1,%xmm0
divsd  -0x10(%rbp),%xmm0
mov    -0x24(%rbp),%edi
call   1050 <pow@plt>
movsd  -0x38(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
cvtsi2sd %eax,%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    118a <main+0x5a>
cvttsd2si -0x10(%rbp),%esi
cvttsd2si -0x18(%rbp),%edx
movsd  -0x20(%rbp),%xmm0
lea    0xe28(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0xed4(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xede(%rip),%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <scanf@plt>
lea    0xece(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <scanf@plt>
movsd  (%rsp),%xmm2
xorpd  %xmm0,%xmm0
ucomisd 0xe7f(%rip),%xmm2
jb     1201 <main+0xd1>
xorpd  %xmm0,%xmm0
mov    $0x1,%ebx
movsd  0xe6c(%rip),%xmm1
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  %xmm0,0x18(%rsp)
movsd  0xe52(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm1
movapd %xmm1,%xmm0
addsd  0xe3e(%rip),%xmm0
divsd  %xmm1,%xmm0
mov    %ebx,%edi
call   1050 <pow@plt>
movsd  0x18(%rsp),%xmm0
xorps  %xmm2,%xmm2
cvtsi2sd %eax,%xmm2
mulsd  0x10(%rsp),%xmm2
add    $0x1,%ebx
xorps  %xmm1,%xmm1
cvtsi2sd %ebx,%xmm1
addsd  %xmm2,%xmm0
movsd  (%rsp),%xmm2
ucomisd %xmm1,%xmm2
jae    11a0 <main+0x70>
cvttsd2si 0x8(%rsp),%esi
cvttsd2si %xmm2,%edx
lea    0xe39(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Enter the value of x : \00"
@.str.1 = [4 x i8] c"%lf\00"
@.str.2 = [31 x i8] c"Enter the number of terms n : \00"
@.str.3 = [56 x i8] c"The value of log(%d) approximated to %d terms is : %lf\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double* %2)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double* %3)
store double 0.000000e+00, double* %4
store i32 1, i32* %5
br label %10
10: ; preds = %28, %0
%11 = load i32, i32* %5
%12 = sitofp i32 %11 to double
%13 = load double, double* %3
%14 = fcmp ole double %12, %13
br i1 %14, label %15, label %31
15: ; preds = %10
%16 = load double, double* %4
%17 = load i32, i32* %5
%18 = sitofp i32 %17 to double
%19 = fdiv double 1.000000e+00, %18
%20 = load double, double* %2
%21 = fsub double %20, 1.000000e+00
%22 = load double, double* %2
%23 = fdiv double %21, %22
%24 = load i32, i32* %5
%25 = call i32 @pow(double %23, i32 %24)
%26 = sitofp i32 %25 to double
%27 = call double @llvm.fmuladd.f64(double %19, double %26, double %16)
store double %27, double* %4
br label %28
28: ; preds = %15
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
br label %10
31: ; preds = %10
%32 = load double, double* %2
%33 = fptosi double %32 to i32
%34 = load double, double* %3
%35 = fptosi double %34 to i32
%36 = load double, double* %4
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %33, i32 %35, double %36)
ret i32 0
}

LLVM IR (-O2):
@.str = [24 x i8] c"Enter the value of x : \00"
@.str.1 = [4 x i8] c"%lf\00"
@.str.2 = [31 x i8] c"Enter the number of terms n : \00"
@.str.3 = [56 x i8] c"The value of log(%d) approximated to %d terms is : %lf\0A\00"
define i32 @main() {
%1 = alloca double
%2 = alloca double
%3 = bitcast double* %1 to i8*
%4 = bitcast double* %2 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double* %1)
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double* %2)
%9 = load double, double* %2
%10 = fcmp ult double %9, 1.000000e+00
br i1 %10, label %26, label %11
11: ; preds = %0, %11
%12 = phi double [ %23, %11 ], [ 1.000000e+00, %0 ]
%13 = phi i32 [ %22, %11 ], [ 1, %0 ]
%14 = phi double [ %21, %11 ], [ 0.000000e+00, %0 ]
%15 = fdiv double 1.000000e+00, %12
%16 = load double, double* %1
%17 = fadd double %16, -1.000000e+00
%18 = fdiv double %17, %16
%19 = call i32 @pow(double %18, i32 %13)
%20 = sitofp i32 %19 to double
%21 = call double @llvm.fmuladd.f64(double %15, double %20, double %14)
%22 = add nuw nsw i32 %13, 1
%23 = sitofp i32 %22 to double
%24 = load double, double* %2
%25 = fcmp ult double %24, %23
br i1 %25, label %26, label %11
26: ; preds = %11, %0
%27 = phi double [ 0.000000e+00, %0 ], [ %21, %11 ]
%28 = phi double [ %9, %0 ], [ %24, %11 ]
%29 = load double, double* %1
%30 = fptosi double %29 to i32
%31 = fptosi double %28 to i32
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %30, i32 %31, double %27)
ret i32 0
}