// File: /workspace/github_cpp_makefile_star2_O3/maclellant[P]ReliableGoBackN/server.cpp
// Function: send_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, sockaddr_in, GremlinInfo&)
bool send_file(std::string filename, int sockfd, struct sockaddr_in client_addr, GremlinInfo& info)
{
    socklen_t slen = sizeof(client_addr);

    vector<Packet> packets;
    vector<Timer> timers;
    parse_file(filename, packets, timers);

    int window_end = packets.size();
    int window_base = 0;
    int current = 0;
    int timeout_counter = 0;

    vector<Packet> delay_packets;
    vector<Timer> delay_timers;

    while (window_base < window_end)
    {
        // Check on the delayed packets
        if (!delay_timers.empty())
        {
            for (int i = 0; i < delay_timers.size(); ++i)
            {
                if (delay_timers[i].timeout(info.delay_amount_ms))
                {
                    std::cout << "SENDING: sequence " << (int)delay_packets[i].sequence() << "\n";
                    std::cout << "DATA:\n";
                    std::cout << packet_string(delay_packets[i], 48);
                    std::cout << "\n\n";
                    if (sendto(sockfd, delay_packets[i].buffer, PACKET_SIZE, 0, (struct sockaddr*) &client_addr, slen) == -1)
                    {
                        std::cerr << "Error: could not send packet to client" << std::endl;
                        close(sockfd);
                        exit(EXIT_FAILURE);
                    }

                    delay_timers.erase(delay_timers.begin() + i);
                    delay_packets.erase(delay_packets.begin() + i);
                    break;
                }
            }
        }
        else if (current < (window_base + WINDOW_SIZE) && current < window_end)
        {
            Packet temp = packets[current];
            int result = send_packet(sockfd, client_addr, temp, info);
            if (result == DELAYED)
            {
                Timer delay_timer = Timer();
                delay_timer.start();
                delay_timers.push_back(delay_timer);
                delay_packets.push_back(temp);
            }
            timers[current].start();
            current++;
        }
        else if (timers[window_base].timeout(SERVER_TIMEOUT_MSEC))
        {
            std::cout << "TIMEOUT: Retransmitting current window\n\n";
            current = window_base;
            timeout_counter++;
            if (timeout_counter > SERVER_CANCEL_TIMEOUT_COUNT)
            {
                return false;
            }
        }

        Packet received;
        struct sockaddr_in client_rec;
        socklen_t slen_rec = sizeof(client_addr);

        int receiver = recvfrom(sockfd, received.buffer, PACKET_SIZE, 0, (struct sockaddr*)&client_rec, &slen_rec);
        if (receiver < 0)
        {          
            if (errno == EWOULDBLOCK)
            {
                errno = 0;
            }
            else
            {
                std::cerr << "Error: Could not receive from client" << std::endl;
                close(sockfd);
                exit(EXIT_FAILURE);
            }
        }
        else if (receiver > 0)
        {
            timeout_counter = 0;

            if (received.type() == ACK)
            {
                std::cout << "ACKNOLEDGE: sequence " << (int) received.sequence() << "\n\n";
                if (received.sequence() == ((window_base + 1) % SEQ_NUM))
                {
                    window_base++;
                    if (current < window_base)
                        current = window_base;
                }             
            }
            else if (received.type() == NAK)
            {
                std::cout << "DAMAGED DATA: sequence " << (int) received.sequence() << "\n\n";
                current = window_base;
            }
        }
    }

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2d8, %rsp            # imm = 0x2D8
movq %r8, -0xb8(%rbp)
movl %esi, %r13d
movq %rdx, -0xe0(%rbp)
movq %rcx, -0xd8(%rbp)
xorl %eax, %eax
leaq -0xd0(%rbp), %rsi
movq %rax, 0x10(%rsi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsi)
leaq -0x100(%rbp), %rdx
movq %rax, 0x10(%rdx)
movaps %xmm0, (%rdx)
callq 0x3032
movq -0xc8(%rbp), %r12
subq -0xd0(%rbp), %r12
shrq $0x9, %r12
xorl %r15d, %r15d
movq %r15, -0x60(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movq %r15, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
testl %r12d, %r12d
jle 0x2d71
xorl %r14d, %r14d
movl $0x0, -0x30(%rbp)
movl %r13d, -0x2c(%rbp)
movq %r12, -0x78(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
movl %r14d, -0x34(%rbp)
je 0x28f8
movq %r15, -0x80(%rbp)
movl $0x200, %r12d           # imm = 0x200
movl $0x10, %r15d
xorl %ebx, %ebx
leaq (%rax,%r15), %rdi
addq $-0x10, %rdi
movq -0xb8(%rbp), %rax
movl 0xc(%rax), %esi
callq 0x37f8
testb %al, %al
jne 0x299d
incq %rbx
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
addq $0x200, %r12            # imm = 0x200
addq $0x10, %r15
cmpq %rbx, %rcx
ja 0x28aa
movq -0x80(%rbp), %r15
movq -0x78(%rbp), %r12
jmp 0x2bbc
leal 0x10(%r15), %eax
cmpl %eax, %r14d
setge %al
cmpl %r12d, %r14d
setge %cl
orb %al, %cl
jne 0x2b6a
movl %r13d, %ebx
movslq %r14d, %r13
movq %r13, %rsi
shlq $0x9, %rsi
addq -0xd0(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
leaq -0x300(%rbp), %r14
movq %r14, %rdi
callq 0x2150
movq -0xe0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
movl %ebx, %edi
movq %r14, %rcx
movq -0xb8(%rbp), %r8
callq 0x32c7
cmpl $0x2, %eax
jne 0x2c07
xorps %xmm0, %xmm0
movaps %xmm0, -0xb0(%rbp)
xorl %edi, %edi
leaq -0xb0(%rbp), %rbx
movq %rbx, %rsi
callq 0x2190
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x2bc5
movaps -0xb0(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x48(%rbp)
jmp 0x2bd1
movl $0x12, %edx
movq 0x361f(%rip), %rdi      # 0x5fc8
leaq 0x17dc(%rip), %rsi      # 0x418c
callq 0x21f0
movq -0x70(%rbp), %rax
movzbl -0x1ff(%rax,%r12), %esi
movq 0x35ff(%rip), %rdi      # 0x5fc8
callq 0x2270
movl $0x1, %edx
movq %rax, %rdi
leaq 0x1767(%rip), %rsi      # 0x4144
callq 0x21f0
movl $0x6, %edx
movq 0x35da(%rip), %rdi      # 0x5fc8
leaq 0x17aa(%rip), %rsi      # 0x419f
callq 0x21f0
movq -0x70(%rbp), %rax
leaq (%rax,%r12), %rbx
addq $-0x1fa, %rbx           # imm = 0xFE06
movzwl -0x2(%rbx), %eax
cmpw $0x30, %ax
movl $0x30, %ecx
cmovael %ecx, %eax
movzwl %ax, %r13d
movl $0x200, %edx            # imm = 0x200
leaq -0x300(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x20f0
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x2150
leaq -0xa0(%rbp), %r13
movq %r13, -0xb0(%rbp)
movq %r14, %rdi
callq 0x20c0
leaq (%rax,%rbp), %rdx
addq $-0x300, %rdx           # imm = 0xFD00
leaq -0xb0(%rbp), %rdi
movq %r14, %rsi
callq 0x3908
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
movq 0x3542(%rip), %rdi      # 0x5fc8
callq 0x21f0
movq -0xb0(%rbp), %rdi
cmpq %r13, %rdi
movl -0x2c(%rbp), %r13d
je 0x2aaa
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x21e0
movl $0x2, %edx
movq 0x3512(%rip), %rdi      # 0x5fc8
leaq 0x1686(%rip), %rsi      # 0x4143
callq 0x21f0
movq -0x70(%rbp), %rax
leaq (%rax,%r12), %rsi
addq $-0x200, %rsi           # imm = 0xFE00
movl $0x200, %edx            # imm = 0x200
movl %r13d, %edi
xorl %ecx, %ecx
leaq -0xe0(%rbp), %r8
movl $0x10, %r9d
callq 0x2060
cmpq $-0x1, %rax
je 0x2e03
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rdx
leaq (%rax,%r15), %rsi
cmpq %rdx, %rsi
je 0x2b1f
leaq (%rax,%r15), %rdi
addq $-0x10, %rdi
subq %rax, %rdx
subq %r15, %rdx
callq 0x2260
movq -0x48(%rbp), %rdx
movq -0x80(%rbp), %r15
addq $-0x10, %rdx
movq %rdx, -0x48(%rbp)
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rdx
leaq (%rax,%r12), %rsi
cmpq %rdx, %rsi
je 0x2b56
leaq (%rax,%r12), %rdi
addq $-0x200, %rdi           # imm = 0xFE00
subq %rax, %rdx
subq %r12, %rdx
callq 0x2260
movq -0x68(%rbp), %rdx
movq -0x78(%rbp), %r12
addq $-0x200, %rdx           # imm = 0xFE00
movq %rdx, -0x68(%rbp)
jmp 0x2c23
movslq %r15d, %rdi
shlq $0x4, %rdi
addq -0x100(%rbp), %rdi
movl $0xa, %esi
callq 0x37f8
testb %al, %al
je 0x2bbc
movl $0x28, %edx
movq 0x3436(%rip), %rdi      # 0x5fc8
leaq 0x1634(%rip), %rsi      # 0x41cd
callq 0x21f0
leaq -0x300(%rbp), %r14
movl -0x30(%rbp), %eax
cmpl $0x9, %eax
jg 0x2dd1
incl %eax
movl %eax, -0x30(%rbp)
movl %r15d, -0x34(%rbp)
jmp 0x2c23
leaq -0x300(%rbp), %r14
jmp 0x2c23
leaq -0x50(%rbp), %rdi
movq %rbx, %rdx
callq 0x3b32
movq -0x68(%rbp), %rbx
cmpq -0x60(%rbp), %rbx
je 0x2bf8
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2150
addq $0x200, %rbx            # imm = 0x200
movq %rbx, -0x68(%rbp)
jmp 0x2c07
leaq -0x70(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x39a6
shlq $0x4, %r13
addq -0x100(%rbp), %r13
xorl %edi, %edi
movq %r13, %rsi
callq 0x2190
incl -0x34(%rbp)
movl -0x2c(%rbp), %r13d
movq %r14, %rdi
callq 0x3c80
movl $0x10, -0x84(%rbp)
movl $0x200, %edx            # imm = 0x200
movl %r13d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
leaq -0xb0(%rbp), %r8
leaq -0x84(%rbp), %r9
callq 0x2130
movl -0x34(%rbp), %r14d
testl %eax, %eax
js 0x2cf3
je 0x2d4b
movzbl -0x300(%rbp), %eax
movl $0x0, -0x30(%rbp)
cmpl $0x1, %eax
je 0x2d09
testl %eax, %eax
jne 0x2d4b
movl $0x15, %edx
movq 0x3336(%rip), %rdi      # 0x5fc8
leaq 0x1582(%rip), %rsi      # 0x421b
callq 0x21f0
movzbl -0x2ff(%rbp), %esi
movq 0x331c(%rip), %rdi      # 0x5fc8
callq 0x2270
movl $0x2, %edx
movq %rax, %rdi
leaq 0x1483(%rip), %rsi      # 0x4143
callq 0x21f0
movzbl -0x2ff(%rbp), %ecx
leal 0x1(%r15), %eax
leal 0x20(%r15), %edx
testl %eax, %eax
cmovnsl %eax, %edx
andl $-0x20, %edx
negl %edx
addl %r15d, %edx
incl %edx
cmpl %ecx, %edx
jne 0x2d4b
cmpl %r15d, %r14d
cmovlel %eax, %r14d
movl %eax, %r15d
jmp 0x2d4b
callq 0x2040
cmpl $0xb, (%rax)
jne 0x2dd5
movl $0x0, (%rax)
jmp 0x2d4b
movl $0x17, %edx
movq 0x32b3(%rip), %rdi      # 0x5fc8
leaq 0x1515(%rip), %rsi      # 0x4231
callq 0x21f0
movzbl -0x2ff(%rbp), %esi
movq 0x3299(%rip), %rdi      # 0x5fc8
callq 0x2270
movl $0x2, %edx
movq %rax, %rdi
leaq 0x1400(%rip), %rsi      # 0x4143
callq 0x21f0
movl %r15d, %r14d
cmpl %r12d, %r15d
jl 0x288a
movb $0x1, %bl
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x2d6b
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq -0x70(%rbp), %r15
jmp 0x2d73
movb $0x1, %bl
testq %r15, %r15
je 0x2d87
movq -0x60(%rbp), %rsi
subq %r15, %rsi
movq %r15, %rdi
callq 0x21e0
movq -0x100(%rbp), %rdi
testq %rdi, %rdi
je 0x2da2
movq -0xf0(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x2dbd
movq -0xc0(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movl %ebx, %eax
addq $0x2d8, %rsp            # imm = 0x2D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x2d56
movq 0x3214(%rip), %rdi      # 0x5ff0
leaq 0x1413(%rip), %rsi      # 0x41f6
movl $0x24, %edx
callq 0x21f0
movq 0x31fc(%rip), %rdi      # 0x5ff0
callq 0x20d0
movl -0x2c(%rbp), %edi
callq 0x22b0
jmp 0x2e2f
movq 0x31e6(%rip), %rdi      # 0x5ff0
leaq 0x1395(%rip), %rsi      # 0x41a6
movl $0x26, %edx
callq 0x21f0
movq 0x31ce(%rip), %rdi      # 0x5ff0
callq 0x20d0
movl -0x2c(%rbp), %edi
callq 0x22b0
movl $0x1, %edi
callq 0x2210
movq %rax, %rbx
jmp 0x2e9b
jmp 0x2e6e
jmp 0x2e6e
jmp 0x2e6e
jmp 0x2e6e
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
cmpq %r13, %rdi
je 0x2e71
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x21e0
jmp 0x2e71
jmp 0x2e6e
jmp 0x2e6e
jmp 0x2e6e
jmp 0x2e6e
movq %rax, %rbx
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x2e86
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x2e9b
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq -0x100(%rbp), %rdi
testq %rdi, %rdi
je 0x2eb6
movq -0xf0(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x2ed1
movq -0xc0(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x2280