// File: /workspace/github_cpp_makefile_star2_O1/adhipradhana[P]sliding-window-protocol/src/recvfile.cpp
// Function: main
int main(int argc, char *argv[]) {
    // Initialize socket variables
	int sock, socket_length, window_size;
    unsigned int port;
    socklen_t fromlen;
    struct sockaddr_in server;
    struct sockaddr_in from;

    // Initialize sliding window variables
    int laf, lfr;
    int buffer_size, max_buffer_size;
    char *buffer;
    char *fname;
    FILE *file;
    bool eot;

    // Initialize packet variables
    unsigned int seq_num;
    bool is_check_sum_valid;
    size_t data_length;
    char ack[ACK_LENGTH];
    char packet[MAX_PACKET_LENGTH];
    char data[MAX_DATA_LENGTH];

    // Read data
    if (argc != 5) {
    	cerr << "usage: ./recvfile <filename> <window_size> <buffer_size> <port>" << endl;
    	exit(1);
    }
    fname = argv[1];
    window_size = atoi(argv[2]);
    max_buffer_size = (unsigned int)1024 * atoi(argv[3]);
    port = atoi(argv[4]);

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

    // Assign server port and address
    socket_length = sizeof(server);
    bzero(&server, socket_length);

    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(port);

    // Bind socket
    if (bind(sock, (struct sockaddr *) &server, socket_length) < 0) {
        cerr << "Can't bind to address" << endl;
        exit(1);
    }

    // Write to file
    file = fopen(fname, "wb");

    bool recv_done = false;
    while (!recv_done) {
        buffer = new char[max_buffer_size];
        buffer_size = max_buffer_size;

        int seq_count = max_buffer_size / MAX_DATA_LENGTH;
        bool packet_received[window_size];
        for (int i = 0; i < window_size; i++) {
            packet_received[i] = false;
        }

        lfr = -1;
        laf = lfr + window_size;

        fromlen = sizeof(struct sockaddr_in);
        while (true) {
        	// Block receiving message
            int packet_size = recvfrom(sock, packet, MAX_PACKET_LENGTH, MSG_WAITALL, (struct sockaddr *)&from, &fromlen);
            if (packet_size < 0) {
                cout << "Error on receiving message\n";
                // exit(1);
            }

            // Get packet
            read_packet(packet, &seq_num, &data_length, data, &is_check_sum_valid, &eot);

            // Create ack
            create_ack(ack, seq_num, is_check_sum_valid);

            // Send ack
            int ack_size = sendto(sock, ack, ACK_LENGTH, MSG_WAITALL, (struct sockaddr *)&from, fromlen);
            if (ack_size < 0) {
                cout << "Fail sending ack\n";
            }

            if (seq_num <= laf) {
                
                if (is_check_sum_valid) {
                    int buffer_shift = seq_num * MAX_DATA_LENGTH;

                    if (seq_num == lfr + 1) {
                        memcpy(buffer + buffer_shift, data, data_length);
                        unsigned int shift = 1;
                        for (unsigned int i = 1; i < window_size; i++) {
                            if (!packet_received[i]) {
                                break;
                            }
                            shift++;
                        }
                        cout << "shift packet_received\n";
                        for (unsigned int i = 0; i < window_size - shift; i++) {
                            packet_received[i] = packet_received[i + shift];
                        }
                        cout << "false-in packet_received\n";
                        for (unsigned int i = window_size - shift; i < window_size; i++) {
                            packet_received[i] = false;
                        }
                        lfr += shift;
                        laf = lfr + window_size;
                    } else if (seq_num > lfr + 1) {
                        if (!packet_received[seq_num - (lfr + 1)]) {
                            memcpy(buffer + buffer_shift, data, data_length);
                            packet_received[seq_num - (lfr + 1)] = true;

                        }
                    }

                    if (eot) {
                        buffer_size = buffer_shift + data_length;
                        seq_count = seq_num + 1;
                        recv_done = true;
                        cout << "Receive packet eot " << seq_num << endl;
                        cout << "Sending ack eot " << seq_num << endl;
                    } else {
                        cout << "Receive packet  " << seq_num << endl;
                        cout << "Sending ack  " << seq_num << endl;
                    }
                } else {
                    cout << "ERROR packet " << seq_num << endl;
                    cout << "Sending NAK : " << seq_num << endl;
                }
            } else {
                // Send negative ack
                cout << "lfr : " << lfr << " laf : " << laf << "\n";
                cout << "SeqNum out of range : " << seq_num << endl;
            }

            if (lfr >= seq_count - 1) {
                break;
            }
        }

        fwrite(buffer, 1, buffer_size, file);
    }

    fclose(file);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8b8, %rsp            # imm = 0x8B8
cmpl $0x5, %edi
jne 0x1a71
movq %rsi, %r14
movq 0x8(%rsi), %rbx
movq 0x10(%rsi), %rdi
callq 0x1160
movl %eax, %r15d
movq 0x18(%r14), %rdi
callq 0x1160
movl %eax, %r13d
movq 0x20(%r14), %rdi
callq 0x1160
movl %eax, %r12d
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1070
testl %eax, %eax
js 0x1a81
xorps %xmm0, %xmm0
leaq -0xd0(%rbp), %rsi
movaps %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
rolw $0x8, %r12w
movw %r12w, 0x2(%rsi)
movl %eax, -0x44(%rbp)
movl %eax, %edi
movl $0x10, %edx
callq 0x1060
testl %eax, %eax
js 0x1a91
movl %r15d, %r14d
shll $0xa, %r13d
leaq 0xafb(%rip), %rsi       # 0x2070
movq %rbx, %rdi
callq 0x1130
movq %rax, -0x80(%rbp)
movl %r13d, -0x74(%rbp)
movslq %r13d, %rax
movq %rax, -0xa0(%rbp)
sarl $0xa, %eax
movl %eax, -0x70(%rbp)
leal -0x1(%r14), %eax
movl %eax, -0x6c(%rbp)
movq $0x0, -0x88(%rbp)
leaq 0xf(%r14), %rax
andq $-0x10, %rax
movq %rax, -0x98(%rbp)
leaq -0x4e0(%rbp), %rbx
movl -0x44(%rbp), %r13d
movq %r14, -0x58(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x1030
movq %rax, -0x60(%rbp)
movq %rsp, -0xa8(%rbp)
movq %rsp, %r12
subq -0x98(%rbp), %r12
movq %r12, %rsp
testl %r14d, %r14d
jle 0x15fb
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x1090
movl $0x10, -0x50(%rbp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl -0x70(%rbp), %eax
movq %rax, -0x90(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq %r15, -0x40(%rbp)
movl $0x40a, %edx            # imm = 0x40A
movl %r13d, %edi
movq %rbx, %rsi
movl $0x100, %ecx            # imm = 0x100
leaq -0xc0(%rbp), %r8
leaq -0x50(%rbp), %r9
callq 0x10c0
testl %eax, %eax
movq 0x2985(%rip), %r14      # 0x3fd0
jns 0x1661
movl $0x1b, %edx
movq %r14, %rdi
leaq 0xa17(%rip), %rsi       # 0x2073
callq 0x1120
movq %rbx, %rdi
leaq -0x30(%rbp), %rsi
leaq -0x68(%rbp), %rdx
leaq -0x8e0(%rbp), %rcx
leaq -0x29(%rbp), %r8
leaq -0x31(%rbp), %r9
callq 0x13d9
movl -0x30(%rbp), %esi
movzbl -0x29(%rbp), %edx
leaq -0xae(%rbp), %rbx
movq %rbx, %rdi
callq 0x1468
movl -0x50(%rbp), %r9d
movl $0x6, %edx
movl %r13d, %edi
movq %rbx, %rsi
movl $0x100, %ecx            # imm = 0x100
leaq -0xc0(%rbp), %r8
callq 0x1050
testl %eax, %eax
movl -0x4c(%rbp), %r15d
jns 0x16d2
movl $0x11, %edx
movq %r14, %rdi
leaq 0x9c2(%rip), %rsi       # 0x208f
callq 0x1120
movl -0x30(%rbp), %ebx
cmpl %r15d, %ebx
jbe 0x1760
movl $0x6, %edx
movq %r14, %rdi
leaq 0xa46(%rip), %rsi       # 0x2133
callq 0x1120
movq %r14, %rdi
movl %r15d, %ebx
movq -0x40(%rbp), %r15
movl %r15d, %esi
callq 0x1170
movq %rax, %r14
movl $0x7, %edx
movq %rax, %rdi
leaq 0xa24(%rip), %rsi       # 0x213a
callq 0x1120
movq %r14, %rdi
movq 0x28ab(%rip), %r14      # 0x3fd0
movl %ebx, %esi
callq 0x1170
movl $0x1, %edx
movq %rax, %rdi
leaq 0x995(%rip), %rsi       # 0x20d0
callq 0x1120
movl $0x16, %edx
movq %r14, %rdi
leaq 0x9f3(%rip), %rsi       # 0x2142
callq 0x1120
leaq -0x4e0(%rbp), %rbx
jmp 0x19e2
cmpb $0x1, -0x29(%rbp)
jne 0x1849
movl %ebx, %r13d
shll $0xa, %r13d
movq -0x40(%rbp), %rax
incl %eax
subl %eax, %ebx
jne 0x18b9
movslq %r13d, %rdi
addq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rdx
leaq -0x8e0(%rbp), %rsi
callq 0x10d0
movl $0x1, %r14d
movq -0x58(%rbp), %rbx
cmpl $0x2, %ebx
jb 0x17bd
movl $0x1, %r14d
cmpb $0x1, (%r12,%r14)
jne 0x17bd
incq %r14
cmpq %r14, %rbx
jne 0x17ab
movl %ebx, %r14d
movl $0x16, %edx
movq 0x2807(%rip), %rdi      # 0x3fd0
leaq 0x8d1(%rip), %rsi       # 0x20a1
callq 0x1120
subl %r14d, %ebx
movl %ebx, %r15d
je 0x17f5
movl %r14d, %eax
xorl %ecx, %ecx
leal (%rax,%rcx), %edx
movb (%r12,%rdx), %dl
movb %dl, (%r12,%rcx)
incq %rcx
cmpq %rcx, %r15
jne 0x17e2
movl $0x19, %edx
movq 0x27cf(%rip), %rdi      # 0x3fd0
leaq 0x8b0(%rip), %rsi       # 0x20b8
callq 0x1120
cmpl -0x58(%rbp), %ebx
jae 0x1826
addq %r12, %r15
leal -0x1(%r14), %edx
incq %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x1090
addl -0x40(%rbp), %r14d
movq -0x58(%rbp), %rax
leal (%r14,%rax), %r15d
movq %r14, -0x40(%rbp)
leaq -0x4e0(%rbp), %rbx
movq 0x278c(%rip), %r14      # 0x3fd0
jmp 0x18e5
movl $0xd, %edx
movq %r14, %rdi
leaq 0x8be(%rip), %rsi       # 0x2116
callq 0x1120
movl -0x30(%rbp), %esi
movq %r14, %rdi
callq 0x10a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x10f0
movsbl %al, %esi
movq %r14, %rdi
movq 0x2744(%rip), %r14      # 0x3fd0
callq 0x1040
movq %rax, %rdi
callq 0x10b0
movl $0xe, %edx
movq %r14, %rdi
leaq 0x87c(%rip), %rsi       # 0x2124
callq 0x1120
leaq -0x4e0(%rbp), %rbx
jmp 0x19de
jbe 0x18de
cmpb $0x0, (%r12,%rbx)
jne 0x18de
movslq %r13d, %rdi
addq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rdx
leaq -0x8e0(%rbp), %rsi
callq 0x10d0
movb $0x1, (%r12,%rbx)
leaq -0x4e0(%rbp), %rbx
movl %r15d, -0x4c(%rbp)
cmpb $0x1, -0x31(%rbp)
jne 0x1976
addl -0x68(%rbp), %r13d
movl -0x30(%rbp), %eax
incl %eax
movq %rax, -0x90(%rbp)
movl $0x13, %edx
movq %r14, %rdi
leaq 0x7c0(%rip), %rsi       # 0x20d2
callq 0x1120
movl -0x30(%rbp), %esi
movq %r14, %rdi
callq 0x10a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x10f0
movsbl %al, %esi
movq %r14, %rdi
movq 0x268a(%rip), %r14      # 0x3fd0
callq 0x1040
movq %rax, %rdi
callq 0x10b0
movl $0x10, %edx
movq %r14, %rdi
leaq 0x784(%rip), %rsi       # 0x20e6
callq 0x1120
movb $0x1, %al
movq %rax, -0x88(%rbp)
movl %r13d, -0x48(%rbp)
jmp 0x19da
movl $0x10, %edx
movq %r14, %rdi
leaq 0x772(%rip), %rsi       # 0x20f7
callq 0x1120
movl -0x30(%rbp), %esi
movq %r14, %rdi
callq 0x10a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x10f0
movsbl %al, %esi
movq %r14, %rdi
movq 0x2617(%rip), %r14      # 0x3fd0
callq 0x1040
movq %rax, %rdi
callq 0x10b0
movl $0xd, %edx
movq %r14, %rdi
leaq 0x733(%rip), %rsi       # 0x2108
callq 0x1120
movl -0x44(%rbp), %r13d
movq -0x40(%rbp), %r15
movl -0x30(%rbp), %esi
movq %r14, %rdi
callq 0x10a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x10f0
movsbl %al, %esi
movq %r14, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10b0
movq -0x90(%rbp), %rax
decl %eax
cmpl %eax, %r15d
jl 0x161e
movslq -0x48(%rbp), %rdx
movl $0x1, %esi
movq -0x60(%rbp), %rdi
movq -0x80(%rbp), %rcx
callq 0x1180
movq -0xa8(%rbp), %rsp
testb $0x1, -0x88(%rbp)
movq -0x58(%rbp), %r14
je 0x15c5
movq -0x80(%rbp), %rdi
callq 0x1110
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2578(%rip), %rdi      # 0x3ff0
leaq 0x585(%rip), %rsi       # 0x2004
jmp 0x1a9f
movq 0x2568(%rip), %rdi      # 0x3ff0
leaq 0x5b5(%rip), %rsi       # 0x2044
jmp 0x1a9f
movq 0x2558(%rip), %rdi      # 0x3ff0
leaq 0x5bb(%rip), %rsi       # 0x205a
callq 0x1100
movq %rax, %rdi
callq 0x1080
movl $0x1, %edi
callq 0x1140