#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 */
scalar_t__ eq(double, double);
int /*<<< orphan*/ fatal(char *);
int bsearch_double(double *arr, int n, double ele, double **loc) {
  if (n < 0)
    fatal("wrong index in binary search\n");
  if (n == 0) {
    *loc = arr;
    return 0;
  }
  if (eq(arr[n / 2], ele)) {
    *loc = &arr[n / 2];
    return 1;
  } else if (arr[n / 2] < ele) {
    return bsearch_double(&arr[n / 2 + 1], (n - 1) / 2, ele, loc);
  } else
    return bsearch_double(arr, n / 2, ele, loc);
}


x86 assembly (clang, -O0):
<bsearch_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movsd  %xmm0,-0x20(%rbp)
mov    %rdx,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    115e <bsearch_double+0x2e>
lea    0xea7(%rip),%rdi
call   1040 <fatal@plt>
cmpl   $0x0,-0x14(%rbp)
jne    117f <bsearch_double+0x4f>
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1272 <bsearch_double+0x142>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  -0x20(%rbp),%xmm1
call   1050 <eq@plt>
cmp    $0x0,%rax
je     11df <bsearch_double+0xaf>
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%eax
mov    $0x2,%esi
cltd
idiv   %esi
cltq
shl    $0x3,%rax
add    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1272 <bsearch_double+0x142>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1250 <bsearch_double+0x120>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
movsd  -0x20(%rbp),%xmm0
mov    -0x28(%rbp),%rdx
call   1030 <bsearch_double@plt>
mov    %eax,-0x4(%rbp)
jmp    1272 <bsearch_double+0x142>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
movsd  -0x20(%rbp),%xmm0
mov    -0x28(%rbp),%rdx
call   1030 <bsearch_double@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bsearch_double>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x10(%rsp)
movapd %xmm0,%xmm1
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xebd(%rip),%r15
movsd  %xmm0,0x8(%rsp)
nopl   0x0(%rax)
mov    %ebp,%r13d
test   %ebp,%ebp
js     1160 <bsearch_double+0x40>
jne    116e <bsearch_double+0x4e>
jmp    11c4 <bsearch_double+0xa4>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <fatal@plt>
movsd  0x8(%rsp),%xmm1
mov    %r13d,%ebp
shr    $0x1f,%ebp
add    %r13d,%ebp
sar    %ebp
movslq %ebp,%rbx
lea    (%r14,%rbx,8),%r12
movsd  (%r14,%rbx,8),%xmm0
call   1040 <eq@plt>
test   %rax,%rax
jne    11ba <bsearch_double+0x9a>
movsd  0x8(%rsp),%xmm1
ucomisd (%r12),%xmm1
jbe    1150 <bsearch_double+0x30>
lea    (%r14,%rbx,8),%r14
add    $0x8,%r14
mov    %r13d,%eax
add    $0xffffffff,%eax
shr    $0x1f,%eax
lea    (%rax,%r13,1),%ebp
add    $0xffffffff,%ebp
sar    %ebp
jmp    1150 <bsearch_double+0x30>
mov    $0x1,%eax
mov    %r12,%r14
jmp    11c6 <bsearch_double+0xa6>
xor    %eax,%eax
mov    0x10(%rsp),%rcx
mov    %r14,(%rcx)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"wrong index in binary search\0A\00"
define i32 @bsearch_double(double* %0, i32 %1, double %2, double** %3) {
%5 = alloca i32
%6 = alloca double*
%7 = alloca i32
%8 = alloca double
%9 = alloca double**
store double* %0, double** %6
store i32 %1, i32* %7
store double %2, double* %8
store double** %3, double*** %9
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = call i32 @fatal(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %4
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load double*, double** %6
%19 = load double**, double*** %9
store double* %18, double** %19
store i32 0, i32* %5
br label %66
20: ; preds = %14
%21 = load double*, double** %6
%22 = load i32, i32* %7
%23 = sdiv i32 %22, 2
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds double, double* %21, i64 %24
%26 = load double, double* %25
%27 = load double, double* %8
%28 = call i64 @eq(double %26, double %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %20
%31 = load double*, double** %6
%32 = load i32, i32* %7
%33 = sdiv i32 %32, 2
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %31, i64 %34
%36 = load double**, double*** %9
store double* %35, double** %36
store i32 1, i32* %5
br label %66
37: ; preds = %20
%38 = load double*, double** %6
%39 = load i32, i32* %7
%40 = sdiv i32 %39, 2
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds double, double* %38, i64 %41
%43 = load double, double* %42
%44 = load double, double* %8
%45 = fcmp olt double %43, %44
br i1 %45, label %46, label %59
46: ; preds = %37
%47 = load double*, double** %6
%48 = load i32, i32* %7
%49 = sdiv i32 %48, 2
%50 = add nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds double, double* %47, i64 %51
%53 = load i32, i32* %7
%54 = sub nsw i32 %53, 1
%55 = sdiv i32 %54, 2
%56 = load double, double* %8
%57 = load double**, double*** %9
%58 = call i32 @bsearch_double(double* %52, i32 %55, double %56, double** %57)
store i32 %58, i32* %5
br label %66
59: ; preds = %37
%60 = load double*, double** %6
%61 = load i32, i32* %7
%62 = sdiv i32 %61, 2
%63 = load double, double* %8
%64 = load double**, double*** %9
%65 = call i32 @bsearch_double(double* %60, i32 %62, double %63, double** %64)
store i32 %65, i32* %5
br label %66
66: ; preds = %59, %46, %30, %17
%67 = load i32, i32* %5
ret i32 %67
}

LLVM IR (-O2):
@.str = [30 x i8] c"wrong index in binary search\0A\00"
define i32 @bsearch_double(double* %0, i32 %1, double %2, double** %3) {
br label %5
5: ; preds = %25, %4
%6 = phi double* [ %28, %25 ], [ %0, %4 ]
%7 = phi i32 [ %30, %25 ], [ %1, %4 ]
br label %8
8: ; preds = %5, %22
%9 = phi i32 [ %16, %22 ], [ %7, %5 ]
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @fatal(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %15
13: ; preds = %8
%14 = icmp eq i32 %9, 0
br i1 %14, label %31, label %15
15: ; preds = %11, %13
%16 = sdiv i32 %9, 2
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds double, double* %6, i64 %17
%19 = load double, double* %18
%20 = tail call i64 @eq(double %19, double %2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %15
%23 = load double, double* %18
%24 = fcmp olt double %23, %2
br i1 %24, label %25, label %8
25: ; preds = %22
%26 = add nsw i32 %16, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds double, double* %6, i64 %27
%29 = add nsw i32 %9, -1
%30 = sdiv i32 %29, 2
br label %5
31: ; preds = %15, %13
%32 = phi double* [ %6, %13 ], [ %18, %15 ]
%33 = phi i32 [ 0, %13 ], [ 1, %15 ]
store double* %32, double** %3
ret i32 %33
}