#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 f1(double);
double f_prime1(double);
double fabs(double);
int /*<<< orphan*/ printf(char *, ...);
double newtons_method(double x_0, double error_tol, double max_iters) {
  int count = 0;
  double x_new = x_0;
  printf("Now calculating \n");
  printf(". \n");
  printf(".. \n\n");
  do {
    x_0 = x_new;
    count++;
    if (f_prime1(x_0) == 0) {
      printf("f_prime( x_old ) is zero!\n");
      break;
    }
    x_new = x_0 - f1(x_0) / f_prime1(x_0);
  } while (fabs(x_new - x_0) > error_tol && (count <= max_iters));
  printf("Results: \n");
  printf("x_0 = %lf \n", x_0);
  printf("x_new = %lf \n", x_new);
  printf("Number of iterations = %d \n", count);
  printf("count = %d \n", count);
  printf("error_tol = %lf \n", error_tol);
  if (count > max_iters) {
    printf("Iteration did not convereg.\n\n");
  } else {
    printf("Iteration did convereg.\n\n");
  }
  return x_new;
}


x86 assembly (clang, -O0):
<newtons_method>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeb5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1050 <f_prime1@plt>
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11c5 <newtons_method+0x95>
jp     11c5 <newtons_method+0x95>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1246 <newtons_method+0x116>
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1040 <f1@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1050 <f_prime1@plt>
movsd  -0x38(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x30(%rbp),%xmm0
divsd  %xmm2,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
movaps 0xded(%rip),%xmm1
pand   %xmm1,%xmm0
xor    %eax,%eax
ucomisd -0x10(%rbp),%xmm0
mov    %al,-0x39(%rbp)
jbe    123b <newtons_method+0x10b>
cvtsi2sdl -0x1c(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
setae  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1182 <newtons_method+0x52>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsd  -0x8(%rbp),%xmm0
lea    0xdf2(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
movsd  -0x28(%rbp),%xmm0
lea    0xdeb(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsd  -0x10(%rbp),%xmm0
lea    0xded(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
cvtsi2sdl -0x1c(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    12d2 <newtons_method+0x1a2>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12e0 <newtons_method+0x1b0>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsd  -0x28(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newtons_method>:
push   %rbx
sub    $0x30,%rsp
movsd  %xmm2,0x20(%rsp)
movsd  %xmm1,0x28(%rsp)
movsd  %xmm0,0x8(%rsp)
lea    0xf0f(%rip),%rdi
call   1030 <puts@plt>
lea    0xf14(%rip),%rdi
call   1030 <puts@plt>
lea    0xf0b(%rip),%rdi
call   1030 <puts@plt>
movsd  0x8(%rsp),%xmm1
mov    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
movapd %xmm1,%xmm0
mov    %eax,%ebx
movsd  %xmm1,0x18(%rsp)
call   1060 <f_prime1@plt>
ucomisd 0xe67(%rip),%xmm0
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
movsd  %xmm0,0x10(%rsp)
jne    11ba <newtons_method+0x7a>
jnp    1221 <newtons_method+0xe1>
movsd  0x18(%rsp),%xmm0
call   1050 <f1@plt>
movsd  %xmm0,0x8(%rsp)
movsd  0x18(%rsp),%xmm0
call   1060 <f_prime1@plt>
movsd  0x8(%rsp),%xmm3
divsd  %xmm0,%xmm3
movsd  0x18(%rsp),%xmm2
movapd %xmm2,%xmm1
subsd  %xmm3,%xmm1
movapd %xmm1,%xmm0
subsd  %xmm2,%xmm0
andpd  0xe02(%rip),%xmm0
ucomisd 0x28(%rsp),%xmm0
jbe    1233 <newtons_method+0xf3>
lea    0x1(%rbx),%eax
movsd  0x20(%rsp),%xmm0
movsd  0x10(%rsp),%xmm2
ucomisd %xmm2,%xmm0
jae    1190 <newtons_method+0x50>
jmp    1239 <newtons_method+0xf9>
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
movsd  0x18(%rsp),%xmm1
movsd  0x10(%rsp),%xmm2
movsd  %xmm2,0x10(%rsp)
movsd  %xmm1,0x8(%rsp)
lea    0xe3a(%rip),%rdi
call   1030 <puts@plt>
lea    0xdc0(%rip),%rdi
movsd  0x18(%rsp),%xmm0
mov    $0x1,%al
call   1040 <printf@plt>
lea    0xdb8(%rip),%rdi
movsd  0x8(%rsp),%xmm0
mov    $0x1,%al
call   1040 <printf@plt>
lea    0xdb2(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdbe(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdbb(%rip),%rdi
movsd  0x28(%rsp),%xmm0
mov    $0x1,%al
call   1040 <printf@plt>
movsd  0x10(%rsp),%xmm0
ucomisd 0x20(%rsp),%xmm0
lea    0xde9(%rip),%rax
lea    0xdc9(%rip),%rdi
cmova  %rax,%rdi
call   1030 <puts@plt>
movsd  0x8(%rsp),%xmm0
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Now calculating \0A\00"
@.str.1 = [4 x i8] c". \0A\00"
@.str.2 = [6 x i8] c".. \0A\0A\00"
@.str.3 = [27 x i8] c"f_prime( x_old ) is zero!\0A\00"
@.str.4 = [11 x i8] c"Results: \0A\00"
@.str.5 = [12 x i8] c"x_0 = %lf \0A\00"
@.str.6 = [14 x i8] c"x_new = %lf \0A\00"
@.str.7 = [28 x i8] c"Number of iterations = %d \0A\00"
@.str.8 = [13 x i8] c"count = %d \0A\00"
@.str.9 = [18 x i8] c"error_tol = %lf \0A\00"
@.str.10 = [30 x i8] c"Iteration did not convereg.\0A\0A\00"
@.str.11 = [26 x i8] c"Iteration did convereg.\0A\0A\00"
define double @newtons_method(double %0, double %1, double %2) {
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca double
store double %0, double* %4
store double %1, double* %5
store double %2, double* %6
store i32 0, i32* %7
%9 = load double, double* %4
store double %9, double* %8
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %42, %3
%14 = load double, double* %8
store double %14, double* %4
%15 = load i32, i32* %7
%16 = add nsw i32 %15, 1
store i32 %16, i32* %7
%17 = load double, double* %4
%18 = call double @f_prime1(double %17)
%19 = fcmp oeq double %18, 0.000000e+00
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %44
22: ; preds = %13
%23 = load double, double* %4
%24 = load double, double* %4
%25 = call double @f1(double %24)
%26 = load double, double* %4
%27 = call double @f_prime1(double %26)
%28 = fdiv double %25, %27
%29 = fsub double %23, %28
store double %29, double* %8
br label %30
30: ; preds = %22
%31 = load double, double* %8
%32 = load double, double* %4
%33 = fsub double %31, %32
%34 = call double @llvm.fabs.f64(double %33)
%35 = load double, double* %5
%36 = fcmp ogt double %34, %35
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = load i32, i32* %7
%39 = sitofp i32 %38 to double
%40 = load double, double* %6
%41 = fcmp ole double %39, %40
br label %42
42: ; preds = %37, %30
%43 = phi i1 [ false, %30 ], [ %41, %37 ]
br i1 %43, label %13, label %44
44: ; preds = %42, %20
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%46 = load double, double* %4
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), double %46)
%48 = load double, double* %8
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), double %48)
%50 = load i32, i32* %7
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %7
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 %52)
%54 = load double, double* %5
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), double %54)
%56 = load i32, i32* %7
%57 = sitofp i32 %56 to double
%58 = load double, double* %6
%59 = fcmp ogt double %57, %58
br i1 %59, label %60, label %62
60: ; preds = %44
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.10, i64 0, i64 0))
br label %64
62: ; preds = %44
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.11, i64 0, i64 0))
br label %64
64: ; preds = %62, %60
%65 = load double, double* %8
ret double %65
}

