#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 /*<<< orphan*/ clrscr();
float df(float);
float f(float);
double fabs(float);
int /*<<< orphan*/ getch();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, float *);
int main() {
  int i, n = 30;
  float x_new, x_old;
  clrscr();
  printf("Finding real roots of x^3 - 4x + 1 = 0 by Newton Raphson\n\n");
  printf("Enter x : ");
  scanf("%f", &x_new);
  printf("\nComputing roots...\n");
  for (i = 0; i < n; i++) {
    x_old = x_new;
    if (df(x_new) <= 0) {
      printf("\nDerivate of f(x) is approx. 0.\nMethod Failed.");
      getch();
      return 1;
    }
    x_new = x_old - (f(x_new) / df(x_new));
    printf("\nPass[%d] :\tx = %.4f", i + 1, x_new);
    if (fabs((x_new - x_old) / x_old) <= 0.0001) {
      printf("\n\nReal roots is %.4f", x_new);
      getch();
      return 0;
    }
  }
  printf("\nThe required solution does not converge or iterations are "
         "insufficient.");
  getch();
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1e,-0xc(%rbp)
mov    $0x0,%al
call   1030 <clrscr@plt>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe9e(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <scanf@plt>
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12de <main+0x16e>
movss  -0x10(%rbp),%xmm0
movss  %xmm0,-0x14(%rbp)
movss  -0x10(%rbp),%xmm0
call   1090 <df@plt>
movaps %xmm0,%xmm1
xorps  %xmm0,%xmm0
ucomiss %xmm1,%xmm0
jb     121e <main+0xae>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <getch@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fa <main+0x18a>
movss  -0x14(%rbp),%xmm0
movss  %xmm0,-0x18(%rbp)
movss  -0x10(%rbp),%xmm0
call   1040 <f@plt>
movss  %xmm0,-0x1c(%rbp)
movss  -0x10(%rbp),%xmm0
call   1090 <df@plt>
movss  -0x1c(%rbp),%xmm1
movaps %xmm0,%xmm2
movss  -0x18(%rbp),%xmm0
divss  %xmm2,%xmm1
subss  %xmm1,%xmm0
movss  %xmm0,-0x10(%rbp)
mov    -0x8(%rbp),%esi
add    $0x1,%esi
movss  -0x10(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe24(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
movss  -0x10(%rbp),%xmm0
subss  -0x14(%rbp),%xmm0
divss  -0x14(%rbp),%xmm0
call   1060 <fabs@plt>
movaps %xmm0,%xmm1
movsd  0xd69(%rip),%xmm0
ucomisd %xmm1,%xmm0
jb     12cb <main+0x15b>
movss  -0x10(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdf9(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <getch@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fa <main+0x18a>
jmp    12d0 <main+0x160>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11ce <main+0x5e>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <getch@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1030 <clrscr@plt>
lea    0xf27(%rip),%rdi
call   1040 <puts@plt>
lea    0xe6b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe68(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1080 <scanf@plt>
lea    0xf36(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
lea    0xe78(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x1f,%ebx
je     1296 <main+0x116>
movss  0xc(%rsp),%xmm0
movss  %xmm0,0x10(%rsp)
call   10a0 <df@plt>
xorps  %xmm1,%xmm1
ucomiss %xmm0,%xmm1
jae    129f <main+0x11f>
movss  0xc(%rsp),%xmm0
call   1050 <f@plt>
movss  %xmm0,0x14(%rsp)
movss  0xc(%rsp),%xmm0
call   10a0 <df@plt>
movss  0x14(%rsp),%xmm1
divss  %xmm0,%xmm1
movss  0x10(%rsp),%xmm0
subss  %xmm1,%xmm0
movss  %xmm0,0xc(%rsp)
cvtss2sd %xmm0,%xmm0
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x1,%al
call   1060 <printf@plt>
movss  0xc(%rsp),%xmm0
movss  0x10(%rsp),%xmm1
subss  %xmm1,%xmm0
divss  %xmm1,%xmm0
call   1070 <fabs@plt>
add    $0x1,%ebx
movsd  0xd90(%rip),%xmm1
ucomisd %xmm0,%xmm1
jb     11e0 <main+0x60>
movss  0xc(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdd3(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
xor    %ebx,%ebx
jmp    12b2 <main+0x132>
lea    0xdd6(%rip),%rdi
jmp    12a6 <main+0x126>
lea    0xd74(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebx
xor    %eax,%eax
call   1090 <getch@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [59 x i8] c"Finding real roots of x^3 - 4x + 1 = 0 by Newton Raphson\0A\0A\00"
@.str.1 = [11 x i8] c"Enter x : \00"
@.str.2 = [3 x i8] c"%f\00"
@.str.3 = [21 x i8] c"\0AComputing roots...\0A\00"
@.str.4 = [47 x i8] c"\0ADerivate of f(x) is approx. 0.\0AMethod Failed.\00"
@.str.5 = [21 x i8] c"\0APass[%d] :\09x = %.4f\00"
@.str.6 = [21 x i8] c"\0A\0AReal roots is %.4f\00"
@.str.7 = [73 x i8] c"\0AThe required solution does not converge or iterations are insufficient.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca float
%5 = alloca float
store i32 0, i32* %1
store i32 30, i32* %3
%6 = call i32 (...) @clrscr()
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), float* %4)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %11
11: ; preds = %49, %0
%12 = load i32, i32* %2
%13 = load i32, i32* %3
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %52
15: ; preds = %11
%16 = load float, float* %4
store float %16, float* %5
%17 = load float, float* %4
%18 = call float @df(float %17)
%19 = fcmp ole float %18, 0.000000e+00
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 (...) @getch()
store i32 1, i32* %1
br label %55
23: ; preds = %15
%24 = load float, float* %5
%25 = load float, float* %4
%26 = call float @f(float %25)
%27 = load float, float* %4
%28 = call float @df(float %27)
%29 = fdiv float %26, %28
%30 = fsub float %24, %29
store float %30, float* %4
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
%33 = load float, float* %4
%34 = fpext float %33 to double
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %32, double %34)
%36 = load float, float* %4
%37 = load float, float* %5
%38 = fsub float %36, %37
%39 = load float, float* %5
%40 = fdiv float %38, %39
%41 = call double @fabs(float %40)
%42 = fcmp ole double %41, 1.000000e-04
br i1 %42, label %43, label %48
43: ; preds = %23
%44 = load float, float* %4
%45 = fpext float %44 to double
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), double %45)
%47 = call i32 (...) @getch()
store i32 0, i32* %1
br label %55
48: ; preds = %23
br label %49
49: ; preds = %48
%50 = load i32, i32* %2
%51 = add nsw i32 %50, 1
store i32 %51, i32* %2
br label %11
52: ; preds = %11
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.7, i64 0, i64 0))
%54 = call i32 (...) @getch()
store i32 1, i32* %1
br label %55
55: ; preds = %52, %43, %20
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"Enter x : \00"
@.str.2 = [3 x i8] c"%f\00"
@.str.4 = [47 x i8] c"\0ADerivate of f(x) is approx. 0.\0AMethod Failed.\00"
@.str.5 = [21 x i8] c"\0APass[%d] :\09x = %.4f\00"
@.str.6 = [21 x i8] c"\0A\0AReal roots is %.4f\00"
@.str.7 = [73 x i8] c"\0AThe required solution does not converge or iterations are insufficient.\00"
@str = [58 x i8] c"Finding real roots of x^3 - 4x + 1 = 0 by Newton Raphson\0A\00"
@str.8 = [20 x i8] c"\0AComputing roots...\00"
define i32 @main() {
%1 = alloca float
%2 = bitcast float* %1 to i8*
%3 = tail call i32 (...) @clrscr()
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), float* %1)
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0))
br label %8
8: ; preds = %17, %0
%9 = phi i32 [ 0, %0 ], [ %24, %17 ]
%10 = icmp eq i32 %9, 30
br i1 %10, label %36, label %11
11: ; preds = %8
%12 = load float, float* %1
%13 = call float @df(float %12)
%14 = fcmp ugt float %13, 0.000000e+00
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
br label %38
17: ; preds = %11
%18 = load float, float* %1
%19 = call float @f(float %18)
%20 = load float, float* %1
%21 = call float @df(float %20)
%22 = fdiv float %19, %21
%23 = fsub float %12, %22
store float %23, float* %1
%24 = add nuw nsw i32 %9, 1
%25 = fpext float %23 to double
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %24, double %25)
%27 = load float, float* %1
%28 = fsub float %27, %12
%29 = fdiv float %28, %12
%30 = call double @fabs(float %29)
%31 = fcmp ugt double %30, 1.000000e-04
br i1 %31, label %8, label %32
32: ; preds = %17
%33 = load float, float* %1
%34 = fpext float %33 to double
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), double %34)
br label %38
36: ; preds = %8
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([73 x i8], [73 x i8]* @.str.7, i64 0, i64 0))
br label %38
38: ; preds = %36, %32, %15
%39 = phi i32 [ 1, %15 ], [ 0, %32 ], [ 1, %36 ]
%40 = call i32 (...) @getch()
ret i32 %39
}