#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*/ bubble_sort(int *, int);
int mid_search(int *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int a[20], i, element, index;
  for (i = 0; i < 20; i++)
    a[i] = rand() % 10;
  bubble_sort(a, 20);
  printf("Please input the element to search:\n");
  scanf("%d", &element);
  index = mid_search(a, element, 20);
  if (-1 == index)
    printf("not found %d\n", element);
  else
    printf("%d is located at %d", element, index + 1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x64(%rbp)
cmpl   $0x14,-0x64(%rbp)
jge    1195 <main+0x45>
mov    $0x0,%al
call   1060 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
movslq -0x64(%rbp),%rax
mov    %edx,-0x60(%rbp,%rax,4)
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    1166 <main+0x16>
lea    -0x60(%rbp),%rdi
mov    $0x14,%esi
call   1070 <bubble_sort@plt>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe8c(%rip),%rdi
lea    -0x68(%rbp),%rsi
call   1040 <scanf@plt>
lea    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%esi
mov    $0x14,%edx
call   1050 <mid_search@plt>
mov    %eax,-0x6c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x6c(%rbp),%eax
jne    11f9 <main+0xa9>
mov    -0x68(%rbp),%esi
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1210 <main+0xc0>
mov    -0x68(%rbp),%esi
mov    -0x6c(%rbp),%edx
add    $0x1,%edx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x60,%rsp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
mov    %eax,0x10(%rsp,%rbx,4)
add    $0x1,%rbx
cmp    $0x14,%rbx
jne    1170 <main+0x10>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x14,%esi
call   1080 <bubble_sort@plt>
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
lea    0xe58(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
mov    0xc(%rsp),%esi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1060 <mid_search@plt>
mov    0xc(%rsp),%esi
cmp    $0xffffffff,%eax
je     1200 <main+0xa0>
add    $0x1,%eax
lea    0xe19(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    120e <main+0xae>
lea    0xdf9(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Please input the element to search:\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [14 x i8] c"not found %d\0A\00"
@.str.3 = [20 x i8] c"%d is located at %d\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
store i32 0, i32* %3
br label %6
6: ; preds = %15, %0
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 20
br i1 %8, label %9, label %18
9: ; preds = %6
%10 = call i32 (...) @rand()
%11 = srem i32 %10, 10
%12 = load i32, i32* %3
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [20 x i32], [20 x i32]* %2, i64 0, i64 %13
store i32 %11, i32* %14
br label %15
15: ; preds = %9
%16 = load i32, i32* %3
%17 = add nsw i32 %16, 1
store i32 %17, i32* %3
br label %6
18: ; preds = %6
%19 = getelementptr inbounds [20 x i32], [20 x i32]* %2, i64 0, i64 0
%20 = call i32 @bubble_sort(i32* %19, i32 20)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%23 = getelementptr inbounds [20 x i32], [20 x i32]* %2, i64 0, i64 0
%24 = load i32, i32* %4
%25 = call i32 @mid_search(i32* %23, i32 %24, i32 20)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp eq i32 -1, %26
br i1 %27, label %28, label %31
28: ; preds = %18
%29 = load i32, i32* %4
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %29)
br label %36
31: ; preds = %18
%32 = load i32, i32* %4
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %32, i32 %34)
br label %36
36: ; preds = %31, %28
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [14 x i8] c"not found %d\0A\00"
@.str.3 = [20 x i8] c"%d is located at %d\00"
@str = [36 x i8] c"Please input the element to search:\00"
define i32 @main() {
%1 = alloca [20 x i32]
%2 = alloca i32
%3 = bitcast [20 x i32]* %1 to i8*
%4 = bitcast i32* %2 to i8*
br label %5
5: ; preds = %0, %5
%6 = phi i64 [ 0, %0 ], [ %10, %5 ]
%7 = tail call i32 (...) @rand()
%8 = srem i32 %7, 10
%9 = getelementptr inbounds [20 x i32], [20 x i32]* %1, i64 0, i64 %6
store i32 %8, i32* %9
%10 = add nuw nsw i64 %6, 1
%11 = icmp eq i64 %10, 20
br i1 %11, label %12, label %5
12: ; preds = %5
%13 = getelementptr inbounds [20 x i32], [20 x i32]* %1, i64 0, i64 0
%14 = call i32 @bubble_sort(i32* %13, i32 20)
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%17 = load i32, i32* %2
%18 = call i32 @mid_search(i32* %13, i32 %17, i32 20)
%19 = icmp eq i32 %18, -1
%20 = load i32, i32* %2
br i1 %19, label %21, label %23
21: ; preds = %12
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %26
23: ; preds = %12
%24 = add nuw nsw i32 %18, 1
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %20, i32 %24)
br label %26
26: ; preds = %23, %21
ret i32 0
}