#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 *, unsigned short *);
int main() {
  printf("Number Guessing Game!\n");
  printf("I'm going to try to guess a number that you have thought of.\n");
  printf("To keep things from getting too hairy, keep the number you think of "
         "<= 1000 ok?\n");
  unsigned long high = 1000;
  unsigned long currGuess = 500;
  unsigned long low = 0;
  unsigned short where;
  unsigned short numGuessMade = 0;
  do {
    numGuessMade++;
    printf("My guess number %hu is %lu\n", numGuessMade, currGuess);
    printf("Was I right? 1) higher, 2) lower, 3) yes!\n");
    scanf("%hu", &where);
    if (where == 1) {
      low = currGuess;
      currGuess = (low + high) / 2;
      if (currGuess == low) {
        printf("What you told me is impossible... You cheated!\n");
        return 0;
      }
    } else if (where == 2) {
      high = currGuess;
      currGuess = (low + high) / 2;
      if (currGuess == high) {
        printf("What you told me is impossible... You cheated!\n");
        return 0;
      }
    }
  } while (where != 3);
  printf("I guessed your number: %lu in %hu tries!\n", currGuess, numGuessMade);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xed3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf03(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x3e8,-0x10(%rbp)
movq   $0x1f4,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movw   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x24(%rbp)
movzwl -0x24(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xf14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf3f(%rip),%rdi
lea    -0x22(%rbp),%rsi
call   1040 <scanf@plt>
movzwl -0x22(%rbp),%eax
cmp    $0x1,%eax
jne    1209 <main+0xe9>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    1204 <main+0xe4>
lea    0xf00(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    128f <main+0x16f>
jmp    1260 <main+0x140>
movzwl -0x22(%rbp),%eax
cmp    $0x2,%eax
jne    125b <main+0x13b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1256 <main+0x136>
lea    0xeae(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    128f <main+0x16f>
jmp    125b <main+0x13b>
jmp    1260 <main+0x140>
jmp    1265 <main+0x145>
movzwl -0x22(%rbp),%eax
cmp    $0x3,%eax
jne    1177 <main+0x57>
mov    -0x18(%rbp),%rsi
movzwl -0x24(%rbp),%edx
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xf08(%rip),%rdi
call   1030 <puts@plt>
lea    0xf12(%rip),%rdi
call   1030 <puts@plt>
lea    0xf43(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1f4,%esi
mov    $0x3e8,%r14d
mov    $0x1,%cx
lea    0xea7(%rip),%r12
lea    0x6(%rsp),%r13
xor    %r15d,%r15d
jmp    1192 <main+0x62>
nop
mov    %rbp,%rsi
mov    %r15,%rbp
lea    0x1(%rbx),%ecx
mov    %rbp,%r15
cmp    $0x3,%ax
je     1210 <main+0xe0>
mov    %rsi,%rbp
movzwl %cx,%ebx
lea    0xe61(%rip),%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xf3b(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1050 <scanf@plt>
movzwl 0x6(%rsp),%eax
cmp    $0x2,%ax
je     11f0 <main+0xc0>
cmp    $0x1,%ax
jne    1180 <main+0x50>
lea    (%r14,%rbp,1),%rsi
shr    %rsi
cmp    %rbp,%rsi
jne    1186 <main+0x56>
jmp    1202 <main+0xd2>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%r15,%rbp,1),%rsi
shr    %rsi
mov    %rbp,%r14
cmp    %rbp,%rsi
mov    %r15,%rbp
jne    1186 <main+0x56>
lea    0xf0e(%rip),%rdi
call   1030 <puts@plt>
jmp    1220 <main+0xf0>
lea    0xe09(%rip),%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Number Guessing Game!\0A\00"
@.str.1 = [62 x i8] c"I'm going to try to guess a number that you have thought of.\0A\00"
@.str.2 = [81 x i8] c"To keep things from getting too hairy, keep the number you think of <= 1000 ok?\0A\00"
@.str.3 = [28 x i8] c"My guess number %hu is %lu\0A\00"
@.str.4 = [43 x i8] c"Was I right? 1) higher, 2) lower, 3) yes!\0A\00"
@.str.5 = [4 x i8] c"%hu\00"
@.str.6 = [48 x i8] c"What you told me is impossible... You cheated!\0A\00"
@.str.7 = [42 x i8] c"I guessed your number: %lu in %hu tries!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i16
%6 = alloca i16
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.2, i64 0, i64 0))
store i64 1000, i64* %2
store i64 500, i64* %3
store i64 0, i64* %4
store i16 0, i16* %6
br label %10
10: ; preds = %52, %0
%11 = load i16, i16* %6
%12 = add i16 %11, 1
store i16 %12, i16* %6
%13 = load i16, i16* %6
%14 = zext i16 %13 to i32
%15 = load i64, i64* %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %14, i64 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
%18 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i16* %5)
%19 = load i16, i16* %5
%20 = zext i16 %19 to i32
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %34
22: ; preds = %10
%23 = load i64, i64* %3
store i64 %23, i64* %4
%24 = load i64, i64* %4
%25 = load i64, i64* %2
%26 = add i64 %24, %25
%27 = udiv i64 %26, 2
store i64 %27, i64* %3
%28 = load i64, i64* %3
%29 = load i64, i64* %4
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %61
33: ; preds = %22
br label %51
34: ; preds = %10
%35 = load i16, i16* %5
%36 = zext i16 %35 to i32
%37 = icmp eq i32 %36, 2
br i1 %37, label %38, label %50
38: ; preds = %34
%39 = load i64, i64* %3
store i64 %39, i64* %2
%40 = load i64, i64* %4
%41 = load i64, i64* %2
%42 = add i64 %40, %41
%43 = udiv i64 %42, 2
store i64 %43, i64* %3
%44 = load i64, i64* %3
%45 = load i64, i64* %2
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %49
47: ; preds = %38
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %61
49: ; preds = %38
br label %50
50: ; preds = %49, %34
br label %51
51: ; preds = %50, %33
br label %52
52: ; preds = %51
%53 = load i16, i16* %5
%54 = zext i16 %53 to i32
%55 = icmp ne i32 %54, 3
br i1 %55, label %10, label %56
56: ; preds = %52
%57 = load i64, i64* %3
%58 = load i16, i16* %6
%59 = zext i16 %58 to i32
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.7, i64 0, i64 0), i64 %57, i32 %59)
store i32 0, i32* %1
br label %61
61: ; preds = %56, %47, %31
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
@.str.3 = [28 x i8] c"My guess number %hu is %lu\0A\00"
@.str.5 = [4 x i8] c"%hu\00"
@.str.7 = [42 x i8] c"I guessed your number: %lu in %hu tries!\0A\00"
@str = [22 x i8] c"Number Guessing Game!\00"
@str.8 = [61 x i8] c"I'm going to try to guess a number that you have thought of.\00"
@str.9 = [80 x i8] c"To keep things from getting too hairy, keep the number you think of <= 1000 ok?\00"
@str.10 = [42 x i8] c"Was I right? 1) higher, 2) lower, 3) yes!\00"
@str.12 = [47 x i8] c"What you told me is impossible... You cheated!\00"
define i32 @main() {
%1 = alloca i16
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str.8, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([80 x i8], [80 x i8]* @str.9, i64 0, i64 0))
%5 = bitcast i16* %1 to i8*
br label %6
6: ; preds = %29, %0
%7 = phi i64 [ 1000, %0 ], [ %30, %29 ]
%8 = phi i64 [ 500, %0 ], [ %31, %29 ]
%9 = phi i64 [ 0, %0 ], [ %32, %29 ]
%10 = phi i16 [ 0, %0 ], [ %11, %29 ]
%11 = add i16 %10, 1
%12 = zext i16 %11 to i32
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %12, i64 %8)
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.10, i64 0, i64 0))
%15 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i16* %1)
%16 = load i16, i16* %1
switch i16 %16, label %29 [
i16 1, label %17
i16 2, label %23
]
17: ; preds = %6
%18 = add i64 %8, %7
%19 = lshr i64 %18, 1
%20 = icmp eq i64 %19, %8
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.12, i64 0, i64 0))
br label %36
23: ; preds = %6
%24 = add i64 %9, %8
%25 = lshr i64 %24, 1
%26 = icmp eq i64 %25, %8
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.12, i64 0, i64 0))
br label %36
29: ; preds = %6, %17, %23
%30 = phi i64 [ %7, %17 ], [ %8, %23 ], [ %7, %6 ]
%31 = phi i64 [ %19, %17 ], [ %25, %23 ], [ %8, %6 ]
%32 = phi i64 [ %8, %17 ], [ %9, %23 ], [ %9, %6 ]
%33 = icmp eq i16 %16, 3
br i1 %33, label %34, label %6
34: ; preds = %29
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.7, i64 0, i64 0), i64 %31, i32 %12)
br label %36
36: ; preds = %34, %27, %21
ret i32 0
}