// File: /workspace/github_cpp_makefile_star2_O3/Lartu[P]coraldb/source/coraldb.cpp
// Function: handle_socket(int)
void handle_socket(int new_socket_fd)
{
    // TODO add alarm with timeout https://stackoverflow.com/questions/9163308/how-to-use-timeouts-with-read-on-a-socket-in-c-on-unix/9163476
    std::string str_buffer = "";
    char buffer[1024];
    size_t bytes_read;
    bool request_read_end = false;
    bool last_char_was_cr = false;
    while ((bytes_read = recv(new_socket_fd, buffer, sizeof(buffer), 0)) > 0)
    {
        for (unsigned int i = 0; i < bytes_read; ++i)
        {
            // \r\n terminates the message.
            if (buffer[i] == '\r')
            {
                last_char_was_cr = true;
            }
            else if (buffer[i] == '\n' && last_char_was_cr)
            {
                request_read_end = true;
                break;
            }
            else
            {
                if (last_char_was_cr)
                {
                    // If we found a CR but it wasn't a CRLF, add the CR to the buffer.
                    str_buffer += '\r';
                    last_char_was_cr = false;
                }
                str_buffer += buffer[i];
            }
        }
        if (request_read_end)
            break;
    }
    int retcode = execute_command(str_buffer, new_socket_fd);
    if (retcode == 1)
    {
        send(new_socket_fd, ERROR_MSG, strlen(ERROR_MSG), 0);
    }
    else if (retcode == 0)
    {
        send(new_socket_fd, OK_MSG, strlen(OK_MSG), 0);
    }
    else if (retcode == 3)
    {
        send(new_socket_fd, KEY_ERROR_MSG, strlen(KEY_ERROR_MSG), 0);
    }
    // retcode 2 means "don't send anything"
    shutdown(new_socket_fd, SHUT_RDWR);
    recv(new_socket_fd, buffer, sizeof(buffer), 0);
    close(new_socket_fd);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movl %edi, -0x2c(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1f5e(%rip), %rdx      # 0x71b4
leaq -0x50(%rbp), %r14
movq %r14, %rdi
movq %rdx, %rsi
callq 0x60ac
xorl %r13d, %r13d
movl $0x400, %edx            # imm = 0x400
movl -0x2c(%rbp), %edi
leaq -0x450(%rbp), %rsi
xorl %ecx, %ecx
callq 0x30f0
movq %rax, %r12
testq %rax, %rax
je 0x52e4
movl $0x1, %r15d
xorl %ebx, %ebx
movzbl -0x450(%rbp,%rbx), %eax
cmpl $0xd, %eax
je 0x52aa
cmpl $0xa, %eax
jne 0x52af
movb $0xa, %al
testb $0x1, %r13b
je 0x52cc
jmp 0x52e4
movb $0x1, %r13b
jmp 0x52d7
testb $0x1, %r13b
je 0x52cc
movq %r14, %rdi
movl $0xd, %esi
callq 0x3270
movb -0x450(%rbp,%rbx), %al
xorl %r13d, %r13d
movsbl %al, %esi
movq %r14, %rdi
callq 0x3270
movl %r15d, %ebx
incl %r15d
cmpq %rbx, %r12
ja 0x528e
jmp 0x5268
leaq -0x50(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x3c5b
movl %eax, %eax
leaq 0x1d2f(%rip), %rcx      # 0x7028
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x4fb5(%rip), %r14      # 0xa2be
jmp 0x531b
leaq 0x4fa3(%rip), %r14      # 0xa2b5
jmp 0x531b
leaq 0x4f8d(%rip), %r14      # 0xa2a8
movq %r14, %rdi
callq 0x3150
movl -0x2c(%rbp), %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x3190
movl -0x2c(%rbp), %ebx
movl %ebx, %edi
movl $0x2, %esi
callq 0x33c0
leaq -0x450(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x30f0
movl -0x2c(%rbp), %edi
callq 0x34e0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x5371
callq 0x32c0
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5387
jmp 0x5387
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x539c
callq 0x32c0
movq %rbx, %rdi
callq 0x34a0