#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 /*<<< orphan*/ error_handler(char *);
int nearest_index(double value, const double *array, int ia) {
  int index, i;
  int keep_going;
  for (i = 1; i < ia; i++) {
    if (array[i] < array[i - 1])
      error_handler("nearest_index: array must be monotonically increasing");
  }
  if (value < array[0])
    index = 0;
  else if (value > array[ia - 1])
    index = ia - 1;
  else {
    i = 0;
    keep_going = 1;
    while (i < ia && keep_going) {
      i = i + 1;
      if (value <= array[i]) {
        index = i;
        if (array[i] - value > value - array[i - 1])
          index = i - 1;
        keep_going = 0;
      }
    }
  }
  return index;
}


x86 assembly (clang, -O0):
<nearest_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    117f <nearest_index+0x6f>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd %xmm1,%xmm0
jbe    116c <nearest_index+0x5c>
lea    0xe99(%rip),%rdi
call   1030 <error_handler@plt>
jmp    1171 <nearest_index+0x61>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112b <nearest_index+0x1b>
movsd  -0x8(%rbp),%xmm1
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
ucomisd %xmm1,%xmm0
jbe    11a2 <nearest_index+0x92>
movl   $0x0,-0x18(%rbp)
jmp    1283 <nearest_index+0x173>
movsd  -0x8(%rbp),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
ucomisd (%rax,%rcx,8),%xmm0
jbe    11cd <nearest_index+0xbd>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    127e <nearest_index+0x16e>
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x21(%rbp)
jge    11f6 <nearest_index+0xe6>
cmpl   $0x0,-0x20(%rbp)
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1206 <nearest_index+0xf6>
jmp    1279 <nearest_index+0x169>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movsd  -0x8(%rbp),%xmm1
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd %xmm1,%xmm0
jb     1274 <nearest_index+0x164>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
subsd  -0x8(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
subsd  (%rax,%rcx,8),%xmm1
ucomisd %xmm1,%xmm0
jbe    126d <nearest_index+0x15d>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
jmp    11db <nearest_index+0xcb>
jmp    127e <nearest_index+0x16e>
jmp    1283 <nearest_index+0x173>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nearest_index>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
movsd  %xmm0,0x8(%rsp)
cmp    $0x2,%esi
jl     1160 <nearest_index+0x50>
mov    %r14d,%r12d
mov    $0x1,%ebp
lea    0xec4(%rip),%r15
jmp    1149 <nearest_index+0x39>
xchg   %ax,%ax
add    $0x1,%rbp
cmp    %rbp,%r12
je     1160 <nearest_index+0x50>
movsd  -0x8(%rbx,%rbp,8),%xmm0
ucomisd (%rbx,%rbp,8),%xmm0
jbe    1140 <nearest_index+0x30>
mov    %r15,%rdi
call   1030 <error_handler@plt>
jmp    1140 <nearest_index+0x30>
movsd  (%rbx),%xmm0
xor    %eax,%eax
movsd  0x8(%rsp),%xmm1
ucomisd %xmm1,%xmm0
ja     11de <nearest_index+0xce>
movslq %r14d,%rax
ucomisd -0x8(%rbx,%rax,8),%xmm1
jbe    1186 <nearest_index+0x76>
add    $0xffffffff,%r14d
mov    %r14d,%eax
jmp    11de <nearest_index+0xce>
test   %r14d,%r14d
jle    11c2 <nearest_index+0xb2>
mov    %r14d,%edx
mov    $0x1,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  (%rbx,%rcx,8),%xmm0
ucomisd %xmm1,%xmm0
jae    11c4 <nearest_index+0xb4>
cmp    %rdx,%rcx
movsd  0x8(%rsp),%xmm1
jae    11de <nearest_index+0xce>
add    $0x1,%rcx
ucomisd %xmm1,%xmm0
jb     11a0 <nearest_index+0x90>
jmp    11de <nearest_index+0xce>
jmp    11de <nearest_index+0xce>
movsd  0x8(%rsp),%xmm1
subsd  %xmm1,%xmm0
subsd  -0x8(%rbx,%rcx,8),%xmm1
ucomisd %xmm1,%xmm0
lea    -0x1(%rcx),%eax
cmovbe %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [54 x i8] c"nearest_index: array must be monotonically increasing\00"
define i32 @nearest_index(double %0, double* %1, i32 %2) {
%4 = alloca double
%5 = alloca double*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store double %0, double* %4
store double* %1, double** %5
store i32 %2, i32* %6
store i32 1, i32* %8
br label %10
10: ; preds = %30, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %6
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %33
14: ; preds = %10
%15 = load double*, double** %5
%16 = load i32, i32* %8
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds double, double* %15, i64 %17
%19 = load double, double* %18
%20 = load double*, double** %5
%21 = load i32, i32* %8
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds double, double* %20, i64 %23
%25 = load double, double* %24
%26 = fcmp olt double %19, %25
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 @error_handler(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %14
br label %30
30: ; preds = %29
%31 = load i32, i32* %8
%32 = add nsw i32 %31, 1
store i32 %32, i32* %8
br label %10
33: ; preds = %10
%34 = load double, double* %4
%35 = load double*, double** %5
%36 = getelementptr inbounds double, double* %35, i64 0
%37 = load double, double* %36
%38 = fcmp olt double %34, %37
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 0, i32* %7
br label %97
40: ; preds = %33
%41 = load double, double* %4
%42 = load double*, double** %5
%43 = load i32, i32* %6
%44 = sub nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds double, double* %42, i64 %45
%47 = load double, double* %46
%48 = fcmp ogt double %41, %47
br i1 %48, label %49, label %52
49: ; preds = %40
%50 = load i32, i32* %6
%51 = sub nsw i32 %50, 1
store i32 %51, i32* %7
br label %96
52: ; preds = %40
store i32 0, i32* %8
store i32 1, i32* %9
br label %53
53: ; preds = %94, %52
%54 = load i32, i32* %8
%55 = load i32, i32* %6
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = load i32, i32* %9
%59 = icmp ne i32 %58, 0
br label %60
60: ; preds = %57, %53
%61 = phi i1 [ false, %53 ], [ %59, %57 ]
br i1 %61, label %62, label %95
62: ; preds = %60
%63 = load i32, i32* %8
%64 = add nsw i32 %63, 1
store i32 %64, i32* %8
%65 = load double, double* %4
%66 = load double*, double** %5
%67 = load i32, i32* %8
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds double, double* %66, i64 %68
%70 = load double, double* %69
%71 = fcmp ole double %65, %70
br i1 %71, label %72, label %94
72: ; preds = %62
%73 = load i32, i32* %8
store i32 %73, i32* %7
%74 = load double*, double** %5
%75 = load i32, i32* %8
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds double, double* %74, i64 %76
%78 = load double, double* %77
%79 = load double, double* %4
%80 = fsub double %78, %79
%81 = load double, double* %4
%82 = load double*, double** %5
%83 = load i32, i32* %8
%84 = sub nsw i32 %83, 1
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds double, double* %82, i64 %85
%87 = load double, double* %86
%88 = fsub double %81, %87
%89 = fcmp ogt double %80, %88
br i1 %89, label %90, label %93
90: ; preds = %72
%91 = load i32, i32* %8
%92 = sub nsw i32 %91, 1
store i32 %92, i32* %7
br label %93
93: ; preds = %90, %72
store i32 0, i32* %9
br label %94
94: ; preds = %93, %62
br label %53
95: ; preds = %60
br label %96
96: ; preds = %95, %49
br label %97
97: ; preds = %96, %39
%98 = load i32, i32* %7
ret i32 %98
}

LLVM IR (-O2):
@.str = [54 x i8] c"nearest_index: array must be monotonically increasing\00"
define i32 @nearest_index(double %0, double* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 1
br i1 %4, label %5, label %20
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %17
%8 = phi i64 [ 1, %5 ], [ %18, %17 ]
%9 = getelementptr inbounds double, double* %1, i64 %8
%10 = load double, double* %9
%11 = add nsw i64 %8, -1
%12 = getelementptr inbounds double, double* %1, i64 %11
%13 = load double, double* %12
%14 = fcmp olt double %10, %13
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = tail call i32 @error_handler(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %7, %15
%18 = add nuw nsw i64 %8, 1
%19 = icmp eq i64 %18, %6
br i1 %19, label %20, label %7
20: ; preds = %17, %3
%21 = load double, double* %1
%22 = fcmp ogt double %21, %0
br i1 %22, label %50, label %23
23: ; preds = %20
%24 = add nsw i32 %2, -1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds double, double* %1, i64 %25
%27 = load double, double* %26
%28 = fcmp olt double %27, %0
br i1 %28, label %50, label %29
29: ; preds = %23
%30 = icmp sgt i32 %2, 0
br i1 %30, label %31, label %50
31: ; preds = %29
%32 = zext i32 %2 to i64
br label %33
33: ; preds = %31, %47
%34 = phi i64 [ 0, %31 ], [ %35, %47 ]
%35 = add nuw nsw i64 %34, 1
%36 = getelementptr inbounds double, double* %1, i64 %35
%37 = load double, double* %36
%38 = fcmp ult double %37, %0
br i1 %38, label %47, label %39
39: ; preds = %33
%40 = fsub double %37, %0
%41 = getelementptr inbounds double, double* %1, i64 %34
%42 = load double, double* %41
%43 = fsub double %0, %42
%44 = fcmp ogt double %40, %43
%45 = select i1 %44, i64 %34, i64 %35
%46 = trunc i64 %45 to i32
br label %50
47: ; preds = %33
%48 = icmp ult i64 %35, %32
%49 = and i1 %48, %38
br i1 %49, label %33, label %50
50: ; preds = %47, %39, %29, %23, %20
%51 = phi i32 [ 0, %20 ], [ %24, %23 ], [ undef, %29 ], [ %46, %39 ], [ undef, %47 ]
ret i32 %51
}