// File: /workspace/github_cpp_makefile_star2_O2/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 0x18ef(%rip), %rsi      # 0x61b4
leaq -0x50(%rbp), %r14
leaq -0x450(%rbp), %rdx
movq %r14, %rdi
callq 0x3120
xorl %r15d, %r15d
pushq $0xd
popq %r12
movl $0x400, %edx            # imm = 0x400
movl -0x2c(%rbp), %edi
leaq -0x450(%rbp), %rsi
xorl %ecx, %ecx
callq 0x30f0
movq %rax, %r13
testq %rax, %rax
je 0x4951
xorl %ebx, %ebx
movl %ebx, %ebx
cmpq %rbx, %r13
jbe 0x48df
movzbl -0x450(%rbp,%rbx), %eax
cmpl $0xd, %eax
je 0x4922
cmpl $0xa, %eax
jne 0x4927
movb $0xa, %al
testb $0x1, %r15b
je 0x4942
jmp 0x4951
movb $0x1, %r15b
jmp 0x494d
testb $0x1, %r15b
je 0x4942
movq %r14, %rdi
movl %r12d, %esi
callq 0x3250
movb -0x450(%rbp,%rbx), %al
xorl %r15d, %r15d
movsbl %al, %esi
movq %r14, %rdi
callq 0x3250
incl %ebx
jmp 0x48ff
leaq -0x50(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x3995
movl %eax, %eax
leaq 0x16c2(%rip), %rcx      # 0x6028
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x4918(%rip), %r14      # 0x928e
jmp 0x4988
leaq 0x4906(%rip), %r14      # 0x9285
jmp 0x4988
leaq 0x48f0(%rip), %r14      # 0x9278
movq %r14, %rdi
callq 0x3140
movl -0x2c(%rbp), %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x31a0
pushq $0x2
popq %rsi
movl -0x2c(%rbp), %ebx
movl %ebx, %edi
callq 0x3390
leaq -0x450(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x30f0
movl -0x2c(%rbp), %edi
callq 0x3480
leaq -0x50(%rbp), %rdi
callq 0x3498
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x49e9
jmp 0x49e9
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x3498
movq %rbx, %rdi
callq 0x3450