// File: /workspace/github_cpp_makefile_star2_O1/bchu7796[P]p2p-file-sharing/peers.cpp
// Function: client::file_list_request()
int client::file_list_request(){
    /*
    send:
        request type
    receive:
        number of file a client can download
        a list of file name
        a list of file length
    */
    int peer_fd;
    const uint32_t request_type = 1;
    uint16_t number_of_file;
    std::string file_name;
    uint32_t file_length;

    //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);
    }
    //get files a client can download
    send(peer_fd, &request_type, sizeof(request_type), 0);
    recv(peer_fd, &number_of_file, sizeof(number_of_file), 0);
    for(int i = 0; i < number_of_file; i++){
        recv(peer_fd, &file_name, sizeof(file_name), 0);
        recv(peer_fd, &file_length, sizeof(file_length), 0);
        std::cout << file_name << ": "<< file_length <<" bytes";
        if(i != number_of_file - 1){
            std::cout<<", ";
        }
    }
    std::cout << std::endl;
    close(peer_fd);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl $0x1, 0x14(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3070
testl %eax, %eax
js 0x3b6b
movl %eax, %ebx
leaq 0x78c6(%rip), %rsi      # 0xb2d8
movl %eax, %edi
movl $0x10, %edx
callq 0x3240
testl %eax, %eax
js 0x3b70
leaq 0x14(%rsp), %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x3160
leaq 0xe(%rsp), %rsi
movl $0x2, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x30e0
cmpw $0x0, 0xe(%rsp)
je 0x3b08
xorl %r15d, %r15d
movq 0x756e(%rip), %r12      # 0xafd0
leaq 0x464b(%rip), %r13      # 0x80b4
leaq 0x4612(%rip), %rbp      # 0x8082
movl $0x20, %edx
movl %ebx, %edi
leaq 0x18(%rsp), %rsi
xorl %ecx, %ecx
callq 0x30e0
movl $0x4, %edx
movl %ebx, %edi
leaq 0x10(%rsp), %rsi
xorl %ecx, %ecx
callq 0x30e0
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %r12, %rdi
callq 0x3330
movq %rax, %r14
movl $0x2, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x3330
movl 0x10(%rsp), %esi
movq %r14, %rdi
callq 0x31c0
movl $0x6, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3330
movzwl 0xe(%rsp), %eax
decl %eax
cmpl %eax, %r15d
je 0x3af7
movl $0x2, %edx
movq %r12, %rdi
leaq 0x4597(%rip), %rsi      # 0x8089
callq 0x3330
incl %r15d
movzwl 0xe(%rsp), %eax
cmpl %eax, %r15d
jb 0x3a70
movq 0x74c1(%rip), %rdi      # 0xafd0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x32d0
movsbl %al, %esi
movq 0x74a6(%rip), %rdi      # 0xafd0
callq 0x3030
movq %rax, %rdi
callq 0x31d0
movl %ebx, %edi
callq 0x3500
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x3b5a
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3300
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x355c
callq 0x3548
jmp 0x3b77
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x3b96
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3300
movq %rbx, %rdi
callq 0x3480