// File: /workspace/github_cpp_makefile_star2_O2/bchu7796[P]p2p-file-sharing/peers.cpp
// Function: client::register_request(unsigned short, std::vector<file_info, std::allocator<file_info>>)
int client::register_request(uint16_t number_of_file, std::vector<file_info> file_infos){
    /*
    send:
        request type
        the port number a peer is listening to
        number of file a peer want to share
        a list of file name
        a list of file length
    receive:
        OK
    */
    int peer_fd;
    char receiveMessage[100] = {};
    uint32_t file_length = 0;
    std::string file_name;
    const uint32_t request_type = 0;
    uint16_t port = PORT;
    std::string response_message;

    //create socket
    if((peer_fd = socket(AF_INET, SOCK_STREAM , 0)) < 0){
        perror("Failed to create socket"); 
        exit(0);
    }

    //connect to server
    if(connect(peer_fd,(struct sockaddr*) &server_address, sizeof(server_address)) < 0){
        perror("Failed to connect"); 
        exit(0);
    }
    
    //register the file a peer want to share
    send(peer_fd, &request_type, sizeof(request_type), 0);
    send(peer_fd, &port, sizeof(port), 0);
    send(peer_fd, &number_of_file, sizeof(number_of_file), 0);
    for(int i=0; i < number_of_file; i++){
        send(peer_fd, &file_infos[i].file_name, sizeof(file_infos[i].file_name), 0);
        send(peer_fd, &file_infos[i].file_length, sizeof(file_infos[i].file_length), 0);
    }
    if(recv(peer_fd, &response_message, sizeof(response_message), 0) < 0){
        perror("server response failed");
        return -1;
    }
    std::cout << "server response:" << response_message << std::endl;
    close(peer_fd);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r14
movw %si, (%rsp)
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
andl $0x0, 0x4(%rsp)
movb %cl, (%rax)
movw $0x1f92, 0x2(%rsp)      # imm = 0x1F92
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x3060
testl %eax, %eax
js 0x37bb
movl %eax, %ebx
leaq 0x7c05(%rip), %rsi      # 0xb2b8
pushq $0x10
popq %rdx
movl %eax, %edi
callq 0x3210
testl %eax, %eax
js 0x37c4
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x3160
leaq 0x2(%rsp), %rsi
pushq $0x2
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x3160
movq %rsp, %rsi
pushq $0x2
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x3160
xorl %r13d, %r13d
pushq $0x20
popq %r15
pushq $0x4
popq %r12
xorl %ebp, %ebp
movzwl (%rsp), %eax
cmpq %rax, %rbp
jae 0x373e
movq (%r14), %rsi
addq %r13, %rsi
movl %ebx, %edi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x3160
movq (%r14), %rax
leaq (%rax,%r13), %rsi
addq $0x20, %rsi
movl %ebx, %edi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x3160
incq %rbp
addq $0x28, %r13
jmp 0x3703
leaq 0x8(%rsp), %rsi
pushq $0x20
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x30d0
testq %rax, %rax
js 0x37aa
movq 0x7875(%rip), %rdi      # 0xafd0
leaq 0x38fa(%rip), %rsi      # 0x705c
callq 0x32a0
leaq 0x8(%rsp), %rsi
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
callq 0x3150
xorl %ebp, %ebp
movl %ebx, %edi
callq 0x34c0
leaq 0x8(%rsp), %rdi
callq 0x3110
leaq 0x28(%rsp), %rdi
callq 0x3110
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3894(%rip), %rdi      # 0x7045
callq 0x3220
pushq $-0x1
popq %rbp
jmp 0x3785
leaq 0x3859(%rip), %rdi      # 0x701b
jmp 0x37cb
leaq 0x3868(%rip), %rdi      # 0x7033
callq 0x3220
xorl %edi, %edi
callq 0x3300
jmp 0x37d9
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x3110
leaq 0x28(%rsp), %rdi
callq 0x3110
movq %rbx, %rdi
callq 0x3440