#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 rand();
int /*<<< orphan*/ scanf(char *, int *);
int task3_guess_control(int, int);
scalar_t__ task3_valid_input(int);
int task3_main(void) {
  int number_to_guess = 0, number_guessed = 0, guesses = 0, game_state = 1,
      guess_state = 0;
  number_to_guess = rand() % 200 - 100;
  while (game_state == 1) {
    do {
      printf("Guess a number (-100 to 100): ");
      scanf(" %d", &number_guessed);
    } while (task3_valid_input(number_guessed) == 0);
    guess_state = task3_guess_control(number_to_guess, number_guessed);
    if (guess_state == 0) {
      game_state = 2;
    }
    guesses++;
  }
  while (game_state == 2) {
    printf("Congratulations, you have guessed the number!\nYou guessed it in "
           "%d guesses!\n\n",
           guesses);
    game_state = -1;
  }
}


x86 assembly (clang, -O0):
<task3_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1060 <rand@plt>
mov    $0xc8,%ecx
cltd
idiv   %ecx
sub    $0x64,%edx
mov    %edx,-0x8(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    11fc <task3_main+0xac>
jmp    119f <task3_main+0x4f>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe6b(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0xc(%rbp),%edi
call   1070 <task3_valid_input@plt>
cmp    $0x0,%rax
je     119f <task3_main+0x4f>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <task3_guess_control@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11ee <task3_main+0x9e>
movl   $0x2,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1190 <task3_main+0x40>
jmp    1201 <task3_main+0xb1>
cmpl   $0x2,-0x14(%rbp)
jne    1228 <task3_main+0xd8>
mov    -0x10(%rbp),%esi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    1201 <task3_main+0xb1>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<task3_main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
call   1060 <rand@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
imul   $0xc8,%ecx,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%r14d
add    $0xffffff9c,%r14d
lea    0xe68(%rip),%r12
lea    0xe80(%rip),%rbx
lea    0xc(%rsp),%rbp
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <scanf@plt>
mov    0xc(%rsp),%edi
call   1070 <task3_valid_input@plt>
test   %rax,%rax
je     11b0 <task3_main+0x60>
mov    0xc(%rsp),%esi
mov    %r14d,%edi
call   1040 <task3_guess_control@plt>
add    $0x1,%r15d
test   %eax,%eax
jne    11b0 <task3_main+0x60>
lea    0xe35(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Guess a number (-100 to 100): \00"
@.str.1 = [4 x i8] c" %d\00"
@.str.2 = [78 x i8] c"Congratulations, you have guessed the number!\0AYou guessed it in %d guesses!\0A\0A\00"
define i32 @task3_main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
store i32 1, i32* %5
store i32 0, i32* %6
%7 = call i32 (...) @rand()
%8 = srem i32 %7, 200
%9 = sub nsw i32 %8, 100
store i32 %9, i32* %2
br label %10
10: ; preds = %28, %0
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %31
13: ; preds = %10
br label %14
14: ; preds = %17, %13
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %3)
br label %17
17: ; preds = %14
%18 = load i32, i32* %3
%19 = call i64 @task3_valid_input(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %14, label %21
21: ; preds = %17
%22 = load i32, i32* %2
%23 = load i32, i32* %3
%24 = call i32 @task3_guess_control(i32 %22, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 2, i32* %5
br label %28
28: ; preds = %27, %21
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
br label %10
31: ; preds = %10
br label %32
32: ; preds = %35, %31
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, 2
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32, i32* %4
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.2, i64 0, i64 0), i32 %36)
store i32 -1, i32* %5
br label %32
38: ; preds = %32
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [31 x i8] c"Guess a number (-100 to 100): \00"
@.str.1 = [4 x i8] c" %d\00"
@.str.2 = [78 x i8] c"Congratulations, you have guessed the number!\0AYou guessed it in %d guesses!\0A\0A\00"
define i32 @task3_main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i32 (...) @rand()
%4 = srem i32 %3, 200
%5 = add nsw i32 %4, -100
br label %6
6: ; preds = %0, %14
%7 = phi i32 [ 0, %0 ], [ %18, %14 ]
br label %8
8: ; preds = %6, %8
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%11 = load i32, i32* %1
%12 = call i64 @task3_valid_input(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %8, label %14
14: ; preds = %8
%15 = load i32, i32* %1
%16 = call i32 @task3_guess_control(i32 %5, i32 %15)
%17 = icmp eq i32 %16, 0
%18 = add nuw nsw i32 %7, 1
br i1 %17, label %19, label %6
19: ; preds = %14
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([78 x i8], [78 x i8]* @.str.2, i64 0, i64 0), i32 %18)
ret i32 undef
}