// File: /workspace/github_cpp_makefile_star2_O1/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 0x2d39
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 0x2d49
movw $0x2, 0x2978(%rip)      # 0x516c
movq 0x299d(%rip), %rdi      # 0x5198
callq 0x2190
movq %rax, 0x2989(%rip)      # 0x5190
testq %rax, %rax
je 0x2d59
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 0x2d76
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 0x2db2
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 0x2d32
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)
xorl %r14d, %r14d
movq %r15, -0x60(%rbp)
movq %rbx, -0x80(%rbp)
leaq 0x2815(%rip), %rdi      # 0x51c8
callq 0x21e0
testl %eax, %eax
jne 0x2d2b
movl %r14d, -0x48(%rbp)
movq 0x27dd(%rip), %r12      # 0x51a8
cmpb $0x1, (%r12)
jne 0x2a89
movl 0x2783(%rip), %r15d     # 0x5160
movl $0x1, %r14d
cmpq $0x2, %r15
jb 0x2a01
movl $0x1, %r14d
cmpb $0x1, (%r12,%r14)
jne 0x2a01
incq %r14
cmpq %r14, %r15
jne 0x29ef
movl %r15d, %r14d
movl %r15d, %eax
subl %r14d, %eax
cmpl %r14d, %r15d
je 0x2a3e
movq 0x27a5(%rip), %rcx      # 0x51b8
movl %eax, %edx
movl %r14d, %esi
xorl %edi, %edi
leal (%rsi,%rdi), %r8d
movb (%rbx,%r8), %r9b
movb %r9b, (%rbx,%rdi)
movb (%r12,%r8), %r9b
movb %r9b, (%r12,%rdi)
movq (%rcx,%r8,8), %r8
movq %r8, (%rcx,%rdi,8)
incq %rdi
cmpq %rdi, %rdx
jne 0x2a1a
cmpl %r15d, %eax
jae 0x2a6d
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 0x272c(%rip), %r14d     # 0x51a0
movl %r14d, 0x2725(%rip)     # 0x51a0
addl %r15d, %r14d
movl %r14d, 0x271f(%rip)     # 0x51a4
movq -0x60(%rbp), %r15
leaq 0x2738(%rip), %rdi      # 0x51c8
callq 0x20f0
cmpl $0x0, 0x26c4(%rip)      # 0x5160
je 0x2c61
xorl %r14d, %r14d
movl 0x26f4(%rip), %r13d     # 0x51a0
leaq (%r14,%r13), %r12
incq %r12
cmpl %r15d, %r12d
jae 0x2c4f
cmpb $0x1, (%rbx,%r14)
jne 0x2af1
movq 0x26de(%rip), %rax      # 0x51a8
cmpb $0x0, (%rax,%r14)
jne 0x2c4f
callq 0x2050
movq 0x26d7(%rip), %rcx      # 0x51b8
subq (%rcx,%r14,8), %rax
cmpq $0x3baa0c40, %rax       # imm = 0x3BAA0C40
jl 0x2c4f
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 0x25fb(%rip), %edi      # 0x515c
movl %eax, %edx
movl 0x25fa(%rip), %r9d      # 0x5164
movq %rbx, %rsi
movl $0x100, %ecx            # imm = 0x100
leaq 0x25f3(%rip), %r8       # 0x516c
callq 0x2060
movq -0x80(%rbp), %rbx
movq -0x60(%rbp), %r15
testl %eax, %eax
js 0x2be4
movb $0x1, (%rbx,%r14)
callq 0x2050
movq 0x261d(%rip), %rcx      # 0x51b8
movq %rax, (%rcx,%r14,8)
testb %r13b, %r13b
je 0x2bfe
movl $0x14, %edx
movq 0x2420(%rip), %rdi      # 0x4fd0
leaq 0x57a(%rip), %rsi       # 0x3131
callq 0x21a0
movl %r12d, %esi
movq 0x240a(%rip), %rdi      # 0x4fd0
callq 0x20d0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2140
jmp 0x2c3c
movl $0x15, %edx
movq 0x2400(%rip), %rdi      # 0x4ff0
leaq 0x513(%rip), %rsi       # 0x310a
callq 0x21a0
jmp 0x2c4f
movl $0x10, %edx
movq 0x23c6(%rip), %rdi      # 0x4fd0
leaq 0x50f(%rip), %rsi       # 0x3120
callq 0x21a0
movl %r12d, %esi
movq 0x23b0(%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 0x2508(%rip), %eax      # 0x5160
cmpq %rax, %r14
jb 0x2aa5
movl -0x44(%rbp), %eax
cmpl %eax, 0x2536(%rip)      # 0x51a0
movl -0x48(%rbp), %r14d
movzbl %r14b, %r14d
movl $0x1, %eax
cmovgel %eax, %r14d
testb $0x1, %r14b
je 0x29ac
movl $0xf, %edx
movq 0x233f(%rip), %rdi      # 0x4fd0
leaq 0x4ae(%rip), %rsi       # 0x3146
callq 0x21a0
xorl %esi, %esi
movq -0x38(%rbp), %rax
cmpl %eax, -0x2c(%rbp)
seta %sil
movq 0x231f(%rip), %rdi      # 0x4fd0
callq 0x2230
movl $0x1, %edx
movq %rax, %rdi
leaq 0x459(%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 0x24cd(%rip), %rdi      # 0x51b8
testq %rdi, %rdi
je 0x2cf5
callq 0x21d0
movq 0x24ac(%rip), %rdi      # 0x51a8
testq %rdi, %rdi
je 0x2d06
callq 0x21d0
leaq -0x40(%rbp), %rdi
callq 0x2250
cmpq $0x0, -0x40(%rbp)
jne 0x2dac
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 0x22b0(%rip), %rdi      # 0x4ff0
leaq 0x31f(%rip), %rsi       # 0x3066
jmp 0x2d67
movq 0x22a0(%rip), %rdi      # 0x4ff0
leaq 0x36c(%rip), %rsi       # 0x30c3
jmp 0x2d67
movq 0x2290(%rip), %rdi      # 0x4ff0
leaq 0x375(%rip), %rsi       # 0x30dc
callq 0x2150
movq %rax, %rdi
callq 0x20a0
jmp 0x2d89
movq 0x2273(%rip), %rdi      # 0x4ff0
leaq 0x373(%rip), %rsi       # 0x30f7
callq 0x2150
movl $0x1, %edi
callq 0x21c0
jmp 0x2d9d
jmp 0x2d9d
jmp 0x2d9d
jmp 0x2d9d
jmp 0x2d9d
cmpq $0x0, -0x40(%rbp)
jne 0x2dac
movq %rax, %rdi
callq 0x2260
callq 0x2080
nop