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


x86 assembly (clang, -O0):
<newtonSqaureRoot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  %xmm1,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
movsd  0xeb7(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1030 <fabs@plt>
cvtsi2sd %eax,%xmm0
xor    %eax,%eax
movsd  0xea2(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x35(%rbp)
jbe    1180 <newtonSqaureRoot+0x60>
cmpl   $0xc8,-0x34(%rbp)
setl   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    1190 <newtonSqaureRoot+0x70>
jmp    11db <newtonSqaureRoot+0xbb>
mov    -0x8(%rbp),%rax
movsd  -0x18(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
call   *%rax
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x18(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x30(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
divsd  -0x30(%rbp),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    114e <newtonSqaureRoot+0x2e>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
movsd  -0x18(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
mov    -0x34(%rbp),%edx
lea    0xe18(%rip),%rsi
call   1040 <fprintf@plt>
movsd  -0x18(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newtonSqaureRoot>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movsd  %xmm1,0x10(%rsp)
movsd  %xmm0,(%rsp)
mov    %rsi,%r14
mov    %rdi,%r15
movsd  0xebd(%rip),%xmm0
call   1030 <fabs@plt>
test   %eax,%eax
jle    11a4 <newtonSqaureRoot+0x84>
xor    %ebx,%ebx
xchg   %ax,%ax
movsd  (%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
call   *%r15
movsd  %xmm0,0x8(%rsp)
movsd  (%rsp),%xmm0
call   *%r14
movsd  0x8(%rsp),%xmm2
movapd %xmm2,%xmm1
divsd  %xmm0,%xmm1
movsd  (%rsp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,(%rsp)
lea    0x1(%rbx),%ebp
movapd %xmm2,%xmm0
call   1030 <fabs@plt>
test   %eax,%eax
jle    11b4 <newtonSqaureRoot+0x94>
cmp    $0xc7,%ebx
mov    %ebp,%ebx
jb     1150 <newtonSqaureRoot+0x30>
jmp    11b4 <newtonSqaureRoot+0x94>
movsd  0xe54(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %ebp,%ebp
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
movsd  (%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
mov    %ebp,%edx
call   1040 <fprintf@plt>
movsd  (%rsp),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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