#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 INFINITY;
double ln_evaluate_asymptotic(double const *, double const *, int const,
                              double);
double ln_evaluate_exact(double const *, double const *, int const, double);
double ln_evaluate_lcv(double const *, double const *, int const, double,
                       double const);
double ln_evaluate_lscv(double const *, double const *, int const, double,
                        double const);
int /*<<< orphan*/ printf(char *, char const *);
scalar_t__ strcmp(char const *, char *);
int ln_grid_optimal(const double *data, const double *weight, const int n,
                    const double *bandwidths, const int k,
                    const char *risk_function, const double threshold) {
  double minimum = INFINITY;
  int idx = -1;
  for (int i = 0; i < k; i++) {
    double bandwidth = bandwidths[i], current;
    if (strcmp(risk_function, "asymptotic") == 0) {
      current = ln_evaluate_asymptotic(data, weight, n, bandwidth);
    } else if (strcmp(risk_function, "exact") == 0) {
      current = ln_evaluate_exact(data, weight, n, bandwidth);
    } else if (strcmp(risk_function, "lscv") == 0) {
      current = ln_evaluate_lscv(data, weight, n, bandwidth, threshold);
    } else if (strcmp(risk_function, "lcv") == 0) {
      current = ln_evaluate_lcv(data, weight, n, bandwidth, threshold);
    } else {
      printf("`%s` is not a valid risk function.\n", risk_function);
      return idx;
    }
    if (current < minimum) {
      minimum = current;
      idx = i;
    }
  }
  return idx;
}


