// File: /workspace/github_cpp_makefile_star2_O1/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), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movb $0x0, (%r15)
leaq 0x78(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x23b2(%rip), %rsi      # 0x5040
leaq 0x23b5(%rip), %rdx      # 0x504a
leaq 0x68(%rsp), %rdi
callq 0x3846
leaq 0x48(%rsp), %rsi
movl $0x20, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2080
testq %rax, %rax
js 0x2ee6
leaq 0x8(%rsp), %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2080
testq %rax, %rax
js 0x2ee6
leaq 0x6(%rsp), %rsi
movl $0x2, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2080
testq %rax, %rax
js 0x2ee6
leaq 0x548e(%rip), %rsi      # 0x8188
leaq 0x10(%rsp), %rdi
callq 0x31b0
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x2e43
shrq $0x20, %r14
xorl %r13d, %r13d
movq 0x52b6(%rip), %r15      # 0x7fd8
movq %r13, %rbp
shlq $0x6, %rbp
movq 0x50(%rsp), %rdx
cmpq 0x8(%rax,%rbp), %rdx
jne 0x2e26
testq %rdx, %rdx
je 0x2d56
addq %rbp, %rax
movq (%rax), %rsi
movq 0x48(%rsp), %rdi
callq 0x21e0
testl %eax, %eax
jne 0x2e26
movl $0x5, %edx
movq %r15, %rdi
leaq 0x2324(%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 0x10(%rsp), %rax
movq 0x28(%rax,%rbp), %rdi
movq 0x30(%rax,%rbp), %rax
subq %rdi, %rax
je 0x2dda
sarq $0x5, %rax
movzwl 0x6(%rsp), %ecx
cmpq $0x1, %rax
adcq $0x0, %rax
addq $0x18, %rdi
xorl %r12d, %r12d
cmpl %r14d, -0x18(%rdi)
jne 0x2dc9
cmpw %cx, -0x14(%rdi)
je 0x2ddf
incq %r12
addq $0x20, %rdi
cmpq %r12, %rax
jne 0x2dbd
movq %rax, %r12
jmp 0x2e0a
xorl %r12d, %r12d
jmp 0x2e0a
movl 0x8(%rsp), %eax
movl %eax, 0x28(%rsp)
movq -0x8(%rdi), %rsi
cmpq (%rdi), %rsi
je 0x2dfc
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x8(%rdi)
jmp 0x2e0a
addq $-0x10, %rdi
leaq 0x28(%rsp), %rdx
callq 0x38d6
movq 0x10(%rsp), %rax
movq 0x30(%rax,%rbp), %rcx
subq 0x28(%rax,%rbp), %rcx
sarq $0x5, %rcx
cmpq %r12, %rcx
je 0x2ef9
incq %r13
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x6, %rcx
cmpq %r13, %rcx
ja 0x2d22
leaq 0x5356(%rip), %rdi      # 0x81a0
callq 0x2230
testl %eax, %eax
jne 0x2f7e
leaq 0x532a(%rip), %rdi      # 0x8188
leaq 0x10(%rsp), %rsi
leaq 0x58(%rsp), %r15
leaq 0x78(%rsp), %r12
callq 0x32cc
leaq 0x5327(%rip), %rdi      # 0x81a0
callq 0x2150
leaq 0x68(%rsp), %rsi
movl $0x20, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20e0
movl %ebx, %edi
callq 0x22b0
leaq 0x10(%rsp), %rdi
callq 0x3180
xorl %ebx, %ebx
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x2ebb
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x2ed2
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21d0
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2166(%rip), %rdi      # 0x5053
callq 0x2180
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x2ea4
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movl %r14d, 0x28(%rsp)
movzwl 0x6(%rsp), %eax
movw %ax, 0x2c(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x38(%rsp), %rsi
testq %rsi, %rsi
je 0x2f38
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0x2f47
leaq 0x30(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x38d6
movq 0x10(%rsp), %rax
leaq (%rax,%rbp), %rdi
addq $0x28, %rdi
leaq 0x28(%rsp), %rsi
callq 0x323a
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x2e43
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x2e43
movl %eax, %edi
callq 0x2110
jmp 0x2f87
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x2fb6
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x2fb6
jmp 0x2fb3
jmp 0x2fac
movq %rax, %rbx
jmp 0x2fdc
movq %rax, %rbx
jmp 0x2fc0
jmp 0x2fb3
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3180
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x2fdc
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x2ff8
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %rbx, %rdi
callq 0x2290