#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 */
unsigned int const callendar_van_dusen(double);
unsigned int const callendar_van_dusen_derivative(double);
double const fabs(double);
double newton_approx(const double R, const unsigned int R0,
                     const double max_residual) {
  double T = 0.0;
  double residual;
  do {
    residual = R - R0 * callendar_van_dusen(T);
    T += residual / (R0 * callendar_van_dusen_derivative(T));
  } while (fabs(residual) > max_residual);
  return T;
}


x86 assembly (clang, -O0):
<newton_approx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
movsd  %xmm1,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1030 <callendar_van_dusen@plt>
movsd  -0x48(%rbp),%xmm0
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
imul   %ecx,%eax
cvtsi2sd %rax,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1050 <callendar_van_dusen_derivative@plt>
movsd  -0x38(%rbp),%xmm0
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
imul   %ecx,%eax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1040 <fabs@plt>
ucomisd -0x18(%rbp),%xmm0
ja     114d <newton_approx+0x1d>
movsd  -0x20(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newton_approx>:
push   %rbx
sub    $0x30,%rsp
movsd  %xmm1,0x18(%rsp)
mov    %edi,%ebx
movsd  %xmm0,0x10(%rsp)
xorpd  %xmm0,%xmm0
nopw   0x0(%rax,%rax,1)
movsd  %xmm0,0x8(%rsp)
call   1030 <callendar_van_dusen@plt>
imul   %ebx,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  0x10(%rsp),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,0x20(%rsp)
movsd  0x8(%rsp),%xmm0
call   1040 <callendar_van_dusen_derivative@plt>
imul   %ebx,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
movapd 0x20(%rsp),%xmm2
movapd %xmm2,%xmm0
divsd  %xmm1,%xmm0
addsd  0x8(%rsp),%xmm0
andpd  0xe68(%rip),%xmm2
ucomisd 0x18(%rsp),%xmm2
ja     1140 <newton_approx+0x20>
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define double @newton_approx(double %0, i32 %1, double %2) {
%4 = alloca double
%5 = alloca i32
%6 = alloca double
%7 = alloca double
%8 = alloca double
store double %0, double* %4
store i32 %1, i32* %5
store double %2, double* %6
store double 0.000000e+00, double* %7
br label %9
9: ; preds = %26, %3
%10 = load double, double* %4
%11 = load i32, i32* %5
%12 = load double, double* %7
%13 = call i32 @callendar_van_dusen(double %12)
%14 = mul i32 %11, %13
%15 = uitofp i32 %14 to double
%16 = fsub double %10, %15
store double %16, double* %8
%17 = load double, double* %8
%18 = load i32, i32* %5
%19 = load double, double* %7
%20 = call i32 @callendar_van_dusen_derivative(double %19)
%21 = mul i32 %18, %20
%22 = uitofp i32 %21 to double
%23 = fdiv double %17, %22
%24 = load double, double* %7
%25 = fadd double %24, %23
store double %25, double* %7
br label %26
26: ; preds = %9
%27 = load double, double* %8
%28 = call double @fabs(double %27)
%29 = load double, double* %6
%30 = fcmp ogt double %28, %29
br i1 %30, label %9, label %31
31: ; preds = %26
%32 = load double, double* %7
ret double %32
}

LLVM IR (-O2):
define double @newton_approx(double %0, i32 %1, double %2) {
br label %4
4: ; preds = %4, %3
%5 = phi double [ 0.000000e+00, %3 ], [ %14, %4 ]
%6 = tail call i32 @callendar_van_dusen(double %5)
%7 = mul i32 %6, %1
%8 = uitofp i32 %7 to double
%9 = fsub double %0, %8
%10 = tail call i32 @callendar_van_dusen_derivative(double %5)
%11 = mul i32 %10, %1
%12 = uitofp i32 %11 to double
%13 = fdiv double %9, %12
%14 = fadd double %5, %13
%15 = tail call double @llvm.fabs.f64(double %9)
%16 = fcmp ogt double %15, %2
br i1 %16, label %4, label %17
17: ; preds = %4
ret double %14
}