// File: bchu7796[P]p2p-file-sharing/server.cpp
// Function: server::chunk_register_reply(int, sockaddr_in)
int server::chunk_register_reply(int process_fd, struct sockaddr_in client_address){
    std::string file_name;
    int index;
    uint16_t port;
    std::string response = "registered";

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

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

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

    std::vector<file_info> temp_files = files;
    for(int i=0; i<temp_files.size(); i++){
        if(file_name == temp_files[i].file_name){
            std::cout<<"hello"<<std::endl;
            int j = 0;
            for(j=0; j<temp_files[i].peers.size(); j++){
                if(temp_files[i].peers[j].address == client_address.sin_addr.s_addr && temp_files[i].peers[j].port == port){
                    temp_files[i].peers[j].chunks.push_back(index);
                    break;
                }
            }
            if (j == temp_files[i].peers.size()){
                struct peer temp_peer;
                temp_peer.address = client_address.sin_addr.s_addr;
                temp_peer.port = port;
                temp_peer.chunks.push_back(index);
                temp_files[i].peers.push_back(temp_peer);
                break;
            }
        }
    }
    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 $0x120, %rsp            # imm = 0x120
movq %rdx, -0x14(%rbp)
movq %rcx, -0xc(%rbp)
movq %rdi, -0x20(%rbp)
movl %esi, -0x24(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2300
leaq -0x71(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x2390
movq -0xe0(%rbp), %rdx
leaq 0x714b(%rip), %rsi      # 0xa02c
leaq -0x70(%rbp), %rdi
callq 0x37c0
jmp 0x2eec
leaq -0x71(%rbp), %rdi
callq 0x2280
movl -0x24(%rbp), %edi
leaq -0x48(%rbp), %rsi
movl $0x20, %edx
xorl %ecx, %ecx
callq 0x20a0
movq %rax, -0xe8(%rbp)
jmp 0x2f11
movq -0xe8(%rbp), %rax
cmpq $0x0, %rax
jge 0x2f73
leaq 0x711a(%rip), %rdi      # 0xa03f
callq 0x21e0
jmp 0x2f2c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x88(%rbp)
jmp 0x334c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x71(%rbp), %rdi
callq 0x2280
jmp 0x3373
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x336a
movl -0x24(%rbp), %edi
leaq -0x4c(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x20a0
movq %rax, -0xf0(%rbp)
jmp 0x2f8f
movq -0xf0(%rbp), %rax
cmpq $0x0, %rax
jge 0x2fc0
leaq 0x709c(%rip), %rdi      # 0xa03f
callq 0x21e0
jmp 0x2faa
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x88(%rbp)
jmp 0x334c
movl -0x24(%rbp), %edi
leaq -0x4e(%rbp), %rsi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x20a0
movq %rax, -0xf8(%rbp)
jmp 0x2fdc
movq -0xf8(%rbp), %rax
cmpq $0x0, %rax
jge 0x300d
leaq 0x704f(%rip), %rdi      # 0xa03f
callq 0x21e0
jmp 0x2ff7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x88(%rbp)
jmp 0x334c
leaq 0xd1fc(%rip), %rsi      # 0x10210
leaq -0xa0(%rbp), %rdi
callq 0x36d0
jmp 0x3022
movl $0x0, -0xa4(%rbp)
movslq -0xa4(%rbp), %rax
movq %rax, -0x100(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x3cc0
movq %rax, %rcx
movq -0x100(%rbp), %rax
cmpq %rcx, %rax
jae 0x32cf
movslq -0xa4(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3ce0
movq %rax, %rsi
leaq -0x48(%rbp), %rdi
callq 0x3d00
testb $0x1, %al
jne 0x3081
jmp 0x32b9
movq 0xcf50(%rip), %rdi      # 0xffd8
leaq 0x6fe6(%rip), %rsi      # 0xa075
callq 0x2230
movq %rax, -0x108(%rbp)
jmp 0x309d
movq -0x108(%rbp), %rdi
movq 0xcf15(%rip), %rsi      # 0xffc0
callq 0x2260
jmp 0x30b2
movl $0x0, -0xa8(%rbp)
movl $0x0, -0xa8(%rbp)
movslq -0xa8(%rbp), %rax
movq %rax, -0x110(%rbp)
movslq -0xa4(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3ce0
movq %rax, %rdi
addq $0x28, %rdi
callq 0x3d90
movq %rax, %rcx
movq -0x110(%rbp), %rax
cmpq %rcx, %rax
jae 0x31d9
movslq -0xa4(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3ce0
movq %rax, %rdi
addq $0x28, %rdi
movslq -0xa8(%rbp), %rsi
callq 0x3db0
movl (%rax), %eax
cmpl -0x10(%rbp), %eax
jne 0x31c3
movslq -0xa4(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3ce0
movq %rax, %rdi
addq $0x28, %rdi
movslq -0xa8(%rbp), %rsi
callq 0x3db0
movzwl 0x4(%rax), %eax
movzwl -0x4e(%rbp), %ecx
cmpl %ecx, %eax
jne 0x31c3
movslq -0xa4(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3ce0
movq %rax, %rdi
addq $0x28, %rdi
movslq -0xa8(%rbp), %rsi
callq 0x3db0
movq %rax, %rdi
addq $0x8, %rdi
movl -0x4c(%rbp), %eax
movl %eax, -0xac(%rbp)
leaq -0xac(%rbp), %rsi
callq 0x3890
jmp 0x31ad
jmp 0x31d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x333e
jmp 0x31c5
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x30c6
movslq -0xa8(%rbp), %rax
movq %rax, -0x118(%rbp)
movslq -0xa4(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3ce0
movq %rax, %rdi
addq $0x28, %rdi
callq 0x3d90
movq %rax, %rcx
movq -0x118(%rbp), %rax
cmpq %rcx, %rax
jne 0x32b7
leaq -0xd0(%rbp), %rdi
callq 0x3870
movl -0x10(%rbp), %eax
movl %eax, -0xd0(%rbp)
movw -0x4e(%rbp), %ax
movw %ax, -0xcc(%rbp)
leaq -0xc8(%rbp), %rdi
movl -0x4c(%rbp), %eax
movl %eax, -0xd4(%rbp)
leaq -0xd4(%rbp), %rsi
callq 0x3890
jmp 0x3257
movslq -0xa4(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3ce0
movq %rax, %rdi
addq $0x28, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x38f0
jmp 0x327f
movl $0x2, -0x88(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3a00
jmp 0x32cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3a00
jmp 0x333e
jmp 0x32b9
jmp 0x32bb
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x302c
leaq 0xcf52(%rip), %rdi      # 0x10228
callq 0x3a20
jmp 0x32dd
leaq 0xcf2c(%rip), %rdi      # 0x10210
leaq -0xa0(%rbp), %rsi
callq 0x3a50
jmp 0x32f2
leaq 0xcf2f(%rip), %rdi      # 0x10228
callq 0x3ca0
jmp 0x3300
movl -0x24(%rbp), %edi
leaq -0x70(%rbp), %rsi
movl $0x20, %edx
xorl %ecx, %ecx
callq 0x2120
jmp 0x3315
movl -0x24(%rbp), %edi
callq 0x23b0
jmp 0x331f
movl $0x0, -0x4(%rbp)
movl $0x1, -0x88(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x3670
jmp 0x334c
leaq -0xa0(%rbp), %rdi
callq 0x3670
jmp 0x336a
leaq -0x70(%rbp), %rdi
callq 0x2160
leaq -0x48(%rbp), %rdi
callq 0x2160
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
leaq -0x70(%rbp), %rdi
callq 0x2160
leaq -0x48(%rbp), %rdi
callq 0x2160
movq -0x80(%rbp), %rdi
callq 0x2380
nopw %cs:(%rax,%rax)