#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 */
float f(float);
double fabs(float);
float fprime(float);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, float *);
int main() {
  int iter = 0;
  float xnew, xold;
  printf("\nDOWN THE TOILET COMPANY");
  printf("\nNewton-Raphson to find zero of f(x) = x^3 - 0.165x^2 + 0.0003993");
  printf("\nPlease enter initial value:");
  scanf("%f", &xnew);
  do {
    xold = xnew;
    if (fprime(xold) == 0)
      return -1;
    xnew = xold - f(xold) / fprime(xold);
  } while ((fabs(xnew - xold) > 0.000001) && (++iter < 10000));
  if (iter > 10000)
    return -1;
  printf("\nThe zero found is: %.5f", xnew);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xee9(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1070 <scanf@plt>
movss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x10(%rbp)
movss  -0x10(%rbp),%xmm0
call   1060 <fprime@plt>
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jne    11d2 <main+0x82>
jp     11d2 <main+0x82>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <main+0x13b>
movss  -0x10(%rbp),%xmm0
movss  %xmm0,-0x14(%rbp)
movss  -0x10(%rbp),%xmm0
call   1030 <f@plt>
movss  %xmm0,-0x18(%rbp)
movss  -0x10(%rbp),%xmm0
call   1060 <fprime@plt>
movss  -0x18(%rbp),%xmm1
movaps %xmm0,%xmm2
movss  -0x14(%rbp),%xmm0
divss  %xmm2,%xmm1
subss  %xmm1,%xmm0
movss  %xmm0,-0xc(%rbp)
movss  -0xc(%rbp),%xmm0
subss  -0x10(%rbp),%xmm0
call   1050 <fabs@plt>
xor    %eax,%eax
movsd  0xdd8(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x19(%rbp)
jbe    1249 <main+0xf9>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
cmp    $0x2710,%eax
setl   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11a0 <main+0x50>
cmpl   $0x2710,-0x8(%rbp)
jle    126d <main+0x11d>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <main+0x13b>
movss  -0xc(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe06(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xeb0(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xebb(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeef(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xefe(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1070 <scanf@plt>
mov    $0x270f,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movss  0x8(%rsp),%xmm0
movss  %xmm0,0x4(%rsp)
call   1060 <fprime@plt>
ucomiss 0xe50(%rip),%xmm0
jne    11bc <main+0x6c>
jnp    122e <main+0xde>
movss  0x4(%rsp),%xmm0
call   1030 <f@plt>
movss  %xmm0,0xc(%rsp)
movss  0x4(%rsp),%xmm0
call   1060 <fprime@plt>
movss  0xc(%rsp),%xmm2
divss  %xmm0,%xmm2
movss  0x4(%rsp),%xmm1
movaps %xmm1,%xmm0
subss  %xmm2,%xmm0
movss  %xmm0,0x8(%rsp)
subss  %xmm1,%xmm0
call   1050 <fabs@plt>
sub    $0x1,%ebx
setb   %al
ucomisd 0xdf4(%rip),%xmm0
jbe    1212 <main+0xc2>
test   %al,%al
je     11a0 <main+0x50>
movss  0x8(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe64(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
jmp    1233 <main+0xe3>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [25 x i8] c"\0ADOWN THE TOILET COMPANY\00"
@.str.1 = [66 x i8] c"\0ANewton-Raphson to find zero of f(x) = x^3 - 0.165x^2 + 0.0003993\00"
@.str.2 = [29 x i8] c"\0APlease enter initial value:\00"
@.str.3 = [3 x i8] c"%f\00"
@.str.4 = [25 x i8] c"\0AThe zero found is: %.5f\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca float
%4 = alloca float
store i32 0, i32* %1
store i32 0, i32* %2
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), float* %3)
br label %9
9: ; preds = %33, %0
%10 = load float, float* %3
store float %10, float* %4
%11 = load float, float* %4
%12 = call float @fprime(float %11)
%13 = fcmp oeq float %12, 0.000000e+00
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 -1, i32* %1
br label %43
15: ; preds = %9
%16 = load float, float* %4
%17 = load float, float* %4
%18 = call float @f(float %17)
%19 = load float, float* %4
%20 = call float @fprime(float %19)
%21 = fdiv float %18, %20
%22 = fsub float %16, %21
store float %22, float* %3
br label %23
23: ; preds = %15
%24 = load float, float* %3
%25 = load float, float* %4
%26 = fsub float %24, %25
%27 = call double @fabs(float %26)
%28 = fcmp ogt double %27, 0x3EB0C6F7A0B5ED8D
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i32, i32* %2
%31 = add nsw i32 %30, 1
store i32 %31, i32* %2
%32 = icmp slt i32 %31, 10000
br label %33
33: ; preds = %29, %23
%34 = phi i1 [ false, %23 ], [ %32, %29 ]
br i1 %34, label %9, label %35
35: ; preds = %33
%36 = load i32, i32* %2
%37 = icmp sgt i32 %36, 10000
br i1 %37, label %38, label %39
38: ; preds = %35
store i32 -1, i32* %1
br label %43
39: ; preds = %35
%40 = load float, float* %3
%41 = fpext float %40 to double
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), double %41)
store i32 0, i32* %1
br label %43
43: ; preds = %39, %38, %14
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str = [25 x i8] c"\0ADOWN THE TOILET COMPANY\00"
@.str.1 = [66 x i8] c"\0ANewton-Raphson to find zero of f(x) = x^3 - 0.165x^2 + 0.0003993\00"
@.str.2 = [29 x i8] c"\0APlease enter initial value:\00"
@.str.3 = [3 x i8] c"%f\00"
@.str.4 = [25 x i8] c"\0AThe zero found is: %.5f\00"
define i32 @main() {
%1 = alloca float
%2 = bitcast float* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), float* %1)
br label %7
7: ; preds = %12, %0
%8 = phi i32 [ 0, %0 ], [ %20, %12 ]
%9 = load float, float* %1
%10 = call float @fprime(float %9)
%11 = fcmp oeq float %10, 0.000000e+00
br i1 %11, label %27, label %12
12: ; preds = %7
%13 = call float @f(float %9)
%14 = call float @fprime(float %9)
%15 = fdiv float %13, %14
%16 = fsub float %9, %15
store float %16, float* %1
%17 = fsub float %16, %9
%18 = call double @fabs(float %17)
%19 = fcmp ule double %18, 0x3EB0C6F7A0B5ED8D
%20 = add nuw nsw i32 %8, 1
%21 = icmp eq i32 %20, 10000
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %23, label %7
23: ; preds = %12
%24 = load float, float* %1
%25 = fpext float %24 to double
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), double %25)
br label %27
27: ; preds = %7, %23
%28 = phi i32 [ 0, %23 ], [ -1, %7 ]
ret i32 %28
}