#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 fabs(double);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, double, double, int);
int /*<<< orphan*/ stdout;
double secant(double (*f)(double), double x1, double x2) {
  double f1, f2 = 1.0, x;
  int i = 0;
  while ((fabs(f2) > 1e-9) && (i < 200)) {
    f1 = f(x1);
    f2 = f(x2);
    x = x2 - ((f2 * (x2 - x1)) / (f2 - f1));
    x1 = x2;
    x2 = x;
    i++;
  }
  fprintf(stdout, "Secant: %f %f %d\n", f1, x, i);
  return x;
}


x86 assembly (clang, -O0):
<secant>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  0xec2(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1030 <fabs@plt>
cvtsi2sd %eax,%xmm0
xor    %eax,%eax
movsd  0xea6(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x35(%rbp)
jbe    117c <secant+0x5c>
cmpl   $0xc8,-0x34(%rbp)
setl   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    118c <secant+0x6c>
jmp    11fd <secant+0xdd>
mov    -0x8(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x18(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm2
subsd  -0x10(%rbp),%xmm2
mulsd  %xmm2,%xmm1
movsd  -0x28(%rbp),%xmm2
subsd  -0x20(%rbp),%xmm2
divsd  %xmm2,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    114a <secant+0x2a>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
movsd  -0x20(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
mov    -0x34(%rbp),%edx
lea    0xdf6(%rip),%rsi
call   1040 <fprintf@plt>
movsd  -0x30(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secant>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movsd  %xmm1,(%rsp)
movsd  %xmm0,0x8(%rsp)
mov    %rdi,%r14
movsd  0xec2(%rip),%xmm0
call   1030 <fabs@plt>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x18(%rsp)
movsd  (%rsp),%xmm1
movsd  %xmm1,0x8(%rsp)
call   *%r14
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
call   *%r14
movsd  0x8(%rsp),%xmm1
subsd  0x18(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movapd %xmm0,%xmm2
subsd  0x10(%rsp),%xmm2
divsd  %xmm2,%xmm1
movsd  (%rsp),%xmm2
subsd  %xmm1,%xmm2
movsd  %xmm2,(%rsp)
lea    0x1(%rbx),%ebp
call   1030 <fabs@plt>
test   %eax,%eax
jle    11bb <secant+0x9b>
cmp    $0xc7,%ebx
mov    %ebp,%ebx
jb     1150 <secant+0x30>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
movsd  0x10(%rsp),%xmm0
movsd  (%rsp),%xmm1
mov    %ebp,%edx
call   1040 <fprintf@plt>
movsd  (%rsp),%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [18 x i8] c"Secant: %f %f %d\0A\00"
define double @secant(double (double)* %0, double %1, double %2) {
%4 = alloca double (double)*
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca i32
store double (double)* %0, double (double)** %4
store double %1, double* %5
store double %2, double* %6
store double 1.000000e+00, double* %8
store i32 0, i32* %10
br label %11
11: ; preds = %21, %3
%12 = load double, double* %8
%13 = call i32 @fabs(double %12)
%14 = sitofp i32 %13 to double
%15 = fcmp ogt double %14, 1.000000e-09
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i32, i32* %10
%18 = icmp slt i32 %17, 200
br label %19
19: ; preds = %16, %11
%20 = phi i1 [ false, %11 ], [ %18, %16 ]
br i1 %20, label %21, label %43
21: ; preds = %19
%22 = load double (double)*, double (double)** %4
%23 = load double, double* %5
%24 = call double %22(double %23)
store double %24, double* %7
%25 = load double (double)*, double (double)** %4
%26 = load double, double* %6
%27 = call double %25(double %26)
store double %27, double* %8
%28 = load double, double* %6
%29 = load double, double* %8
%30 = load double, double* %6
%31 = load double, double* %5
%32 = fsub double %30, %31
%33 = fmul double %29, %32
%34 = load double, double* %8
%35 = load double, double* %7
%36 = fsub double %34, %35
%37 = fdiv double %33, %36
%38 = fsub double %28, %37
store double %38, double* %9
%39 = load double, double* %6
store double %39, double* %5
%40 = load double, double* %9
store double %40, double* %6
%41 = load i32, i32* %10
%42 = add nsw i32 %41, 1
store i32 %42, i32* %10
br label %11
43: ; preds = %19
%44 = load i32, i32* @stdout
%45 = load double, double* %7
%46 = load double, double* %9
%47 = load i32, i32* %10
%48 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), double %45, double %46, i32 %47)
%49 = load double, double* %9
ret double %49
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [18 x i8] c"Secant: %f %f %d\0A\00"
define double @secant(double (double)* nocapture %0, double %1, double %2) {
%4 = tail call i32 @fabs(double 1.000000e+00)
%5 = icmp sgt i32 %4, 0
br label %6
6: ; preds = %3, %6
%7 = phi i32 [ %17, %6 ], [ 0, %3 ]
%8 = phi double [ %16, %6 ], [ %2, %3 ]
%9 = phi double [ %8, %6 ], [ %1, %3 ]
%10 = tail call double %0(double %9)
%11 = tail call double %0(double %8)
%12 = fsub double %8, %9
%13 = fmul double %12, %11
%14 = fsub double %11, %10
%15 = fdiv double %13, %14
%16 = fsub double %8, %15
%17 = add nuw nsw i32 %7, 1
%18 = tail call i32 @fabs(double %11)
%19 = icmp sgt i32 %18, 0
%20 = icmp ult i32 %7, 199
%21 = select i1 %19, i1 %20, i1 false
br i1 %21, label %6, label %22
22: ; preds = %6
%23 = load i32, i32* @stdout
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), double %10, double %16, i32 %17)
ret double %16
}