// File: /workspace/github_cpp_makefile_star2_O3/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 $0x878, %rsp            # imm = 0x878
cmpl $0x6, %edi
jne 0x2d1e
movq %rsi, %r14
movq 0x8(%rsi), %rbx
movq 0x10(%rsi), %rdi
callq 0x2210
movl %eax, 0x29ba(%rip)      # 0x5160
movq 0x18(%r14), %rdi
callq 0x2210
movl %eax, %r15d
movq 0x20(%r14), %rax
movq %rax, 0x29db(%rip)      # 0x5198
movq 0x28(%r14), %rdi
callq 0x2210
movl %eax, 0x299c(%rip)      # 0x5168
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x2070
movl %eax, 0x2979(%rip)      # 0x515c
testl %eax, %eax
js 0x2d2e
movw $0x2, 0x2978(%rip)      # 0x516c
movq 0x299d(%rip), %rdi      # 0x5198
callq 0x2190
movq %rax, 0x2989(%rip)      # 0x5190
testq %rax, %rax
je 0x2d3e
movq 0x18(%rax), %rcx
movq (%rcx), %rsi
movslq 0x14(%rax), %rdx
leaq 0x294e(%rip), %rdi      # 0x5170
callq 0x2220
movzwl 0x293a(%rip), %eax      # 0x5168
rolw $0x8, %ax
movw %ax, 0x2935(%rip)       # 0x516e
movl $0x10, 0x2921(%rip)     # 0x5164
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2240
testl %eax, %eax
js 0x2d5b
shll $0xa, %r15d
leaq 0x8a7(%rip), %rsi       # 0x3107
movq %rbx, %rdi
callq 0x21b0
movq %rax, -0x50(%rbp)
movl %r15d, -0x2c(%rbp)
movl %r15d, %edi
movq %rdi, -0x68(%rbp)
callq 0x2030
movq %rax, -0x58(%rbp)
leaq -0x2db(%rip), %rsi      # 0x25ac
leaq -0x40(%rbp), %rdi
callq 0x2d96
movl $0x1, %esi
movq -0x58(%rbp), %rdi
movq -0x68(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x21f0
movq %rax, -0x38(%rbp)
leaq 0x2917(%rip), %rdi      # 0x51c8
callq 0x21e0
testl %eax, %eax
jne 0x2d17
movslq 0x289b(%rip), %rbx      # 0x5160
movq %rbx, %rdi
callq 0x2030
movq %rax, 0x28d4(%rip)      # 0x51a8
leaq (,%rbx,8), %r12
testl %ebx, %ebx
movq %r12, %rdi
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r14
testl %ebx, %ebx
je 0x2905
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x20b0
movq %rsp, -0x70(%rbp)
movq %r14, 0x28a8(%rip)      # 0x51b8
movl %ebx, %eax
movq %rsp, %rbx
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
xorl %r15d, %r15d
testl $0x3ff, -0x38(%rbp)     # imm = 0x3FF
setne %r14b
movslq 0x2828(%rip), %r12      # 0x5160
testq %r12, %r12
jle 0x295e
movq 0x2864(%rip), %r13      # 0x51a8
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x20b0
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x20b0
movq -0x38(%rbp), %rax
shrl $0xa, %eax
movb %r14b, %r15b
addl %eax, %r15d
movl $0xffffffff, 0x282b(%rip) # imm = 0xFFFFFFFF
decl %r12d
movl %r12d, 0x2825(%rip)     # 0x51a4
leaq 0x2842(%rip), %rdi      # 0x51c8
callq 0x20f0
leal -0x1(%r15), %eax
movl %eax, -0x44(%rbp)
movl $0x2, %eax
subl %r15d, %eax
movq %rax, -0x88(%rbp)
movq %r15, -0x60(%rbp)
movq %rbx, -0x80(%rbp)
leaq 0x2818(%rip), %rdi      # 0x51c8
callq 0x21e0
testl %eax, %eax
jne 0x2d10
movq 0x27e4(%rip), %r12      # 0x51a8
cmpb $0x1, (%r12)
jne 0x2a83
movl 0x278a(%rip), %r15d     # 0x5160
movl $0x1, %ecx
cmpq $0x2, %r15
jb 0x29fc
movl $0x1, %ecx
cmpb $0x1, (%r12,%rcx)
jne 0x29fc
incq %rcx
cmpq %rcx, %r15
jne 0x29e6
xorl %eax, %eax
movl %r15d, %r14d
jmp 0x2a38
movl %ecx, %r14d
movl %r15d, %eax
subl %ecx, %eax
cmpl %ecx, %r15d
je 0x2a38
movq 0x27a8(%rip), %rcx      # 0x51b8
movl %eax, %edx
xorl %esi, %esi
leal (%r14,%rsi), %edi
movb (%rbx,%rdi), %r8b
movb %r8b, (%rbx,%rsi)
movb (%r12,%rdi), %r8b
movb %r8b, (%r12,%rsi)
movq (%rcx,%rdi,8), %rdi
movq %rdi, (%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %rdx
jne 0x2a14
cmpl %r15d, %eax
jae 0x2a67
movl %eax, %r13d
leaq (%rbx,%r13), %rdi
leal -0x1(%r14), %edx
incq %rdx
movq %rdx, -0x78(%rbp)
xorl %esi, %esi
callq 0x20b0
addq %r13, %r12
movq %r12, %rdi
xorl %esi, %esi
movq -0x78(%rbp), %rdx
callq 0x20b0
addl 0x2732(%rip), %r14d     # 0x51a0
movl %r14d, 0x272b(%rip)     # 0x51a0
addl %r15d, %r14d
movl %r14d, 0x2725(%rip)     # 0x51a4
movq -0x60(%rbp), %r15
leaq 0x273e(%rip), %rdi      # 0x51c8
callq 0x20f0
cmpl $0x0, 0x26ca(%rip)      # 0x5160
je 0x2c5b
xorl %r14d, %r14d
movl 0x26fa(%rip), %r13d     # 0x51a0
leaq (%r14,%r13), %r12
incq %r12
cmpl %r15d, %r12d
jae 0x2c49
cmpb $0x1, (%rbx,%r14)
jne 0x2aeb
movq 0x26e4(%rip), %rax      # 0x51a8
cmpb $0x0, (%rax,%r14)
jne 0x2c49
callq 0x2050
movq 0x26dd(%rip), %rcx      # 0x51b8
subq (%rcx,%r14,8), %rax
cmpq $0x3baa0c40, %rax       # imm = 0x3BAA0C40
jl 0x2c49
movq -0x88(%rbp), %rax
addq %r14, %rax
addq %rax, %r13
movq -0x38(%rbp), %r15
cmpl %r15d, -0x2c(%rbp)
seta %bl
movl %r12d, %esi
shll $0xa, %esi
subl %esi, %r15d
movl $0x400, %eax            # imm = 0x400
cmpl %eax, %r15d
cmovael %eax, %r15d
addq -0x58(%rbp), %rsi
leaq -0x490(%rbp), %rdi
movq %r15, %rdx
callq 0x2110
testl %r13d, %r13d
sete %r13b
andb %bl, %r13b
leaq -0x8a0(%rbp), %rbx
movzbl %r13b, %r8d
movq %rbx, %rdi
movl %r12d, %esi
movq %r15, %rdx
leaq -0x490(%rbp), %rcx
callq 0x240c
movl 0x2601(%rip), %edi      # 0x515c
movl %eax, %edx
movl 0x2600(%rip), %r9d      # 0x5164
movq %rbx, %rsi
movl $0x100, %ecx            # imm = 0x100
leaq 0x25f9(%rip), %r8       # 0x516c
callq 0x2060
movq -0x80(%rbp), %rbx
movq -0x60(%rbp), %r15
testl %eax, %eax
js 0x2bde
movb $0x1, (%rbx,%r14)
callq 0x2050
movq 0x2623(%rip), %rcx      # 0x51b8
movq %rax, (%rcx,%r14,8)
testb %r13b, %r13b
je 0x2bf8
movl $0x14, %edx
movq 0x2426(%rip), %rdi      # 0x4fd0
leaq 0x580(%rip), %rsi       # 0x3131
callq 0x21a0
movl %r12d, %esi
movq 0x2410(%rip), %rdi      # 0x4fd0
callq 0x20d0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2140
jmp 0x2c36
movl $0x15, %edx
movq 0x2406(%rip), %rdi      # 0x4ff0
leaq 0x519(%rip), %rsi       # 0x310a
callq 0x21a0
jmp 0x2c49
movl $0x10, %edx
movq 0x23cc(%rip), %rdi      # 0x4fd0
leaq 0x515(%rip), %rsi       # 0x3120
callq 0x21a0
movl %r12d, %esi
movq 0x23b6(%rip), %rdi      # 0x4fd0
callq 0x20d0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20e0
incq %r14
movl 0x250e(%rip), %eax      # 0x5160
cmpq %rax, %r14
jb 0x2a9f
movl -0x44(%rbp), %eax
cmpl %eax, 0x253c(%rip)      # 0x51a0
jl 0x29a9
movl $0xf, %edx
movq 0x235a(%rip), %rdi      # 0x4fd0
leaq 0x4c9(%rip), %rsi       # 0x3146
callq 0x21a0
xorl %esi, %esi
movq -0x38(%rbp), %rax
cmpl %eax, -0x2c(%rbp)
seta %sil
movq 0x233a(%rip), %rdi      # 0x4fd0
callq 0x2230
movl $0x1, %edx
movq %rax, %rdi
leaq 0x474(%rip), %rsi       # 0x311e
callq 0x21a0
movq -0x38(%rbp), %rax
cmpl %eax, -0x2c(%rbp)
movq -0x70(%rbp), %rsp
jbe 0x2890
movq -0x50(%rbp), %rdi
callq 0x2160
movq 0x24e8(%rip), %rdi      # 0x51b8
testq %rdi, %rdi
je 0x2cda
callq 0x21d0
movq 0x24c7(%rip), %rdi      # 0x51a8
testq %rdi, %rdi
je 0x2ceb
callq 0x21d0
leaq -0x40(%rbp), %rdi
callq 0x2250
cmpq $0x0, -0x40(%rbp)
jne 0x2d91
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x20c0
movl %eax, %edi
callq 0x20c0
movq 0x22cb(%rip), %rdi      # 0x4ff0
leaq 0x33a(%rip), %rsi       # 0x3066
jmp 0x2d4c
movq 0x22bb(%rip), %rdi      # 0x4ff0
leaq 0x387(%rip), %rsi       # 0x30c3
jmp 0x2d4c
movq 0x22ab(%rip), %rdi      # 0x4ff0
leaq 0x390(%rip), %rsi       # 0x30dc
callq 0x2150
movq %rax, %rdi
callq 0x20a0
jmp 0x2d6e
movq 0x228e(%rip), %rdi      # 0x4ff0
leaq 0x38e(%rip), %rsi       # 0x30f7
callq 0x2150
movl $0x1, %edi
callq 0x21c0
jmp 0x2d82
jmp 0x2d82
jmp 0x2d82
jmp 0x2d82
jmp 0x2d82
cmpq $0x0, -0x40(%rbp)
jne 0x2d91
movq %rax, %rdi
callq 0x2260
callq 0x2080