// File: /workspace/github_cpp_makefile_star2_O1/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 $0x438, %rsp            # imm = 0x438
movl %edi, -0x2c(%rbp)
leaq -0x48(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1f3f(%rip), %rdx      # 0x71b4
leaq -0x58(%rbp), %r14
movq %r14, %rdi
movq %rdx, %rsi
callq 0x610a
movq $0x0, -0x38(%rbp)
xorl %r15d, %r15d
movl $0x400, %edx            # imm = 0x400
movl -0x2c(%rbp), %edi
leaq -0x460(%rbp), %rsi
xorl %ecx, %ecx
callq 0x30f0
movq %rax, %r12
testq %rax, %rax
je 0x5316
movl $0x1, %ebx
xorl %r13d, %r13d
movzbl -0x460(%rbp,%r13), %eax
cmpl $0xd, %eax
je 0x52f7
cmpl $0xa, %eax
jne 0x52ce
testb $0x1, %r15b
jne 0x5306
testb $0x1, %r15b
je 0x52e4
movq %r14, %rdi
movl $0xd, %esi
callq 0x3270
xorl %r15d, %r15d
movsbl -0x460(%rbp,%r13), %esi
movq %r14, %rdi
callq 0x3270
jmp 0x52fa
movb $0x1, %r15b
movl %ebx, %r13d
incl %ebx
cmpq %r13, %r12
ja 0x52b5
jmp 0x530c
movb $0x1, %al
movq %rax, -0x38(%rbp)
testb $0x1, -0x38(%rbp)
je 0x528f
leaq -0x58(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x3c5b
movl %eax, %eax
leaq 0x1cfd(%rip), %rcx      # 0x7028
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x4f83(%rip), %r14      # 0xa2be
jmp 0x534d
leaq 0x4f71(%rip), %r14      # 0xa2b5
jmp 0x534d
leaq 0x4f5b(%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 -0x460(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x30f0
movl -0x2c(%rbp), %edi
callq 0x34e0
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x53a3
callq 0x32c0
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x53b9
jmp 0x53b9
movq %rax, %rbx
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x53ce
callq 0x32c0
movq %rbx, %rdi
callq 0x34a0