// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
movl %esi, %r15d
movq %rdx, -0xa0(%rbp)
movq %rcx, -0x98(%rbp)
xorps %xmm0, %xmm0
leaq -0xc0(%rbp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq -0xe0(%rbp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
callq 0x2d68
movq -0xb8(%rbp), %rax
subq -0xc0(%rbp), %rax
shrq $0x9, %rax
movq %rax, -0x68(%rbp)
leaq -0x90(%rbp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq -0x60(%rbp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq $0x0, -0x40(%rbp)
leaq -0x2f0(%rbp), %r14
xorl %r13d, %r13d
xorl %ebx, %ebx
movl %r15d, -0x2c(%rbp)
cmpl -0x68(%rbp), %ebx
jge 0x2b84
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
movl %r13d, -0x30(%rbp)
je 0x297b
movq %rbx, -0x38(%rbp)
pushq $-0x1
popq %rbx
xorl %r13d, %r13d
xorl %r15d, %r15d
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rax
subq %rdi, %rax
sarq $0x4, %rax
incq %rbx
cmpq %rbx, %rax
jbe 0x2a2a
addq $0x200, %r15            # imm = 0x200
addq %r13, %rdi
addq $0x10, %r13
movl 0xc(%r12), %esi
callq 0x337a
testb %al, %al
je 0x27ef
movq 0x379b(%rip), %rbx      # 0x5fc8
movq %rbx, %rdi
leaq 0x1955(%rip), %rsi      # 0x418c
callq 0x21b0
movq -0x90(%rbp), %rcx
movzbl -0x1ff(%rcx,%r15), %esi
movq %rax, %rdi
callq 0x2270
movq %rax, %rdi
leaq 0x18e6(%rip), %rsi      # 0x4144
callq 0x21b0
movq %rbx, %rdi
leaq 0x1932(%rip), %rsi      # 0x419f
callq 0x21b0
movq -0x90(%rbp), %rax
leaq (%rax,%r15), %rsi
addq $-0x200, %rsi           # imm = 0xFE00
leaq -0x2f0(%rbp), %rbx
movq %rbx, %rdi
pushq $0x30
popq %rdx
callq 0x24a0
movq 0x372b(%rip), %r14      # 0x5fc8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x20e0
movq %r14, %rdi
leaq 0x1889(%rip), %rsi      # 0x4143
callq 0x21b0
movq -0x90(%rbp), %rax
leaq (%rax,%r15), %rsi
addq $-0x200, %rsi           # imm = 0xFE00
movl $0x200, %edx            # imm = 0x200
movl -0x2c(%rbp), %edi
xorl %ecx, %ecx
leaq -0xa0(%rbp), %r8
pushq $0x10
popq %r9
callq 0x2050
cmpq $-0x1, %rax
je 0x2bf0
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rdx
leaq (%rax,%r13), %rsi
cmpq %rdx, %rsi
je 0x291d
leaq (%rax,%r13), %rdi
addq $-0x10, %rdi
subq %rax, %rdx
subq %r13, %rdx
callq 0x2260
movq -0x58(%rbp), %rdx
movq -0x38(%rbp), %rbx
addq $-0x10, %rdx
movq %rdx, -0x58(%rbp)
movq -0x90(%rbp), %rax
movq -0x88(%rbp), %rdx
leaq (%rax,%r15), %rsi
cmpq %rdx, %rsi
leaq -0x2f0(%rbp), %r14
je 0x2964
leaq (%rax,%r15), %rdi
addq $-0x200, %rdi           # imm = 0xFE00
subq %rax, %rdx
subq %r15, %rdx
callq 0x2260
movq -0x88(%rbp), %rdx
movl -0x2c(%rbp), %r15d
addq $-0x200, %rdx           # imm = 0xFE00
movq %rdx, -0x88(%rbp)
jmp 0x2a39
leal 0x10(%rbx), %eax
cmpl %eax, %r13d
setge %al
cmpl -0x68(%rbp), %r13d
setge %cl
orb %al, %cl
jne 0x2b02
movq %rbx, -0x38(%rbp)
movl %r15d, %ebx
movslq %r13d, %r15
movq %r15, %rsi
shlq $0x9, %rsi
addq -0xc0(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
callq 0x2160
movq -0xa0(%rbp), %rsi
movq -0x98(%rbp), %rdx
movl %ebx, %r13d
movl %ebx, %edi
movq %r14, %rcx
movq %r12, %r8
callq 0x2f13
cmpl $0x2, %eax
jne 0x2a0f
xorps %xmm0, %xmm0
movaps %xmm0, -0xf0(%rbp)
leaq -0xf0(%rbp), %rbx
movq %rbx, %rdi
callq 0x33ca
leaq -0x60(%rbp), %rdi
movq %rbx, %rsi
callq 0x3356
leaq -0x90(%rbp), %rdi
movq %r14, %rsi
callq 0x3310
shlq $0x4, %r15
addq -0xe0(%rbp), %r15
movq %r15, %rdi
callq 0x33ca
incl -0x30(%rbp)
movl %r13d, %r15d
jmp 0x2a35
movl -0x2c(%rbp), %r15d
leaq -0x2f0(%rbp), %r14
movq -0x38(%rbp), %rbx
movq %r14, %rdi
callq 0x37f4
movl $0x10, -0x6c(%rbp)
movl $0x200, %edx            # imm = 0x200
movl %r15d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
leaq -0xf0(%rbp), %r8
leaq -0x6c(%rbp), %r9
callq 0x2140
movl -0x30(%rbp), %r13d
testl %eax, %eax
js 0x2aec
je 0x27c7
movzbl -0x2f0(%rbp), %eax
movq $0x0, -0x40(%rbp)
cmpl $0x1, %eax
je 0x2b4b
testl %eax, %eax
jne 0x27c7
movq 0x352e(%rip), %rdi      # 0x5fc8
leaq 0x177a(%rip), %rsi      # 0x421b
callq 0x21b0
movzbl -0x2ef(%rbp), %esi
movq %rax, %rdi
callq 0x2270
movq %rax, %rdi
leaq 0x1684(%rip), %rsi      # 0x4143
callq 0x21b0
movzbl -0x2ef(%rbp), %esi
leal 0x1(%rbx), %ecx
movl %ecx, %eax
cltd
pushq $0x20
popq %rdi
idivl %edi
cmpl %esi, %edx
jne 0x27c7
cmpl %ebx, %r13d
cmovlel %ecx, %r13d
movl %ecx, %ebx
jmp 0x27c7
callq 0x2030
cmpl $0xb, (%rax)
jne 0x2bcb
andl $0x0, (%rax)
jmp 0x27c7
movslq %ebx, %rdi
shlq $0x4, %rdi
addq -0xe0(%rbp), %rdi
pushq $0xa
popq %rsi
callq 0x337a
testb %al, %al
je 0x2a39
movq 0x34a1(%rip), %rdi      # 0x5fc8
leaq 0x169f(%rip), %rsi      # 0x41cd
callq 0x21b0
cmpl $0x9, -0x40(%rbp)
jg 0x2b84
movq -0x40(%rbp), %rax
incl %eax
movq %rax, -0x40(%rbp)
movl %ebx, -0x30(%rbp)
jmp 0x2a39
movq 0x3476(%rip), %rdi      # 0x5fc8
leaq 0x16d8(%rip), %rsi      # 0x4231
callq 0x21b0
movzbl -0x2ef(%rbp), %esi
movq %rax, %rdi
callq 0x2270
movq %rax, %rdi
leaq 0x15cc(%rip), %rsi      # 0x4143
callq 0x21b0
movl %ebx, %r13d
jmp 0x27c7
cmpl -0x68(%rbp), %ebx
setge %bl
leaq -0x60(%rbp), %rdi
callq 0x34c4
leaq -0x90(%rbp), %rdi
callq 0x3486
leaq -0xe0(%rbp), %rdi
callq 0x34c4
leaq -0xc0(%rbp), %rdi
callq 0x3486
movl %ebx, %eax
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x341e(%rip), %rdi      # 0x5ff0
leaq 0x161d(%rip), %rsi      # 0x41f6
callq 0x21b0
movq %rax, %rdi
callq 0x20f0
movl -0x2c(%rbp), %edi
callq 0x22b0
jmp 0x2c13
movq 0x33f9(%rip), %rdi      # 0x5ff0
leaq 0x15a8(%rip), %rsi      # 0x41a6
callq 0x21b0
movq %rax, %rdi
callq 0x20f0
movl -0x2c(%rbp), %edi
callq 0x22b0
pushq $0x1
popq %rdi
callq 0x2200
movq %rax, %rbx
jmp 0x2c59
jmp 0x2c41
jmp 0x2c41
jmp 0x2c41
jmp 0x2c41
movq %rax, %rbx
leaq -0x2f0(%rbp), %rdi
callq 0x20e0
jmp 0x2c44
jmp 0x2c41
jmp 0x2c41
jmp 0x2c41
jmp 0x2c41
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x34c4
leaq -0x90(%rbp), %rdi
callq 0x3486
leaq -0xe0(%rbp), %rdi
callq 0x34c4
leaq -0xc0(%rbp), %rdi
callq 0x3486
movq %rbx, %rdi
callq 0x2280