x86 assembly (clang, -O0):
<ln_grid_optimal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    %r9,-0x38(%rbp)
movsd  %xmm0,-0x40(%rbp)
mov    0x2e5d(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x48(%rbp)
movl   $0xffffffff,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    130f <ln_grid_optimal+0x1af>
mov    -0x28(%rbp),%rax
movslq -0x50(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11f9 <ln_grid_optimal+0x99>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
movsd  -0x58(%rbp),%xmm0
call   1050 <ln_evaluate_asymptotic@plt>
movsd  %xmm0,-0x60(%rbp)
jmp    12d8 <ln_grid_optimal+0x178>
mov    -0x38(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1232 <ln_grid_optimal+0xd2>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
movsd  -0x58(%rbp),%xmm0
call   1080 <ln_evaluate_exact@plt>
movsd  %xmm0,-0x60(%rbp)
jmp    12d3 <ln_grid_optimal+0x173>
mov    -0x38(%rbp),%rdi
lea    0xdd4(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1270 <ln_grid_optimal+0x110>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
movsd  -0x58(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm1
call   1070 <ln_evaluate_lscv@plt>
movsd  %xmm0,-0x60(%rbp)
jmp    12ce <ln_grid_optimal+0x16e>
mov    -0x38(%rbp),%rdi
lea    0xd9b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12ae <ln_grid_optimal+0x14e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
movsd  -0x58(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm1
call   1030 <ln_evaluate_lcv@plt>
movsd  %xmm0,-0x60(%rbp)
jmp    12c9 <ln_grid_optimal+0x169>
mov    -0x38(%rbp),%rsi
lea    0xd61(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1315 <ln_grid_optimal+0x1b5>
jmp    12ce <ln_grid_optimal+0x16e>
jmp    12d3 <ln_grid_optimal+0x173>
jmp    12d8 <ln_grid_optimal+0x178>
movsd  -0x60(%rbp),%xmm1
movsd  -0x48(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    12fc <ln_grid_optimal+0x19c>
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x50(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    1301 <ln_grid_optimal+0x1a1>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    11a2 <ln_grid_optimal+0x42>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ln_grid_optimal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movsd  %xmm0,0x18(%rsp)
mov    %rcx,0x20(%rsp)
mov    %edx,0x4(%rsp)
test   %r8d,%r8d
jle    12aa <ln_grid_optimal+0x14a>
mov    %r9,%rbx
mov    %rsi,%r12
mov    %rdi,%r13
mov    0x2e52(%rip),%rax
movsd  (%rax),%xmm0
mov    %r8d,%r15d
mov    $0xffffffff,%ebp
xor    %r14d,%r14d
jmp    11e4 <ln_grid_optimal+0x84>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r12,%rsi
mov    0x4(%rsp),%edx
movsd  0x8(%rsp),%xmm0
call   1050 <ln_evaluate_asymptotic@plt>
movsd  0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
cmova  %r14d,%ebp
minsd  %xmm1,%xmm0
add    $0x1,%r14
cmp    %r14,%r15
je     12c0 <ln_grid_optimal+0x160>
movsd  %xmm0,0x10(%rsp)
mov    0x20(%rsp),%rax
movsd  (%rax,%r14,8),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
lea    0xdfb(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11b0 <ln_grid_optimal+0x50>
mov    %rbx,%rdi
lea    0xdf2(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1270 <ln_grid_optimal+0x110>
mov    %rbx,%rdi
lea    0xde4(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     128a <ln_grid_optimal+0x12a>
mov    %rbx,%rdi
lea    0xdd5(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    12b1 <ln_grid_optimal+0x151>
mov    %r13,%rdi
mov    %r12,%rsi
mov    0x4(%rsp),%edx
movsd  0x8(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
call   1030 <ln_evaluate_lcv@plt>
jmp    11c5 <ln_grid_optimal+0x65>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r12,%rsi
mov    0x4(%rsp),%edx
movsd  0x8(%rsp),%xmm0
call   1080 <ln_evaluate_exact@plt>
jmp    11c5 <ln_grid_optimal+0x65>
mov    %r13,%rdi
mov    %r12,%rsi
mov    0x4(%rsp),%edx
movsd  0x8(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
call   1070 <ln_evaluate_lscv@plt>
jmp    11c5 <ln_grid_optimal+0x65>
mov    $0xffffffff,%ebp
jmp    12c0 <ln_grid_optimal+0x160>
lea    0xd62(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@INFINITY = global double 0.000000e+00
@.str = [11 x i8] c"asymptotic\00"
@.str.1 = [6 x i8] c"exact\00"
@.str.2 = [5 x i8] c"lscv\00"
@.str.3 = [4 x i8] c"lcv\00"
@.str.4 = [36 x i8] c"`%s` is not a valid risk function.\0A\00"
define i32 @ln_grid_optimal(double* %0, double* %1, i32 %2, double* %3, i32 %4, i8* %5, double %6) {
%8 = alloca i32
%9 = alloca double*
%10 = alloca double*
%11 = alloca i32
%12 = alloca double*
%13 = alloca i32
%14 = alloca i8*
%15 = alloca double
%16 = alloca double
%17 = alloca i32
%18 = alloca i32
%19 = alloca double
%20 = alloca double
store double* %0, double** %9
store double* %1, double** %10
store i32 %2, i32* %11
store double* %3, double** %12
store i32 %4, i32* %13
store i8* %5, i8** %14
store double %6, double* %15
%21 = load double, double* @INFINITY
store double %21, double* %16
store i32 -1, i32* %17
store i32 0, i32* %18
br label %22
22: ; preds = %88, %7
%23 = load i32, i32* %18
%24 = load i32, i32* %13
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %91
26: ; preds = %22
%27 = load double*, double** %12
%28 = load i32, i32* %18
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds double, double* %27, i64 %29
%31 = load double, double* %30
store double %31, double* %19
%32 = load i8*, i8** %14
%33 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %26
%36 = load double*, double** %9
%37 = load double*, double** %10
%38 = load i32, i32* %11
%39 = load double, double* %19
%40 = call double @ln_evaluate_asymptotic(double* %36, double* %37, i32 %38, double %39)
store double %40, double* %20
br label %80
41: ; preds = %26
%42 = load i8*, i8** %14
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = load double*, double** %9
%47 = load double*, double** %10
%48 = load i32, i32* %11
%49 = load double, double* %19
%50 = call double @ln_evaluate_exact(double* %46, double* %47, i32 %48, double %49)
store double %50, double* %20
br label %79
51: ; preds = %41
%52 = load i8*, i8** %14
%53 = call i64 @strcmp(i8* %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %62
55: ; preds = %51
%56 = load double*, double** %9
%57 = load double*, double** %10
%58 = load i32, i32* %11
%59 = load double, double* %19
%60 = load double, double* %15
%61 = call double @ln_evaluate_lscv(double* %56, double* %57, i32 %58, double %59, double %60)
store double %61, double* %20
br label %78
62: ; preds = %51
%63 = load i8*, i8** %14
%64 = call i64 @strcmp(i8* %63, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %73
66: ; preds = %62
%67 = load double*, double** %9
%68 = load double*, double** %10
%69 = load i32, i32* %11
%70 = load double, double* %19
%71 = load double, double* %15
%72 = call double @ln_evaluate_lcv(double* %67, double* %68, i32 %69, double %70, double %71)
store double %72, double* %20
br label %77
73: ; preds = %62
%74 = load i8*, i8** %14
%75 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %74)
%76 = load i32, i32* %17
store i32 %76, i32* %8
br label %93
77: ; preds = %66
br label %78
78: ; preds = %77, %55
br label %79
79: ; preds = %78, %45
br label %80
80: ; preds = %79, %35
%81 = load double, double* %20
%82 = load double, double* %16
%83 = fcmp olt double %81, %82
br i1 %83, label %84, label %87
84: ; preds = %80
%85 = load double, double* %20
store double %85, double* %16
%86 = load i32, i32* %18
store i32 %86, i32* %17
br label %87
87: ; preds = %84, %80
br label %88
88: ; preds = %87
%89 = load i32, i32* %18
%90 = add nsw i32 %89, 1
store i32 %90, i32* %18
br label %22
91: ; preds = %22
%92 = load i32, i32* %17
store i32 %92, i32* %8
br label %93
93: ; preds = %91, %73
%94 = load i32, i32* %8
ret i32 %94
}

LLVM IR (-O2):
@INFINITY = global double 0.000000e+00
@.str = [11 x i8] c"asymptotic\00"
@.str.1 = [6 x i8] c"exact\00"
@.str.2 = [5 x i8] c"lscv\00"
@.str.3 = [4 x i8] c"lcv\00"
@.str.4 = [36 x i8] c"`%s` is not a valid risk function.\0A\00"
define i32 @ln_grid_optimal(double* %0, double* %1, i32 %2, double* nocapture %3, i32 %4, i8* %5, double %6) {
%8 = icmp sgt i32 %4, 0
br i1 %8, label %9, label %47
9: ; preds = %7
%10 = load double, double* @INFINITY
%11 = zext i32 %4 to i64
br label %12
12: ; preds = %9, %39
%13 = phi i64 [ 0, %9 ], [ %45, %39 ]
%14 = phi i32 [ -1, %9 ], [ %44, %39 ]
%15 = phi double [ %10, %9 ], [ %42, %39 ]
%16 = getelementptr inbounds double, double* %3, i64 %13
%17 = load double, double* %16
%18 = tail call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = tail call double @ln_evaluate_asymptotic(double* %0, double* %1, i32 %2, double %17)
br label %39
22: ; preds = %12
%23 = tail call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call double @ln_evaluate_exact(double* %0, double* %1, i32 %2, double %17)
br label %39
27: ; preds = %22
%28 = tail call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call double @ln_evaluate_lscv(double* %0, double* %1, i32 %2, double %17, double %6)
br label %39
32: ; preds = %27
%33 = tail call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = tail call double @ln_evaluate_lcv(double* %0, double* %1, i32 %2, double %17, double %6)
br label %39
37: ; preds = %32
%38 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %5)
br label %47
39: ; preds = %20, %30, %35, %25
%40 = phi double [ %21, %20 ], [ %26, %25 ], [ %31, %30 ], [ %36, %35 ]
%41 = fcmp olt double %40, %15
%42 = select i1 %41, double %40, double %15
%43 = trunc i64 %13 to i32
%44 = select i1 %41, i32 %43, i32 %14
%45 = add nuw nsw i64 %13, 1
%46 = icmp eq i64 %45, %11
br i1 %46, label %47, label %12
47: ; preds = %39, %7, %37
%48 = phi i32 [ %14, %37 ], [ -1, %7 ], [ %44, %39 ]
ret i32 %48
}