#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*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
busca(int *a) {
  int val, inicio, fim, meio, cont = 0, posicao;
  bool achou;
  achou = false;
  printf(" Entre com o valor a ser buscado: ");
  scanf("%d", &val);
  inicio = 0;
  fim = 99;
  while (inicio <= fim && !achou) {
    cont++;
    meio = (inicio + fim) / 2;
    if (a[meio] == val)
      achou = true;
    else if (val > a[meio]) {
      achou = false;
      inicio = meio + 1;
    } else {
      achou = false;
      fim = meio - 1;
    }
  }
  if (achou) {
    printf(" Numero encontrado!\n Quantidade de verificacoes executadas: %d\n "
           "Posicao:%d",
           cont, meio);
  } else {
    printf(" Numero nao encontrado!");
  }
}


x86 assembly (clang, -O0):
<busca>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    0xebf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf1c(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x63,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
cmp    -0x1c(%rbp),%ecx
mov    %al,-0x2d(%rbp)
jg     1183 <busca+0x63>
cmpl   $0x0,-0x2c(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    1193 <busca+0x73>
jmp    1210 <busca+0xf0>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jne    11cd <busca+0xad>
movl   $0x1,-0x2c(%rbp)
jmp    120b <busca+0xeb>
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11f6 <busca+0xd6>
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1206 <busca+0xe6>
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    120b <busca+0xeb>
jmp    1166 <busca+0x46>
cmpl   $0x0,-0x2c(%rbp)
je     1233 <busca+0x113>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1241 <busca+0x121>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<busca>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xed2(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xf2d(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1040 <scanf@plt>
mov    $0x63,%ecx
mov    $0x1,%esi
mov    0x4(%rsp),%eax
jmp    116c <busca+0x4c>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%edx
mov    %edx,%ecx
add    $0x1,%esi
cmp    %ecx,%ebx
jg     118c <busca+0x6c>
lea    (%rbx,%rcx,1),%edi
mov    %edi,%edx
shr    $0x1f,%edx
add    %edi,%edx
sar    %edx
movslq %edx,%rdi
mov    (%r14,%rdi,4),%edi
cmp    %eax,%edi
je     119c <busca+0x7c>
jge    1160 <busca+0x40>
add    $0x1,%edx
mov    %edx,%ebx
jmp    1165 <busca+0x45>
lea    0xedb(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11aa <busca+0x8a>
lea    0xe80(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [35 x i8] c" Entre com o valor a ser buscado: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [75 x i8] c" Numero encontrado!\0A Quantidade de verificacoes executadas: %d\0A Posicao:%d\00"
@.str.3 = [24 x i8] c" Numero nao encontrado!\00"
define i32 @busca(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %3
store i32 0, i32* %8
store i32 0, i32* %10
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
store i32 0, i32* %5
store i32 99, i32* %6
br label %13
13: ; preds = %53, %1
%14 = load i32, i32* %5
%15 = load i32, i32* %6
%16 = icmp sle i32 %14, %15
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* %10
%19 = icmp ne i32 %18, 0
%20 = xor i1 %19, true
br label %21
21: ; preds = %17, %13
%22 = phi i1 [ false, %13 ], [ %20, %17 ]
br i1 %22, label %23, label %54
23: ; preds = %21
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = add nsw i32 %26, %27
%29 = sdiv i32 %28, 2
store i32 %29, i32* %7
%30 = load i32*, i32** %3
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32, i32* %33
%35 = load i32, i32* %4
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %23
store i32 1, i32* %10
br label %53
38: ; preds = %23
%39 = load i32, i32* %4
%40 = load i32*, i32** %3
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
%44 = load i32, i32* %43
%45 = icmp sgt i32 %39, %44
br i1 %45, label %46, label %49
46: ; preds = %38
store i32 0, i32* %10
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 1
store i32 %48, i32* %5
br label %52
49: ; preds = %38
store i32 0, i32* %10
%50 = load i32, i32* %7
%51 = sub nsw i32 %50, 1
store i32 %51, i32* %6
br label %52
52: ; preds = %49, %46
br label %53
53: ; preds = %52, %37
br label %13
54: ; preds = %21
%55 = load i32, i32* %10
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = load i32, i32* %8
%59 = load i32, i32* %7
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.2, i64 0, i64 0), i32 %58, i32 %59)
br label %63
61: ; preds = %54
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %63
63: ; preds = %61, %57
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
@.str = [35 x i8] c" Entre com o valor a ser buscado: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [75 x i8] c" Numero encontrado!\0A Quantidade de verificacoes executadas: %d\0A Posicao:%d\00"
@.str.3 = [24 x i8] c" Numero nao encontrado!\00"
define i32 @busca(i32* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%6 = load i32, i32* %2
br label %7
7: ; preds = %1, %18
%8 = phi i32 [ 0, %1 ], [ %23, %18 ]
%9 = phi i32 [ 0, %1 ], [ %11, %18 ]
%10 = phi i32 [ 99, %1 ], [ %22, %18 ]
%11 = add nuw nsw i32 %9, 1
%12 = add nsw i32 %8, %10
%13 = sdiv i32 %12, 2
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i32, i32* %0, i64 %14
%16 = load i32, i32* %15
%17 = icmp eq i32 %16, %6
br i1 %17, label %25, label %18
18: ; preds = %7
%19 = icmp sgt i32 %6, %16
%20 = add nsw i32 %13, 1
%21 = add nsw i32 %13, -1
%22 = select i1 %19, i32 %10, i32 %21
%23 = select i1 %19, i32 %20, i32 %8
%24 = icmp sgt i32 %23, %22
br i1 %24, label %27, label %7
25: ; preds = %7
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @.str.2, i64 0, i64 0), i32 %11, i32 %13)
br label %29
27: ; preds = %18
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %25
ret i32 undef
}