// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, %r14
movl %esi, %ebx
leaq 0x68(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x5ca5(%rip), %rsi      # 0x8188
leaq 0x80(%rsp), %rdi
callq 0x31b0
leaq 0x28(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x2b40(%rip), %rsi      # 0x5040
leaq 0x2b43(%rip), %rdx      # 0x504a
leaq 0x18(%rsp), %rdi
callq 0x3846
movq 0x5ac0(%rip), %r15      # 0x7fd8
leaq 0x2b2c(%rip), %rsi      # 0x504b
movl $0x7, %edx
movq %r15, %rdi
callq 0x2200
movq %r14, %rdi
shrq $0x20, %rdi
movq %rdi, 0x78(%rsp)
callq 0x20c0
testq %rax, %rax
je 0x2561
movq %rax, %r12
movq %rax, %rdi
callq 0x20b0
movq 0x5a84(%rip), %rdi      # 0x7fd8
movq %r12, %rsi
movq %rax, %rdx
callq 0x2200
jmp 0x2579
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x2270
movq 0x5a58(%rip), %rdi      # 0x7fd8
leaq 0x2aee(%rip), %rsi      # 0x5075
movl $0x1, %edx
callq 0x2200
shrq $0x10, %r14
movzwl %r14w, %esi
movq 0x5a38(%rip), %rdi      # 0x7fd8
callq 0x2120
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
leaq 0x16(%rsp), %rsi
movl $0x2, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2080
testq %rax, %rax
js 0x28b4
leaq 0xe(%rsp), %rsi
movl $0x2, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2080
testq %rax, %rax
js 0x28b4
cmpw $0x0, 0xe(%rsp)
je 0x280b
leaq 0x40(%rsp), %r15
movq 0x59b9(%rip), %r12      # 0x7fd8
leaq 0x98(%rsp), %r14
xorl %ebp, %ebp
movl $0x20, %edx
movl %ebx, %edi
leaq 0x58(%rsp), %rsi
xorl %ecx, %ecx
callq 0x2080
testq %rax, %rax
js 0x28b4
movl $0x4, %edx
movl %ebx, %edi
leaq 0x10(%rsp), %rsi
xorl %ecx, %ecx
callq 0x2080
testq %rax, %rax
js 0x28b4
movl $0xa, %edx
movq %r12, %rdi
leaq 0x29fc(%rip), %rsi      # 0x506c
callq 0x2200
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq %r12, %rdi
callq 0x2200
movq %rax, %r13
movl $0x2, %edx
movq %rax, %rdi
leaq 0x29de(%rip), %rsi      # 0x5077
callq 0x2200
movl $0xc, %edx
movq %r13, %rdi
leaq 0x29cd(%rip), %rsi      # 0x507a
callq 0x2200
movl 0x10(%rsp), %esi
movq %r13, %rdi
callq 0x2120
movl $0x1, %edx
movq %rax, %rdi
leaq 0x29ba(%rip), %rsi      # 0x5087
callq 0x2200
movl %ebx, %r12d
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq 0x78(%rsp), %rax
movl %eax, 0x38(%rsp)
movzwl 0x16(%rsp), %eax
movw %ax, 0x3c(%rsp)
movl 0x10(%rsp), %eax
movl %eax, %ecx
shrl $0xa, %ecx
xorl %r13d, %r13d
testl $0x3ff, %eax            # imm = 0x3FF
setne %r13b
addl %ecx, %r13d
je 0x2745
xorl %ebx, %ebx
movl %ebx, 0x98(%rsp)
movq 0x48(%rsp), %rsi
cmpq 0x50(%rsp), %rsi
je 0x2733
movl %ebx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x48(%rsp)
jmp 0x273e
movq %r15, %rdi
movq %r14, %rdx
callq 0x38d6
incl %ebx
cmpl %ebx, %r13d
jne 0x2713
leaq 0xa8(%rsp), %rax
movq %rax, 0x98(%rsp)
xorl %eax, %eax
movq %rax, 0xa0(%rsp)
movb $0x0, 0xa8(%rsp)
leaq 0xc0(%rsp), %rbx
movq %rax, 0x10(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq %r14, %rdi
leaq 0x58(%rsp), %rsi
callq 0x2100
movl 0x10(%rsp), %eax
movl %eax, 0xb8(%rsp)
movq %rbx, %rdi
leaq 0x38(%rsp), %rsi
callq 0x323a
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
callq 0x327a
movq %rbx, %rdi
callq 0x3444
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x27db
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
movl %r12d, %ebx
je 0x27f5
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
incl %ebp
movzwl 0xe(%rsp), %eax
cmpl %eax, %ebp
movq 0x57d3(%rip), %r12      # 0x7fd8
jb 0x2629
leaq 0x598e(%rip), %rdi      # 0x81a0
callq 0x2230
testl %eax, %eax
jne 0x28c7
leaq 0x5962(%rip), %rdi      # 0x8188
leaq 0x80(%rsp), %rsi
callq 0x32cc
leaq 0x5966(%rip), %rdi      # 0x81a0
callq 0x2150
leaq 0x18(%rsp), %rsi
xorl %ebp, %ebp
movl $0x20, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20e0
movl %ebx, %edi
callq 0x22b0
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x2877
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21d0
leaq 0x80(%rsp), %rdi
callq 0x3180
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x28a0
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21d0
movl %ebp, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2798(%rip), %rdi      # 0x5053
callq 0x2180
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x285b
movl %eax, %edi
callq 0x2110
movq %rax, %rbx
jmp 0x2931
movq %rax, %rbx
jmp 0x293e
jmp 0x2912
movq %rax, %rbx
jmp 0x291a
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x32a4
jmp 0x28f4
movq %rax, %rbx
leaq 0x28(%rsp), %r13
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x291a
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x291a
movq %rax, %rbx
leaq 0x28(%rsp), %r13
movq 0x18(%rsp), %rdi
cmpq %r13, %rdi
je 0x2931
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21d0
leaq 0x80(%rsp), %rdi
callq 0x3180
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x295a
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %rbx, %rdi
callq 0x2290