// File: /workspace/github_cpp_makefile_star2_O2/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 $0x8c8, %rsp            # imm = 0x8C8
cmpl $0x5, %edi
jne 0x18ba
movq %rsi, %r14
movq 0x8(%rsi), %rbx
movq 0x10(%rsi), %rdi
callq 0x1110
movq %rax, -0x68(%rbp)
movq 0x18(%r14), %rdi
callq 0x1110
movl %eax, %r15d
movq 0x20(%r14), %rdi
callq 0x1110
movl %eax, %r14d
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1060
testl %eax, %eax
js 0x18ca
leaq -0xe0(%rbp), %rsi
andq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
andl $0x0, 0x4(%rsi)
rolw $0x8, %r14w
movw %r14w, 0x2(%rsi)
pushq $0x10
popq %rdx
movl %eax, -0x44(%rbp)
movl %eax, %edi
callq 0x1050
testl %eax, %eax
js 0x18da
shll $0xa, %r15d
leaq 0xbb0(%rip), %rsi       # 0x2070
movq %rbx, %rdi
callq 0x10e0
movq %rax, -0x88(%rbp)
movl %r15d, -0x7c(%rbp)
movslq %r15d, %rax
movq %rax, -0xb0(%rbp)
sarl $0xa, %eax
movl %eax, -0x78(%rbp)
movq -0x68(%rbp), %rax
movl %eax, %edx
leal -0x1(%rax), %ecx
movl %ecx, -0x74(%rbp)
xorl %r14d, %r14d
testl %eax, %eax
cmovgl %eax, %r14d
cmpl $0x1, %eax
adcl $0x0, %eax
movl %eax, -0x80(%rbp)
movq %rdx, -0x90(%rbp)
leaq 0xf(%rdx), %rax
andq $-0x10, %rax
movq %rax, -0xa0(%rbp)
movl -0x44(%rbp), %r13d
movq %r14, -0xa8(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x1030
movq %rax, -0x60(%rbp)
movq %rsp, -0xb8(%rbp)
movq %rsp, %rbx
subq -0xa0(%rbp), %rbx
movq %rbx, %rsp
xorl %eax, %eax
cmpq %rax, %r14
je 0x1556
movb $0x0, (%rbx,%rax)
incq %rax
jmp 0x1548
movl $0x10, -0x54(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x78(%rbp), %eax
movq %rax, -0x98(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x48(%rbp)
pushq $-0x1
popq %rax
movl %eax, %r12d
movl -0x74(%rbp), %eax
movl %eax, -0x50(%rbp)
movq 0x2a49(%rip), %r14      # 0x3fd0
movq %r12, -0x40(%rbp)
movl $0x40a, %edx            # imm = 0x40A
movl %r13d, %edi
leaq -0x8f0(%rbp), %rsi
movl $0x100, %ecx            # imm = 0x100
leaq -0xd0(%rbp), %r8
leaq -0x54(%rbp), %r9
callq 0x1090
testl %eax, %eax
jns 0x15c2
movq %r14, %rdi
leaq 0xab6(%rip), %rsi       # 0x2073
callq 0x10c0
leaq -0x8f0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x70(%rbp), %rdx
leaq -0x4e0(%rbp), %rcx
leaq -0x29(%rbp), %r8
leaq -0x31(%rbp), %r9
callq 0x135c
movl -0x30(%rbp), %esi
movzbl -0x29(%rbp), %edx
leaq -0xbe(%rbp), %r14
movq %r14, %rdi
callq 0x13c4
movl -0x54(%rbp), %r9d
movl %r13d, %edi
movq %r14, %rsi
pushq $0x6
popq %rdx
movl $0x100, %ecx            # imm = 0x100
leaq -0xd0(%rbp), %r8
callq 0x1040
testl %eax, %eax
movl -0x50(%rbp), %r12d
jns 0x1634
movq 0x29a8(%rip), %rdi      # 0x3fd0
leaq 0xa60(%rip), %rsi       # 0x208f
callq 0x10c0
movl -0x30(%rbp), %r14d
cmpl %r12d, %r14d
jbe 0x169a
movq 0x298c(%rip), %r14      # 0x3fd0
movq %r14, %rdi
leaq 0xae5(%rip), %rsi       # 0x2133
callq 0x10c0
movq %rax, %rdi
movl %r12d, %r15d
movq -0x40(%rbp), %r12
movl %r12d, %esi
callq 0x1120
movq %rax, %rdi
leaq 0xacb(%rip), %rsi       # 0x213a
callq 0x10c0
movq %rax, %rdi
movl %r15d, %esi
callq 0x1120
movq %rax, %rdi
leaq 0xa47(%rip), %rsi       # 0x20d0
callq 0x10c0
leaq 0xaad(%rip), %rsi       # 0x2142
jmp 0x1841
cmpb $0x1, -0x29(%rbp)
jne 0x16e5
movl %r14d, %r15d
shll $0xa, %r15d
movq -0x40(%rbp), %rax
incl %eax
movslq %r15d, %rdi
subl %eax, %r14d
jne 0x171a
addq -0x60(%rbp), %rdi
movq -0x70(%rbp), %rdx
leaq -0x4e0(%rbp), %rsi
callq 0x10a0
pushq $0x1
popq %r14
movq -0x90(%rbp), %rax
cmpq %rax, %r14
jae 0x1746
cmpb $0x1, (%rbx,%r14)
jne 0x174c
incq %r14
jmp 0x16d4
movq 0x28e4(%rip), %r14      # 0x3fd0
movq %r14, %rdi
leaq 0xa20(%rip), %rsi       # 0x2116
callq 0x10c0
movl -0x30(%rbp), %esi
movq %rax, %rdi
callq 0x1080
movq %rax, %rdi
callq 0x1070
leaq 0xa0f(%rip), %rsi       # 0x2124
jmp 0x183d
jbe 0x17bb
cmpb $0x0, (%rbx,%r14)
jne 0x17bb
addq -0x60(%rbp), %rdi
movq -0x70(%rbp), %rdx
leaq -0x4e0(%rbp), %rsi
callq 0x10a0
movb $0x1, (%rbx,%r14)
jmp 0x17bb
movl -0x80(%rbp), %eax
movl %eax, %r14d
movq 0x287d(%rip), %rdi      # 0x3fd0
leaq 0x947(%rip), %rsi       # 0x20a1
callq 0x10c0
movq -0x68(%rbp), %r12
subl %r14d, %r12d
movl %r14d, %r14d
xorl %eax, %eax
cmpq %rax, %r12
je 0x177f
leal (%r14,%rax), %ecx
movb (%rbx,%rcx), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x176b
movq 0x284a(%rip), %rdi      # 0x3fd0
leaq 0x92b(%rip), %rsi       # 0x20b8
callq 0x10c0
movq -0x40(%rbp), %rax
movq -0x90(%rbp), %rcx
cmpq %rcx, %r12
jae 0x17ac
movb $0x0, (%rbx,%r12)
incq %r12
jmp 0x179d
addl %eax, %r14d
movq -0x68(%rbp), %rax
leal (%r14,%rax), %r12d
movq %r14, -0x40(%rbp)
movl %r12d, -0x50(%rbp)
cmpb $0x1, -0x31(%rbp)
movq 0x2806(%rip), %r14      # 0x3fd0
jne 0x1810
addl -0x70(%rbp), %r15d
movl -0x30(%rbp), %eax
incl %eax
movq %rax, -0x98(%rbp)
movq %r14, %rdi
leaq 0x8ec(%rip), %rsi       # 0x20d2
callq 0x10c0
movl -0x30(%rbp), %esi
movq %rax, %rdi
callq 0x1080
movq %rax, %rdi
callq 0x1070
movb $0x1, %al
movl %eax, -0x4c(%rbp)
leaq 0x8dc(%rip), %rsi       # 0x20e6
movl %r15d, -0x48(%rbp)
jmp 0x1839
movq %r14, %rdi
leaq 0x8dd(%rip), %rsi       # 0x20f7
callq 0x10c0
movl -0x30(%rbp), %esi
movq %rax, %rdi
callq 0x1080
movq %rax, %rdi
callq 0x1070
leaq 0x8cf(%rip), %rsi       # 0x2108
movl -0x44(%rbp), %r13d
movq -0x40(%rbp), %r12
movq %r14, %rdi
callq 0x10c0
movl -0x30(%rbp), %esi
movq %rax, %rdi
callq 0x1080
movq %rax, %rdi
callq 0x1070
movq -0x98(%rbp), %rax
decl %eax
cmpl %eax, %r12d
jl 0x1587
movslq -0x48(%rbp), %rdx
movq -0x60(%rbp), %rdi
pushq $0x1
popq %rsi
movq -0x88(%rbp), %rcx
callq 0x1130
movq -0xb8(%rbp), %rsp
testb $0x1, -0x4c(%rbp)
movq -0xa8(%rbp), %r14
je 0x1522
movq -0x88(%rbp), %rdi
callq 0x10d0
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x272f(%rip), %rdi      # 0x3ff0
leaq 0x73c(%rip), %rsi       # 0x2004
jmp 0x18e8
movq 0x271f(%rip), %rdi      # 0x3ff0
leaq 0x76c(%rip), %rsi       # 0x2044
jmp 0x18e8
movq 0x270f(%rip), %rdi      # 0x3ff0
leaq 0x772(%rip), %rsi       # 0x205a
callq 0x10c0
movq %rax, %rdi
callq 0x1070
pushq $0x1
popq %rdi
callq 0x10f0