// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movq %rsi, %rbx
movl %edi, %ebp
pushq $0x11
popq %rdi
pushq $0x1
popq %r14
movq %r14, %rsi
callq 0x2230
pushq $0xd
popq %rdi
movq %r14, %rsi
callq 0x2230
cmpl $0x4, %ebp
jle 0x46c4
movq 0x8(%rbx), %rdi
callq 0x22f0
movl %eax, 0xc(%rsp)
movq 0x10(%rbx), %rdi
callq 0x22f0
movl %eax, 0x8(%rsp)
movq 0x18(%rbx), %rsi
leaq 0x3cec(%rip), %rdi      # 0x8220
callq 0x23a0
movq 0x20(%rbx), %rsi
leaq 0x3cfc(%rip), %rdi      # 0x8240
callq 0x23a0
leaq 0x30(%rsp), %r15
leaq 0x80(%rsp), %r12
leaq 0x10(%rsp), %r13
leaq 0x20(%rsp), %rbx
xorl %ebp, %ebp
leaq 0x38(%rsp), %r14
cmpq $0x40, %rbp
je 0x45bf
leaq 0x3cac(%rip), %rax      # 0x8220
movq (%rbp,%rax), %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %rcx
movq %r15, %r8
callq 0x4209
movq (%r12), %rcx
movq %r14, %rdi
xorl %esi, %esi
leaq -0x174(%rip), %rdx      # 0x4426
callq 0x2370
movq 0x38(%rsp), %rdi
callq 0x2150
addq $0x20, %rbp
addq $0x4, %r15
addq $0x8, %r12
addq $0x8, %r13
addq $0x8, %rbx
jmp 0x4567
leaq 0x38(%rsp), %r14
movq %r14, %rdi
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
callq 0x253c
movb $0x1, %bpl
movq 0x39da(%rip), %r13      # 0x7fb8
leaq 0xf96(%rip), %r15       # 0x557b
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x2810
testb %al, %al
jne 0x4667
xorl %eax, %eax
testl %ebx, %ebx
sete %cl
testb $0x1, %bpl
jne 0x4604
movb %cl, %al
movl %eax, %ebx
movq (%r13), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x3176
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 0x3176
movq 0x10(%rsp,%r12,8), %rdi
callq 0x2e81
movw %ax, 0x4(%rsp)
movl 0x4(%rsp), %r12d
movq %r14, %rdi
movl %r12d, %esi
callq 0x26a4
testb %al, %al
je 0x4692
movq %r14, %rdi
movl %ebx, %esi
movl %r12d, %edx
callq 0x2582
movl %eax, %ebp
jmp 0x45e7
leaq 0x38(%rsp), %rdi
pushq $-0x1
popq %rsi
callq 0x4470
leaq 0x48(%rsp), %rdi
callq 0x2c44
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf17(%rip), %rdi       # 0x55b0
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq (%r13), %rsi
leaq 0x4(%rsp), %rdi
callq 0x2e48
xorl %esi, %esi
testl %ebx, %ebx
sete %sil
leaq 0x38(%rsp), %rdi
callq 0x4470
jmp 0x471a
leaq 0xf1c(%rip), %rdi       # 0x55e7
callq 0x22d0
pushq $0x1
popq %rdi
callq 0x2250
jmp 0x4735
jmp 0x4735
movq %rax, %r14
cmpl $0x1, %edx
jne 0x4738
movq %r14, %rdi
callq 0x20c0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0xe94(%rip), %rdi       # 0x5590
movl %ebp, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2040
xorl %esi, %esi
testl %ebx, %ebx
sete %sil
leaq 0x38(%rsp), %rdi
callq 0x4470
xorl %edi, %edi
callq 0x2250
movq %rax, %r14
callq 0x2330
jmp 0x4738
movq %rax, %rdi
callq 0x2bc4
jmp 0x4735
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x2c44
movq %r14, %rdi
callq 0x2350