// File: /workspace/github_cpp_makefile_star2_O1/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), %r15
movq %r15, -0x10(%r15)
xorl %eax, %eax
movq %rax, -0x8(%r15)
movb %al, (%r15)
movl $0x0, 0x4(%rsp)
movw $0x1f92, 0x2(%rsp)      # imm = 0x1F92
leaq 0x18(%rsp), %r12
movq %r12, -0x10(%r12)
movq %rax, -0x8(%r12)
movb %al, (%r12)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3070
testl %eax, %eax
js 0x3980
movl %eax, %ebx
leaq 0x7abb(%rip), %rsi      # 0xb2d8
movl %eax, %edi
movl $0x10, %edx
callq 0x3240
testl %eax, %eax
js 0x3985
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x3160
leaq 0x2(%rsp), %rsi
movl $0x2, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x3160
movq %rsp, %rsi
movl $0x2, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x3160
cmpw $0x0, (%rsp)
je 0x38b1
xorl %r13d, %r13d
xorl %ebp, %ebp
movq (%r14), %rsi
addq %r13, %rsi
movl $0x20, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x3160
movq (%r14), %rax
leaq (%rax,%r13), %rsi
addq $0x20, %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x3160
incq %rbp
movzwl (%rsp), %eax
addq $0x28, %r13
cmpq %rax, %rbp
jb 0x3874
leaq 0x8(%rsp), %rsi
movl $0x20, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x30e0
testq %rax, %rax
js 0x396d
movq 0x76fc(%rip), %rdi      # 0xafd0
leaq 0x4796(%rip), %rsi      # 0x8071
movl $0x10, %edx
callq 0x3330
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x76da(%rip), %rdi      # 0xafd0
callq 0x3330
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x32d0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31d0
xorl %ebp, %ebp
movl %ebx, %edi
callq 0x3500
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x3945
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3300
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x395c
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3300
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x46e6(%rip), %rdi      # 0x805a
callq 0x3250
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x392e
callq 0x3534
callq 0x3520
jmp 0x398c
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x39a6
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3300
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x39bd
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3300
movq %rbx, %rdi
callq 0x3480
nop