// File: /workspace/github_cpp_makefile_star2_O1/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, %r15d
leaq -0x288(%rbp), %rdi
callq 0x3cb4
leaq -0x30(%rbp), %r14
movl $0x10, (%r14)
movq 0x3a32(%rip), %rdi      # 0x5fc8
leaq 0x1a83(%rip), %rsi      # 0x4020
movl $0x22, %edx
callq 0x21f0
leaq -0x40(%rbp), %r13
leaq -0x50(%rbp), %rbx
movl %r15d, -0x2c(%rbp)
movl $0x200, %edx            # imm = 0x200
movl %r15d, %edi
leaq -0x288(%rbp), %rsi
xorl %ecx, %ecx
leaq -0x88(%rbp), %r8
movq %r14, %r9
callq 0x2130
testl %eax, %eax
js 0x2733
je 0x2743
xorl %r12d, %r12d
cmpb $0x2, -0x288(%rbp)
jne 0x2749
movl $0x200, %edx            # imm = 0x200
leaq -0x490(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x20f0
movzwl -0x284(%rbp), %edx
movq %r15, %rdi
leaq -0x282(%rbp), %rsi
callq 0x2150
movq %r13, -0x50(%rbp)
movq %r15, %rdi
callq 0x20c0
leaq (%rax,%rbp), %rdx
addq $-0x490, %rdx           # imm = 0xFB70
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3940
cmpq $0x0, -0x48(%rbp)
je 0x2755
movq %rbx, %rdi
leaq 0x1a4f(%rip), %rsi      # 0x40a3
callq 0x20a0
testl %eax, %eax
je 0x2763
movl $0x22, %edx
movq 0x395b(%rip), %rdi      # 0x5fc8
leaq 0x1a76(%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 0x38a6
movq -0x88(%rbp), %rdx
movq -0x80(%rbp), %rcx
leaq -0x70(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq -0x78(%rbp), %r8
callq 0x2817
movl %eax, %r13d
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x26cc
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x21e0
movl $0x38, %r15d
leaq 0x1a34(%rip), %rsi      # 0x410d
testb %r13b, %r13b
je 0x26f3
movl $0x22, %r15d
movl -0x2c(%rbp), %edi
callq 0x2f16
leaq 0x192d(%rip), %rsi      # 0x4020
movq 0x38ce(%rip), %rdi      # 0x5fc8
movq %r15, %rdx
callq 0x21f0
testb %r13b, %r13b
leaq -0x40(%rbp), %rax
leaq -0x30(%rbp), %r14
movl -0x2c(%rbp), %r15d
jne 0x2782
movq -0x50(%rbp), %rdi
movq %rax, %r13
cmpq %rax, %rdi
je 0x272b
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x21e0
movl $0x1, %r12d
jmp 0x2749
callq 0x2040
cmpl $0xb, (%rax)
jne 0x27ae
movl $0x0, (%rax)
movl $0x2, %r12d
testb $0x1, %r12b
je 0x25b3
jmp 0x279c
movl $0x38, %edx
leaq 0x1909(%rip), %rsi      # 0x406a
jmp 0x276f
movl $0x2f, %edx
leaq 0x194b(%rip), %rsi      # 0x40ba
movl -0x2c(%rbp), %r15d
movq 0x384e(%rip), %rdi      # 0x5fc8
callq 0x21f0
movq %r13, %rax
movq -0x50(%rbp), %rdi
movq %rax, %r13
cmpq %rax, %rdi
je 0x2749
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x21e0
jmp 0x2749
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x383b(%rip), %rdi      # 0x5ff0
leaq 0x1887(%rip), %rsi      # 0x4043
callq 0x21b0
movl %r15d, %edi
callq 0x22b0
movl $0x1, %edi
callq 0x2210
movq %rax, %rbx
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x27f6
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x21e0
jmp 0x27f6
jmp 0x27f3
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x280f
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x2280