#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 f(double);
double f_prime(double);
double fabs(double);
int /*<<< orphan*/ printf(char *);
double newton(double x_0, double tol, int max_iters, int *iters_p,
              int *converged_p) {
  double x_new = x_0;
  double x_old;
  double divisor;
  int iter = 0;
  do {
    iter++;
    x_old = x_new;
    divisor = f_prime(x_old);
    if (divisor == 0.0) {
      printf("f_prime = 0!\n");
      break;
    }
    x_new = x_old - f(x_old) / divisor;
  } while (fabs(x_new - x_old) > tol && iter < max_iters);
  if (fabs(x_new - x_old) <= tol)
    *converged_p = 1;
  else
    *converged_p = 0;
  *iters_p = iter;
  return x_new;
}


x86 assembly (clang, -O0):
<newton>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    %rsi,-0x20(%rbp)
mov    %rdx,-0x28(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1050 <f_prime@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11a9 <newton+0x79>
jp     11a9 <newton+0x79>
lea    0xe71(%rip),%rdi
call   1040 <printf@plt>
jmp    120f <newton+0xdf>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1030 <f@plt>
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
divsd  -0x40(%rbp),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
subsd  -0x38(%rbp),%xmm0
movaps 0xe1c(%rip),%xmm1
pand   %xmm1,%xmm0
xor    %eax,%eax
ucomisd -0x10(%rbp),%xmm0
mov    %al,-0x51(%rbp)
jbe    1204 <newton+0xd4>
mov    -0x44(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    115e <newton+0x2e>
movsd  -0x30(%rbp),%xmm1
subsd  -0x38(%rbp),%xmm1
movaps 0xde0(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     1242 <newton+0x112>
mov    -0x28(%rbp),%rax
movl   $0x1,(%rax)
jmp    124c <newton+0x11c>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x44(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movsd  -0x30(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newton>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
movsd  %xmm1,0x8(%rsp)
movapd %xmm0,%xmm2
mov    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %eax,%ebx
movsd  %xmm2,(%rsp)
movapd %xmm2,%xmm0
call   1050 <f_prime@plt>
ucomisd 0xe98(%rip),%xmm0
jne    117c <newton+0x4c>
jnp    11c8 <newton+0x98>
movsd  %xmm0,0x10(%rsp)
movsd  (%rsp),%xmm0
call   1040 <f@plt>
movapd %xmm0,%xmm1
divsd  0x10(%rsp),%xmm1
movsd  (%rsp),%xmm2
movapd %xmm2,%xmm0
subsd  %xmm1,%xmm0
movapd %xmm0,%xmm1
subsd  %xmm2,%xmm1
andpd  0xe4d(%rip),%xmm1
ucomisd 0x8(%rsp),%xmm1
jbe    11e9 <newton+0xb9>
lea    0x1(%rbx),%eax
movapd %xmm0,%xmm2
cmp    %ebp,%ebx
jl     1160 <newton+0x30>
jmp    11e9 <newton+0xb9>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
movsd  (%rsp),%xmm0
movapd %xmm0,%xmm1
subsd  %xmm0,%xmm1
andpd  0xe17(%rip),%xmm1
xor    %eax,%eax
movsd  0x8(%rsp),%xmm2
ucomisd %xmm1,%xmm2
setae  %al
mov    %eax,(%r14)
mov    %ebx,(%r15)
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"f_prime = 0!\0A\00"
define double @newton(double %0, double %1, i32 %2, i32* %3, i32* %4) {
%6 = alloca double
%7 = alloca double
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca i32
store double %0, double* %6
store double %1, double* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32* %4, i32** %10
%15 = load double, double* %6
store double %15, double* %11
store i32 0, i32* %14
br label %16
16: ; preds = %44, %5
%17 = load i32, i32* %14
%18 = add nsw i32 %17, 1
store i32 %18, i32* %14
%19 = load double, double* %11
store double %19, double* %12
%20 = load double, double* %12
%21 = call double @f_prime(double %20)
store double %21, double* %13
%22 = load double, double* %13
%23 = fcmp oeq double %22, 0.000000e+00
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %46
26: ; preds = %16
%27 = load double, double* %12
%28 = load double, double* %12
%29 = call double @f(double %28)
%30 = load double, double* %13
%31 = fdiv double %29, %30
%32 = fsub double %27, %31
store double %32, double* %11
br label %33
33: ; preds = %26
%34 = load double, double* %11
%35 = load double, double* %12
%36 = fsub double %34, %35
%37 = call double @llvm.fabs.f64(double %36)
%38 = load double, double* %7
%39 = fcmp ogt double %37, %38
br i1 %39, label %40, label %44
40: ; preds = %33
%41 = load i32, i32* %14
%42 = load i32, i32* %8
%43 = icmp slt i32 %41, %42
br label %44
44: ; preds = %40, %33
%45 = phi i1 [ false, %33 ], [ %43, %40 ]
br i1 %45, label %16, label %46
46: ; preds = %44, %24
%47 = load double, double* %11
%48 = load double, double* %12
%49 = fsub double %47, %48
%50 = call double @llvm.fabs.f64(double %49)
%51 = load double, double* %7
%52 = fcmp ole double %50, %51
br i1 %52, label %53, label %55
53: ; preds = %46
%54 = load i32*, i32** %10
store i32 1, i32* %54
br label %57
55: ; preds = %46
%56 = load i32*, i32** %10
store i32 0, i32* %56
br label %57
57: ; preds = %55, %53
%58 = load i32, i32* %14
%59 = load i32*, i32** %9
store i32 %58, i32* %59
%60 = load double, double* %11
ret double %60
}

LLVM IR (-O2):
@str = [13 x i8] c"f_prime = 0!\00"
define double @newton(double %0, double %1, i32 %2, i32* nocapture writeonly %3, i32* nocapture writeonly %4) {
br label %6
6: ; preds = %16, %5
%7 = phi double [ %0, %5 ], [ %19, %16 ]
%8 = phi i32 [ 0, %5 ], [ %9, %16 ]
%9 = add nuw nsw i32 %8, 1
%10 = tail call double @f_prime(double %7)
%11 = fcmp oeq double %10, 0.000000e+00
br i1 %11, label %12, label %16
12: ; preds = %6
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%14 = fsub double %7, %7
%15 = tail call double @llvm.fabs.f64(double %14)
br label %25
16: ; preds = %6
%17 = tail call double @f(double %7)
%18 = fdiv double %17, %10
%19 = fsub double %7, %18
%20 = fsub double %19, %7
%21 = tail call double @llvm.fabs.f64(double %20)
%22 = fcmp ogt double %21, %1
%23 = icmp slt i32 %9, %2
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %6, label %25
25: ; preds = %16, %12
%26 = phi double [ %15, %12 ], [ %21, %16 ]
%27 = phi double [ %7, %12 ], [ %19, %16 ]
%28 = fcmp ole double %26, %1
%29 = zext i1 %28 to i32
store i32 %29, i32* %4
store i32 %9, i32* %3
ret double %27
}