// File: nesro[P]NCursesGomoku/src/server.cpp
// Function: CServer::nextMove()
void CServer::nextMove(void) {
    int sendSize;
    char sendData[4];
    memset(sendData, '\0', 4);

    int recvSize;
    char recvData[4];
    memset(recvData, '\0', 4);

    char message[CBoard::MESSAGE_LENGTH];

    /* prepare send data */
    if (m_Game->m_Players[0]->m_Surrender) {
        sendData[2] = (char) 1;
    } else {
        sendData[0] = (char) m_Board->m_LastStone.m_X;
        sendData[1] = (char) m_Board->m_LastStone.m_Y;
    }

    /* send */
    memset(message, 0, CBoard::MESSAGE_LENGTH);
    sprintf(message, " Server sending: %d,%d,%d", sendData[0],
            sendData[1], sendData[2]);
    m_Board->printMessage(message);
    usleep(1000000);


    if ((sendSize = send(m_Game->m_ConnectionSocket, sendData, 4, 0)) == -1) {
        throw CNetworkException("Fatal network error: Server send failed.");
    }

    /* nothing to do here */
    if (m_Game->m_Players[0]->m_Surrender || m_IsWinner == true) {
        return;
    }

    /* recv */
    for (int i = 0; i < 10; i++) {

        m_Board->printMessage(" Server waits for client move.");
        usleep(500000);

        if ((recvSize = recv(m_Game->m_ConnectionSocket, recvData, 4, 0)) ==
                -1) {
            throw CNetworkException("Fatal network error: Server recv failed.");
        }

        /* parse */
        memset(message, 0, CBoard::MESSAGE_LENGTH);
        sprintf(message, " Server received: %d,%d,%d", recvData[0],
                recvData[1], recvData[2]);
        m_Board->printMessage(message);

        usleep(500000);

        if (recvData[0] == -1 || recvData[1] == -1) {
            continue;
        }

        if (recvData[2] == 1) {
            m_Surrender = true;
            return;
        }

        if (m_Board->addStone(m_Mark, (int) recvData[1], (int) recvData[0]) ==
                false) {
            throw CNetworkException("Fatal network error: Bad data recevied.");
        } else {
            return;
        }
    }
    throw CNetworkException("Fatal network error: Please, wait and then \
run again program with another port.");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x3180
leaq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x3180
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x8(%rax)
je 0x6e5f
movb $0x1, -0xe(%rbp)
jmp 0x6e77
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rcx
movl 0x28(%rcx), %ecx
movb %cl, -0x10(%rbp)
movq 0x10(%rax), %rax
movl 0x2c(%rax), %eax
movb %al, -0xf(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3180
leaq -0x40(%rbp), %rdi
movsbl -0x10(%rbp), %edx
movsbl -0xf(%rbp), %ecx
movsbl -0xe(%rbp), %r8d
leaq 0x20ab(%rip), %rsi      # 0x8f4a
movb $0x0, %al
callq 0x3080
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4d00
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x3390
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rax
movl 0xc(%rax), %edi
leaq -0x10(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x3170
movl %eax, -0xc(%rbp)
cmpl $-0x1, %eax
jne 0x6f37
movl $0x28, %edi
callq 0x3160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x60(%rbp)
leaq 0x2065(%rip), %rsi      # 0x8f64
callq 0x6480
jmp 0x6f06
movq -0x60(%rbp), %rdi
leaq 0x4daf(%rip), %rsi      # 0xbcc0
leaq -0x9f8(%rip), %rdx      # 0x6520
callq 0x3400
movq -0x60(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x31d0
jmp 0x7159
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x8(%rax)
jne 0x6f5a
movq -0x58(%rbp), %rax
movb 0xa(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x6f5f
jmp 0x7150
movl $0x0, -0x50(%rbp)
cmpl $0xa, -0x50(%rbp)
jge 0x7100
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x200e(%rip), %rsi      # 0x8f8d
callq 0x4d00
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x3390
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rax
movl 0xc(%rax), %edi
leaq -0x18(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x30f0
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
jne 0x7004
movl $0x28, %edi
callq 0x3160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x68(%rbp)
leaq 0x1fe0(%rip), %rsi      # 0x8fac
callq 0x6480
jmp 0x6fd3
movq -0x68(%rbp), %rdi
leaq 0x4ce2(%rip), %rsi      # 0xbcc0
leaq -0xac5(%rip), %rdx      # 0x6520
callq 0x3400
movq -0x68(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x31d0
jmp 0x7159
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3180
leaq -0x40(%rbp), %rdi
movsbl -0x18(%rbp), %edx
movsbl -0x17(%rbp), %ecx
movsbl -0x16(%rbp), %r8d
leaq 0x1fa9(%rip), %rsi      # 0x8fd5
movb $0x0, %al
callq 0x3080
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4d00
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x3390
movsbl -0x18(%rbp), %eax
cmpl $-0x1, %eax
je 0x7060
movsbl -0x17(%rbp), %eax
cmpl $-0x1, %eax
jne 0x7065
jmp 0x70f2
movsbl -0x16(%rbp), %eax
cmpl $0x1, %eax
jne 0x707b
movq -0x58(%rbp), %rax
movb $0x1, 0x8(%rax)
jmp 0x7150
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rdi
movb 0x9(%rax), %al
movsbl -0x17(%rbp), %edx
movsbl -0x18(%rbp), %ecx
movsbl %al, %esi
callq 0x4590
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x70f0
movl $0x28, %edi
callq 0x3160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x70(%rbp)
leaq 0x1f35(%rip), %rsi      # 0x8ff0
callq 0x6480
jmp 0x70c2
movq -0x70(%rbp), %rdi
leaq 0x4bf3(%rip), %rsi      # 0xbcc0
leaq -0xbb4(%rip), %rdx      # 0x6520
callq 0x3400
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x31d0
jmp 0x7159
jmp 0x7150
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x6f66
movl $0x28, %edi
callq 0x3160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x78(%rbp)
leaq 0x1efd(%rip), %rsi      # 0x9018
callq 0x6480
jmp 0x7122
movq -0x78(%rbp), %rdi
leaq 0x4b93(%rip), %rsi      # 0xbcc0
leaq -0xc14(%rip), %rdx      # 0x6520
callq 0x3400
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x31d0
jmp 0x7159
addq $0x80, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3410