// File: /workspace/github_cpp_makefile_star2_O1/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 $0x2c8, %rsp            # imm = 0x2C8
movq %r8, -0xa8(%rbp)
movl %esi, %ebx
movq %rdx, -0xd0(%rbp)
movq %rcx, -0xc8(%rbp)
xorl %eax, %eax
leaq -0xc0(%rbp), %rsi
movq %rax, 0x10(%rsi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsi)
leaq -0xf0(%rbp), %rdx
movq %rax, 0x10(%rdx)
movaps %xmm0, (%rdx)
callq 0x306f
movq -0xb8(%rbp), %r15
subq -0xc0(%rbp), %r15
shrq $0x9, %r15
xorl %eax, %eax
movq %rax, -0x60(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
xorl %r14d, %r14d
movq %rax, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
testl %r15d, %r15d
setle %r12b
jle 0x2d9c
leaq -0x2f0(%rbp), %r13
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl %ebx, -0x2c(%rbp)
movq %r15, -0xa0(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x2934
movq -0x50(%rbp), %rax
cmpq %rax, -0x48(%rbp)
je 0x2c59
movl $0x200, %r15d           # imm = 0x200
movl $0x10, %ebx
xorl %r12d, %r12d
leaq (%rax,%rbx), %rdi
addq $-0x10, %rdi
movq -0xa8(%rbp), %rax
movl 0xc(%rax), %esi
callq 0x3834
testb %al, %al
jne 0x29d1
incq %r12
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
addq $0x200, %r15            # imm = 0x200
addq $0x10, %rbx
cmpq %r12, %rcx
ja 0x28e4
movl -0x2c(%rbp), %ebx
movq -0xa0(%rbp), %r15
jmp 0x2c59
leal 0x10(%r14), %eax
movl -0x30(%rbp), %esi
cmpl %eax, %esi
setge %al
cmpl %r15d, %esi
setge %cl
orb %al, %cl
jne 0x2b9c
movslq %esi, %r12
movq %r12, %rsi
shlq $0x9, %rsi
addq -0xc0(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
callq 0x2150
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
movl %ebx, %edi
movq %r13, %rcx
movq -0xa8(%rbp), %r8
callq 0x3304
cmpl $0x2, %eax
jne 0x2c41
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
xorl %edi, %edi
leaq -0x90(%rbp), %r13
movq %r13, %rsi
callq 0x2190
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x2bed
movaps -0x90(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x48(%rbp)
jmp 0x2bf9
movl $0x12, %edx
movq 0x35eb(%rip), %rdi      # 0x5fc8
leaq 0x17a8(%rip), %rsi      # 0x418c
callq 0x21f0
movq -0x70(%rbp), %rax
movzbl -0x1ff(%rax,%r15), %esi
movq 0x35cb(%rip), %rdi      # 0x5fc8
callq 0x2270
movl $0x1, %edx
movq %rax, %rdi
leaq 0x1733(%rip), %rsi      # 0x4144
callq 0x21f0
movl $0x6, %edx
movq 0x35a6(%rip), %rdi      # 0x5fc8
leaq 0x1776(%rip), %rsi      # 0x419f
callq 0x21f0
movq -0x70(%rbp), %rax
leaq (%rax,%r15), %r12
addq $-0x1fa, %r12           # imm = 0xFE06
movzwl -0x2(%r12), %eax
cmpw $0x30, %ax
movl $0x30, %ecx
cmovael %ecx, %eax
movzwl %ax, %r13d
movl $0x200, %edx            # imm = 0x200
leaq -0x2f0(%rbp), %rdi
xorl %esi, %esi
callq 0x20f0
leaq -0x2f0(%rbp), %rdi
movq %r12, %rsi
movq %r13, %rdx
leaq -0x2f0(%rbp), %r13
callq 0x2150
leaq -0x80(%rbp), %r12
movq %r12, -0x90(%rbp)
movq %r13, %rdi
callq 0x20c0
leaq (%rax,%rbp), %rdx
addq $-0x2f0, %rdx           # imm = 0xFD10
leaq -0x90(%rbp), %rdi
movq %r13, %rsi
callq 0x3940
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
movq 0x3507(%rip), %rdi      # 0x5fc8
callq 0x21f0
movq -0x90(%rbp), %rdi
cmpq %r12, %rdi
je 0x2ade
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x21e0
movl $0x2, %edx
movq 0x34de(%rip), %rdi      # 0x5fc8
leaq 0x1652(%rip), %rsi      # 0x4143
callq 0x21f0
movq -0x70(%rbp), %rax
leaq (%rax,%r15), %rsi
addq $-0x200, %rsi           # imm = 0xFE00
movl $0x200, %edx            # imm = 0x200
movl -0x2c(%rbp), %edi
xorl %ecx, %ecx
leaq -0xd0(%rbp), %r8
movl $0x10, %r9d
callq 0x2060
cmpq $-0x1, %rax
je 0x2e43
movq %r14, %r12
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rdx
leaq (%rax,%rbx), %rsi
cmpq %rdx, %rsi
leaq -0x90(%rbp), %r14
je 0x2b59
leaq (%rax,%rbx), %rdi
addq $-0x10, %rdi
subq %rax, %rdx
subq %rbx, %rdx
callq 0x2260
addq $-0x10, -0x48(%rbp)
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rdx
leaq (%rax,%r15), %rsi
cmpq %rdx, %rsi
je 0x2b85
leaq (%rax,%r15), %rdi
addq $-0x200, %rdi           # imm = 0xFE00
subq %rax, %rdx
subq %r15, %rdx
callq 0x2260
addq $-0x200, -0x68(%rbp)    # imm = 0xFE00
movl -0x2c(%rbp), %ebx
movq -0xa0(%rbp), %r15
jmp 0x2c63
movq %r14, %rdx
movslq %edx, %rdi
shlq $0x4, %rdi
addq -0xf0(%rbp), %rdi
movl $0xa, %esi
callq 0x3834
testb %al, %al
je 0x2c59
movl $0x28, %edx
movq 0x33fd(%rip), %rdi      # 0x5fc8
leaq 0x15fb(%rip), %rsi      # 0x41cd
callq 0x21f0
cmpl $0x9, -0x34(%rbp)
jg 0x2d9c
incl -0x34(%rbp)
movq %r14, %r12
movl %r12d, -0x30(%rbp)
jmp 0x2c5c
leaq -0x50(%rbp), %rdi
movq %r13, %rdx
callq 0x3b66
movq -0x68(%rbp), %r13
cmpq -0x60(%rbp), %r13
je 0x2c2b
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
leaq -0x2f0(%rbp), %rsi
callq 0x2150
addq $0x200, %r13            # imm = 0x200
movq %r13, -0x68(%rbp)
leaq -0x2f0(%rbp), %r13
jmp 0x2c41
leaq -0x70(%rbp), %rdi
movq %r13, %rsi
leaq -0x2f0(%rbp), %r13
movq %r13, %rdx
callq 0x39da
shlq $0x4, %r12
addq -0xf0(%rbp), %r12
xorl %edi, %edi
movq %r12, %rsi
callq 0x2190
incl -0x30(%rbp)
movq %r14, %r12
leaq -0x90(%rbp), %r14
movq %r13, %rdi
callq 0x3cb4
movl $0x10, -0x94(%rbp)
movl $0x200, %edx            # imm = 0x200
movl %ebx, %edi
movq %r13, %rsi
xorl %ecx, %ecx
movq %r14, %r8
leaq -0x94(%rbp), %r9
callq 0x2130
movl -0x30(%rbp), %r14d
testl %eax, %eax
js 0x2d30
je 0x2d88
movzbl -0x2f0(%rbp), %eax
movl $0x0, -0x34(%rbp)
cmpl $0x1, %eax
je 0x2d46
testl %eax, %eax
jne 0x2d88
movl $0x15, %edx
movq 0x32fb(%rip), %rdi      # 0x5fc8
leaq 0x1547(%rip), %rsi      # 0x421b
callq 0x21f0
movzbl -0x2ef(%rbp), %esi
movq 0x32e1(%rip), %rdi      # 0x5fc8
callq 0x2270
movl $0x2, %edx
movq %rax, %rdi
leaq 0x1448(%rip), %rsi      # 0x4143
callq 0x21f0
movzbl -0x2ef(%rbp), %ecx
leal 0x1(%r12), %eax
leal 0x20(%r12), %edx
testl %eax, %eax
cmovnsl %eax, %edx
andl $-0x20, %edx
negl %edx
addl %r12d, %edx
incl %edx
cmpl %ecx, %edx
jne 0x2d88
cmpl %r12d, %r14d
cmovlel %eax, %r14d
movl %eax, %r12d
jmp 0x2d88
callq 0x2040
cmpl $0xb, (%rax)
jne 0x2e15
movl $0x0, (%rax)
jmp 0x2d88
movl $0x17, %edx
movq 0x3276(%rip), %rdi      # 0x5fc8
leaq 0x14d8(%rip), %rsi      # 0x4231
callq 0x21f0
movzbl -0x2ef(%rbp), %esi
movq 0x325c(%rip), %rdi      # 0x5fc8
callq 0x2270
movl $0x2, %edx
movq %rax, %rdi
leaq 0x13c3(%rip), %rsi      # 0x4143
callq 0x21f0
movl %r12d, %r14d
movl %r14d, -0x30(%rbp)
movq %r12, %r14
cmpl %r15d, %r12d
setge %r12b
jl 0x28be
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x2db1
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x2dc6
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq -0xf0(%rbp), %rdi
testq %rdi, %rdi
je 0x2de1
movq -0xe0(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x2dfc
movq -0xb0(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
andb $0x1, %r12b
movl %r12d, %eax
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x31d4(%rip), %rdi      # 0x5ff0
leaq 0x13d3(%rip), %rsi      # 0x41f6
movl $0x24, %edx
callq 0x21f0
movq 0x31bc(%rip), %rdi      # 0x5ff0
callq 0x20d0
movl -0x2c(%rbp), %edi
callq 0x22b0
jmp 0x2e6f
movq 0x31a6(%rip), %rdi      # 0x5ff0
leaq 0x1355(%rip), %rsi      # 0x41a6
movl $0x26, %edx
callq 0x21f0
movq 0x318e(%rip), %rdi      # 0x5ff0
callq 0x20d0
movl -0x2c(%rbp), %edi
callq 0x22b0
movl $0x1, %edi
callq 0x2210
movq %rax, %rbx
jmp 0x2ed8
jmp 0x2eab
jmp 0x2eab
jmp 0x2eab
jmp 0x2eab
movq %rax, %rbx
movq -0x90(%rbp), %rdi
cmpq %r12, %rdi
je 0x2eae
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x21e0
jmp 0x2eae
jmp 0x2eab
jmp 0x2eab
jmp 0x2eab
jmp 0x2eab
movq %rax, %rbx
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x2ec3
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x2ed8
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq -0xf0(%rbp), %rdi
testq %rdi, %rdi
je 0x2ef3
movq -0xe0(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x2f0e
movq -0xb0(%rbp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x2280