#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ ask(char *, int);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ init_randomness();
char *malloc(int);
int /*<<< orphan*/ printf(char *);
int random_int(int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, char, int);
int main(int argc, char *argv[]) {
  int x, y, op, prev_x, prev_y, prev_op, result;
  char operator, * str;
  printf("mathprep\n\n");
  init_randomness();
  x = y = op = 0;
  if ((str = malloc(100)) == NULL)
    exit(EXIT_FAILURE);
  for (;;) {
    prev_x = x;
    prev_y = y;
    prev_op = op;
    do {
      x = random_int(1, 8);
      y = random_int(1, 3);
      op = random_int(1, 2);
    } while (x == prev_x && y == prev_y && op == prev_op);
    switch (op) {
    case 1:
      operator= '+';
      result = x + y;
      break;
    case 2:
      operator= '-';
      result = x - y;
      break;
    default:
      assert(0);
    };
    snprintf(str, 100, "%i %c %i", x, operator, y);
    if (!ask(str, result))
      break;
  }
  free(str);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe53(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1090 <init_randomness@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x64,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11f4 <main+0x64>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    11f9 <main+0x69>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x1,%edi
mov    $0x8,%esi
call   10a0 <random_int@plt>
mov    %eax,-0x14(%rbp)
mov    $0x1,%edi
mov    $0x3,%esi
call   10a0 <random_int@plt>
mov    %eax,-0x18(%rbp)
mov    $0x1,%edi
mov    $0x2,%esi
call   10a0 <random_int@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%ecx
xor    %eax,%eax
cmp    -0x20(%rbp),%ecx
mov    %al,-0x39(%rbp)
jne    126f <main+0xdf>
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
cmp    -0x24(%rbp),%ecx
mov    %al,-0x39(%rbp)
jne    126f <main+0xdf>
mov    -0x1c(%rbp),%eax
cmp    -0x28(%rbp),%eax
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    120b <main+0x7b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
sub    $0x1,%eax
je     129f <main+0x10f>
jmp    128e <main+0xfe>
mov    -0x40(%rbp),%eax
sub    $0x2,%eax
je     12b1 <main+0x121>
jmp    12c3 <main+0x133>
movb   $0x2b,-0x2d(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    12ca <main+0x13a>
movb   $0x2d,-0x2d(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    12ca <main+0x13a>
xor    %edi,%edi
call   1070 <assert@plt>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    -0x2d(%rbp),%al
mov    -0x18(%rbp),%r9d
mov    $0x64,%esi
lea    0xd27(%rip),%rdx
movsbl %al,%r8d
call   1060 <snprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1040 <ask@plt>
cmp    $0x0,%eax
jne    1307 <main+0x177>
jmp    130c <main+0x17c>
jmp    11f9 <main+0x69>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    0x2cbc(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe64(%rip),%rdi
call   1040 <puts@plt>
xor    %r14d,%r14d
xor    %eax,%eax
call   1090 <init_randomness@plt>
mov    $0x64,%edi
call   1080 <malloc@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
jne    11d6 <main+0x46>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
xor    %r15d,%r15d
xor    %r12d,%r12d
jmp    1240 <main+0xb0>
xor    %edi,%edi
call   1070 <assert@plt>
mov    0xc(%rsp),%r15d
movzbl 0xb(%rsp),%eax
mov    %al,0xb(%rsp)
movsbl %al,%r8d
mov    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x64,%esi
lea    0xdf5(%rip),%rdx
mov    %r13d,%ecx
mov    %ebp,%r9d
call   1060 <snprintf@plt>
mov    %r14,%rdi
mov    %r15d,0xc(%rsp)
mov    %r15d,%esi
call   1050 <ask@plt>
mov    %ebx,%r14d
mov    %ebp,%r15d
mov    %r13d,%r12d
test   %eax,%eax
je     12aa <main+0x11a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1,%edi
mov    $0x8,%esi
call   10a0 <random_int@plt>
mov    %eax,%r13d
mov    $0x1,%edi
mov    $0x3,%esi
call   10a0 <random_int@plt>
mov    %eax,%ebp
mov    $0x1,%edi
mov    $0x2,%esi
call   10a0 <random_int@plt>
mov    %eax,%ebx
cmp    %r12d,%r13d
jne    1283 <main+0xf3>
cmp    %r15d,%ebp
jne    1283 <main+0xf3>
cmp    %r14d,%ebx
je     1240 <main+0xb0>
cmp    $0x2,%ebx
je     129d <main+0x10d>
cmp    $0x1,%ebx
jne    11de <main+0x4e>
lea    0x0(%rbp,%r13,1),%r15d
mov    $0x2b,%al
jmp    11ef <main+0x5f>
mov    %r13d,%r15d
sub    %ebp,%r15d
mov    $0x2d,%al
jmp    11ef <main+0x5f>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"mathprep\0A\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"%i %c %i\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8
%14 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%16 = call i32 (...) @init_randomness()
store i32 0, i32* %8
store i32 0, i32* %7
store i32 0, i32* %6
%17 = call i8* @malloc(i32 100)
store i8* %17, i8** %14
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
br label %23
23: ; preds = %68, %22
%24 = load i32, i32* %6
store i32 %24, i32* %9
%25 = load i32, i32* %7
store i32 %25, i32* %10
%26 = load i32, i32* %8
store i32 %26, i32* %11
br label %27
27: ; preds = %43, %23
%28 = call i32 @random_int(i32 1, i32 8)
store i32 %28, i32* %6
%29 = call i32 @random_int(i32 1, i32 3)
store i32 %29, i32* %7
%30 = call i32 @random_int(i32 1, i32 2)
store i32 %30, i32* %8
br label %31
31: ; preds = %27
%32 = load i32, i32* %6
%33 = load i32, i32* %9
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %43
35: ; preds = %31
%36 = load i32, i32* %7
%37 = load i32, i32* %10
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i32, i32* %8
%41 = load i32, i32* %11
%42 = icmp eq i32 %40, %41
br label %43
43: ; preds = %39, %35, %31
%44 = phi i1 [ false, %35 ], [ false, %31 ], [ %42, %39 ]
br i1 %44, label %27, label %45
45: ; preds = %43
%46 = load i32, i32* %8
switch i32 %46, label %55 [
i32 1, label %47
i32 2, label %51
]
47: ; preds = %45
store i8 43, i8* %13
%48 = load i32, i32* %6
%49 = load i32, i32* %7
%50 = add nsw i32 %48, %49
store i32 %50, i32* %12
br label %57
51: ; preds = %45
store i8 45, i8* %13
%52 = load i32, i32* %6
%53 = load i32, i32* %7
%54 = sub nsw i32 %52, %53
store i32 %54, i32* %12
br label %57
55: ; preds = %45
%56 = call i32 @assert(i32 0)
br label %57
57: ; preds = %55, %51, %47
%58 = load i8*, i8** %14
%59 = load i32, i32* %6
%60 = load i8, i8* %13
%61 = load i32, i32* %7
%62 = call i32 @snprintf(i8* %58, i32 100, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %59, i8 signext %60, i32 %61)
%63 = load i8*, i8** %14
%64 = load i32, i32* %12
%65 = call i32 @ask(i8* %63, i32 %64)
%66 = icmp ne i32 %65, 0
br i1 %66, label %68, label %67
67: ; preds = %57
br label %69
68: ; preds = %57
br label %23
69: ; preds = %67
%70 = load i8*, i8** %14
%71 = call i32 @free(i8* %70)
%72 = load i32, i32* @EXIT_SUCCESS
%73 = call i32 @exit(i32 %72)
unreachable
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"%i %c %i\00"
@EXIT_SUCCESS = global i32 0
@str = [10 x i8] c"mathprep\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @init_randomness()
%5 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2, %32
%11 = phi i32 [ %19, %32 ], [ 0, %2 ]
%12 = phi i32 [ %18, %32 ], [ 0, %2 ]
%13 = phi i32 [ %33, %32 ], [ undef, %2 ]
%14 = phi i8 [ %34, %32 ], [ undef, %2 ]
%15 = phi i32 [ %17, %32 ], [ 0, %2 ]
br label %16
16: ; preds = %16, %10
%17 = tail call i32 @random_int(i32 1, i32 8)
%18 = tail call i32 @random_int(i32 1, i32 3)
%19 = tail call i32 @random_int(i32 1, i32 2)
%20 = icmp eq i32 %17, %15
%21 = icmp eq i32 %18, %12
%22 = select i1 %20, i1 %21, i1 false
%23 = icmp eq i32 %19, %11
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %16, label %25
25: ; preds = %16
switch i32 %19, label %30 [
i32 1, label %26
i32 2, label %28
]
26: ; preds = %25
%27 = add nsw i32 %18, %17
br label %32
28: ; preds = %25
%29 = sub nsw i32 %17, %18
br label %32
30: ; preds = %25
%31 = tail call i32 @assert(i32 0)
br label %32
32: ; preds = %30, %28, %26
%33 = phi i32 [ %13, %30 ], [ %29, %28 ], [ %27, %26 ]
%34 = phi i8 [ %14, %30 ], [ 45, %28 ], [ 43, %26 ]
%35 = tail call i32 @snprintf(i8* %5, i32 100, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %17, i8 signext %34, i32 %18)
%36 = tail call i32 @ask(i8* %5, i32 %33)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %10
38: ; preds = %32
%39 = tail call i32 @free(i8* %5)
%40 = load i32, i32* @EXIT_SUCCESS
%41 = tail call i32 @exit(i32 %40)
unreachable
}