#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);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  printf("Bisection Method for solution of xsinx + cosx = 0\n");
  printf("x is in the interval of (0, PI)\n");
  double lowerBound = 0;
  double upperBound = 3.14159265;
  double midPoint = (lowerBound + upperBound) / 2;
  double interval;
  int i = 0;
  printf("i\tlowerbound\tupperbound\tmidpoint\tf(mid)\t\tinterval\n");
  while (1) {
    interval = upperBound - lowerBound;
    if (F(midPoint) == 0 || interval < 0.000001) {
      break;
    }
    printf("%d\t%f\t%f\t%f\t%f\t%f\n", i, lowerBound, upperBound, midPoint,
           F(midPoint), interval);
    if (F(midPoint) * F(lowerBound) < 0) {
      upperBound = midPoint;
    } else {
      lowerBound = midPoint;
    }
    i++;
    midPoint = (lowerBound + upperBound) / 2;
  }
  printf("Final Answer: %f\n", midPoint);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xee2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  0xead(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  0xe8e(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    0xee3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1030 <F@plt>
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11c1 <main+0xa1>
jp     11c1 <main+0xa1>
jmp    11d4 <main+0xb4>
movsd  0xe47(%rip),%xmm0
ucomisd -0x28(%rbp),%xmm0
jbe    11d9 <main+0xb9>
jmp    12a0 <main+0x180>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x54(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1030 <F@plt>
mov    -0x54(%rbp),%esi
movsd  -0x50(%rbp),%xmm1
movsd  -0x48(%rbp),%xmm2
movaps %xmm0,%xmm3
movsd  -0x40(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm4
lea    0xe77(%rip),%rdi
mov    $0x5,%al
call   1040 <printf@plt>
movsd  -0x20(%rbp),%xmm0
call   1030 <F@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <F@plt>
movsd  -0x38(%rbp),%xmm1
mulsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    126d <main+0x14d>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    1277 <main+0x157>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
movsd  -0x10(%rbp),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  0xd6e(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1190 <main+0x70>
movsd  -0x20(%rbp),%xmm0
lea    0xe06(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
lea    0xf07(%rip),%rdi
call   1030 <puts@plt>
lea    0xf2d(%rip),%rdi
call   1030 <puts@plt>
lea    0xf41(%rip),%rdi
call   1030 <puts@plt>
movsd  0xe9d(%rip),%xmm0
call   1040 <F@plt>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
movsd  0xe88(%rip),%xmm0
jne    1180 <main+0x50>
jnp    129e <main+0x16e>
movsd  0xe78(%rip),%xmm0
xor    %ebx,%ebx
movsd  0xe76(%rip),%xmm2
lea    0xe87(%rip),%r14
xorpd  %xmm1,%xmm1
movsd  %xmm1,0x8(%rsp)
movapd %xmm2,%xmm1
nopw   0x0(%rax,%rax,1)
movapd %xmm1,0x20(%rsp)
movapd %xmm2,0x30(%rsp)
movaps %xmm0,0x10(%rsp)
movaps 0x10(%rsp),%xmm0
call   1040 <F@plt>
movaps %xmm0,%xmm3
mov    %r14,%rdi
mov    %ebx,%esi
movsd  0x8(%rsp),%xmm0
movapd 0x20(%rsp),%xmm1
movaps 0x10(%rsp),%xmm2
movaps 0x30(%rsp),%xmm4
mov    $0x5,%al
call   1050 <printf@plt>
movaps 0x10(%rsp),%xmm0
call   1040 <F@plt>
movsd  %xmm0,0x30(%rsp)
movsd  0x8(%rsp),%xmm0
call   1040 <F@plt>
xorpd  %xmm1,%xmm1
movapd %xmm0,%xmm2
movapd 0x10(%rsp),%xmm0
mulsd  0x30(%rsp),%xmm2
ucomisd %xmm2,%xmm1
ja     122b <main+0xfb>
movsd  %xmm0,0x8(%rsp)
cmpltsd %xmm1,%xmm2
andpd  %xmm2,%xmm0
andnpd 0x20(%rsp),%xmm2
orpd   %xmm0,%xmm2
movapd %xmm2,0x20(%rsp)
movsd  0x8(%rsp),%xmm0
addsd  %xmm2,%xmm0
mulsd  0xdba(%rip),%xmm0
movapd %xmm0,0x10(%rsp)
call   1040 <F@plt>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    126d <main+0x13d>
jnp    1299 <main+0x169>
add    $0x1,%ebx
movapd 0x20(%rsp),%xmm1
movapd %xmm1,%xmm2
subsd  0x8(%rsp),%xmm2
movsd  0xd90(%rip),%xmm0
ucomisd %xmm2,%xmm0
movaps 0x10(%rsp),%xmm0
jbe    11b0 <main+0x80>
jmp    129e <main+0x16e>
movaps 0x10(%rsp),%xmm0
lea    0xd8e(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [51 x i8] c"Bisection Method for solution of xsinx + cosx = 0\0A\00"
@.str.1 = [33 x i8] c"x is in the interval of (0, PI)\0A\00"
@.str.2 = [51 x i8] c"i\09lowerbound\09upperbound\09midpoint\09f(mid)\09\09interval\0A\00"
@.str.3 = [19 x i8] c"%d\09%f\09%f\09%f\09%f\09%f\0A\00"
@.str.4 = [18 x i8] c"Final Answer: %f\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store double 0.000000e+00, double* %2
store double 0x400921FB53C8D4F1, double* %3
%9 = load double, double* %2
%10 = load double, double* %3
%11 = fadd double %9, %10
%12 = fdiv double %11, 2.000000e+00
store double %12, double* %4
store i32 0, i32* %6
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %0, %44
%15 = load double, double* %3
%16 = load double, double* %2
%17 = fsub double %15, %16
store double %17, double* %5
%18 = load double, double* %4
%19 = call double @F(double %18)
%20 = fcmp oeq double %19, 0.000000e+00
br i1 %20, label %24, label %21
21: ; preds = %14
%22 = load double, double* %5
%23 = fcmp olt double %22, 0x3EB0C6F7A0B5ED8D
br i1 %23, label %24, label %25
24: ; preds = %21, %14
br label %51
25: ; preds = %21
%26 = load i32, i32* %6
%27 = load double, double* %2
%28 = load double, double* %3
%29 = load double, double* %4
%30 = load double, double* %4
%31 = call double @F(double %30)
%32 = load double, double* %5
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %26, double %27, double %28, double %29, double %31, double %32)
%34 = load double, double* %4
%35 = call double @F(double %34)
%36 = load double, double* %2
%37 = call double @F(double %36)
%38 = fmul double %35, %37
%39 = fcmp olt double %38, 0.000000e+00
br i1 %39, label %40, label %42
40: ; preds = %25
%41 = load double, double* %4
store double %41, double* %3
br label %44
42: ; preds = %25
%43 = load double, double* %4
store double %43, double* %2
br label %44
44: ; preds = %42, %40
%45 = load i32, i32* %6
%46 = add nsw i32 %45, 1
store i32 %46, i32* %6
%47 = load double, double* %2
%48 = load double, double* %3
%49 = fadd double %47, %48
%50 = fdiv double %49, 2.000000e+00
store double %50, double* %4
br label %14
51: ; preds = %24
%52 = load double, double* %4
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), double %52)
ret i32 0
}

LLVM IR (-O2):
@.str.3 = [19 x i8] c"%d\09%f\09%f\09%f\09%f\09%f\0A\00"
@.str.4 = [18 x i8] c"Final Answer: %f\0A\00"
@str = [50 x i8] c"Bisection Method for solution of xsinx + cosx = 0\00"
@str.5 = [32 x i8] c"x is in the interval of (0, PI)\00"
@str.6 = [50 x i8] c"i\09lowerbound\09upperbound\09midpoint\09f(mid)\09\09interval\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.5, i64 0, i64 0))
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.6, i64 0, i64 0))
%4 = tail call double @F(double 0x3FF921FB53C8D4F1)
%5 = fcmp oeq double %4, 0.000000e+00
br i1 %5, label %28, label %6
6: ; preds = %0, %6
%7 = phi double [ %23, %6 ], [ 0x400921FB53C8D4F1, %0 ]
%8 = phi i32 [ %20, %6 ], [ 0, %0 ]
%9 = phi double [ %19, %6 ], [ 0.000000e+00, %0 ]
%10 = phi double [ %22, %6 ], [ 0x3FF921FB53C8D4F1, %0 ]
%11 = phi double [ %18, %6 ], [ 0x400921FB53C8D4F1, %0 ]
%12 = tail call double @F(double %10)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %8, double %9, double %11, double %10, double %12, double %7)
%14 = tail call double @F(double %10)
%15 = tail call double @F(double %9)
%16 = fmul double %14, %15
%17 = fcmp olt double %16, 0.000000e+00
%18 = select i1 %17, double %10, double %11
%19 = select i1 %17, double %9, double %10
%20 = add nuw nsw i32 %8, 1
%21 = fadd double %19, %18
%22 = fmul double %21, 5.000000e-01
%23 = fsub double %18, %19
%24 = tail call double @F(double %22)
%25 = fcmp oeq double %24, 0.000000e+00
%26 = fcmp olt double %23, 0x3EB0C6F7A0B5ED8D
%27 = select i1 %25, i1 true, i1 %26
br i1 %27, label %28, label %6
28: ; preds = %6, %0
%29 = phi double [ 0x3FF921FB53C8D4F1, %0 ], [ %22, %6 ]
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), double %29)
ret i32 0
}