// File: grogers0[P]dots_and_boxes/DotsDriver.cpp
// Function: main
int main(int argc, char **argv)
{
    signal(SIGCHLD, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);

    parseArgs(argc, argv);

    FILE *player_stdin[2], *player_stdout[2], *player_stderr[2];
    pid_t player_pid[2];

    for (int player = 0; player <= 1; ++player) {
        spawn_child(command[player].c_str(), player_stdin[player],
                player_stdout[player], player_stderr[player],
                player_pid[player]);

        pthread_t thread;
        pthread_create(&thread, NULL, &echo, player_stderr[player]);
        pthread_detach(thread);
    }

    Board board(width, height);

    int player = 0;
    bool same_player = true;
    while (!board.is_game_over()) {
        if (!same_player)
            player = !player;

        board.print(stdout);
        printf("player %d's turn...\n", player + 1);

        board.print(player_stdin[player], player);

        Edge move;
        try {
            move = read_edge(player_stdout[player]);
        } catch (const std::exception &ex) {
            printf("player %d is disqualified (%s)\n", player + 1, ex.what());
            print_win_info(board, !player);
            exit(0);
        }

        if (!board.is_move_valid(move)) {
            printf("player %d is disqualified for making an invalid move: ", player + 1);
            move.print(stdout);
            print_win_info(board, !player);
            exit(0);
        }

        same_player = board.move(player, move);
    }

    print_win_info(board);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x11, %edi
movl $0x1, %esi
callq 0x22c0
movl $0xd, %edi
movl $0x1, %esi
callq 0x22c0
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x93b0
movl $0x0, -0x4c(%rbp)
cmpl $0x1, -0x4c(%rbp)
jg 0x90d3
movslq -0x4c(%rbp), %rax
leaq 0x6221(%rip), %rdi      # 0xf270
shlq $0x5, %rax
addq %rax, %rdi
callq 0x20e0
movq %rax, %rdi
movslq -0x4c(%rbp), %rax
leaq -0x20(%rbp), %rsi
shlq $0x3, %rax
addq %rax, %rsi
movslq -0x4c(%rbp), %rax
leaq -0x30(%rbp), %rdx
shlq $0x3, %rax
addq %rax, %rdx
movslq -0x4c(%rbp), %rax
leaq -0x40(%rbp), %rcx
shlq $0x3, %rax
addq %rax, %rcx
movslq -0x4c(%rbp), %rax
leaq -0x48(%rbp), %r8
shlq $0x2, %rax
addq %rax, %r8
callq 0x8c90
movslq -0x4c(%rbp), %rax
movq -0x40(%rbp,%rax,8), %rcx
leaq -0x58(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq -0x1d7(%rip), %rdx      # 0x8ee0
callq 0x2430
movq -0x58(%rbp), %rdi
callq 0x2180
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x903a
movl 0x61d7(%rip), %esi      # 0xf2b0
movl 0x61d5(%rip), %edx      # 0xf2b4
leaq -0xa0(%rbp), %rdi
callq 0x2640
movl $0x0, -0xa4(%rbp)
movb $0x1, -0xa5(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x2c50
movb %al, -0xd1(%rbp)
jmp 0x9110
movb -0xd1(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x9121
jmp 0x9363
testb $0x1, -0xa5(%rbp)
jne 0x915a
cmpl $0x0, -0xa4(%rbp)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0xa4(%rbp)
jmp 0x915a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x938e
movq 0x5e57(%rip), %rax      # 0xefb8
movq (%rax), %rsi
leaq -0xa0(%rbp), %rdi
xorl %edx, %edx
callq 0x4a00
jmp 0x9174
movl -0xa4(%rbp), %esi
incl %esi
leaq 0x148f(%rip), %rdi      # 0xa612
xorl %eax, %eax
callq 0x2040
jmp 0x918c
movslq -0xa4(%rbp), %rax
movl %eax, %edx
movq -0x20(%rbp,%rax,8), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x4a00
jmp 0x91a8
leaq -0xb8(%rbp), %rdi
callq 0x5060
jmp 0x91b6
movslq -0xa4(%rbp), %rax
movq -0x30(%rbp,%rax,8), %rdi
callq 0x4560
movl %eax, -0xd8(%rbp)
jmp 0x91cf
movl -0xd8(%rbp), %eax
movw %ax, -0xbc(%rbp)
movw -0xbc(%rbp), %ax
movw %ax, -0xb8(%rbp)
jmp 0x929f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x938e
movq -0xb0(%rbp), %rdi
callq 0x20d0
movq %rax, -0xc8(%rbp)
movl -0xa4(%rbp), %eax
incl %eax
movl %eax, -0xdc(%rbp)
movq -0xc8(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movl -0xdc(%rbp), %esi
movq %rax, %rdx
leaq 0x13d2(%rip), %rdi      # 0xa627
xorl %eax, %eax
callq 0x2040
jmp 0x925e
movl -0xa4(%rbp), %eax
testl %eax, %eax
sete %al
movzbl %al, %esi
leaq -0xa0(%rbp), %rdi
callq 0x8f40
jmp 0x927a
xorl %edi, %edi
callq 0x22e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
callq 0x23e0
jmp 0x929a
jmp 0x938e
movl -0xb8(%rbp), %eax
movl %eax, -0xcc(%rbp)
movl -0xcc(%rbp), %esi
leaq -0xa0(%rbp), %rdi
callq 0x27e0
movb %al, -0xdd(%rbp)
jmp 0x92c5
movb -0xdd(%rbp), %al
testb $0x1, %al
jne 0x9324
jmp 0x92d1
movl -0xa4(%rbp), %esi
incl %esi
leaq 0x1367(%rip), %rdi      # 0xa647
xorl %eax, %eax
callq 0x2040
jmp 0x92e9
movq 0x5cc8(%rip), %rax      # 0xefb8
movq (%rax), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x44d0
jmp 0x9301
movl -0xa4(%rbp), %eax
testl %eax, %eax
sete %al
movzbl %al, %esi
leaq -0xa0(%rbp), %rdi
callq 0x8f40
jmp 0x931d
xorl %edi, %edi
callq 0x22e0
movl -0xa4(%rbp), %esi
movl -0xb8(%rbp), %eax
movl %eax, -0xd0(%rbp)
movl -0xd0(%rbp), %edx
leaq -0xa0(%rbp), %rdi
callq 0x2700
movb %al, -0xde(%rbp)
jmp 0x9350
movb -0xde(%rbp), %al
andb $0x1, %al
movb %al, -0xa5(%rbp)
jmp 0x90fc
leaq -0xa0(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x8f40
jmp 0x9376
leaq -0xa0(%rbp), %rdi
callq 0x50d0
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
leaq -0xa0(%rbp), %rdi
callq 0x50d0
movq -0xb0(%rbp), %rdi
callq 0x2400
movq %rax, %rdi
callq 0x38d0
nop