// File: bchu7796[P]p2p-file-sharing/server.cpp
// Function: server::register_reply(int, sockaddr_in)
int server::register_reply(int process_fd, struct sockaddr_in client_address){
    uint16_t number_of_file;
    std::string file_name;
    uint32_t file_length;
    std::vector<file_info> temp_files = files;
    std::string response = "registered";

    //client's address info
    std::cout<<"client:"<< inet_ntoa(client_address.sin_addr)<<":"<<client_address.sin_port<<std::endl;
    uint16_t port;
    if(recv(process_fd, &port, sizeof(port), 0) < 0){
        perror("client connection failed");
        return -1;
    }

    if(recv(process_fd, &number_of_file, sizeof(number_of_file), 0) < 0){
        perror("client connection failed");
        return -1;
    }

    for(int i=0; i<number_of_file; i++){
        if(recv(process_fd, &file_name, sizeof(file_name), 0) < 0){
            perror("client connection failed");
            return -1;
        }
        if(recv(process_fd, &file_length, sizeof(file_length), 0)<0){
            perror("client connection failed");
            return -1;
        }
        std::cout<<"file name:"<<file_name<<", "<<"file length:"<<file_length<<"\n";

        //register for all the chunks
        struct peer temp_peer;
        temp_peer.address = client_address.sin_addr.s_addr;
        temp_peer.port = port;
        int chunk_size = caculate_chunk_number(file_length);
        
        for(int j=0; j< chunk_size; j++){
            temp_peer.chunks.push_back(j);
        }
        //fill in other data
        struct file_info temp_file_info;
        temp_file_info.file_name = file_name;
        temp_file_info.file_length = file_length;
        temp_file_info.peers.push_back(temp_peer);
        temp_files.push_back(temp_file_info);
    }
    mtx.lock();
    files = temp_files;
    mtx.unlock();

    send(process_fd, &response, sizeof(response), 0);

    close(process_fd);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdx, -0x14(%rbp)
movq %rcx, -0xc(%rbp)
movq %rdi, -0x20(%rbp)
movl %esi, -0x24(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2300
leaq 0xdbeb(%rip), %rsi      # 0x10210
leaq -0x68(%rbp), %rdi
callq 0x36d0
jmp 0x2630
leaq -0x99(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x2390
movq -0x130(%rbp), %rdx
leaq 0x79db(%rip), %rsi      # 0xa02c
leaq -0x98(%rbp), %rdi
callq 0x37c0
jmp 0x265f
leaq -0x99(%rbp), %rdi
callq 0x2280
movq 0xd966(%rip), %rdi      # 0xffd8
leaq 0x79be(%rip), %rsi      # 0xa037
callq 0x2230
movq %rax, -0x138(%rbp)
jmp 0x2687
movl -0x10(%rbp), %eax
movl %eax, -0xa0(%rbp)
movl -0xa0(%rbp), %edi
callq 0x20f0
movq -0x138(%rbp), %rdi
movq %rax, %rsi
callq 0x2230
movq %rax, -0x140(%rbp)
jmp 0x26b3
movq -0x140(%rbp), %rdi
leaq 0x79a0(%rip), %rsi      # 0xa061
callq 0x2230
movq %rax, -0x148(%rbp)
jmp 0x26cf
movq -0x148(%rbp), %rdi
movzwl -0x12(%rbp), %esi
callq 0x2360
movq %rax, -0x150(%rbp)
jmp 0x26e8
movq -0x150(%rbp), %rdi
movq 0xd8ca(%rip), %rsi      # 0xffc0
callq 0x2260
jmp 0x26fd
movl -0x24(%rbp), %edi
leaq -0xa2(%rbp), %rsi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x20a0
movq %rax, -0x158(%rbp)
jmp 0x271c
movq -0x158(%rbp), %rax
cmpq $0x0, %rax
jge 0x278c
leaq 0x790f(%rip), %rdi      # 0xa03f
callq 0x21e0
jmp 0x2737
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0xa8(%rbp)
jmp 0x2ac9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x2b08
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x99(%rbp), %rdi
callq 0x2280
jmp 0x2aff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x2af3
movl -0x24(%rbp), %edi
leaq -0x26(%rbp), %rsi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x20a0
movq %rax, -0x160(%rbp)
jmp 0x27a8
movq -0x160(%rbp), %rax
cmpq $0x0, %rax
jge 0x27d9
leaq 0x7883(%rip), %rdi      # 0xa03f
callq 0x21e0
jmp 0x27c3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0xa8(%rbp)
jmp 0x2ac9
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
movzwl -0x26(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2a68
movl -0x24(%rbp), %edi
leaq -0x48(%rbp), %rsi
movl $0x20, %edx
xorl %ecx, %ecx
callq 0x20a0
movq %rax, -0x168(%rbp)
jmp 0x2811
movq -0x168(%rbp), %rax
cmpq $0x0, %rax
jge 0x2842
leaq 0x781a(%rip), %rdi      # 0xa03f
callq 0x21e0
jmp 0x282c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0xa8(%rbp)
jmp 0x2ac9
movl -0x24(%rbp), %edi
leaq -0x4c(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x20a0
movq %rax, -0x170(%rbp)
jmp 0x285e
movq -0x170(%rbp), %rax
cmpq $0x0, %rax
jge 0x288f
leaq 0x77cd(%rip), %rdi      # 0xa03f
callq 0x21e0
jmp 0x2879
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0xa8(%rbp)
jmp 0x2ac9
movq 0xd742(%rip), %rdi      # 0xffd8
leaq 0x77bb(%rip), %rsi      # 0xa058
callq 0x2230
movq %rax, -0x178(%rbp)
jmp 0x28ab
movq -0x178(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x2210
movq %rax, -0x180(%rbp)
jmp 0x28c4
movq -0x180(%rbp), %rdi
leaq 0x7791(%rip), %rsi      # 0xa063
callq 0x2230
movq %rax, -0x188(%rbp)
jmp 0x28e0
movq -0x188(%rbp), %rdi
leaq 0x7778(%rip), %rsi      # 0xa066
callq 0x2230
movq %rax, -0x190(%rbp)
jmp 0x28fc
movq -0x190(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x22f0
movq %rax, -0x198(%rbp)
jmp 0x2914
movq -0x198(%rbp), %rdi
leaq 0x7751(%rip), %rsi      # 0xa073
callq 0x2230
jmp 0x2929
leaq -0xd0(%rbp), %rdi
callq 0x3870
movq -0x128(%rbp), %rdi
movl -0x10(%rbp), %eax
movl %eax, -0xd0(%rbp)
movw -0xa2(%rbp), %ax
movw %ax, -0xcc(%rbp)
movl -0x4c(%rbp), %esi
callq 0x25c0
movl %eax, -0xd4(%rbp)
movl $0x0, -0xd8(%rbp)
movl -0xd8(%rbp), %eax
cmpl -0xd4(%rbp), %eax
jge 0x29be
leaq -0xc8(%rbp), %rdi
movl -0xd8(%rbp), %eax
movl %eax, -0xdc(%rbp)
leaq -0xdc(%rbp), %rsi
callq 0x3890
jmp 0x299a
jmp 0x299c
movl -0xd8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd8(%rbp)
jmp 0x296b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x2a57
leaq -0x120(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x38c0
movq -0x1a0(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x2030
jmp 0x29e3
movl -0x4c(%rbp), %eax
movl %eax, -0x100(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x38f0
jmp 0x2a01
leaq -0x68(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x3960
jmp 0x2a13
leaq -0x120(%rbp), %rdi
callq 0x39d0
leaq -0xd0(%rbp), %rdi
callq 0x3a00
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x27e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x39d0
leaq -0xd0(%rbp), %rdi
callq 0x3a00
jmp 0x2af3
leaq 0xd7b9(%rip), %rdi      # 0x10228
callq 0x3a20
jmp 0x2a76
leaq 0xd793(%rip), %rdi      # 0x10210
leaq -0x68(%rbp), %rsi
callq 0x3a50
jmp 0x2a88
leaq 0xd799(%rip), %rdi      # 0x10228
callq 0x3ca0
jmp 0x2a96
movl -0x24(%rbp), %edi
leaq -0x98(%rbp), %rsi
movl $0x20, %edx
xorl %ecx, %ecx
callq 0x2120
jmp 0x2aae
movl -0x24(%rbp), %edi
callq 0x23b0
jmp 0x2ab8
movl $0x0, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x2160
leaq -0x68(%rbp), %rdi
callq 0x3670
leaq -0x48(%rbp), %rdi
callq 0x2160
movl -0x4(%rbp), %eax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
leaq -0x98(%rbp), %rdi
callq 0x2160
leaq -0x68(%rbp), %rdi
callq 0x3670
leaq -0x48(%rbp), %rdi
callq 0x2160
movq -0x70(%rbp), %rdi
callq 0x2380
nopw (%rax,%rax)