// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %r14
movl %esi, %ebx
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x2596(%rip), %rsi      # 0x5040
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x2ec8
leaq 0x48(%rsp), %rsi
pushq $0x20
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2070
testq %rax, %rax
js 0x2ca7
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2070
testq %rax, %rax
js 0x2ca7
leaq 0x2(%rsp), %rsi
pushq $0x2
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2070
testq %rax, %rax
js 0x2ca7
leaq 0x568a(%rip), %rsi      # 0x8198
leaq 0x8(%rsp), %rdi
callq 0x2e74
shrq $0x20, %r14
xorl %r12d, %r12d
leaq 0x48(%rsp), %r15
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rax
subq %rsi, %rax
sarq $0x6, %rax
cmpq %r12, %rax
jbe 0x2c32
movq %r12, %rbp
shlq $0x6, %rbp
addq %rbp, %rsi
movq %r15, %rdi
callq 0x304f
testb %al, %al
je 0x2bd9
movq 0x5479(%rip), %rdi      # 0x7fd8
leaq 0x2523(%rip), %rsi      # 0x5089
callq 0x21d0
movq %rax, %rdi
callq 0x20f0
movq 0x8(%rsp), %rax
movq 0x28(%rax,%rbp), %rdi
movq 0x30(%rax,%rbp), %rax
subq %rdi, %rax
sarq $0x5, %rax
movzwl 0x2(%rsp), %ecx
addq $0x8, %rdi
xorl %r13d, %r13d
cmpq %r13, %rax
je 0x2be1
cmpl %r14d, -0x8(%rdi)
jne 0x2ba6
cmpw %cx, -0x4(%rdi)
je 0x2baf
incq %r13
addq $0x20, %rdi
jmp 0x2b95
movl 0x4(%rsp), %eax
movl %eax, 0x28(%rsp)
leaq 0x28(%rsp), %rsi
callq 0x35d2
movq 0x8(%rsp), %rax
movq 0x30(%rax,%rbp), %rcx
subq 0x28(%rax,%rbp), %rcx
sarq $0x5, %rcx
cmpq %r13, %rcx
je 0x2be1
incq %r12
jmp 0x2b24
leaq 0x30(%rsp), %r15
andq $0x0, 0x10(%r15)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movl %r14d, -0x8(%r15)
movzwl 0x2(%rsp), %eax
movw %ax, -0x4(%r15)
movl 0x4(%rsp), %eax
leaq 0x24(%rsp), %rsi
movl %eax, (%rsi)
movq %r15, %rdi
callq 0x35d2
movq 0x8(%rsp), %rax
leaq (%rax,%rbp), %rdi
addq $0x28, %rdi
leaq 0x28(%rsp), %rsi
callq 0x2f04
movq %r15, %rdi
callq 0x31a6
leaq 0x5577(%rip), %rdi      # 0x81b0
callq 0x2f72
leaq 0x5553(%rip), %rdi      # 0x8198
leaq 0x8(%rsp), %rsi
callq 0x2f86
leaq 0x555a(%rip), %rdi      # 0x81b0
callq 0x2160
leaq 0x68(%rsp), %rsi
pushq $0x20
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2100
movl %ebx, %edi
callq 0x22d0
leaq 0x8(%rsp), %rdi
callq 0x2e52
xorl %ebx, %ebx
leaq 0x68(%rsp), %rdi
callq 0x20d0
leaq 0x48(%rsp), %rdi
callq 0x20d0
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x23a5(%rip), %rdi      # 0x5053
callq 0x2190
pushq $-0x1
popq %rbx
jmp 0x2c7f
jmp 0x2cd7
jmp 0x2cbc
movq %rax, %rbx
movq %r15, %rdi
callq 0x31a6
jmp 0x2cda
jmp 0x2cd2
movq %rax, %rbx
jmp 0x2cee
jmp 0x2cd7
movq %rax, %rbx
jmp 0x2ce4
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x2e52
leaq 0x68(%rsp), %rdi
callq 0x20d0
leaq 0x48(%rsp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x22b0