// File: /workspace/github_cpp_makefile_star2_O3/maclellant[P]ReliableGoBackN/server.cpp
// Function: receive_commands(int, GremlinInfo&)
void receive_commands(int sockfd, GremlinInfo& info)
{
    Packet packet;
    struct sockaddr_in client_addr;
    socklen_t slen = sizeof(client_addr);

    std::cout << "Waiting for client connection...\n\n";

    while (1)
    {

        int receiver = recvfrom(sockfd, packet.buffer, PACKET_SIZE, 0, (struct sockaddr*)&client_addr, &slen);
        if (receiver < 0)
        {          
            if (errno == EWOULDBLOCK)
            {
                errno = 0;
                continue;
            }
            else
            {
                std::cerr << "Error: Could not receive from client\n\n";
                close(sockfd);
                exit(EXIT_FAILURE);
            }
        }
        else if (receiver == 0)
        {
            continue;
        }
        else
        {
            if (packet.type() == GET)
            {
                std::string filename = packet_string(packet);

                if (filename.empty())
                {
                    std::cout << "Warning: Received invalid filename request: Discarding\n\n";
                }
                else if (filename == SUCCESS_MSG)
                {
                    std::cout << "Warning: Received success message: Discarding\n\n";
                }
                else
                {
                    std::cout << "Received GET request from client\n\n";
                    vector<Packet> packets;
                    vector<Timer> timers;
                    if (!send_file(filename, sockfd, client_addr, info))
                    {
                        std::cout << "ERROR: Client stopped responding. Ending connection...\n\n";
                        return;
                    }
                    receive_success_msg(sockfd, client_addr);

                    std::cout << "Waiting for client connection...\n\n";
                }
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x468, %rsp            # imm = 0x468
movq %rsi, -0x78(%rbp)
movl %edi, %ebx
leaq -0x288(%rbp), %rdi
callq 0x3c80
leaq -0x2c(%rbp), %rax
movl $0x10, (%rax)
movq 0x3a34(%rip), %rdi      # 0x5fc8
leaq 0x1a85(%rip), %rsi      # 0x4020
movl $0x22, %edx
callq 0x21f0
leaq -0x40(%rbp), %r15
leaq -0x88(%rbp), %r14
leaq -0x490(%rbp), %r13
movl $0x200, %edx            # imm = 0x200
movl %ebx, %edi
leaq -0x288(%rbp), %rsi
xorl %ecx, %ecx
movq %r14, %r8
leaq -0x2c(%rbp), %r9
callq 0x2130
testl %eax, %eax
js 0x26ed
je 0x25b7
cmpb $0x2, -0x288(%rbp)
jne 0x25b7
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
xorl %esi, %esi
callq 0x20f0
movzwl -0x284(%rbp), %edx
movq %r13, %rdi
leaq -0x282(%rbp), %rsi
callq 0x2150
movq %r15, -0x50(%rbp)
movq %r13, %rdi
callq 0x20c0
leaq (%rax,%rbp), %rdx
addq $-0x490, %rdx           # imm = 0xFB70
leaq -0x50(%rbp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x3908
cmpq $0x0, -0x48(%rbp)
je 0x2706
movq %r12, %rdi
leaq 0x1a5d(%rip), %rsi      # 0x40a3
callq 0x20a0
testl %eax, %eax
je 0x2714
movl $0x22, %edx
movq 0x3969(%rip), %rdi      # 0x5fc8
leaq 0x1a84(%rip), %rsi      # 0x40ea
callq 0x21f0
leaq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0x70(%rbp), %rdi
callq 0x386a
movq %r15, %r12
movq -0x88(%rbp), %rdx
movq -0x80(%rbp), %rcx
leaq -0x70(%rbp), %rdi
movl %ebx, %esi
movq -0x78(%rbp), %r8
callq 0x27f5
movl %eax, %r15d
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x26c0
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x21e0
testb %r15b, %r15b
je 0x274a
movl %ebx, %edi
movq %r12, %r15
callq 0x2ed9
movl $0x22, %edx
movq 0x38e9(%rip), %rdi      # 0x5fc8
leaq 0x193a(%rip), %rsi      # 0x4020
callq 0x21f0
jmp 0x272c
callq 0x2040
cmpl $0xb, (%rax)
jne 0x278c
movl $0x0, (%rax)
jmp 0x25b7
movl $0x38, %edx
leaq 0x1958(%rip), %rsi      # 0x406a
jmp 0x2720
movl $0x2f, %edx
leaq 0x199a(%rip), %rsi      # 0x40ba
movq 0x38a1(%rip), %rdi      # 0x5fc8
callq 0x21f0
movq -0x50(%rbp), %rdi
cmpq %r15, %rdi
je 0x25b7
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x21e0
jmp 0x25b7
movq 0x3877(%rip), %rdi      # 0x5fc8
leaq 0x19b5(%rip), %rsi      # 0x410d
movl $0x38, %edx
movq %r12, %r15
callq 0x21f0
movq -0x50(%rbp), %rdi
cmpq %r15, %rdi
je 0x277a
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x21e0
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x385d(%rip), %rdi      # 0x5ff0
leaq 0x18a9(%rip), %rsi      # 0x4043
callq 0x21b0
movl %ebx, %edi
callq 0x22b0
movl $0x1, %edi
callq 0x2210
jmp 0x27d5
movq %rax, %rbx
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x27ce
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x21e0
movq %r12, %r15
jmp 0x27d8
jmp 0x27d5
movq %rax, %rbx
movq -0x50(%rbp), %rdi
cmpq %r15, %rdi
je 0x27ed
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x2280