#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
double INFINITY;
double NAN;
scalar_t__ autodifference(float *, size_t, double);
double bpm_to_interval(double);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, double, double);
double interval_to_bpm(double);
double scan_for_bpm(float nrg[], size_t len, double slowest, double fastest,
                    unsigned int steps, unsigned int samples, FILE *file) {
  double step, interval, trough, height;
  unsigned int s;
  slowest = bpm_to_interval(slowest);
  fastest = bpm_to_interval(fastest);
  step = (slowest - fastest) / steps;
  height = INFINITY;
  trough = NAN;
  for (interval = fastest; interval <= slowest; interval += step) {
    double t;
    t = 0.0;
    for (s = 0; s < samples; s++)
      t += autodifference(nrg, len, interval);
    if (file != NULL) {
      fprintf(file, "%lf\t%lf\n", interval_to_bpm(interval), t / samples);
    }
    if (t < height) {
      trough = interval;
      height = t;
    }
  }
  return interval_to_bpm(trough);
}


x86 assembly (clang, -O0):
<scan_for_bpm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  %xmm1,-0x20(%rbp)
mov    %edx,-0x24(%rbp)
mov    %ecx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1030 <bpm_to_interval@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1030 <bpm_to_interval@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
subsd  %xmm1,%xmm0
mov    -0x24(%rbp),%eax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    0x2e38(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    0x2e40(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     12a8 <scan_for_bpm+0x168>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x60(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x28(%rbp),%eax
jae    1229 <scan_for_bpm+0xe9>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movsd  -0x40(%rbp),%xmm0
call   1040 <autodifference@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11ee <scan_for_bpm+0xae>
cmpq   $0x0,-0x30(%rbp)
je     1267 <scan_for_bpm+0x127>
mov    -0x30(%rbp),%rax
mov    %rax,-0x68(%rbp)
movsd  -0x40(%rbp),%xmm0
call   1060 <interval_to_bpm@plt>
mov    -0x68(%rbp),%rdi
movsd  -0x60(%rbp),%xmm1
mov    -0x28(%rbp),%eax
cvtsi2sd %rax,%xmm2
divsd  %xmm2,%xmm1
lea    0xd9e(%rip),%rsi
call   1050 <fprintf@plt>
movsd  -0x60(%rbp),%xmm1
movsd  -0x50(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    128f <scan_for_bpm+0x14f>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
jmp    1294 <scan_for_bpm+0x154>
movsd  -0x38(%rbp),%xmm0
addsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
jmp    11cb <scan_for_bpm+0x8b>
movsd  -0x48(%rbp),%xmm0
call   1060 <interval_to_bpm@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_for_bpm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r8,%r14
mov    %ecx,%r15d
mov    %edx,%r12d
movsd  %xmm1,0x8(%rsp)
mov    %rsi,%r13
mov    %rdi,%rbp
call   1030 <bpm_to_interval@plt>
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
call   1030 <bpm_to_interval@plt>
movsd  0x10(%rsp),%xmm1
mov    0x2e72(%rip),%rax
movsd  (%rax),%xmm4
ucomisd %xmm0,%xmm1
jae    11a7 <scan_for_bpm+0x67>
movapd %xmm4,%xmm0
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <interval_to_bpm@plt>
movapd %xmm0,%xmm2
movapd %xmm1,%xmm3
mov    %r12d,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
subsd  %xmm2,%xmm3
divsd  %xmm0,%xmm3
movsd  %xmm3,0x20(%rsp)
mov    0x2e11(%rip),%rax
movsd  (%rax),%xmm3
mov    %r15d,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  %xmm0,0x18(%rsp)
lea    0xe15(%rip),%r12
jmp    1233 <scan_for_bpm+0xf3>
nopl   (%rax)
movapd %xmm4,%xmm0
movsd  0x28(%rsp),%xmm3
cmpltsd %xmm3,%xmm0
movapd %xmm0,%xmm1
andpd  %xmm2,%xmm1
andnpd 0x40(%rsp),%xmm0
orpd   %xmm1,%xmm0
minsd  %xmm3,%xmm4
addsd  0x20(%rsp),%xmm2
movsd  0x10(%rsp),%xmm1
ucomisd %xmm2,%xmm1
movapd %xmm4,%xmm3
movapd %xmm0,%xmm4
jb     1194 <scan_for_bpm+0x54>
test   %r15d,%r15d
movapd %xmm2,0x30(%rsp)
movapd %xmm4,0x40(%rsp)
movsd  %xmm3,0x28(%rsp)
xorpd  %xmm4,%xmm4
je     1292 <scan_for_bpm+0x152>
mov    %r15d,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  %xmm4,0x8(%rsp)
mov    %rbp,%rdi
mov    %r13,%rsi
movapd %xmm2,%xmm0
call   1040 <autodifference@plt>
movsd  0x8(%rsp),%xmm4
movapd 0x30(%rsp),%xmm2
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
addsd  %xmm0,%xmm4
add    $0xffffffff,%ebx
jne    1260 <scan_for_bpm+0x120>
test   %r14,%r14
je     11f0 <scan_for_bpm+0xb0>
movapd %xmm2,%xmm0
movsd  %xmm4,0x8(%rsp)
call   1060 <interval_to_bpm@plt>
movsd  0x8(%rsp),%xmm1
divsd  0x18(%rsp),%xmm1
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <fprintf@plt>
movsd  0x8(%rsp),%xmm4
movapd 0x30(%rsp),%xmm2
jmp    11f0 <scan_for_bpm+0xb0>

LLVM IR (-O0):
@INFINITY = global double 0.000000e+00
@NAN = global double 0.000000e+00
@.str = [9 x i8] c"%lf\09%lf\0A\00"
define double @scan_for_bpm(float* %0, i64 %1, double %2, double %3, i32 %4, i32 %5, i32* %6) {
%8 = alloca float*
%9 = alloca i64
%10 = alloca double
%11 = alloca double
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
%15 = alloca double
%16 = alloca double
%17 = alloca double
%18 = alloca double
%19 = alloca i32
%20 = alloca double
store float* %0, float** %8
store i64 %1, i64* %9
store double %2, double* %10
store double %3, double* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32* %6, i32** %14
%21 = load double, double* %10
%22 = call double @bpm_to_interval(double %21)
store double %22, double* %10
%23 = load double, double* %11
%24 = call double @bpm_to_interval(double %23)
store double %24, double* %11
%25 = load double, double* %10
%26 = load double, double* %11
%27 = fsub double %25, %26
%28 = load i32, i32* %12
%29 = uitofp i32 %28 to double
%30 = fdiv double %27, %29
store double %30, double* %15
%31 = load double, double* @INFINITY
store double %31, double* %18
%32 = load double, double* @NAN
store double %32, double* %17
%33 = load double, double* %11
store double %33, double* %16
br label %34
34: ; preds = %74, %7
%35 = load double, double* %16
%36 = load double, double* %10
%37 = fcmp ole double %35, %36
br i1 %37, label %38, label %78
38: ; preds = %34
store double 0.000000e+00, double* %20
store i32 0, i32* %19
br label %39
39: ; preds = %51, %38
%40 = load i32, i32* %19
%41 = load i32, i32* %13
%42 = icmp ult i32 %40, %41
br i1 %42, label %43, label %54
43: ; preds = %39
%44 = load float*, float** %8
%45 = load i64, i64* %9
%46 = load double, double* %16
%47 = call i64 @autodifference(float* %44, i64 %45, double %46)
%48 = sitofp i64 %47 to double
%49 = load double, double* %20
%50 = fadd double %49, %48
store double %50, double* %20
br label %51
51: ; preds = %43
%52 = load i32, i32* %19
%53 = add i32 %52, 1
store i32 %53, i32* %19
br label %39
54: ; preds = %39
%55 = load i32*, i32** %14
%56 = icmp ne i32* %55, null
br i1 %56, label %57, label %66
57: ; preds = %54
%58 = load i32*, i32** %14
%59 = load double, double* %16
%60 = call double @interval_to_bpm(double %59)
%61 = load double, double* %20
%62 = load i32, i32* %13
%63 = uitofp i32 %62 to double
%64 = fdiv double %61, %63
%65 = call i32 @fprintf(i32* %58, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), double %60, double %64)
br label %66
66: ; preds = %57, %54
%67 = load double, double* %20
%68 = load double, double* %18
%69 = fcmp olt double %67, %68
br i1 %69, label %70, label %73
70: ; preds = %66
%71 = load double, double* %16
store double %71, double* %17
%72 = load double, double* %20
store double %72, double* %18
br label %73
73: ; preds = %70, %66
br label %74
74: ; preds = %73
%75 = load double, double* %15
%76 = load double, double* %16
%77 = fadd double %76, %75
store double %77, double* %16
br label %34
78: ; preds = %34
%79 = load double, double* %17
%80 = call double @interval_to_bpm(double %79)
ret double %80
}

LLVM IR (-O2):
@INFINITY = global double 0.000000e+00
@NAN = global double 0.000000e+00
@.str = [9 x i8] c"%lf\09%lf\0A\00"
define double @scan_for_bpm(float* %0, i64 %1, double %2, double %3, i32 %4, i32 %5, i32* %6) {
%8 = tail call double @bpm_to_interval(double %2)
%9 = tail call double @bpm_to_interval(double %3)
%10 = fsub double %8, %9
%11 = uitofp i32 %4 to double
%12 = fdiv double %10, %11
%13 = load double, double* @NAN
%14 = fcmp ugt double %9, %8
br i1 %14, label %44, label %15
15: ; preds = %7
%16 = load double, double* @INFINITY
%17 = icmp eq i32 %5, 0
%18 = icmp eq i32* %6, null
%19 = uitofp i32 %5 to double
br label %20
20: ; preds = %15, %38
%21 = phi double [ %16, %15 ], [ %41, %38 ]
%22 = phi double [ %13, %15 ], [ %40, %38 ]
%23 = phi double [ %9, %15 ], [ %42, %38 ]
br i1 %17, label %32, label %24
24: ; preds = %20, %24
%25 = phi double [ %29, %24 ], [ 0.000000e+00, %20 ]
%26 = phi i32 [ %30, %24 ], [ 0, %20 ]
%27 = tail call i64 @autodifference(float* %0, i64 %1, double %23)
%28 = sitofp i64 %27 to double
%29 = fadd double %25, %28
%30 = add nuw i32 %26, 1
%31 = icmp eq i32 %30, %5
br i1 %31, label %32, label %24
32: ; preds = %24, %20
%33 = phi double [ 0.000000e+00, %20 ], [ %29, %24 ]
br i1 %18, label %38, label %34
34: ; preds = %32
%35 = tail call double @interval_to_bpm(double %23)
%36 = fdiv double %33, %19
%37 = tail call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), double %35, double %36)
br label %38
38: ; preds = %34, %32
%39 = fcmp olt double %33, %21
%40 = select i1 %39, double %23, double %22
%41 = select i1 %39, double %33, double %21
%42 = fadd double %12, %23
%43 = fcmp ugt double %42, %8
br i1 %43, label %44, label %20
44: ; preds = %38, %7
%45 = phi double [ %13, %7 ], [ %40, %38 ]
%46 = tail call double @interval_to_bpm(double %45)
ret double %46
}