#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, int, int);
int sqrt(int);
int /*<<< orphan*/ stdout;
double golden(double (*f)(double), double a, double b) {
  double goldenRatio = (sqrt(5) - 1) / 2;
  int i = 0;
  double x1, x2;
  x1 = b - goldenRatio * (b - a);
  x2 = a + goldenRatio * (b - a);
  while ((fabs(b - a) > 1e-9) && (i < 200)) {
    if (f(x2) > f(x1)) {
      b = x2;
      x2 = x1;
      x1 = b - goldenRatio * (b - a);
    } else {
      a = x1;
      x1 = x2;
      x2 = a + goldenRatio * (b - a);
    }
    i++;
  }
  fprintf(stdout, "Golden: %f %f %d\n", f(fabs(b + a) / 2), fabs(b + a) / 2, i);
  return fabs(b + a) / 2;
}


x86 assembly (clang, -O0):
<golden>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    $0x5,%edi
call   1050 <sqrt@plt>
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
subsd  -0x10(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
subsd  -0x10(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
call   1030 <fabs@plt>
cvtsi2sd %eax,%xmm0
xor    %eax,%eax
movsd  0xe1f(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x39(%rbp)
jbe    11fb <golden+0xcb>
cmpl   $0xc8,-0x24(%rbp)
setl   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    120b <golden+0xdb>
jmp    12cc <golden+0x19c>
mov    -0x8(%rbp),%rax
movsd  -0x38(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x48(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x30(%rbp),%xmm0
call   *%rax
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1289 <golden+0x159>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
subsd  -0x10(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    12be <golden+0x18e>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
subsd  -0x10(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c4 <golden+0x94>
mov    0x2d0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
movsd  -0x18(%rbp),%xmm0
addsd  -0x10(%rbp),%xmm0
call   1030 <fabs@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x60(%rbp),%rax
cvtsi2sd %ecx,%xmm0
call   *%rax
movsd  %xmm0,-0x50(%rbp)
movsd  -0x18(%rbp),%xmm0
addsd  -0x10(%rbp),%xmm0
call   1030 <fabs@plt>
mov    -0x54(%rbp),%edi
movsd  -0x50(%rbp),%xmm0
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
mov    -0x24(%rbp),%ecx
lea    0xcd5(%rip),%rsi
call   1040 <fprintf@plt>
movsd  -0x18(%rbp),%xmm0
addsd  -0x10(%rbp),%xmm0
call   1030 <fabs@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<golden>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x58,%rsp
movsd  %xmm1,(%rsp)
movsd  %xmm0,0x10(%rsp)
mov    %rdi,%r15
mov    $0x5,%edi
call   1050 <sqrt@plt>
mov    %eax,%ebp
movsd  (%rsp),%xmm0
subsd  0x10(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
call   1030 <fabs@plt>
test   %eax,%eax
jle    1292 <golden+0x162>
mov    %ebp,%eax
add    $0xffffffff,%eax
shr    $0x1f,%eax
add    %ebp,%eax
add    $0xffffffff,%eax
sar    %eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movapd 0xe70(%rip),%xmm1
xorpd  %xmm0,%xmm1
movapd %xmm0,0x40(%rsp)
movsd  0x8(%rsp),%xmm2
mulsd  %xmm2,%xmm0
addsd  0x10(%rsp),%xmm0
movapd %xmm1,0x30(%rsp)
mulsd  %xmm1,%xmm2
addsd  (%rsp),%xmm2
movsd  %xmm2,0x8(%rsp)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movapd %xmm0,0x20(%rsp)
call   *%r15
movsd  %xmm0,0x18(%rsp)
movsd  0x8(%rsp),%xmm0
call   *%r15
movsd  0x18(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jbe    1230 <golden+0x100>
movapd 0x20(%rsp),%xmm2
movapd %xmm2,%xmm0
subsd  0x10(%rsp),%xmm0
movapd 0x30(%rsp),%xmm1
mulsd  %xmm0,%xmm1
addsd  %xmm2,%xmm1
movsd  %xmm1,0x18(%rsp)
movsd  %xmm2,(%rsp)
movsd  0x8(%rsp),%xmm1
jmp    125e <golden+0x12e>
cs nopw 0x0(%rax,%rax,1)
nop
movsd  (%rsp),%xmm0
movsd  0x8(%rsp),%xmm2
subsd  %xmm2,%xmm0
movaps 0x20(%rsp),%xmm1
movsd  %xmm1,0x18(%rsp)
movapd 0x40(%rsp),%xmm1
mulsd  %xmm0,%xmm1
addsd  %xmm2,%xmm1
movsd  %xmm2,0x10(%rsp)
movapd %xmm1,0x20(%rsp)
lea    0x1(%rbx),%ebp
call   1030 <fabs@plt>
test   %eax,%eax
jle    1294 <golden+0x164>
movsd  0x18(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
cmp    $0xc7,%ebx
mov    %ebp,%ebx
movapd 0x20(%rsp),%xmm0
jb     11d0 <golden+0xa0>
jmp    1294 <golden+0x164>
xor    %ebp,%ebp
movsd  (%rsp),%xmm0
addsd  0x10(%rsp),%xmm0
movsd  %xmm0,(%rsp)
mov    0x2d35(%rip),%rax
mov    (%rax),%r14d
call   1030 <fabs@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
call   *%r15
movsd  %xmm0,0x20(%rsp)
movsd  (%rsp),%xmm0
call   1030 <fabs@plt>
mov    %eax,%edx
shr    $0x1f,%edx
add    %eax,%edx
sar    %edx
lea    0xd2a(%rip),%rsi
mov    %r14d,%edi
movsd  0x20(%rsp),%xmm0
mov    %ebp,%ecx
call   1040 <fprintf@plt>
movsd  (%rsp),%xmm0
call   1030 <fabs@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [18 x i8] c"Golden: %f %f %d\0A\00"
define double @golden(double (double)* %0, double %1, double %2) {
%4 = alloca double (double)*
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca i32
%9 = alloca double
%10 = alloca double
store double (double)* %0, double (double)** %4
store double %1, double* %5
store double %2, double* %6
%11 = call i32 @sqrt(i32 5)
%12 = sub nsw i32 %11, 1
%13 = sdiv i32 %12, 2
%14 = sitofp i32 %13 to double
store double %14, double* %7
store i32 0, i32* %8
%15 = load double, double* %6
%16 = load double, double* %7
%17 = load double, double* %6
%18 = load double, double* %5
%19 = fsub double %17, %18
%20 = fneg double %16
%21 = call double @llvm.fmuladd.f64(double %20, double %19, double %15)
store double %21, double* %9
%22 = load double, double* %5
%23 = load double, double* %7
%24 = load double, double* %6
%25 = load double, double* %5
%26 = fsub double %24, %25
%27 = call double @llvm.fmuladd.f64(double %23, double %26, double %22)
store double %27, double* %10
br label %28
28: ; preds = %67, %3
%29 = load double, double* %6
%30 = load double, double* %5
%31 = fsub double %29, %30
%32 = call i32 @fabs(double %31)
%33 = sitofp i32 %32 to double
%34 = fcmp ogt double %33, 1.000000e-09
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = load i32, i32* %8
%37 = icmp slt i32 %36, 200
br label %38
38: ; preds = %35, %28
%39 = phi i1 [ false, %28 ], [ %37, %35 ]
br i1 %39, label %40, label %70
40: ; preds = %38
%41 = load double (double)*, double (double)** %4
%42 = load double, double* %10
%43 = call double %41(double %42)
%44 = load double (double)*, double (double)** %4
%45 = load double, double* %9
%46 = call double %44(double %45)
%47 = fcmp ogt double %43, %46
br i1 %47, label %48, label %58
48: ; preds = %40
%49 = load double, double* %10
store double %49, double* %6
%50 = load double, double* %9
store double %50, double* %10
%51 = load double, double* %6
%52 = load double, double* %7
%53 = load double, double* %6
%54 = load double, double* %5
%55 = fsub double %53, %54
%56 = fneg double %52
%57 = call double @llvm.fmuladd.f64(double %56, double %55, double %51)
store double %57, double* %9
br label %67
58: ; preds = %40
%59 = load double, double* %9
store double %59, double* %5
%60 = load double, double* %10
store double %60, double* %9
%61 = load double, double* %5
%62 = load double, double* %7
%63 = load double, double* %6
%64 = load double, double* %5
%65 = fsub double %63, %64
%66 = call double @llvm.fmuladd.f64(double %62, double %65, double %61)
store double %66, double* %10
br label %67
67: ; preds = %58, %48
%68 = load i32, i32* %8
%69 = add nsw i32 %68, 1
store i32 %69, i32* %8
br label %28
70: ; preds = %38
%71 = load i32, i32* @stdout
%72 = load double (double)*, double (double)** %4
%73 = load double, double* %6
%74 = load double, double* %5
%75 = fadd double %73, %74
%76 = call i32 @fabs(double %75)
%77 = sdiv i32 %76, 2
%78 = sitofp i32 %77 to double
%79 = call double %72(double %78)
%80 = load double, double* %6
%81 = load double, double* %5
%82 = fadd double %80, %81
%83 = call i32 @fabs(double %82)
%84 = sdiv i32 %83, 2
%85 = load i32, i32* %8
%86 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), double %79, i32 %84, i32 %85)
%87 = load double, double* %6
%88 = load double, double* %5
%89 = fadd double %87, %88
%90 = call i32 @fabs(double %89)
%91 = sdiv i32 %90, 2
%92 = sitofp i32 %91 to double
ret double %92
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [18 x i8] c"Golden: %f %f %d\0A\00"
define double @golden(double (double)* nocapture %0, double %1, double %2) {
%4 = tail call i32 @sqrt(i32 5)
%5 = add nsw i32 %4, -1
%6 = sdiv i32 %5, 2
%7 = sitofp i32 %6 to double
%8 = fsub double %2, %1
%9 = fneg double %7
%10 = tail call i32 @fabs(double %8)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %41
12: ; preds = %3
%13 = tail call double @llvm.fmuladd.f64(double %7, double %8, double %1)
%14 = tail call double @llvm.fmuladd.f64(double %9, double %8, double %2)
br label %15
15: ; preds = %12, %30
%16 = phi double [ %35, %30 ], [ %13, %12 ]
%17 = phi double [ %34, %30 ], [ %14, %12 ]
%18 = phi i32 [ %36, %30 ], [ 0, %12 ]
%19 = phi double [ %33, %30 ], [ %1, %12 ]
%20 = phi double [ %32, %30 ], [ %2, %12 ]
%21 = tail call double %0(double %16)
%22 = tail call double %0(double %17)
%23 = fcmp ogt double %21, %22
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = fsub double %16, %19
%26 = tail call double @llvm.fmuladd.f64(double %9, double %25, double %16)
br label %30
27: ; preds = %15
%28 = fsub double %20, %17
%29 = tail call double @llvm.fmuladd.f64(double %7, double %28, double %17)
br label %30
30: ; preds = %27, %24
%31 = phi double [ %28, %27 ], [ %25, %24 ]
%32 = phi double [ %20, %27 ], [ %16, %24 ]
%33 = phi double [ %17, %27 ], [ %19, %24 ]
%34 = phi double [ %16, %27 ], [ %26, %24 ]
%35 = phi double [ %29, %27 ], [ %17, %24 ]
%36 = add nuw nsw i32 %18, 1
%37 = tail call i32 @fabs(double %31)
%38 = icmp sgt i32 %37, 0
%39 = icmp ult i32 %18, 199
%40 = select i1 %38, i1 %39, i1 false
br i1 %40, label %15, label %41
41: ; preds = %30, %3
%42 = phi double [ %2, %3 ], [ %32, %30 ]
%43 = phi double [ %1, %3 ], [ %33, %30 ]
%44 = phi i32 [ 0, %3 ], [ %36, %30 ]
%45 = load i32, i32* @stdout
%46 = fadd double %42, %43
%47 = tail call i32 @fabs(double %46)
%48 = sdiv i32 %47, 2
%49 = sitofp i32 %48 to double
%50 = tail call double %0(double %49)
%51 = tail call i32 @fabs(double %46)
%52 = sdiv i32 %51, 2
%53 = tail call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), double %50, i32 %52, i32 %44)
%54 = tail call i32 @fabs(double %46)
%55 = sdiv i32 %54, 2
%56 = sitofp i32 %55 to double
ret double %56
}