#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 newton(double, double, int, int *, int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, double *, double *, int *);
int main(void) {
  double x_0;
  double x;
  double tol;
  int max_iters;
  int iters;
  int converged;
  printf("Enter x_0, tol, and max_iters\n");
  scanf("%lf %lf %d", &x_0, &tol, &max_iters);
  printf("x_0 = %f, tol = %f, max_iters = %d\n", x_0, tol, max_iters);
  x = newton(x_0, tol, max_iters, &iters, &converged);
  if (converged) {
    printf("Method converged after %d iterations.\n", iters);
    printf("The approximate solution is %.15e\n", x);
    printf("f(%.15e) = %.15e\n", x, f(x));
  } else {
    printf("Method didn't converge after %d iterations.\n", iters);
    printf("The final estimate was %.15e\n", x);
    printf("f(%.15e) = %.15e\n", x, f(x));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xebb(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x24(%rbp),%rcx
call   1060 <scanf@plt>
movsd  -0x10(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
mov    -0x24(%rbp),%esi
lea    0xea1(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
movsd  -0x10(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <newton@plt>
movsd  %xmm0,-0x18(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     120c <main+0xcc>
mov    -0x28(%rbp),%esi
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsd  -0x18(%rbp),%xmm0
lea    0xe9f(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1030 <f@plt>
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
lea    0xe98(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
jmp    125a <main+0x11a>
mov    -0x28(%rbp),%esi
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsd  -0x18(%rbp),%xmm0
lea    0xeae(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1030 <f@plt>
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
lea    0xe45(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
lea    0xf7a(%rip),%rdi
call   1030 <puts@plt>
lea    0xe98(%rip),%rdi
lea    0x28(%rsp),%rsi
lea    0x20(%rsp),%rdx
lea    0xc(%rsp),%rcx
call   1070 <scanf@plt>
movsd  0x28(%rsp),%xmm0
movsd  0x20(%rsp),%xmm1
mov    0xc(%rsp),%esi
lea    0xe78(%rip),%rdi
mov    $0x2,%al
call   1050 <printf@plt>
movsd  0x28(%rsp),%xmm0
movsd  0x20(%rsp),%xmm1
mov    0xc(%rsp),%edi
lea    0x1c(%rsp),%rsi
lea    0x18(%rsp),%rdx
call   1060 <newton@plt>
movsd  %xmm0,0x10(%rsp)
cmpl   $0x0,0x18(%rsp)
mov    0x1c(%rsp),%esi
lea    0xebc(%rip),%rax
lea    0xe59(%rip),%rdi
cmove  %rax,%rdi
lea    0xed7(%rip),%rax
lea    0xe6e(%rip),%rbx
cmove  %rax,%rbx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
movsd  0x10(%rsp),%xmm0
mov    $0x1,%al
call   1050 <printf@plt>
movsd  0x10(%rsp),%xmm0
call   1040 <f@plt>
movaps %xmm0,%xmm1
lea    0xe61(%rip),%rdi
movsd  0x10(%rsp),%xmm0
mov    $0x2,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Enter x_0, tol, and max_iters\0A\00"
@.str.1 = [11 x i8] c"%lf %lf %d\00"
@.str.2 = [36 x i8] c"x_0 = %f, tol = %f, max_iters = %d\0A\00"
@.str.3 = [39 x i8] c"Method converged after %d iterations.\0A\00"
@.str.4 = [35 x i8] c"The approximate solution is %.15e\0A\00"
@.str.5 = [18 x i8] c"f(%.15e) = %.15e\0A\00"
@.str.6 = [45 x i8] c"Method didn't converge after %d iterations.\0A\00"
@.str.7 = [30 x i8] c"The final estimate was %.15e\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), double* %2, double* %4, i32* %5)
%10 = load double, double* %2
%11 = load double, double* %4
%12 = load i32, i32* %5
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), double %10, double %11, i32 %12)
%14 = load double, double* %2
%15 = load double, double* %4
%16 = load i32, i32* %5
%17 = call double @newton(double %14, double %15, i32 %16, i32* %6, i32* %7)
store double %17, double* %3
%18 = load i32, i32* %7
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %0
%21 = load i32, i32* %6
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = load double, double* %3
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), double %23)
%25 = load double, double* %3
%26 = load double, double* %3
%27 = call double @f(double %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), double %25, double %27)
br label %38
29: ; preds = %0
%30 = load i32, i32* %6
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 %30)
%32 = load double, double* %3
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), double %32)
%34 = load double, double* %3
%35 = load double, double* %3
%36 = call double @f(double %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), double %34, double %36)
br label %38
38: ; preds = %29, %20
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"%lf %lf %d\00"
@.str.2 = [36 x i8] c"x_0 = %f, tol = %f, max_iters = %d\0A\00"
@.str.3 = [39 x i8] c"Method converged after %d iterations.\0A\00"
@.str.4 = [35 x i8] c"The approximate solution is %.15e\0A\00"
@.str.5 = [18 x i8] c"f(%.15e) = %.15e\0A\00"
@.str.6 = [45 x i8] c"Method didn't converge after %d iterations.\0A\00"
@.str.7 = [30 x i8] c"The final estimate was %.15e\0A\00"
@str = [30 x i8] c"Enter x_0, tol, and max_iters\00"
define i32 @main() {
%1 = alloca double
%2 = alloca double
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast double* %1 to i8*
%7 = bitcast double* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), double* %1, double* %2, i32* %3)
%13 = load double, double* %1
%14 = load double, double* %2
%15 = load i32, i32* %3
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), double %13, double %14, i32 %15)
%17 = load double, double* %1
%18 = load double, double* %2
%19 = load i32, i32* %3
%20 = call double @newton(double %17, double %18, i32 %19, i32* %4, i32* %5)
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 0
%23 = load i32, i32* %4
%24 = select i1 %22, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0)
%25 = select i1 %22, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %24, i32 %23)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %25, double %20)
%28 = call double @f(double %20)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), double %20, double %28)
ret i32 0
}