// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa8, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movl $0x1, %esi
movl $0x11, %edi
callq 0x2220
movl $0x1, %esi
movl $0xd, %edi
callq 0x2220
cmpl $0x4, %ebp
jle 0x4a8a
movq 0x8(%rbx), %rdi
callq 0x22f0
movl %eax, 0x14(%rsp)
movq 0x10(%rbx), %rdi
callq 0x22f0
movl %eax, 0x10(%rsp)
movq 0x18(%rbx), %r14
movq 0x397b(%rip), %r15      # 0x8218
leaq 0x396c(%rip), %r12      # 0x8210
movq %r14, %rdi
callq 0x20d0
xorl %ebp, %ebp
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x22d0
movq 0x20(%rbx), %rbx
movq 0x396c(%rip), %r14      # 0x8238
movq %rbx, %rdi
callq 0x20d0
leaq 0x3955(%rip), %rdi      # 0x8230
xorl %esi, %esi
movq %r14, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x22d0
leaq 0x88(%rsp), %r15
leaq 0x90(%rsp), %r12
leaq 0x60(%rsp), %r13
leaq 0x70(%rsp), %rbx
leaq 0x18(%rsp), %r14
leaq 0x38ff(%rip), %rax      # 0x8210
movq (%rbp,%rax), %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %rcx
movq %r15, %r8
callq 0x4555
movq (%r12), %rcx
movq %r14, %rdi
xorl %esi, %esi
leaq -0x19c(%rip), %rdx      # 0x479b
callq 0x2370
movq 0x18(%rsp), %rdi
callq 0x2140
addq $0x20, %rbp
addq $0x4, %r15
addq $0x8, %r12
addq $0x8, %r13
addq $0x8, %rbx
cmpq $0x20, %rbp
je 0x490a
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movl 0x14(%rsp), %esi
movl 0x10(%rsp), %edx
callq 0x268c
movb $0x1, %bpl
movq 0x3639(%rip), %r12      # 0x7fb8
leaq 0xbf5(%rip), %r15       # 0x557b
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x2aa2
testb %al, %al
jne 0x4a05
xorl %eax, %eax
testl %ebx, %ebx
sete %cl
testb $0x1, %bpl
jne 0x49a5
movb %cl, %al
movl %eax, %ebx
movq (%r12), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x33a0
leal 0x1(%rbx), %ebp
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movl %ebx, %r13d
movq 0x70(%rsp,%r13,8), %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x33a0
movq 0x60(%rsp,%r13,8), %rdi
callq 0x2fd1
movw %ax, 0xc(%rsp)
movl 0xc(%rsp), %esi
movq %r14, %rdi
callq 0x285c
testb %al, %al
je 0x4a58
movl 0xc(%rsp), %edx
movq %r14, %rdi
movl %ebx, %esi
callq 0x26d4
movl %eax, %ebp
jmp 0x4988
leaq 0x18(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x47d3
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x4a44
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
xorl %eax, %eax
movq %rax, 0x28(%rsp)
movl %eax, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movl %eax, 0x40(%rsp)
movq %rax, 0x48(%rsp)
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb51(%rip), %rdi       # 0x55b0
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq (%r12), %rsi
leaq 0xc(%rsp), %rdi
callq 0x2f94
xorl %esi, %esi
testl %ebx, %ebx
sete %sil
leaq 0x18(%rsp), %rdi
callq 0x47d3
jmp 0x4ae2
leaq 0xb56(%rip), %rdi       # 0x55e7
callq 0x22c0
movl $0x1, %edi
callq 0x2240
jmp 0x4afd
jmp 0x4afd
movq %rax, %r14
cmpl $0x1, %edx
jne 0x4b00
movq %r14, %rdi
callq 0x20c0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0xacc(%rip), %rdi       # 0x5590
movl %ebp, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2040
xorl %esi, %esi
testl %ebx, %ebx
sete %sil
leaq 0x18(%rsp), %rdi
callq 0x47d3
xorl %edi, %edi
callq 0x2240
movq %rax, %r14
callq 0x2330
jmp 0x4b00
movq %rax, %rdi
callq 0x2e2b
jmp 0x4afd
movq %rax, %r14
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x4b30
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
xorl %eax, %eax
movq %rax, 0x28(%rsp)
movl %eax, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movl %eax, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq %r14, %rdi
callq 0x2350