// File: /workspace/github_cpp_makefile_star2_O2/adhipradhana[P]sliding-window-protocol/src/sendfile.cpp
// Function: main
int main(int argc, char *argv[]) {
    // Initialize packet related variables
    unsigned int seq_num;
    unsigned int packet_size, data_size;
    char data[MAX_DATA_LENGTH];
    char packet[MAX_PACKET_LENGTH];

    // Initialize file related variables
    FILE *file;
    char *buffer;
    char *filename;
    unsigned int max_buffer_size, buffer_size;

    if (argc != 6) {
    	cerr << "usage: ./sendfile <filename> <window_size> <buffer_size> <destination_ip> <destination_port>" << endl;
    	exit(1);
    }

    // Get data from argument
    filename = argv[1];
    window_size = atoi(argv[2]);
    max_buffer_size = atoi(argv[3]) * (unsigned int) MAX_DATA_LENGTH;
    ip = argv[4];
    port = atoi(argv[5]);

    // Create socket
    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) {
    	cerr << "ERROR on creating socket" << endl;
    	exit(1);
    }

    // Get host name
    server.sin_family = AF_INET;
    hp = gethostbyname(ip);
    if (hp == 0) {
    	cerr << "ERROR on getting host name" << endl;
    	exit(1);
    }

    // Fill server data struct
    bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length);
    server.sin_port = htons(port);
    sock_length = sizeof(struct sockaddr_in);

    // Open file
    if (access(filename, F_OK) < 0) {
        cerr << "FILE not exist\n";
        exit(1);
    }
    file = fopen(filename, "rb");
    buffer = new char[max_buffer_size];


    // Create a receiver thread
    thread receiver_thread(get_ack);

    bool read_done = false;
    while (!read_done) {

        // Read file from buffer
        buffer_size = fread(buffer, 1, max_buffer_size, file);
        read_done = max_buffer_size > buffer_size;

        // Initialized sliding window variable
        mt.lock();
        has_ack_received = new bool[window_size];
        packet_send_time = new time_stamp[window_size];
        bool has_packet_send[window_size];
        int seq_count = buffer_size / MAX_DATA_LENGTH;
        if (buffer_size % MAX_DATA_LENGTH) {
            seq_count++;
        }

        for (int i = 0; i < window_size; i++) {
            has_packet_send[i] = false;
            has_ack_received[i] = false;
        }

        lar = -1;
        lfs = lar + window_size;
        mt.unlock();

        bool send_done = false;
        while (!send_done) {
            mt.lock();
            if (has_ack_received[0]) {
                unsigned int shift = 1;

                for (unsigned int i = 1; i < window_size; i++) {
                    if (!has_ack_received[i]) {
                        break;
                    }
                    shift++;
                }

                for (unsigned int i = 0; i < window_size - shift; i++) {
                    has_packet_send[i] = has_packet_send[i + shift];
                    has_ack_received[i] = has_ack_received[i + shift];
                    packet_send_time[i] = packet_send_time[i + shift];
                }

                for (unsigned int i = window_size - shift; i < window_size; i++) {
                    has_packet_send[i] = false;
                    has_ack_received[i] = false;
                }

                lar += shift;
                lfs = lar + window_size;
            }
            mt.unlock();

            for (unsigned int i = 0; i < window_size; i ++) {
                seq_num = lar + i + 1;

                if (seq_num < seq_count) {
                    if (!has_packet_send[i] || ((!has_ack_received[i]) && (elapsed_time(current_time(), packet_send_time[i]) > TIMEOUT))) {
                        unsigned int buffer_shift = seq_num * MAX_DATA_LENGTH;
                        data_size = (buffer_size - buffer_shift < MAX_DATA_LENGTH) ? (buffer_size - buffer_shift) : MAX_DATA_LENGTH;
                        memcpy(data, buffer + buffer_shift, data_size);

                        bool eot = (seq_num == seq_count - 1) && (read_done);
                        packet_size = create_packet(packet, seq_num, data_size, data, eot);

                        int n = sendto(sock, packet, packet_size, MSG_WAITALL, (const struct sockaddr *) &server, sock_length);
                        if (n < 0) {
                            cerr << "ERROR sending packet\n";
                            // exit(1);
                        } else {
                            has_packet_send[i] = true;
                            packet_send_time[i] = current_time();

                            if (!eot) {
                                cout << "Sending package " << seq_num << endl;
                            } else {
                                cout << "Sending eot package " << seq_num << endl;
                            }
                        }
                    }
                }
            }

            if (lar >= seq_count - 1) {
                send_done = true;
            }
        }
        cout << "is read done ? " << read_done << "\n";
        if (read_done) {
            break;
        }
    }

    fclose(file);
    delete[] packet_send_time;
    delete[] has_ack_received;
    receiver_thread.detach();

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x868, %rsp            # imm = 0x868
cmpl $0x6, %edi
jne 0x2b08
movq %rsi, %r14
movq 0x8(%rsi), %rbx
movq 0x10(%rsi), %rdi
callq 0x21d0
movl %eax, 0x2ac6(%rip)      # 0x5140
movq 0x18(%r14), %rdi
callq 0x21d0
movl %eax, %r15d
movq 0x20(%r14), %rax
movq %rax, 0x2ae7(%rip)      # 0x5178
movq 0x28(%r14), %rdi
callq 0x21d0
movl %eax, 0x2aa8(%rip)      # 0x5148
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x2060
movl %eax, 0x2a8a(%rip)      # 0x513c
testl %eax, %eax
js 0x2b18
movw $0x2, 0x2a89(%rip)      # 0x514c
movq 0x2aae(%rip), %rdi      # 0x5178
callq 0x2160
movq %rax, 0x2a9a(%rip)      # 0x5170
testq %rax, %rax
je 0x2b28
movq 0x18(%rax), %rcx
movq (%rcx), %rsi
movslq 0x14(%rax), %rdx
leaq 0x2a5f(%rip), %rdi      # 0x5150
callq 0x21e0
movzwl 0x2a4b(%rip), %eax      # 0x5148
rolw $0x8, %ax
movw %ax, 0x2a46(%rip)       # 0x514e
movl $0x10, 0x2a32(%rip)     # 0x5144
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2200
testl %eax, %eax
js 0x2b45
shll $0xa, %r15d
leaq 0x9d5(%rip), %rsi       # 0x3107
movq %rbx, %rdi
callq 0x2170
movq %rax, -0x50(%rbp)
movl %r15d, -0x2c(%rbp)
movl %r15d, %edi
movq %rdi, -0x68(%rbp)
callq 0x2030
movq %rax, -0x58(%rbp)
leaq -0x245(%rip), %rsi      # 0x2514
leaq -0x40(%rbp), %rdi
callq 0x2b94
leaq 0x2a3f(%rip), %r12      # 0x51a8
movq -0x58(%rbp), %rdi
pushq $0x1
popq %rsi
movq -0x68(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x21b0
movq %rax, -0x38(%rbp)
movq %r12, %rdi
callq 0x2b7e
movslq 0x29b0(%rip), %rbx      # 0x5140
movq %rbx, %rdi
callq 0x2030
movq %rax, 0x29e9(%rip)      # 0x5188
leaq (,%rbx,8), %r13
testl %ebx, %ebx
movq %r13, %rdi
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r15
testl %ebx, %ebx
je 0x27cc
movq %r15, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x20a0
movq %rsp, -0x70(%rbp)
movq %r15, 0x29c1(%rip)      # 0x5198
movl %ebx, %eax
movq %rsp, %rdi
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, %rsp
xorl %ebx, %ebx
testw $0x3ff, -0x38(%rbp)     # imm = 0x3FF
setne %bl
movl 0x2945(%rip), %eax      # 0x5140
movq 0x2986(%rip), %rcx      # 0x5188
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x2820
movb %r14b, (%rdi,%rsi)
movb %r14b, (%rcx,%rsi)
incq %rsi
jmp 0x280e
movq %rdi, -0x60(%rbp)
movq -0x38(%rbp), %r15
orl $-0x1, 0x2951(%rip)     # 0x5180
shrl $0xa, %r15d
leal (%r15,%rbx), %r14d
decl %eax
movl %eax, 0x2945(%rip)      # 0x5184
movq %r12, %rdi
callq 0x20d0
leal (%r15,%rbx), %eax
decl %eax
movl %eax, -0x44(%rbp)
movl %r14d, %r15d
movq -0x60(%rbp), %r14
pushq $0x2
popq %rax
subl %r15d, %eax
movq %rax, -0x78(%rbp)
movl %r15d, -0x48(%rbp)
movq %r12, %rdi
callq 0x2b7e
movq 0x2914(%rip), %rcx      # 0x5188
cmpb $0x1, (%rcx)
jne 0x28fe
movl 0x28bd(%rip), %eax      # 0x5140
cmpl $0x2, %eax
pushq $0x1
popq %rdx
cmovael %eax, %edx
pushq $0x1
popq %rsi
xorl %r11d, %r11d
cmpq %rax, %rsi
jae 0x28a4
cmpb $0x1, (%rcx,%rsi)
jne 0x28a2
incq %rsi
jmp 0x2892
movl %esi, %edx
movl %eax, %esi
subl %edx, %esi
movq 0x28e9(%rip), %rdi      # 0x5198
movl %edx, %edx
xorl %r8d, %r8d
cmpq %r8, %rsi
je 0x28e5
leal (%rdx,%r8), %r9d
movb (%r14,%r9), %r10b
movb %r10b, (%r14,%r8)
movb (%rcx,%r9), %r10b
movb %r10b, (%rcx,%r8)
movq (%rdi,%r9,8), %r9
movq %r9, (%rdi,%r8,8)
incq %r8
jmp 0x28b4
movb %r11b, (%r14,%rsi)
movb %r11b, (%rcx,%rsi)
incq %rsi
cmpq %rax, %rsi
jb 0x28da
addl 0x2890(%rip), %edx      # 0x5180
movl %edx, 0x288a(%rip)      # 0x5180
addl %eax, %edx
movl %edx, 0x2886(%rip)      # 0x5184
movq %r12, %rdi
callq 0x20d0
xorl %r13d, %r13d
movl 0x2831(%rip), %eax      # 0x5140
movl 0x286a(%rip), %r12d     # 0x5180
cmpq %rax, %r13
jae 0x2a5b
leaq (%r12,%r13), %rbx
incq %rbx
cmpl %r15d, %ebx
jae 0x2a53
cmpb $0x1, (%r14,%r13)
jne 0x2964
movq 0x284b(%rip), %rax      # 0x5188
cmpb $0x0, (%rax,%r13)
jne 0x2a53
callq 0x2040
movq 0x2844(%rip), %rcx      # 0x5198
subq (%rcx,%r13,8), %rax
cmpq $0x3baa0c40, %rax       # imm = 0x3BAA0C40
jl 0x2a53
addq %r13, %r12
addq -0x78(%rbp), %r12
movq -0x38(%rbp), %r15
cmpl %r15d, -0x2c(%rbp)
seta %r14b
movl %ebx, %esi
shll $0xa, %esi
subl %esi, %r15d
movl $0x400, %eax            # imm = 0x400
cmpl %eax, %r15d
cmovael %eax, %r15d
addq -0x58(%rbp), %rsi
leaq -0x480(%rbp), %rdi
movq %r15, %rdx
callq 0x20f0
testl %r12d, %r12d
sete %r12b
andb %r14b, %r12b
movzbl %r12b, %r8d
leaq -0x890(%rbp), %r14
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
leaq -0x480(%rbp), %rcx
callq 0x23cc
movl 0x276f(%rip), %edi      # 0x513c
movl %eax, %edx
movl 0x276e(%rip), %r9d      # 0x5144
movq %r14, %rsi
movl $0x100, %ecx            # imm = 0x100
leaq 0x2767(%rip), %r8       # 0x514c
callq 0x2050
movq -0x60(%rbp), %r14
movl -0x48(%rbp), %r15d
testl %eax, %eax
js 0x2a40
movb $0x1, (%r14,%r13)
callq 0x2040
movq 0x2791(%rip), %rcx      # 0x5198
movq %rax, (%rcx,%r13,8)
testb %r12b, %r12b
leaq 0x70b(%rip), %rsi       # 0x3120
leaq 0x715(%rip), %rax       # 0x3131
cmovneq %rax, %rsi
movq 0x25a9(%rip), %rdi      # 0x4fd0
callq 0x2120
movl %ebx, %esi
movq %rax, %rdi
callq 0x20c0
movq %rax, %rdi
callq 0x2090
jmp 0x2a53
movq 0x25a9(%rip), %rdi      # 0x4ff0
leaq 0x6bc(%rip), %rsi       # 0x310a
callq 0x2120
incq %r13
jmp 0x2909
cmpl -0x44(%rbp), %r12d
leaq 0x2742(%rip), %r12      # 0x51a8
jl 0x2865
movq 0x255d(%rip), %rdi      # 0x4fd0
leaq 0x6cc(%rip), %rsi       # 0x3146
callq 0x2120
xorl %esi, %esi
movq -0x38(%rbp), %rcx
cmpl %ecx, -0x2c(%rbp)
seta %sil
movq %rax, %rdi
callq 0x21f0
movq %rax, %rdi
leaq 0x680(%rip), %rsi       # 0x311e
callq 0x2120
movq -0x38(%rbp), %rax
cmpl %eax, -0x2c(%rbp)
movq -0x70(%rbp), %rsp
movl $0x0, %r14d
jbe 0x2769
movq -0x50(%rbp), %rdi
callq 0x2130
movq 0x26ce(%rip), %rdi      # 0x5198
testq %rdi, %rdi
je 0x2ad4
callq 0x2190
movq 0x26ad(%rip), %rdi      # 0x5188
testq %rdi, %rdi
je 0x2ae5
callq 0x2190
leaq -0x40(%rbp), %rdi
callq 0x2210
leaq -0x40(%rbp), %rdi
callq 0x2c02
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x24e1(%rip), %rdi      # 0x4ff0
leaq 0x550(%rip), %rsi       # 0x3066
jmp 0x2b36
movq 0x24d1(%rip), %rdi      # 0x4ff0
leaq 0x59d(%rip), %rsi       # 0x30c3
jmp 0x2b36
movq 0x24c1(%rip), %rdi      # 0x4ff0
leaq 0x5a6(%rip), %rsi       # 0x30dc
callq 0x2120
movq %rax, %rdi
callq 0x2090
jmp 0x2b58
movq 0x24a4(%rip), %rdi      # 0x4ff0
leaq 0x5a4(%rip), %rsi       # 0x30f7
callq 0x2120
pushq $0x1
popq %rdi
callq 0x2180
jmp 0x2b6a
jmp 0x2b6a
jmp 0x2b6a
jmp 0x2b6a
jmp 0x2b6a
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0x2c02
movq %rbx, %rdi
callq 0x2220