// File: 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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdx, -0x100(%rbp)
movw %si, %ax
movq %rdi, -0x10(%rbp)
movw %ax, -0x12(%rbp)
movq %rdx, -0x20(%rbp)
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x64, %edx
callq 0x31a0
movl $0x0, -0x94(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x33c0
movl $0x0, -0xbc(%rbp)
movw $0x1f92, -0xbe(%rbp)    # imm = 0x1F92
leaq -0xe0(%rbp), %rdi
callq 0x33c0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3080
movl %eax, -0x24(%rbp)
cmpl $0x0, %eax
jge 0x37d6
leaq 0x9882(%rip), %rdi      # 0xd01b
callq 0x3250
jmp 0x37a0
xorl %edi, %edi
callq 0x3340
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x31d0
leaq -0xb8(%rbp), %rdi
callq 0x31d0
jmp 0x39cc
movl -0x24(%rbp), %edi
leaq 0x10b00(%rip), %rsi     # 0x142e0
movl $0x10, %edx
callq 0x3240
movl %eax, -0x104(%rbp)
jmp 0x37f2
movl -0x104(%rbp), %eax
cmpl $0x0, %eax
jge 0x3812
leaq 0x982f(%rip), %rdi      # 0xd033
callq 0x3250
jmp 0x380b
xorl %edi, %edi
callq 0x3340
movl -0x24(%rbp), %edi
leaq -0xbc(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x3180
jmp 0x382a
movl -0x24(%rbp), %edi
leaq -0xbe(%rbp), %rsi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x3180
jmp 0x3842
movl -0x24(%rbp), %edi
leaq -0x12(%rbp), %rsi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x3180
jmp 0x3857
movl $0x0, -0xf0(%rbp)
movl -0xf0(%rbp), %eax
movzwl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jge 0x38f3
movq -0x100(%rbp), %rdi
movl -0x24(%rbp), %eax
movl %eax, -0x108(%rbp)
movslq -0xf0(%rbp), %rsi
callq 0x5f50
movl -0x108(%rbp), %edi
movq %rax, %rsi
movl $0x20, %edx
xorl %ecx, %ecx
callq 0x3180
jmp 0x38a6
movq -0x100(%rbp), %rdi
movl -0x24(%rbp), %eax
movl %eax, -0x10c(%rbp)
movslq -0xf0(%rbp), %rsi
callq 0x5f50
movl -0x10c(%rbp), %edi
movq %rax, %rsi
addq $0x20, %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x3180
jmp 0x38dd
jmp 0x38df
movl -0xf0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf0(%rbp)
jmp 0x3861
movl -0x24(%rbp), %edi
leaq -0xe0(%rbp), %rsi
movl $0x20, %edx
xorl %ecx, %ecx
callq 0x30f0
movq %rax, -0x118(%rbp)
jmp 0x3912
movq -0x118(%rbp), %rax
cmpq $0x0, %rax
jge 0x3940
leaq 0x971f(%rip), %rdi      # 0xd045
callq 0x3250
jmp 0x392d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0xf4(%rbp)
jmp 0x39a8
movq 0x10689(%rip), %rdi     # 0x13fd0
leaq 0x970e(%rip), %rsi      # 0xd05c
callq 0x32d0
movq %rax, -0x120(%rbp)
jmp 0x395c
movq -0x120(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x3280
movq %rax, -0x128(%rbp)
jmp 0x3978
movq -0x128(%rbp), %rdi
movq 0x10632(%rip), %rsi     # 0x13fb8
callq 0x3310
jmp 0x398d
movl -0x24(%rbp), %edi
callq 0x3510
jmp 0x3997
movl $0x0, -0x4(%rbp)
movl $0x1, -0xf4(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x31d0
leaq -0xb8(%rbp), %rdi
callq 0x31d0
movl -0x4(%rbp), %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0xe8(%rbp), %rdi
callq 0x34a0
nopl (%rax,%rax)