// File: /workspace/github_cpp_makefile_star2_O2/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 $0x298, %rsp            # imm = 0x298
movq %rsi, -0x30(%rbp)
movl %edi, %ebx
leaq -0x2c0(%rbp), %r15
movq %r15, %rdi
callq 0x37f4
leaq -0x44(%rbp), %r12
movl $0x10, (%r12)
movq 0x3a7c(%rip), %rdi      # 0x5fc8
leaq 0x1acd(%rip), %rsi      # 0x4020
callq 0x21b0
leaq -0x40(%rbp), %r14
movl $0x200, %edx            # imm = 0x200
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
movq %r14, %r8
movq %r12, %r9
callq 0x2140
testl %eax, %eax
js 0x25d0
je 0x255c
cmpb $0x2, -0x2c0(%rbp)
jne 0x255c
movl %ebx, %r13d
leaq -0x68(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2441
cmpq $0x0, -0x60(%rbp)
leaq 0x1aca(%rip), %rsi      # 0x406a
je 0x25bc
movq %rbx, %rdi
leaq 0x1af7(%rip), %rsi      # 0x40a3
callq 0x32ff
leaq 0x1b02(%rip), %rsi      # 0x40ba
testb %al, %al
je 0x25e6
movq 0x3a05(%rip), %rdi      # 0x5fc8
callq 0x21b0
movl %r13d, %ebx
jmp 0x26a3
callq 0x2030
cmpl $0xb, (%rax)
jne 0x26cc
andl $0x0, (%rax)
jmp 0x255c
movq 0x39db(%rip), %rdi      # 0x5fc8
leaq 0x1af6(%rip), %rsi      # 0x40ea
callq 0x21b0
movl %r13d, %ebx
movq %r12, %r14
movq 0x39c2(%rip), %r12      # 0x5fc8
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
andq $0x0, -0x90(%rbp)
movaps %xmm0, -0x80(%rbp)
andq $0x0, -0x70(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x2210
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
leaq -0xc0(%rbp), %r15
movq %r15, %rdi
movl %ebx, %esi
movq -0x30(%rbp), %r8
callq 0x272d
movl %eax, %r13d
movq %r15, %rdi
callq 0x20e0
leaq 0x1aad(%rip), %rsi      # 0x410d
testb %r13b, %r13b
je 0x2673
movl %ebx, %edi
callq 0x2c79
leaq 0x19ad(%rip), %rsi      # 0x4020
movq %r12, %rdi
callq 0x21b0
leaq -0x80(%rbp), %rdi
callq 0x34c4
leaq -0xa0(%rbp), %rdi
callq 0x3486
testb %r13b, %r13b
movq %r14, %r12
leaq -0x40(%rbp), %r14
leaq -0x2c0(%rbp), %r15
je 0x26b1
leaq -0x68(%rbp), %rdi
callq 0x20e0
jmp 0x255c
leaq -0x68(%rbp), %rdi
callq 0x20e0
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x391d(%rip), %rdi      # 0x5ff0
leaq 0x1969(%rip), %rsi      # 0x4043
callq 0x21b0
movl %ebx, %edi
callq 0x22b0
pushq $0x1
popq %rdi
callq 0x2200
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x20e0
jmp 0x2702
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x34c4
leaq -0xa0(%rbp), %rdi
callq 0x3486
jmp 0x271c
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x2280