// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movl %esi, %ebx
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x5cc8(%rip), %rsi      # 0x8198
leaq 0x20(%rsp), %rdi
callq 0x2e74
leaq 0x2b5f(%rip), %rsi      # 0x5040
leaq 0xb8(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x2ec8
movq 0x5ade(%rip), %rdi      # 0x7fd8
leaq 0x2b4a(%rip), %rsi      # 0x504b
callq 0x21d0
movq %rax, %r12
movq %r15, %rdi
shrq $0x20, %rdi
movq %rdi, 0x18(%rsp)
callq 0x20b0
movq %r12, %rdi
movq %rax, %rsi
callq 0x21d0
leaq 0x2b49(%rip), %rsi      # 0x5075
movq %rax, %rdi
callq 0x21d0
shrl $0x10, %r15d
movzwl %r15w, %esi
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
callq 0x20f0
leaq 0x10(%rsp), %rsi
pushq $0x2
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2070
testq %rax, %rax
js 0x274a
leaq 0x12(%rsp), %rsi
pushq $0x2
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2070
testq %rax, %rax
js 0x274a
leaq 0xa0(%rsp), %r15
xorl %r13d, %r13d
leaq 0x80(%rsp), %r12
leaq 0x58(%rsp), %rbp
movl %ebx, 0x14(%rsp)
movzwl 0x12(%rsp), %eax
cmpl %eax, %r13d
jae 0x2702
movl %ebx, %edi
leaq 0x38(%rsp), %rsi
pushq $0x20
popq %rdx
xorl %ecx, %ecx
callq 0x2070
testq %rax, %rax
js 0x274a
movl %ebx, %edi
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
xorl %ecx, %ecx
callq 0x2070
testq %rax, %rax
js 0x274a
movq 0x59f3(%rip), %rdi      # 0x7fd8
leaq 0x2a80(%rip), %rsi      # 0x506c
callq 0x21d0
movq %rax, %rdi
leaq 0x38(%rsp), %rsi
callq 0x21b0
movq %rax, %rdi
leaq 0x2a6f(%rip), %rsi      # 0x5077
callq 0x21d0
movq %rax, %rdi
leaq 0x2a63(%rip), %rsi      # 0x507a
callq 0x21d0
movl 0xc(%rsp), %esi
movq %rax, %rdi
callq 0x2140
movq %rax, %rdi
leaq 0x2a55(%rip), %rsi      # 0x5087
callq 0x21d0
movq %r12, %r14
andq $0x0, 0x10(%r15)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq 0x18(%rsp), %rax
movl %eax, 0x98(%rsp)
movzwl 0x10(%rsp), %eax
movw %ax, 0x9c(%rsp)
movl 0xc(%rsp), %eax
movl %eax, %ebx
shrl $0xa, %ebx
andl $0x3ff, %eax            # imm = 0x3FF
cmpl $0x1, %eax
sbbl $-0x1, %ebx
xorl %r12d, %r12d
cmpl %r12d, %ebx
je 0x2690
movl %r12d, 0x58(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x35d2
incl %r12d
jmp 0x2676
leaq 0x68(%rsp), %rax
movq %rax, 0x58(%rsp)
andq $0x0, 0x60(%rsp)
movb $0x0, 0x68(%rsp)
movq %r14, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
andq $0x0, 0x10(%r14)
movq %rbp, %rdi
leaq 0x38(%rsp), %rsi
callq 0x2120
movl 0xc(%rsp), %eax
movl %eax, 0x78(%rsp)
movq %r12, %rdi
leaq 0x98(%rsp), %rsi
callq 0x2f04
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0x2f30
movq %rbp, %rdi
callq 0x2f5c
movq %r15, %rdi
callq 0x31a6
incl %r13d
movl 0x14(%rsp), %ebx
jmp 0x259c
leaq 0x5aa7(%rip), %rdi      # 0x81b0
callq 0x2f72
leaq 0x5a83(%rip), %rdi      # 0x8198
leaq 0x20(%rsp), %rsi
callq 0x2f86
leaq 0x5a8a(%rip), %rdi      # 0x81b0
callq 0x2160
leaq 0xb8(%rsp), %rsi
pushq $0x20
popq %rdx
xorl %ebp, %ebp
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2100
movl %ebx, %edi
callq 0x22d0
jmp 0x2759
leaq 0x2902(%rip), %rdi      # 0x5053
callq 0x2190
pushq $-0x1
popq %rbp
leaq 0xb8(%rsp), %rdi
callq 0x20d0
leaq 0x20(%rsp), %rdi
callq 0x2e52
leaq 0x38(%rsp), %rdi
callq 0x20d0
movl %ebp, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x27c8
movq %rax, %rbx
jmp 0x27d2
jmp 0x27ab
jmp 0x27ab
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x2f5c
jmp 0x27b3
movq %rax, %rbx
jmp 0x27bb
movq %rax, %rbx
movq %r15, %rdi
callq 0x31a6
leaq 0xb8(%rsp), %rdi
callq 0x20d0
leaq 0x20(%rsp), %rdi
callq 0x2e52
leaq 0x38(%rsp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x22b0