// File: TheiaRT[P]libiris/tcp_server.cpp
// Function: read_from_sock(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, unsigned long)
static bool read_from_sock(int client,
                           std::string &res,
                           size_t timeout_microsec)
{
    fd_set read_set;
    /* Timeout after 0.01 seconds. */
    /* TODO: Figure out a heuristic for dynamic timeout selection
     * or a more standard way of calculating it. */
    struct timeval timeout;
    timeout.tv_sec = 1;
    timeout.tv_usec = 0; //timeout_microsec;

    FD_ZERO(&read_set);
    FD_SET(client, &read_set);

    int size = 1024;
    char *buffer = new char[size];
    bzero(buffer, size);
    int recvd = 0;

    int r;
    do {
        /* See what kind of state the descriptor is in. */
        r = select(client + 1, &read_set, NULL, NULL, &timeout);
        if (r < 0) {
            perror("Select failed.");
            return false;
        }
        else if (r == 0) {
            /* Read timed out, most likely after an initial read. So it's fine
               to just return false. */
            break;
        }
        else {
            if ((recvd = recv(client, buffer, size - 1, 0)) > 0) {
                res += buffer;
                bzero(buffer, size);
            }
            else {
                /* We are not expecting any 0-sized messages. */
                perror("Receive failed.");
                return false;
            }
        }
    } while (r > 0);

    delete [] buffer;
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x1, -0xa8(%rbp)
movq $0x0, -0xa0(%rbp)
leaq -0x98(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpq $0x10, %rax
jae 0x2e16
movq -0xb8(%rbp), %rax
movl -0xac(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x2de4
jmp 0x2e18
movl -0x8(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x8(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x98(%rbp,%rax,8), %rcx
movq %rcx, -0x98(%rbp,%rax,8)
movl $0x400, -0xbc(%rbp)     # imm = 0x400
movslq -0xbc(%rbp), %rdi
callq 0x2030
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdi
movslq -0xbc(%rbp), %rdx
xorl %esi, %esi
callq 0x2140
movl $0x0, -0xcc(%rbp)
movl -0x8(%rbp), %edi
addl $0x1, %edi
leaq -0x98(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0xa8(%rbp), %r8
movq %rcx, %rdx
callq 0x2230
movl %eax, -0xd0(%rbp)
cmpl $0x0, -0xd0(%rbp)
jge 0x2ecf
leaq 0x31c0(%rip), %rdi      # 0x6081
callq 0x21e0
movb $0x0, -0x1(%rbp)
jmp 0x2f72
cmpl $0x0, -0xd0(%rbp)
jne 0x2eda
jmp 0x2f4e
movl -0x8(%rbp), %edi
movq -0xc8(%rbp), %rsi
movl -0xbc(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdx
xorl %ecx, %ecx
callq 0x20a0
movl %eax, -0xcc(%rbp)
cmpl $0x0, %eax
jle 0x2f29
movq -0xc8(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0x2240
movq -0xc8(%rbp), %rdi
movslq -0xbc(%rbp), %rdx
xorl %esi, %esi
callq 0x2140
jmp 0x2f3b
leaq 0x3160(%rip), %rdi      # 0x6090
callq 0x21e0
movb $0x0, -0x1(%rbp)
jmp 0x2f72
jmp 0x2f3d
jmp 0x2f3f
jmp 0x2f41
cmpl $0x0, -0xd0(%rbp)
jg 0x2e8b
movq -0xc8(%rbp), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x0, %rax
je 0x2f6e
movq -0xd8(%rbp), %rdi
callq 0x22f0
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xe0, %rsp
popq %rbp
retq