// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r14
movl %edi, %ebx
movl $0x1, %esi
movl $0x11, %edi
callq 0x2220
movl $0x1, %esi
movl $0xd, %edi
callq 0x2220
cmpl $0x4, %ebx
jle 0x4ac8
movq 0x8(%r14), %rdi
callq 0x22f0
movl %eax, %ebx
movq 0x10(%r14), %rdi
callq 0x22f0
movl %eax, %ebp
movq 0x18(%r14), %r12
movq 0x392b(%rip), %r13      # 0x8218
leaq 0x391c(%rip), %r15      # 0x8210
movq %r12, %rdi
callq 0x20d0
movq %r15, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x22d0
movq 0x20(%r14), %r14
movq 0x391e(%rip), %r15      # 0x8238
movq %r14, %rdi
callq 0x20d0
leaq 0x3907(%rip), %rdi      # 0x8230
xorl %esi, %esi
movq %r15, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x22d0
movq 0x38d0(%rip), %rdi      # 0x8210
leaq 0x20(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0x30(%rsp), %r12
leaq 0x8(%rsp), %r8
movq %r12, %rcx
callq 0x45a9
movq (%r12), %rcx
leaq -0x178(%rip), %r15      # 0x47ef
leaq 0x40(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2370
movq (%r14), %rdi
callq 0x2140
movq 0x38a8(%rip), %rdi      # 0x8230
leaq 0x28(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0xc(%rsp), %r8
callq 0x45a9
movq 0x8(%r12), %rcx
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2370
movq (%r14), %rdi
callq 0x2140
leaq 0x40(%rsp), %rdi
movl %ebx, %esi
movl %ebp, %edx
callq 0x268c
movb $0x1, %bpl
movq 0x35e5(%rip), %r13      # 0x7fb8
leaq 0xba1(%rip), %r15       # 0x557b
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x2ad6
testb %al, %al
jne 0x4a5c
xorl %eax, %eax
testl %ebx, %ebx
sete %cl
testb $0x1, %bpl
jne 0x49f9
movb %cl, %al
movl %eax, %ebx
movq (%r13), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x33fe
leal 0x1(%rbx), %ebp
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movl %ebx, %r12d
movq 0x20(%rsp,%r12,8), %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x33fe
movq 0x10(%rsp,%r12,8), %rdi
callq 0x3029
movw %ax, 0x4(%rsp)
movl 0x4(%rsp), %r12d
movq %r14, %rdi
movl %r12d, %esi
callq 0x2890
testb %al, %al
je 0x4a96
movq %r14, %rdi
movl %ebx, %esi
movl %r12d, %edx
callq 0x26d4
movl %eax, %ebp
jmp 0x49dc
leaq 0x40(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x4827
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4a82
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb13(%rip), %rdi       # 0x55b0
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq (%r13), %rsi
leaq 0x4(%rsp), %rdi
callq 0x2fec
xorl %esi, %esi
testl %ebx, %ebx
sete %sil
leaq 0x40(%rsp), %rdi
callq 0x4827
jmp 0x4b20
leaq 0xb18(%rip), %rdi       # 0x55e7
callq 0x22c0
movl $0x1, %edi
callq 0x2240
jmp 0x4b3b
jmp 0x4b3b
movq %rax, %r14
cmpl $0x1, %edx
jne 0x4b3e
movq %r14, %rdi
callq 0x20c0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0xa8e(%rip), %rdi       # 0x5590
movl %ebp, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2040
xorl %esi, %esi
testl %ebx, %ebx
sete %sil
leaq 0x40(%rsp), %rdi
callq 0x4827
xorl %edi, %edi
callq 0x2240
movq %rax, %r14
callq 0x2330
jmp 0x4b3e
movq %rax, %rdi
callq 0x2e7b
jmp 0x4b3b
movq %rax, %r14
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4b55
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq %r14, %rdi
callq 0x2350