// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %rbx
subq $0x50, %rsp
movl %edx, %ebx
leaq 0x40(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
addq %rax, %rdx
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x620c
movl %ebx, 0x4(%rsp)
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movb $0x0, (%r15)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3070
testl %eax, %eax
js 0x4d97
movl %eax, %ebx
leaq 0x6646(%rip), %rsi      # 0xb2d8
movl %eax, %edi
movl $0x10, %edx
callq 0x3240
testl %eax, %eax
js 0x4da0
leaq 0x2c(%rsp), %rsi
movl $0x3, (%rsi)
movw $0x1f92, 0x2(%rsp)      # imm = 0x1F92
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x3160
leaq 0x30(%rsp), %rsi
movl $0x20, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x3160
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
leaq 0x8(%rsp), %rsi
movl $0x20, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x30e0
testq %rax, %rax
js 0x4da9
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x62a4(%rip), %rdi      # 0xafd0
callq 0x3330
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x31d0
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x4d74
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3300
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x4d8b
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3300
movl %ebx, %eax
addq $0x50, %rsp
popq %rbx
popq %r14
popq %r15
retq
leaq 0x3292(%rip), %rdi      # 0x8030
jmp 0x4db0
leaq 0x32a1(%rip), %rdi      # 0x8048
jmp 0x4db0
leaq 0x3307(%rip), %rdi      # 0x80b7
callq 0x3250
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x4d5d
jmp 0x4dbe
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x4dd8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3300
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x4def
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3300
movq %rbx, %rdi
callq 0x3480
nop