// File: /workspace/github_cpp_makefile_star2_O3/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 0x1a96
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 0x1aa6
leaq -0xd0(%rbp), %rsi
movq $0x0, 0x8(%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 0x1ab6
movl %r15d, %r14d
shll $0xa, %r13d
leaq 0xaf9(%rip), %rsi       # 0x2070
movq %rbx, %rdi
callq 0x1130
movq %rax, -0x88(%rbp)
movl %r13d, -0x7c(%rbp)
movslq %r13d, %rax
movq %rax, -0xa0(%rbp)
sarl $0xa, %eax
movl %eax, -0x78(%rbp)
leal -0x1(%r14), %eax
movl %eax, -0x74(%rbp)
leaq 0xf(%r14), %rax
andq $-0x10, %rax
movq %rax, -0x98(%rbp)
leaq -0x4e0(%rbp), %rbx
movl -0x44(%rbp), %r13d
movq %r14, -0x60(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x1030
movq %rax, -0x68(%rbp)
movq %rsp, -0xa8(%rbp)
movq %rsp, %r12
subq -0x98(%rbp), %r12
movq %r12, %rsp
testl %r14d, %r14d
jle 0x15f5
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x1090
movl $0x10, -0x54(%rbp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0x0, -0x4c(%rbp)
movl -0x78(%rbp), %eax
movq %rax, -0x90(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x50(%rbp)
movq %r15, -0x40(%rbp)
movl $0x40a, %edx            # imm = 0x40A
movl %r13d, %edi
movq %rbx, %rsi
movl $0x100, %ecx            # imm = 0x100
leaq -0xc0(%rbp), %r14
movq %r14, %r8
leaq -0x54(%rbp), %r9
callq 0x10c0
testl %eax, %eax
jns 0x1662
movl $0x1b, %edx
movq 0x297a(%rip), %rdi      # 0x3fd0
leaq 0xa16(%rip), %rsi       # 0x2073
callq 0x1120
movq %rbx, %rdi
leaq -0x30(%rbp), %rsi
leaq -0x70(%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 -0x54(%rbp), %r9d
movl $0x6, %edx
movl %r13d, %edi
movq %rbx, %rsi
movl $0x100, %ecx            # imm = 0x100
movq %r14, %r8
callq 0x1050
testl %eax, %eax
movq 0x2914(%rip), %r14      # 0x3fd0
movl -0x50(%rbp), %r15d
jns 0x16d6
movl $0x11, %edx
movq %r14, %rdi
leaq 0x9be(%rip), %rsi       # 0x208f
callq 0x1120
movl -0x30(%rbp), %ebx
cmpl %r15d, %ebx
jbe 0x1764
movl $0x6, %edx
movq %r14, %rdi
leaq 0xa42(%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 0xa20(%rip), %rsi       # 0x213a
callq 0x1120
movq %r14, %rdi
movq 0x28a7(%rip), %r14      # 0x3fd0
movl %ebx, %esi
callq 0x1170
movl $0x1, %edx
movq %rax, %rdi
leaq 0x991(%rip), %rsi       # 0x20d0
callq 0x1120
movl $0x16, %edx
movq %r14, %rdi
leaq 0x9ef(%rip), %rsi       # 0x2142
callq 0x1120
leaq -0x4e0(%rbp), %rbx
jmp 0x1a04
cmpb $0x1, -0x29(%rbp)
jne 0x17e0
movl %ebx, %r13d
shll $0xa, %r13d
movq -0x40(%rbp), %rax
incl %eax
subl %eax, %ebx
jne 0x1850
movslq %r13d, %rdi
addq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rdx
leaq -0x8e0(%rbp), %rsi
callq 0x10d0
movl $0x1, %ebx
movq -0x60(%rbp), %r15
cmpl $0x2, %r15d
jb 0x1881
movl $0x1, %ebx
cmpb $0x1, (%r12,%rbx)
jne 0x1881
incq %rbx
cmpq %rbx, %r15
jne 0x17ae
movl $0x16, %edx
movq %r14, %rdi
leaq 0x8d1(%rip), %rsi       # 0x20a1
callq 0x1120
movq %r15, %r14
xorl %r15d, %r15d
jmp 0x189d
movl $0xd, %edx
movq %r14, %rdi
leaq 0x927(%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 0x27ad(%rip), %r14      # 0x3fd0
callq 0x1040
movq %rax, %rdi
callq 0x10b0
movl $0xe, %edx
movq %r14, %rdi
leaq 0x8e5(%rip), %rsi       # 0x2124
callq 0x1120
leaq -0x4e0(%rbp), %rbx
jmp 0x1a00
jbe 0x1875
cmpb $0x0, (%r12,%rbx)
jne 0x1875
movslq %r13d, %rdi
addq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rdx
leaq -0x8e0(%rbp), %rsi
callq 0x10d0
movb $0x1, (%r12,%rbx)
leaq -0x4e0(%rbp), %rbx
jmp 0x190f
movq %r14, %rdi
movl %ebx, %r14d
movl $0x16, %edx
leaq 0x80e(%rip), %rsi       # 0x20a1
callq 0x1120
subl %ebx, %r15d
jne 0x18a6
leaq -0x4e0(%rbp), %rbx
jmp 0x18c6
movl %r15d, %eax
xorl %ecx, %ecx
leaq -0x4e0(%rbp), %rbx
leal (%r14,%rcx), %edx
movb (%r12,%rdx), %dl
movb %dl, (%r12,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x18b2
movl $0x19, %edx
movq 0x26fe(%rip), %rdi      # 0x3fd0
leaq 0x7df(%rip), %rsi       # 0x20b8
callq 0x1120
cmpl -0x60(%rbp), %r15d
jae 0x18f8
movl %r15d, %edi
addq %r12, %rdi
leal -0x1(%r14), %edx
incq %rdx
xorl %esi, %esi
callq 0x1090
addl -0x40(%rbp), %r14d
movq -0x60(%rbp), %rax
leal (%r14,%rax), %r15d
movq %r14, -0x40(%rbp)
movq 0x26c1(%rip), %r14      # 0x3fd0
movl %r15d, -0x50(%rbp)
cmpb $0x1, -0x31(%rbp)
jne 0x1998
addl -0x70(%rbp), %r13d
movl -0x30(%rbp), %eax
incl %eax
movq %rax, -0x90(%rbp)
movl $0x13, %edx
movq %r14, %rdi
leaq 0x79a(%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 0x2664(%rip), %r14      # 0x3fd0
callq 0x1040
movq %rax, %rdi
callq 0x10b0
movl $0x10, %edx
movq %r14, %rdi
leaq 0x75e(%rip), %rsi       # 0x20e6
callq 0x1120
movb $0x1, %al
movl %eax, -0x4c(%rbp)
movl %r13d, -0x48(%rbp)
jmp 0x19fc
movl $0x10, %edx
movq %r14, %rdi
leaq 0x750(%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 0x25f5(%rip), %r14      # 0x3fd0
callq 0x1040
movq %rax, %rdi
callq 0x10b0
movl $0xd, %edx
movq %r14, %rdi
leaq 0x711(%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 0x161f
movslq -0x48(%rbp), %rdx
movl $0x1, %esi
movq -0x68(%rbp), %rdi
movq -0x88(%rbp), %rcx
callq 0x1180
movq -0xa8(%rbp), %rsp
testb $0x1, -0x4c(%rbp)
movq -0x60(%rbp), %r14
je 0x15bf
movq -0x88(%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 0x2553(%rip), %rdi      # 0x3ff0
leaq 0x560(%rip), %rsi       # 0x2004
jmp 0x1ac4
movq 0x2543(%rip), %rdi      # 0x3ff0
leaq 0x590(%rip), %rsi       # 0x2044
jmp 0x1ac4
movq 0x2533(%rip), %rdi      # 0x3ff0
leaq 0x596(%rip), %rsi       # 0x205a
callq 0x1100
movq %rax, %rdi
callq 0x1080
movl $0x1, %edi
callq 0x1140