// File: bchu7796[P]p2p-file-sharing/peers.cpp
// Function: client::register_chunk(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int)
int client::register_chunk(std::string file_name, int index){
    int peer_fd;
    std::string file = file_name;
    int chunk_index = index;
    std::string response_message;
    //create socket
    if((peer_fd = socket(AF_INET, SOCK_STREAM , 0)) < 0){
        perror("Failed to create socket"); 
        return -1;
    }

    //connect to server
    if(connect(peer_fd,(struct sockaddr*) &server_address, sizeof(server_address)) < 0){
        perror("Failed to connect"); 
        return -1;
    }

    //send request to the server
    uint32_t request_type = 3;
    uint16_t port = PORT;
    send(peer_fd, &request_type, sizeof(request_type), 0);
    send(peer_fd, &file, sizeof(file), 0);
    send(peer_fd, &chunk_index, sizeof(chunk_index), 0);
    send(peer_fd, &port, sizeof(port), 0);

    if(recv(peer_fd, &response_message, sizeof(response_message), 0) < 0){
        perror("No response from server");
        return -1;
    }
    std::cout<<response_message<<std::endl;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3130
movl -0x1c(%rbp), %eax
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x33c0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3080
movl %eax, -0x20(%rbp)
cmpl $0x0, %eax
jge 0x4d8b
leaq 0x82cd(%rip), %rdi      # 0xd01b
callq 0x3250
jmp 0x4d55
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x78(%rbp)
jmp 0x4eb7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x31d0
leaq -0x40(%rbp), %rdi
callq 0x31d0
jmp 0x4ed5
movl -0x20(%rbp), %edi
leaq 0xf54b(%rip), %rsi      # 0x142e0
movl $0x10, %edx
callq 0x3240
movl %eax, -0x84(%rbp)
jmp 0x4da7
movl -0x84(%rbp), %eax
cmpl $0x0, %eax
jge 0x4dd3
leaq 0x827a(%rip), %rdi      # 0xd033
callq 0x3250
jmp 0x4dc0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x78(%rbp)
jmp 0x4eb7
movl $0x3, -0x7c(%rbp)
movw $0x1f92, -0x7e(%rbp)    # imm = 0x1F92
movl -0x20(%rbp), %edi
leaq -0x7c(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x3180
jmp 0x4df5
movl -0x20(%rbp), %edi
leaq -0x40(%rbp), %rsi
movl $0x20, %edx
xorl %ecx, %ecx
callq 0x3180
jmp 0x4e0a
movl -0x20(%rbp), %edi
leaq -0x44(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x3180
jmp 0x4e1f
movl -0x20(%rbp), %edi
leaq -0x7e(%rbp), %rsi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x3180
jmp 0x4e34
movl -0x20(%rbp), %edi
leaq -0x68(%rbp), %rsi
movl $0x20, %edx
xorl %ecx, %ecx
callq 0x30f0
movq %rax, -0x90(%rbp)
jmp 0x4e50
movq -0x90(%rbp), %rax
cmpq $0x0, %rax
jge 0x4e7b
leaq 0x823e(%rip), %rdi      # 0xd0a2
callq 0x3250
jmp 0x4e6b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x78(%rbp)
jmp 0x4eb7
movq 0xf14e(%rip), %rdi      # 0x13fd0
leaq -0x68(%rbp), %rsi
callq 0x3280
movq %rax, -0x98(%rbp)
jmp 0x4e94
movq -0x98(%rbp), %rdi
movq 0xf116(%rip), %rsi      # 0x13fb8
callq 0x3310
jmp 0x4ea9
movl $0x0, -0x4(%rbp)
movl $0x1, -0x78(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x31d0
leaq -0x40(%rbp), %rdi
callq 0x31d0
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x34a0
nop