#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*/ copy(char *, char *);
scalar_t__ end_game(char *, char);
scalar_t__ is_a_valid_move(char);
char opponent(char);
int win(char *, char);
int minimax(char board[], char player) {
  if (end_game(board, player))
    return win(board, player);
  int i, score = 0, best_score = -1;
  char new_board[9];
  score = win(board, player);
  for (i = 0; i < 9; ++i) {
    copy(board, new_board);
    if (is_a_valid_move(new_board[i])) {
      new_board[i] = player;
      score = minimax(new_board, opponent(player)) * -1;
      if (score > best_score) {
        best_score = score;
      }
    }
  }
  return best_score;
}


x86 assembly (clang, -O0):
<minimax>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1080 <end_game@plt>
cmp    $0x0,%rax
je     119e <minimax+0x3e>
mov    -0x10(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1070 <win@plt>
mov    %eax,-0x4(%rbp)
jmp    1250 <minimax+0xf0>
movl   $0x0,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1070 <win@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x9,-0x18(%rbp)
jge    124a <minimax+0xea>
mov    -0x10(%rbp),%rdi
lea    -0x29(%rbp),%rsi
call   1060 <copy@plt>
movslq -0x18(%rbp),%rax
movsbl -0x29(%rbp,%rax,1),%edi
call   1050 <is_a_valid_move@plt>
cmp    $0x0,%rax
je     1237 <minimax+0xd7>
mov    -0x11(%rbp),%cl
movslq -0x18(%rbp),%rax
mov    %cl,-0x29(%rbp,%rax,1)
lea    -0x29(%rbp),%rax
mov    %rax,-0x38(%rbp)
movsbl -0x11(%rbp),%edi
call   1030 <opponent@plt>
mov    -0x38(%rbp),%rdi
movsbl %al,%esi
call   1040 <minimax@plt>
imul   $0xffffffff,%eax,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    1232 <minimax+0xd2>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1237 <minimax+0xd7>
jmp    123c <minimax+0xdc>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c3 <minimax+0x63>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<minimax>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %rdi,%r13
call   1080 <end_game@plt>
test   %rax,%rax
je     1198 <minimax+0x38>
movsbl %r15b,%esi
mov    %r13,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <win@plt>
movsbl %r15b,%r14d
mov    %r13,%rdi
mov    %r14d,%esi
call   1070 <win@plt>
mov    $0xffffffff,%ebp
xor    %ebx,%ebx
lea    0xf(%rsp),%r12
jmp    11ca <minimax+0x6a>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rbx
cmp    $0x9,%rbx
je     1205 <minimax+0xa5>
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <copy@plt>
movsbl 0xf(%rsp,%rbx,1),%edi
call   1050 <is_a_valid_move@plt>
test   %rax,%rax
je     11c0 <minimax+0x60>
mov    %r15b,0xf(%rsp,%rbx,1)
mov    %r14d,%edi
call   1030 <opponent@plt>
movsbl %al,%esi
mov    %r12,%rdi
call   1040 <minimax@plt>
neg    %eax
cmp    %eax,%ebp
cmovl  %eax,%ebp
jmp    11c0 <minimax+0x60>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @minimax(i8* %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [9 x i8]
store i8* %0, i8** %4
store i8 %1, i8* %5
%10 = load i8*, i8** %4
%11 = load i8, i8* %5
%12 = call i64 @end_game(i8* %10, i8 signext %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i8, i8* %5
%17 = call i32 @win(i8* %15, i8 signext %16)
store i32 %17, i32* %3
br label %57
18: ; preds = %2
store i32 0, i32* %7
store i32 -1, i32* %8
%19 = load i8*, i8** %4
%20 = load i8, i8* %5
%21 = call i32 @win(i8* %19, i8 signext %20)
store i32 %21, i32* %7
store i32 0, i32* %6
br label %22
22: ; preds = %52, %18
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 9
br i1 %24, label %25, label %55
25: ; preds = %22
%26 = load i8*, i8** %4
%27 = getelementptr inbounds [9 x i8], [9 x i8]* %9, i64 0, i64 0
%28 = call i32 @copy(i8* %26, i8* %27)
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [9 x i8], [9 x i8]* %9, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = call i64 @is_a_valid_move(i8 signext %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %51
35: ; preds = %25
%36 = load i8, i8* %5
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [9 x i8], [9 x i8]* %9, i64 0, i64 %38
store i8 %36, i8* %39
%40 = getelementptr inbounds [9 x i8], [9 x i8]* %9, i64 0, i64 0
%41 = load i8, i8* %5
%42 = call signext i8 @opponent(i8 signext %41)
%43 = call i32 @minimax(i8* %40, i8 signext %42)
%44 = mul nsw i32 %43, -1
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = load i32, i32* %8
%47 = icmp sgt i32 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %35
%49 = load i32, i32* %7
store i32 %49, i32* %8
br label %50
50: ; preds = %48, %35
br label %51
51: ; preds = %50, %25
br label %52
52: ; preds = %51
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %22
55: ; preds = %22
%56 = load i32, i32* %8
store i32 %56, i32* %3
br label %57
57: ; preds = %55, %14
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
define i32 @minimax(i8* %0, i8 signext %1) {
%3 = alloca [9 x i8]
%4 = tail call i64 @end_game(i8* %0, i8 signext %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @win(i8* %0, i8 signext %1)
br label %30
8: ; preds = %2
%9 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%10 = tail call i32 @win(i8* %0, i8 signext %1)
br label %11
11: ; preds = %8, %25
%12 = phi i64 [ 0, %8 ], [ %27, %25 ]
%13 = phi i32 [ -1, %8 ], [ %26, %25 ]
%14 = call i32 @copy(i8* %0, i8* %9)
%15 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 %12
%16 = load i8, i8* %15
%17 = call i64 @is_a_valid_move(i8 signext %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %11
store i8 %1, i8* %15
%20 = call signext i8 @opponent(i8 signext %1)
%21 = call i32 @minimax(i8* %9, i8 signext %20)
%22 = sub nsw i32 0, %21
%23 = icmp slt i32 %13, %22
%24 = select i1 %23, i32 %22, i32 %13
br label %25
25: ; preds = %19, %11
%26 = phi i32 [ %13, %11 ], [ %24, %19 ]
%27 = add nuw nsw i64 %12, 1
%28 = icmp eq i64 %27, 9
br i1 %28, label %29, label %11
29: ; preds = %25
br label %30
30: ; preds = %29, %6
%31 = phi i32 [ %7, %6 ], [ %26, %29 ]
ret i32 %31
}