#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);
double merit_function_golden(double *, float *, float *, int,
                             double (*)(float, float));
int sqrt(int);
int /*<<< orphan*/ stdout;
double goldenFit(double a, double b, float *x, float *y, int num,
                 double (*f)(float, float)) {
  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 (merit_function_golden(&x2, x, y, num, f) >
        merit_function_golden(&x1, x, y, num, f)) {
      b = x2;
      x2 = x1;
      x1 = b - goldenRatio * (b - a);
    } else {
      a = x1;
      x1 = x2;
      x2 = a + goldenRatio * (b - a);
    }
    i++;
  }
  x2 = fabs(b + a) / 2;
  fprintf(stdout, "Golden: %f %f %d\n",
          merit_function_golden(&x2, x, y, num, f), fabs(b + a) / 2, i);
  return fabs(b + a) / 2;
}


x86 assembly (clang, -O0):
<goldenFit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
mov    %edx,-0x24(%rbp)
mov    %rcx,-0x30(%rbp)
mov    $0x5,%edi
call   1060 <sqrt@plt>
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
subsd  -0x8(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x8(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
subsd  -0x8(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
call   1030 <fabs@plt>
cvtsi2sd %eax,%xmm0
xor    %eax,%eax
movsd  0xe04(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x51(%rbp)
jbe    1216 <goldenFit+0xd6>
cmpl   $0xc8,-0x3c(%rbp)
setl   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    1226 <goldenFit+0xe6>
jmp    1301 <goldenFit+0x1c1>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%r8
lea    -0x50(%rbp),%rdi
call   1050 <merit_function_golden@plt>
movsd  %xmm0,-0x60(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%r8
lea    -0x48(%rbp),%rdi
call   1050 <merit_function_golden@plt>
movaps %xmm0,%xmm1
movsd  -0x60(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    12be <goldenFit+0x17e>
movsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
subsd  -0x8(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
jmp    12f3 <goldenFit+0x1b3>
movsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x8(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
subsd  -0x8(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11df <goldenFit+0x9f>
movsd  -0x10(%rbp),%xmm0
addsd  -0x8(%rbp),%xmm0
call   1030 <fabs@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    0x2cb8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%r8
lea    -0x50(%rbp),%rdi
call   1050 <merit_function_golden@plt>
movsd  %xmm0,-0x68(%rbp)
movsd  -0x10(%rbp),%xmm0
addsd  -0x8(%rbp),%xmm0
call   1030 <fabs@plt>
mov    -0x6c(%rbp),%edi
movsd  -0x68(%rbp),%xmm0
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
mov    -0x3c(%rbp),%ecx
lea    0xc93(%rip),%rsi
call   1040 <fprintf@plt>
movsd  -0x10(%rbp),%xmm0
addsd  -0x8(%rbp),%xmm0
call   1030 <fabs@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<goldenFit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rcx,%r14
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%rbp
movsd  %xmm1,0x8(%rsp)
movsd  %xmm0,0x20(%rsp)
mov    $0x5,%edi
call   1060 <sqrt@plt>
mov    %eax,%ecx
add    $0xffffffff,%ecx
shr    $0x1f,%ecx
add    %ecx,%eax
add    $0xffffffff,%eax
sar    %eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
movsd  0x8(%rsp),%xmm2
movapd %xmm2,%xmm0
movsd  0x20(%rsp),%xmm3
subsd  %xmm3,%xmm0
movapd %xmm1,0x30(%rsp)
mulsd  %xmm0,%xmm1
subsd  %xmm1,%xmm2
movsd  %xmm2,0x18(%rsp)
addsd  %xmm3,%xmm1
movsd  %xmm1,0x10(%rsp)
call   1030 <fabs@plt>
test   %eax,%eax
jle    12b5 <goldenFit+0x175>
movapd 0xe33(%rip),%xmm0
xorpd  0x30(%rsp),%xmm0
movapd %xmm0,0x40(%rsp)
xor    %r13d,%r13d
nopl   0x0(%rax)
lea    0x10(%rsp),%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r15d,%ecx
mov    %r14,%r8
call   1050 <merit_function_golden@plt>
movsd  %xmm0,0x28(%rsp)
lea    0x18(%rsp),%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r15d,%ecx
mov    %r14,%r8
call   1050 <merit_function_golden@plt>
movsd  0x28(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jbe    1260 <goldenFit+0x120>
movsd  0x10(%rsp),%xmm2
movsd  0x18(%rsp),%xmm0
movsd  %xmm0,0x10(%rsp)
movapd %xmm2,%xmm0
subsd  0x20(%rsp),%xmm0
movapd 0x40(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm2,0x8(%rsp)
addsd  %xmm2,%xmm1
movsd  %xmm1,0x18(%rsp)
jmp    1296 <goldenFit+0x156>
cs nopw 0x0(%rax,%rax,1)
movsd  0x18(%rsp),%xmm2
movsd  0x10(%rsp),%xmm0
movsd  %xmm0,0x18(%rsp)
movsd  0x8(%rsp),%xmm0
subsd  %xmm2,%xmm0
movapd 0x30(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm2,0x20(%rsp)
addsd  %xmm2,%xmm1
movsd  %xmm1,0x10(%rsp)
lea    0x1(%r13),%r12d
call   1030 <fabs@plt>
test   %eax,%eax
jle    12b8 <goldenFit+0x178>
cmp    $0xc7,%r13d
mov    %r12d,%r13d
jb     11e0 <goldenFit+0xa0>
jmp    12b8 <goldenFit+0x178>
xor    %r12d,%r12d
movsd  0x8(%rsp),%xmm0
addsd  0x20(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
call   1030 <fabs@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
movsd  %xmm0,0x10(%rsp)
mov    0x2cf4(%rip),%rax
mov    (%rax),%r13d
lea    0x10(%rsp),%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r15d,%ecx
mov    %r14,%r8
call   1050 <merit_function_golden@plt>
movsd  %xmm0,0x28(%rsp)
movsd  0x8(%rsp),%xmm0
call   1030 <fabs@plt>
mov    %eax,%edx
shr    $0x1f,%edx
add    %eax,%edx
sar    %edx
lea    0xcea(%rip),%rsi
mov    %r13d,%edi
movsd  0x28(%rsp),%xmm0
mov    %r12d,%ecx
call   1040 <fprintf@plt>
movsd  0x8(%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    %r12
pop    %r13
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 @goldenFit(double %0, double %1, float* %2, float* %3, i32 %4, double (float, float)* %5) {
%7 = alloca double
%8 = alloca double
%9 = alloca float*
%10 = alloca float*
%11 = alloca i32
%12 = alloca double (float, float)*
%13 = alloca double
%14 = alloca i32
%15 = alloca double
%16 = alloca double
store double %0, double* %7
store double %1, double* %8
store float* %2, float** %9
store float* %3, float** %10
store i32 %4, i32* %11
store double (float, float)* %5, double (float, float)** %12
%17 = call i32 @sqrt(i32 5)
%18 = sub nsw i32 %17, 1
%19 = sdiv i32 %18, 2
%20 = sitofp i32 %19 to double
store double %20, double* %13
store i32 0, i32* %14
%21 = load double, double* %8
%22 = load double, double* %13
%23 = load double, double* %8
%24 = load double, double* %7
%25 = fsub double %23, %24
%26 = fneg double %22
%27 = call double @llvm.fmuladd.f64(double %26, double %25, double %21)
store double %27, double* %15
%28 = load double, double* %7
%29 = load double, double* %13
%30 = load double, double* %8
%31 = load double, double* %7
%32 = fsub double %30, %31
%33 = call double @llvm.fmuladd.f64(double %29, double %32, double %28)
store double %33, double* %16
br label %34
34: ; preds = %77, %6
%35 = load double, double* %8
%36 = load double, double* %7
%37 = fsub double %35, %36
%38 = call i32 @fabs(double %37)
%39 = sitofp i32 %38 to double
%40 = fcmp ogt double %39, 1.000000e-09
br i1 %40, label %41, label %44
41: ; preds = %34
%42 = load i32, i32* %14
%43 = icmp slt i32 %42, 200
br label %44
44: ; preds = %41, %34
%45 = phi i1 [ false, %34 ], [ %43, %41 ]
br i1 %45, label %46, label %80
46: ; preds = %44
%47 = load float*, float** %9
%48 = load float*, float** %10
%49 = load i32, i32* %11
%50 = load double (float, float)*, double (float, float)** %12
%51 = call double @merit_function_golden(double* %16, float* %47, float* %48, i32 %49, double (float, float)* %50)
%52 = load float*, float** %9
%53 = load float*, float** %10
%54 = load i32, i32* %11
%55 = load double (float, float)*, double (float, float)** %12
%56 = call double @merit_function_golden(double* %15, float* %52, float* %53, i32 %54, double (float, float)* %55)
%57 = fcmp ogt double %51, %56
br i1 %57, label %58, label %68
58: ; preds = %46
%59 = load double, double* %16
store double %59, double* %8
%60 = load double, double* %15
store double %60, double* %16
%61 = load double, double* %8
%62 = load double, double* %13
%63 = load double, double* %8
%64 = load double, double* %7
%65 = fsub double %63, %64
%66 = fneg double %62
%67 = call double @llvm.fmuladd.f64(double %66, double %65, double %61)
store double %67, double* %15
br label %77
68: ; preds = %46
%69 = load double, double* %15
store double %69, double* %7
%70 = load double, double* %16
store double %70, double* %15
%71 = load double, double* %7
%72 = load double, double* %13
%73 = load double, double* %8
%74 = load double, double* %7
%75 = fsub double %73, %74
%76 = call double @llvm.fmuladd.f64(double %72, double %75, double %71)
store double %76, double* %16
br label %77
77: ; preds = %68, %58
%78 = load i32, i32* %14
%79 = add nsw i32 %78, 1
store i32 %79, i32* %14
br label %34
80: ; preds = %44
%81 = load double, double* %8
%82 = load double, double* %7
%83 = fadd double %81, %82
%84 = call i32 @fabs(double %83)
%85 = sdiv i32 %84, 2
%86 = sitofp i32 %85 to double
store double %86, double* %16
%87 = load i32, i32* @stdout
%88 = load float*, float** %9
%89 = load float*, float** %10
%90 = load i32, i32* %11
%91 = load double (float, float)*, double (float, float)** %12
%92 = call double @merit_function_golden(double* %16, float* %88, float* %89, i32 %90, double (float, float)* %91)
%93 = load double, double* %8
%94 = load double, double* %7
%95 = fadd double %93, %94
%96 = call i32 @fabs(double %95)
%97 = sdiv i32 %96, 2
%98 = load i32, i32* %14
%99 = call i32 @fprintf(i32 %87, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), double %92, i32 %97, i32 %98)
%100 = load double, double* %8
%101 = load double, double* %7
%102 = fadd double %100, %101
%103 = call i32 @fabs(double %102)
%104 = sdiv i32 %103, 2
%105 = sitofp i32 %104 to double
ret double %105
}

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