#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 interpolation_search(int *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ print_array(int *, int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ selection_sort(int *, int);
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/ *);
int main() {
  int a[20] = {0};
  int index = 0;
  int i;
  int key = 0;
  srand((unsigned int)time(NULL));
  for (i = 0; i < 20; i++)
    a[i] = rand() % 100;
  key = 50;
  printf("binary search demo,\n");
  printf("key = %d\n", key);
  printf("array: ");
  selection_sort(a, 20);
  print_array(a, 20);
  index = interpolation_search(a, 0, 20 - 1, key);
  if (0 <= index)
    printf("success, a[%d] = %d\n", index, a[index]);
  else
    printf("failed.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1060 <memset@plt>
movl   $0x0,-0x64(%rbp)
movl   $0x0,-0x6c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
movl   $0x0,-0x68(%rbp)
cmpl   $0x14,-0x68(%rbp)
jge    11f3 <main+0x73>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
movslq -0x68(%rbp),%rax
mov    %edx,-0x60(%rbp,%rax,4)
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    11c4 <main+0x44>
movl   $0x32,-0x6c(%rbp)
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x6c(%rbp),%esi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    $0x14,%esi
call   1030 <selection_sort@plt>
lea    -0x60(%rbp),%rdi
mov    $0x14,%esi
call   1040 <print_array@plt>
lea    -0x60(%rbp),%rdi
mov    -0x6c(%rbp),%ecx
xor    %esi,%esi
mov    $0x13,%edx
call   1070 <interpolation_search@plt>
mov    %eax,-0x64(%rbp)
xor    %eax,%eax
cmp    -0x64(%rbp),%eax
jg     1282 <main+0x102>
mov    -0x64(%rbp),%esi
movslq -0x64(%rbp),%rax
mov    -0x60(%rbp,%rax,4),%edx
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1290 <main+0x110>
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x50,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10a0 <rand@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x64,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,(%rsp,%rbx,4)
add    $0x1,%rbx
cmp    $0x14,%rbx
jne    11b0 <main+0x30>
lea    0xe41(%rip),%rdi
call   1040 <puts@plt>
lea    0xe0e(%rip),%rdi
mov    $0x32,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe05(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x14,%esi
call   1030 <selection_sort@plt>
mov    %rbx,%rdi
mov    $0x14,%esi
call   1050 <print_array@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x13,%edx
mov    $0x32,%ecx
call   1070 <interpolation_search@plt>
test   %eax,%eax
js     1258 <main+0xd8>
mov    %eax,%ecx
mov    (%rsp,%rcx,4),%edx
lea    0xdc5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1264 <main+0xe4>
lea    0xddc(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [21 x i8] c"binary search demo,\0A\00"
@.str.1 = [10 x i8] c"key = %d\0A\00"
@.str.2 = [8 x i8] c"array: \00"
@.str.3 = [21 x i8] c"success, a[%d] = %d\0A\00"
@.str.4 = [9 x i8] c"failed.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [20 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [20 x i32]* %2 to i8*
store i32 0, i32* %3
store i32 0, i32* %5
%7 = call i64 @time(i32* null)
%8 = trunc i64 %7 to i32
%9 = call i32 @srand(i32 %8)
store i32 0, i32* %4
br label %10
10: ; preds = %19, %0
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 20
br i1 %12, label %13, label %22
13: ; preds = %10
%14 = call i32 (...) @rand()
%15 = srem i32 %14, 100
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [20 x i32], [20 x i32]* %2, i64 0, i64 %17
store i32 %15, i32* %18
br label %19
19: ; preds = %13
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
store i32 %21, i32* %4
br label %10
22: ; preds = %10
store i32 50, i32* %5
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* %5
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%27 = getelementptr inbounds [20 x i32], [20 x i32]* %2, i64 0, i64 0
%28 = call i32 @selection_sort(i32* %27, i32 20)
%29 = getelementptr inbounds [20 x i32], [20 x i32]* %2, i64 0, i64 0
%30 = call i32 @print_array(i32* %29, i32 20)
%31 = getelementptr inbounds [20 x i32], [20 x i32]* %2, i64 0, i64 0
%32 = load i32, i32* %5
%33 = call i32 @interpolation_search(i32* %31, i32 0, i32 19, i32 %32)
store i32 %33, i32* %3
%34 = load i32, i32* %3
%35 = icmp sle i32 0, %34
br i1 %35, label %36, label %43
36: ; preds = %22
%37 = load i32, i32* %3
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [20 x i32], [20 x i32]* %2, i64 0, i64 %39
%41 = load i32, i32* %40
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 %41)
br label %45
43: ; preds = %22
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %36
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"key = %d\0A\00"
@.str.2 = [8 x i8] c"array: \00"
@.str.3 = [21 x i8] c"success, a[%d] = %d\0A\00"
@str = [20 x i8] c"binary search demo,\00"
@str.5 = [8 x i8] c"failed.\00"
define i32 @main() {
%1 = alloca [20 x i32]
%2 = bitcast [20 x i32]* %1 to i8*
%3 = tail call i64 @time(i32* null)
%4 = trunc i64 %3 to i32
%5 = tail call i32 @srand(i32 %4)
br label %6
6: ; preds = %0, %6
%7 = phi i64 [ 0, %0 ], [ %11, %6 ]
%8 = tail call i32 (...) @rand()
%9 = srem i32 %8, 100
%10 = getelementptr inbounds [20 x i32], [20 x i32]* %1, i64 0, i64 %7
store i32 %9, i32* %10
%11 = add nuw nsw i64 %7, 1
%12 = icmp eq i64 %11, 20
br i1 %12, label %13, label %6
13: ; preds = %6
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 50)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%17 = getelementptr inbounds [20 x i32], [20 x i32]* %1, i64 0, i64 0
%18 = call i32 @selection_sort(i32* %17, i32 20)
%19 = call i32 @print_array(i32* %17, i32 20)
%20 = call i32 @interpolation_search(i32* %17, i32 0, i32 19, i32 50)
%21 = icmp sgt i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %13
%23 = zext i32 %20 to i64
%24 = getelementptr inbounds [20 x i32], [20 x i32]* %1, i64 0, i64 %23
%25 = load i32, i32* %24
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %20, i32 %25)
br label %29
27: ; preds = %13
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.5, i64 0, i64 0))
br label %29
29: ; preds = %27, %22
ret i32 0
}