// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movq %rdx, %r14
movl %esi, %ebx
leaq 0x40(%rsp), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movb $0x0, (%r15)
leaq 0x60(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x23a8(%rip), %rsi      # 0x5040
leaq 0x23ab(%rip), %rdx      # 0x504a
leaq 0x50(%rsp), %rdi
callq 0x3856
leaq 0x30(%rsp), %rsi
movl $0x20, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2080
testq %rax, %rax
js 0x2f0d
leaq 0x10(%rsp), %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2080
testq %rax, %rax
js 0x2f0d
leaq 0xe(%rsp), %rsi
movl $0x2, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2080
testq %rax, %rax
js 0x2f0d
movl %ebx, 0x14(%rsp)
leaq 0x5480(%rip), %rsi      # 0x8188
leaq 0x18(%rsp), %rdi
callq 0x31c6
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %rbx
cmpq %r12, %rbx
je 0x2e66
shrq $0x20, %r14
xorl %r13d, %r13d
movq 0x52a5(%rip), %r15      # 0x7fd8
movq %r13, %rbp
shlq $0x6, %rbp
movq 0x38(%rsp), %rdx
cmpq 0x8(%r12,%rbp), %rdx
jne 0x2e50
testq %rdx, %rdx
je 0x2d68
leaq (%r12,%rbp), %rax
movq (%rax), %rsi
movq 0x30(%rsp), %rdi
callq 0x21e0
testl %eax, %eax
jne 0x2e50
movl $0x5, %edx
movq %r15, %rdi
leaq 0x2312(%rip), %rsi      # 0x5089
callq 0x2200
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
movq 0x18(%rsp), %r12
movq 0x28(%r12,%rbp), %rcx
movq 0x30(%r12,%rbp), %rax
movq %rax, %rdx
subq %rcx, %rdx
je 0x2df4
sarq $0x5, %rdx
movzwl 0xe(%rsp), %esi
cmpq $0x1, %rdx
adcq $0x0, %rdx
leaq 0x18(%rcx), %rdi
xorl %ebx, %ebx
cmpl %r14d, -0x18(%rdi)
jne 0x2ddd
cmpw %si, -0x14(%rdi)
je 0x2dfb
incq %rbx
addq $0x20, %rdi
cmpq %rbx, %rdx
jne 0x2dd1
movq %rax, %r8
movq %rcx, %rax
movq %rdx, %rbx
jmp 0x2e3b
movq %rax, %r8
xorl %ebx, %ebx
jmp 0x2e3b
movl 0x10(%rsp), %edx
movl %edx, 0x70(%rsp)
movq -0x8(%rdi), %rsi
cmpq (%rdi), %rsi
je 0x2e1e
movq %rax, %r8
movl %edx, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x8(%rdi)
movq %rcx, %rax
jmp 0x2e3b
addq $-0x10, %rdi
leaq 0x70(%rsp), %rdx
callq 0x38ec
movq 0x18(%rsp), %r12
movq 0x28(%r12,%rbp), %rax
movq 0x30(%r12,%rbp), %r8
subq %rax, %r8
sarq $0x5, %r8
cmpq %rbx, %r8
je 0x2f20
movq 0x20(%rsp), %rbx
incq %r13
movq %rbx, %rax
subq %r12, %rax
sarq $0x6, %rax
cmpq %r13, %rax
ja 0x2d33
leaq 0x5333(%rip), %rdi      # 0x81a0
callq 0x2230
testl %eax, %eax
jne 0x2f8f
leaq 0x5307(%rip), %rdi      # 0x8188
leaq 0x18(%rsp), %rsi
leaq 0x40(%rsp), %r15
leaq 0x60(%rsp), %r12
callq 0x32e2
movl 0x14(%rsp), %ebx
leaq 0x5300(%rip), %rdi      # 0x81a0
callq 0x2150
leaq 0x50(%rsp), %rsi
movl $0x20, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20e0
movl %ebx, %edi
callq 0x22b0
leaq 0x18(%rsp), %rdi
callq 0x3196
xorl %ebx, %ebx
movq 0x50(%rsp), %rdi
cmpq %r12, %rdi
je 0x2ee2
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x30(%rsp), %rdi
cmpq %r15, %rdi
je 0x2ef9
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21d0
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x213f(%rip), %rdi      # 0x5053
callq 0x2180
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x2ecb
leaq 0x78(%rsp), %rdi
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl %r14d, -0x8(%rdi)
movzwl 0xe(%rsp), %eax
movw %ax, -0x4(%rdi)
movl 0x10(%rsp), %eax
leaq 0x94(%rsp), %rdx
movl %eax, (%rdx)
xorl %esi, %esi
callq 0x38ec
movq 0x18(%rsp), %rax
leaq (%rax,%rbp), %rdi
addq $0x28, %rdi
leaq 0x70(%rsp), %rsi
callq 0x3250
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x2e66
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x2e66
movl %eax, %edi
callq 0x2110
jmp 0x2f98
movq %rax, %rbx
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x2fca
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x2fca
jmp 0x2fc7
jmp 0x2fc0
movq %rax, %rbx
jmp 0x2ff0
movq %rax, %rbx
jmp 0x2fd4
jmp 0x2fc7
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x3196
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x2ff0
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x300c
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %rbx, %rdi
callq 0x2290