int main() {
  int i, j;
  char again[2], winner, **board;
  board = (char **)malloc(sizeof(char *) * 3);
  for (i = 0; i < 3; i++)
    board[i] = (char *)malloc(sizeof(char) * 3);
  printf("\nWelcome to a game of Tic-Tac-Toe!\n");
  do {
    board = initboard(board);
    displayboard(board);
    do {
      board = human_move(board, 'x');
      displayboard(board);
      winner = checkwin(board, 'x');
      if (winner != 'x' && winner != ' ') {
        if (computer_move(board, 'o', 'x'))
          winner = checkwin(board, 'o');
        else
          printf("\nComputer passed\n");
        displayboard(board);
      }
    } while (winner == '-');
    if (winner != ' ')
      printf("THE WINNER IS %c!\n", winner);
    else
      printf("IT'S A TIE.\n");
    printf("\nPlay again (y)?: ");
    scanf("%s", again);
  } while (again[0] == 'y');
  for (i = 0; i < 3; i++)
    free(board[i]);
  free(board);
  printf("\nThank you for playing.\n\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x18,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11e5 <main+0x55>
mov    $0x3,%edi
call   1070 <malloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11b4 <main+0x24>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   10a0 <initboard@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   10b0 <displayboard@plt>
mov    -0x18(%rbp),%rdi
mov    $0x78,%esi
mov    $0x0,%al
call   1080 <human_move@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   10b0 <displayboard@plt>
mov    -0x18(%rbp),%rdi
mov    $0x78,%esi
mov    $0x0,%al
call   1040 <checkwin@plt>
mov    %al,-0xf(%rbp)
movsbl -0xf(%rbp),%eax
cmp    $0x78,%eax
je     12ac <main+0x11c>
movsbl -0xf(%rbp),%eax
cmp    $0x20,%eax
je     12ac <main+0x11c>
mov    -0x18(%rbp),%rdi
mov    $0x6f,%esi
mov    $0x78,%edx
mov    $0x0,%al
call   1060 <computer_move@plt>
cmp    $0x0,%eax
je     1293 <main+0x103>
mov    -0x18(%rbp),%rdi
mov    $0x6f,%esi
mov    $0x0,%al
call   1040 <checkwin@plt>
mov    %al,-0xf(%rbp)
jmp    12a1 <main+0x111>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   10b0 <displayboard@plt>
jmp    12b1 <main+0x121>
movsbl -0xf(%rbp),%eax
cmp    $0x2d,%eax
je     120f <main+0x7f>
movsbl -0xf(%rbp),%eax
cmp    $0x20,%eax
je     12e2 <main+0x152>
movsbl -0xf(%rbp),%esi
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12f0 <main+0x160>
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xe(%rbp),%rsi
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1090 <scanf@plt>
movsbl -0xe(%rbp),%eax
cmp    $0x79,%eax
je     11f3 <main+0x63>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    134d <main+0x1bd>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1324 <main+0x194>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xd0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x18,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    $0x3,%edi
call   1080 <malloc@plt>
mov    %rax,(%rbx)
mov    $0x3,%edi
call   1080 <malloc@plt>
mov    %rax,0x8(%rbx)
mov    $0x3,%edi
call   1080 <malloc@plt>
mov    %rax,0x10(%rbx)
lea    0xe41(%rip),%rdi
call   1040 <puts@plt>
lea    0xe1f(%rip),%r15
lea    0xe2b(%rip),%r12
lea    0x6(%rsp),%r13
lea    0xe6a(%rip),%r14
jmp    123e <main+0x9e>
nopl   0x0(%rax)
lea    0xe35(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   10a0 <scanf@plt>
cmpb   $0x79,0x6(%rsp)
jne    1304 <main+0x164>
mov    %rbx,%rdi
xor    %eax,%eax
call   10b0 <initboard@plt>
movslq %eax,%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   10c0 <displayboard@plt>
jmp    1274 <main+0xd4>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
xor    %eax,%eax
call   10c0 <displayboard@plt>
shl    $0x18,%ebp
cmp    $0x2d000000,%ebp
jne    12e0 <main+0x140>
mov    %rbx,%rdi
mov    $0x78,%esi
xor    %eax,%eax
call   1090 <human_move@plt>
movslq %eax,%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   10c0 <displayboard@plt>
mov    %rbx,%rdi
mov    $0x78,%esi
xor    %eax,%eax
call   1050 <checkwin@plt>
mov    %eax,%ebp
shl    $0x18,%eax
cmp    $0x20000000,%eax
je     1269 <main+0xc9>
cmp    $0x78000000,%eax
je     1269 <main+0xc9>
mov    %rbx,%rdi
mov    $0x6f,%esi
mov    $0x78,%edx
xor    %eax,%eax
call   1070 <computer_move@plt>
test   %eax,%eax
je     1257 <main+0xb7>
mov    %rbx,%rdi
mov    $0x6f,%esi
xor    %eax,%eax
call   1050 <checkwin@plt>
mov    %eax,%ebp
jmp    125f <main+0xbf>
nopl   (%rax)
cmp    $0x20000000,%ebp
je     1210 <main+0x70>
sar    $0x18,%ebp
lea    0xd0a(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    121c <main+0x7c>
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    0x8(%rbx),%rdi
call   1030 <free@plt>
mov    0x10(%rbx),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xd2b(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"\0AWelcome to a game of Tic-Tac-Toe!\0A\00"
@.str.1 = [18 x i8] c"\0AComputer passed\0A\00"
@.str.2 = [19 x i8] c"THE WINNER IS %c!\0A\00"
@.str.3 = [13 x i8] c"IT'S A TIE.\0A\00"
@.str.4 = [19 x i8] c"\0APlay again (y)?: \00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [26 x i8] c"\0AThank you for playing.\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i8]
%5 = alloca i8
%6 = alloca i8**
store i32 0, i32* %1
%7 = call i8* @malloc(i64 24)
%8 = bitcast i8* %7 to i8**
store i8** %8, i8*** %6
store i32 0, i32* %2
br label %9
9: ; preds = %18, %0
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %21
12: ; preds = %9
%13 = call i8* @malloc(i64 3)
%14 = load i8**, i8*** %6
%15 = load i32, i32* %2
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8*, i8** %14, i64 %16
store i8* %13, i8** %17
br label %18
18: ; preds = %12
%19 = load i32, i32* %2
%20 = add nsw i32 %19, 1
store i32 %20, i32* %2
br label %9
21: ; preds = %9
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %79, %21
%24 = load i8**, i8*** %6
%25 = call i32 (i8**, ...) bitcast (i32 (...)* @initboard to i32 (i8**, ...)*)(i8** %24)
%26 = sext i32 %25 to i64
%27 = inttoptr i64 %26 to i8**
store i8** %27, i8*** %6
%28 = load i8**, i8*** %6
%29 = call i32 (i8**, ...) bitcast (i32 (...)* @displayboard to i32 (i8**, ...)*)(i8** %28)
br label %30
30: ; preds = %61, %23
%31 = load i8**, i8*** %6
%32 = call i32 (i8**, i32, ...) bitcast (i32 (...)* @human_move to i32 (i8**, i32, ...)*)(i8** %31, i32 120)
%33 = sext i32 %32 to i64
%34 = inttoptr i64 %33 to i8**
store i8** %34, i8*** %6
%35 = load i8**, i8*** %6
%36 = call i32 (i8**, ...) bitcast (i32 (...)* @displayboard to i32 (i8**, ...)*)(i8** %35)
%37 = load i8**, i8*** %6
%38 = call i32 (i8**, i32, ...) bitcast (i32 (...)* @checkwin to i32 (i8**, i32, ...)*)(i8** %37, i32 120)
%39 = trunc i32 %38 to i8
store i8 %39, i8* %5
%40 = load i8, i8* %5
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %41, 120
br i1 %42, label %43, label %60
43: ; preds = %30
%44 = load i8, i8* %5
%45 = sext i8 %44 to i32
%46 = icmp ne i32 %45, 32
br i1 %46, label %47, label %60
47: ; preds = %43
%48 = load i8**, i8*** %6
%49 = call i32 (i8**, i32, i32, ...) bitcast (i32 (...)* @computer_move to i32 (i8**, i32, i32, ...)*)(i8** %48, i32 111, i32 120)
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = load i8**, i8*** %6
%53 = call i32 (i8**, i32, ...) bitcast (i32 (...)* @checkwin to i32 (i8**, i32, ...)*)(i8** %52, i32 111)
%54 = trunc i32 %53 to i8
store i8 %54, i8* %5
br label %57
55: ; preds = %47
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %57
57: ; preds = %55, %51
%58 = load i8**, i8*** %6
%59 = call i32 (i8**, ...) bitcast (i32 (...)* @displayboard to i32 (i8**, ...)*)(i8** %58)
br label %60
60: ; preds = %57, %43, %30
br label %61
61: ; preds = %60
%62 = load i8, i8* %5
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 45
br i1 %64, label %30, label %65
65: ; preds = %61
%66 = load i8, i8* %5
%67 = sext i8 %66 to i32
%68 = icmp ne i32 %67, 32
br i1 %68, label %69, label %73
69: ; preds = %65
%70 = load i8, i8* %5
%71 = sext i8 %70 to i32
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %71)
br label %75
73: ; preds = %65
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %75
75: ; preds = %73, %69
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%77 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%78 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %77)
br label %79
79: ; preds = %75
%80 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%81 = load i8, i8* %80
%82 = sext i8 %81 to i32
%83 = icmp eq i32 %82, 121
br i1 %83, label %23, label %84
84: ; preds = %79
store i32 0, i32* %2
br label %85
85: ; preds = %94, %84
%86 = load i32, i32* %2
%87 = icmp slt i32 %86, 3
br i1 %87, label %88, label %97
88: ; preds = %85
%89 = load i8**, i8*** %6
%90 = load i32, i32* %2
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8*, i8** %89, i64 %91
%93 = load i8*, i8** %92
br label %94
94: ; preds = %88
%95 = load i32, i32* %2
%96 = add nsw i32 %95, 1
store i32 %96, i32* %2
br label %85
97: ; preds = %85
%98 = load i8**, i8*** %6
%99 = bitcast i8** %98 to i8*
%100 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [19 x i8] c"THE WINNER IS %c!\0A\00"
@.str.4 = [19 x i8] c"\0APlay again (y)?: \00"
@.str.5 = [3 x i8] c"%s\00"
@str = [35 x i8] c"\0AWelcome to a game of Tic-Tac-Toe!\00"
@str.7 = [12 x i8] c"IT'S A TIE.\00"
@str.8 = [25 x i8] c"\0AThank you for playing.\0A\00"
@str.9 = [17 x i8] c"\0AComputer passed\00"
define i32 @main() {
%1 = alloca [2 x i8]
%2 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 0
%3 = tail call dereferenceable_or_null(24) i8* @malloc(i64 24)
%4 = bitcast i8* %3 to i8**
%5 = tail call dereferenceable_or_null(3) i8* @malloc(i64 3)
store i8* %5, i8** %4
%6 = tail call dereferenceable_or_null(3) i8* @malloc(i64 3)
%7 = getelementptr inbounds i8*, i8** %4, i64 1
store i8* %6, i8** %7
%8 = tail call dereferenceable_or_null(3) i8* @malloc(i64 3)
%9 = getelementptr inbounds i8*, i8** %4, i64 2
store i8* %8, i8** %9
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %11
11: ; preds = %43, %0
%12 = phi i8** [ %4, %0 ], [ %44, %43 ]
%13 = call i32 (i8**, ...) bitcast (i32 (...)* @initboard to i32 (i8**, ...)*)(i8** %12)
%14 = sext i32 %13 to i64
%15 = inttoptr i64 %14 to i8**
%16 = call i32 (i8**, ...) bitcast (i32 (...)* @displayboard to i32 (i8**, ...)*)(i8** %15)
br label %17
17: ; preds = %35, %11
%18 = phi i8** [ %15, %11 ], [ %21, %35 ]
%19 = call i32 (i8**, i32, ...) bitcast (i32 (...)* @human_move to i32 (i8**, i32, ...)*)(i8** %18, i32 120)
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i8**
%22 = call i32 (i8**, ...) bitcast (i32 (...)* @displayboard to i32 (i8**, ...)*)(i8** %21)
%23 = call i32 (i8**, i32, ...) bitcast (i32 (...)* @checkwin to i32 (i8**, i32, ...)*)(i8** %21, i32 120)
%24 = shl i32 %23, 24
switch i32 %24, label %25 [
i32 2013265920, label %35
i32 536870912, label %35
]
25: ; preds = %17
%26 = call i32 (i8**, i32, i32, ...) bitcast (i32 (...)* @computer_move to i32 (i8**, i32, i32, ...)*)(i8** %21, i32 111, i32 120)
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 (i8**, i32, ...) bitcast (i32 (...)* @checkwin to i32 (i8**, i32, ...)*)(i8** %21, i32 111)
br label %32
30: ; preds = %25
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.9, i64 0, i64 0))
br label %32
32: ; preds = %30, %28
%33 = phi i32 [ %29, %28 ], [ %23, %30 ]
%34 = call i32 (i8**, ...) bitcast (i32 (...)* @displayboard to i32 (i8**, ...)*)(i8** %21)
br label %35
35: ; preds = %17, %17, %32
%36 = phi i32 [ %33, %32 ], [ %23, %17 ], [ %23, %17 ]
%37 = shl i32 %36, 24
switch i32 %37, label %38 [
i32 754974720, label %17
i32 536870912, label %41
]
38: ; preds = %35
%39 = ashr exact i32 %37, 24
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %39)
br label %43
41: ; preds = %35
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = inttoptr i64 %20 to i8**
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%46 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %2)
%47 = load i8, i8* %2
%48 = icmp eq i8 %47, 121
br i1 %48, label %11, label %49
49: ; preds = %43
%50 = inttoptr i64 %20 to i8**
%51 = load i8*, i8** %50
%52 = getelementptr inbounds i8*, i8** %50, i64 1
%53 = load i8*, i8** %52
%54 = getelementptr inbounds i8*, i8** %50, i64 2
%55 = load i8*, i8** %54
%56 = inttoptr i64 %20 to i8*
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.8, i64 0, i64 0))
ret i32 0
}