LLVM IR (-O2):
@.str.5 = [12 x i8] c"x_0 = %lf \0A\00"
@.str.6 = [14 x i8] c"x_new = %lf \0A\00"
@.str.7 = [28 x i8] c"Number of iterations = %d \0A\00"
@.str.8 = [13 x i8] c"count = %d \0A\00"
@.str.9 = [18 x i8] c"error_tol = %lf \0A\00"
@str = [17 x i8] c"Now calculating \00"
@str.12 = [3 x i8] c". \00"
@str.13 = [5 x i8] c".. \0A\00"
@str.14 = [10 x i8] c"Results: \00"
@str.15 = [25 x i8] c"Iteration did convereg.\0A\00"
@str.16 = [29 x i8] c"Iteration did not convereg.\0A\00"
@str.17 = [26 x i8] c"f_prime( x_old ) is zero!\00"
define double @newtons_method(double %0, double %1, double %2) {
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str.12, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.13, i64 0, i64 0))
br label %7
7: ; preds = %16, %3
%8 = phi i32 [ 0, %3 ], [ %10, %16 ]
%9 = phi double [ %0, %3 ], [ %20, %16 ]
%10 = add nuw nsw i32 %8, 1
%11 = tail call double @f_prime1(double %9)
%12 = fcmp oeq double %11, 0.000000e+00
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.17, i64 0, i64 0))
%15 = sitofp i32 %10 to double
br label %27
16: ; preds = %7
%17 = tail call double @f1(double %9)
%18 = tail call double @f_prime1(double %9)
%19 = fdiv double %17, %18
%20 = fsub double %9, %19
%21 = fsub double %20, %9
%22 = tail call double @llvm.fabs.f64(double %21)
%23 = fcmp ogt double %22, %1
%24 = sitofp i32 %10 to double
%25 = fcmp ole double %24, %2
%26 = select i1 %23, i1 %25, i1 false
br i1 %26, label %7, label %27
27: ; preds = %16, %13
%28 = phi double [ %15, %13 ], [ %24, %16 ]
%29 = phi double [ %9, %13 ], [ %20, %16 ]
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.14, i64 0, i64 0))
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), double %9)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), double %29)
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %10)
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 %10)
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), double %1)
%36 = fcmp ogt double %28, %2
%37 = select i1 %36, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @str.16, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @str.15, i64 0, i64 0)
%38 = tail call i32 @puts(i8* dereferenceable(1) %37)
ret double %29
}