#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 fabs(double);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, double, double, int);
int /*<<< orphan*/ stdout;
double newton(double (*f)(double), double (*fd)(double), double x) {
  double fx, dfx;
  int i;
  i = 0;
  fx = 1.0;
  while ((fabs(fx) > 1e-9) && (i < 200)) {
    fx = f(x);
    dfx = fd(x);
    x = x - fx / dfx;
    i++;
  }
  fprintf(stdout, "Newton: %f %f %d\n", fx, x, i);
  return x;
}


x86 assembly (clang, -O0):
<newton>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
movsd  0xebc(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1030 <fabs@plt>
cvtsi2sd %eax,%xmm0
xor    %eax,%eax
movsd  0xea7(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x2d(%rbp)
jbe    117b <newton+0x5b>
cmpl   $0xc8,-0x2c(%rbp)
setl   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    118b <newton+0x6b>
jmp    11d1 <newton+0xb1>
mov    -0x8(%rbp),%rax
movsd  -0x18(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x18(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
divsd  -0x28(%rbp),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1149 <newton+0x29>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
mov    -0x2c(%rbp),%edx
lea    0xe22(%rip),%rsi
call   1040 <fprintf@plt>
movsd  -0x18(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newton>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movsd  %xmm0,0x8(%rsp)
mov    %rsi,%r14
mov    %rdi,%r15
movsd  0xec2(%rip),%xmm0
call   1030 <fabs@plt>
test   %eax,%eax
jle    11a2 <newton+0x82>
xor    %ebx,%ebx
nopl   0x0(%rax)
movsd  0x8(%rsp),%xmm0
call   *%r15
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
call   *%r14
movsd  0x10(%rsp),%xmm2
movapd %xmm2,%xmm1
divsd  %xmm0,%xmm1
movsd  0x8(%rsp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,0x8(%rsp)
lea    0x1(%rbx),%ebp
movapd %xmm2,%xmm0
call   1030 <fabs@plt>
test   %eax,%eax
jle    11b2 <newton+0x92>
cmp    $0xc7,%ebx
mov    %ebp,%ebx
jb     1150 <newton+0x30>
jmp    11b2 <newton+0x92>
movsd  0xe56(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
xor    %ebp,%ebp
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
movsd  0x10(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
mov    %ebp,%edx
call   1040 <fprintf@plt>
movsd  0x8(%rsp),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [18 x i8] c"Newton: %f %f %d\0A\00"
define double @newton(double (double)* %0, double (double)* %1, double %2) {
%4 = alloca double (double)*
%5 = alloca double (double)*
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca i32
store double (double)* %0, double (double)** %4
store double (double)* %1, double (double)** %5
store double %2, double* %6
store i32 0, i32* %9
store double 1.000000e+00, double* %7
br label %10
10: ; preds = %20, %3
%11 = load double, double* %7
%12 = call i32 @fabs(double %11)
%13 = sitofp i32 %12 to double
%14 = fcmp ogt double %13, 1.000000e-09
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 200
br label %18
18: ; preds = %15, %10
%19 = phi i1 [ false, %10 ], [ %17, %15 ]
br i1 %19, label %20, label %34
20: ; preds = %18
%21 = load double (double)*, double (double)** %4
%22 = load double, double* %6
%23 = call double %21(double %22)
store double %23, double* %7
%24 = load double (double)*, double (double)** %5
%25 = load double, double* %6
%26 = call double %24(double %25)
store double %26, double* %8
%27 = load double, double* %6
%28 = load double, double* %7
%29 = load double, double* %8
%30 = fdiv double %28, %29
%31 = fsub double %27, %30
store double %31, double* %6
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
store i32 %33, i32* %9
br label %10
34: ; preds = %18
%35 = load i32, i32* @stdout
%36 = load double, double* %7
%37 = load double, double* %6
%38 = load i32, i32* %9
%39 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), double %36, double %37, i32 %38)
%40 = load double, double* %6
ret double %40
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [18 x i8] c"Newton: %f %f %d\0A\00"
define double @newton(double (double)* nocapture %0, double (double)* nocapture %1, double %2) {
%4 = tail call i32 @fabs(double 1.000000e+00)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %18
6: ; preds = %3, %6
%7 = phi i32 [ %13, %6 ], [ 0, %3 ]
%8 = phi double [ %12, %6 ], [ %2, %3 ]
%9 = tail call double %0(double %8)
%10 = tail call double %1(double %8)
%11 = fdiv double %9, %10
%12 = fsub double %8, %11
%13 = add nuw nsw i32 %7, 1
%14 = tail call i32 @fabs(double %9)
%15 = icmp sgt i32 %14, 0
%16 = icmp ult i32 %7, 199
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %6, label %18
18: ; preds = %6, %3
%19 = phi double [ 1.000000e+00, %3 ], [ %9, %6 ]
%20 = phi double [ %2, %3 ], [ %12, %6 ]
%21 = phi i32 [ 0, %3 ], [ %13, %6 ]
%22 = load i32, i32* @stdout
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), double %19, double %20, i32 %21)
ret double %20
}