#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 checkWinner();
int /*<<< orphan*/ currentBoard(char *);
int play(int, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int player = 0;
  int go = 0;
  int row = 0;
  int column = 0;
  int winner = -1;
  int play_res;
  char buffer[100];
  do {
    do {
      currentBoard(buffer);
      printf("%s\n", buffer);
      printf("\nPlayer %d, please enter the number of the square "
             "where you want to place your %c (or 0 to refresh the board): ",
             player, (player == 1) ? 'X' : 'O');
      scanf(" %d", &go);
      if (go == 0) {
        play_res = 0;
        continue;
      }
      row = --go / 3;
      column = go % 3;
      play_res = play(row, column, player);
      if (play_res != 0) {
        switch (play_res) {
        case 1:
          printf("Position outside board.");
          break;
        case 2:
          printf("Square already taken.");
          break;
        case 3:
          printf("Not your turn.");
          break;
        case 4:
          printf("Game has finished.");
          break;
        }
        printf(" Try again...\n");
      }
    } while (play_res != 0);
    winner = checkWinner();
    player = (player + 1) % 2;
    printf("player %d\n", player);
  } while (winner == -1);
  currentBoard(buffer);
  printf("%s\n", buffer);
  if (winner == 2)
    printf("\nHow boring, it is a draw\n");
  else
    printf("\nCongratulations, player %d, YOU ARE THE WINNER!\n", winner);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
jmp    118a <main+0x3a>
lea    -0x80(%rbp),%rdi
call   1040 <currentBoard@plt>
lea    -0x80(%rbp),%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%esi
mov    -0x8(%rbp),%ecx
mov    $0x4f,%edx
mov    $0x58,%eax
cmp    $0x1,%ecx
cmove  %eax,%edx
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeb4(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1060 <scanf@plt>
cmpl   $0x0,-0xc(%rbp)
jne    11ef <main+0x9f>
movl   $0x0,-0x1c(%rbp)
jmp    12b5 <main+0x165>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%edx
call   1050 <play@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12b0 <main+0x160>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x88(%rbp)
sub    $0x3,%eax
ja     12a2 <main+0x152>
mov    -0x88(%rbp),%rax
lea    0xdae(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12a2 <main+0x152>
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12a2 <main+0x152>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12a2 <main+0x152>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12b5 <main+0x165>
cmpl   $0x0,-0x1c(%rbp)
jne    118a <main+0x3a>
mov    $0x0,%al
call   1070 <checkWinner@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %edx,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0xffffffff,-0x18(%rbp)
je     1185 <main+0x35>
lea    -0x80(%rbp),%rdi
call   1040 <currentBoard@plt>
lea    -0x80(%rbp),%rsi
lea    0xd07(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x2,-0x18(%rbp)
jne    132d <main+0x1dd>
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    133e <main+0x1ee>
mov    -0x18(%rbp),%esi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
movl   $0x0,0xc(%rsp)
lea    0x10(%rsp),%r12
lea    0xe92(%rip),%rbx
lea    0xefb(%rip),%r14
lea    0xe70(%rip),%r15
xor    %r13d,%r13d
jmp    11e6 <main+0x86>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1080 <checkWinner@plt>
mov    %eax,%ebp
mov    %r13d,%r14d
add    $0x1,%r14d
mov    %r14d,%eax
shr    $0x1f,%eax
add    %r13d,%eax
add    $0x1,%eax
and    $0xfffffffe,%eax
sub    %eax,%r14d
lea    0xf0f(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%r13d
lea    0xea7(%rip),%r14
cmp    $0xffffffff,%ebp
jne    12b3 <main+0x153>
mov    %r12,%rdi
call   1050 <currentBoard@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
cmp    $0x1,%r13d
sete   %al
lea    (%rax,%rax,8),%ebp
add    $0x4f,%ebp
jmp    1231 <main+0xd1>
lea    0xeb7(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xf13(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
call   1050 <currentBoard@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
lea    0xc(%rsp),%rsi
call   1070 <scanf@plt>
mov    0xc(%rsp),%eax
test   %eax,%eax
je     11a0 <main+0x40>
add    $0xffffffff,%eax
mov    %eax,0xc(%rsp)
movslq %eax,%rsi
imul   $0x55555556,%rsi,%rdi
mov    %rdi,%rax
shr    $0x3f,%rax
shr    $0x20,%rdi
add    %eax,%edi
lea    (%rdi,%rdi,2),%eax
sub    %eax,%esi
mov    %r13d,%edx
call   1060 <play@plt>
cmp    $0x4,%eax
ja     1215 <main+0xb5>
mov    %eax,%eax
movslq (%r15,%rax,4),%rax
add    %r15,%rax
lea    0xdef(%rip),%rdi
jmp    *%rax
lea    0xdfe(%rip),%rdi
jmp    120e <main+0xae>
lea    0xe08(%rip),%rdi
jmp    120e <main+0xae>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <currentBoard@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
cmp    $0x2,%ebp
jne    12db <main+0x17b>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
jmp    12eb <main+0x18b>
lea    0xe01(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [112 x i8] c"\0APlayer %d, please enter the number of the square where you want to place your %c (or 0 to refresh the board): \00"
@.str.2 = [4 x i8] c" %d\00"
@.str.3 = [24 x i8] c"Position outside board.\00"
@.str.4 = [22 x i8] c"Square already taken.\00"
@.str.5 = [15 x i8] c"Not your turn.\00"
@.str.6 = [19 x i8] c"Game has finished.\00"
@.str.7 = [15 x i8] c" Try again...\0A\00"
@.str.8 = [11 x i8] c"player %d\0A\00"
@.str.9 = [27 x i8] c"\0AHow boring, it is a draw\0A\00"
@.str.10 = [50 x i8] c"\0ACongratulations, player %d, YOU ARE THE WINNER!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i8]
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 -1, i32* %6
br label %9
9: ; preds = %60, %0
br label %10
10: ; preds = %50, %9
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%12 = call i32 @currentBoard(i8* %11)
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* %2
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, 1
%18 = zext i1 %17 to i64
%19 = select i1 %17, i32 88, i32 79
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([112 x i8], [112 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %19)
%21 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %10
store i32 0, i32* %7
br label %50
25: ; preds = %10
%26 = load i32, i32* %3
%27 = add nsw i32 %26, -1
store i32 %27, i32* %3
%28 = sdiv i32 %27, 3
store i32 %28, i32* %4
%29 = load i32, i32* %3
%30 = srem i32 %29, 3
store i32 %30, i32* %5
%31 = load i32, i32* %4
%32 = load i32, i32* %5
%33 = load i32, i32* %2
%34 = call i32 @play(i32 %31, i32 %32, i32 %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %25
%38 = load i32, i32* %7
switch i32 %38, label %47 [
i32 1, label %39
i32 2, label %41
i32 3, label %43
i32 4, label %45
]
39: ; preds = %37
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %47
41: ; preds = %37
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %47
43: ; preds = %37
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %47
45: ; preds = %37
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %47
47: ; preds = %37, %45, %43, %41, %39
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %49
49: ; preds = %47, %25
br label %50
50: ; preds = %49, %24
%51 = load i32, i32* %7
%52 = icmp ne i32 %51, 0
br i1 %52, label %10, label %53
53: ; preds = %50
%54 = call i32 (...) @checkWinner()
store i32 %54, i32* %6
%55 = load i32, i32* %2
%56 = add nsw i32 %55, 1
%57 = srem i32 %56, 2
store i32 %57, i32* %2
%58 = load i32, i32* %2
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %53
%61 = load i32, i32* %6
%62 = icmp eq i32 %61, -1
br i1 %62, label %9, label %63
63: ; preds = %60
%64 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%65 = call i32 @currentBoard(i8* %64)
%66 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %66)
%68 = load i32, i32* %6
%69 = icmp eq i32 %68, 2
br i1 %69, label %70, label %72
70: ; preds = %63
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0))
br label %75
72: ; preds = %63
%73 = load i32, i32* %6
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.10, i64 0, i64 0), i32 %73)
br label %75
75: ; preds = %72, %70
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [112 x i8] c"\0APlayer %d, please enter the number of the square where you want to place your %c (or 0 to refresh the board): \00"
@.str.2 = [4 x i8] c" %d\00"
@.str.3 = [24 x i8] c"Position outside board.\00"
@.str.4 = [22 x i8] c"Square already taken.\00"
@.str.5 = [15 x i8] c"Not your turn.\00"
@.str.6 = [19 x i8] c"Game has finished.\00"
@.str.8 = [11 x i8] c"player %d\0A\00"
@.str.10 = [50 x i8] c"\0ACongratulations, player %d, YOU ARE THE WINNER!\0A\00"
@str = [26 x i8] c"\0AHow boring, it is a draw\00"
@str.11 = [14 x i8] c" Try again...\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
br label %5
5: ; preds = %35, %0
%6 = phi i32 [ 0, %0 ], [ %38, %35 ]
%7 = call i32 @currentBoard(i8* %4)
%8 = call i32 @puts(i8* %4)
%9 = icmp eq i32 %6, 1
%10 = select i1 %9, i32 88, i32 79
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([112 x i8], [112 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %10)
%12 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%13 = load i32, i32* %1
%14 = icmp eq i32 %13, 0
br i1 %14, label %35, label %15
15: ; preds = %5, %27
%16 = phi i32 [ %33, %27 ], [ %13, %5 ]
%17 = add nsw i32 %16, -1
store i32 %17, i32* %1
%18 = sdiv i32 %17, 3
%19 = srem i32 %17, 3
%20 = call i32 @play(i32 %18, i32 %19, i32 %6)
switch i32 %20, label %27 [
i32 0, label %35
i32 1, label %24
i32 2, label %21
i32 3, label %22
i32 4, label %23
]
21: ; preds = %15
br label %24
22: ; preds = %15
br label %24
23: ; preds = %15
br label %24
24: ; preds = %15, %21, %22, %23
%25 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), %23 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), %22 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), %21 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), %15 ]
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %25)
br label %27
27: ; preds = %24, %15
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.11, i64 0, i64 0))
%29 = call i32 @currentBoard(i8* %4)
%30 = call i32 @puts(i8* %4)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([112 x i8], [112 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %10)
%32 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%33 = load i32, i32* %1
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %15
35: ; preds = %27, %15, %5
%36 = call i32 (...) @checkWinner()
%37 = add nsw i32 %6, 1
%38 = srem i32 %37, 2
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i32 %38)
%40 = icmp eq i32 %36, -1
br i1 %40, label %5, label %41
41: ; preds = %35
%42 = call i32 @currentBoard(i8* %4)
%43 = call i32 @puts(i8* %4)
%44 = icmp eq i32 %36, 2
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %49
47: ; preds = %41
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.10, i64 0, i64 0), i32 %36)
br label %49
49: ; preds = %47, %45
ret i32 0
}