// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1, -0x10(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x33c0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3080
movl %eax, -0xc(%rbp)
cmpl $0x0, %eax
jge 0x3a44
leaq 0x95ff(%rip), %rdi      # 0xd01b
callq 0x3250
jmp 0x3a23
xorl %edi, %edi
callq 0x3340
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x31d0
jmp 0x3b99
movl -0xc(%rbp), %edi
leaq 0x10892(%rip), %rsi     # 0x142e0
movl $0x10, %edx
callq 0x3240
movl %eax, -0x54(%rbp)
jmp 0x3a5d
movl -0x54(%rbp), %eax
cmpl $0x0, %eax
jge 0x3a7a
leaq 0x95c7(%rip), %rdi      # 0xd033
callq 0x3250
jmp 0x3a73
xorl %edi, %edi
callq 0x3340
movl -0xc(%rbp), %edi
leaq -0x10(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x3180
jmp 0x3a8f
movl -0xc(%rbp), %edi
leaq -0x12(%rbp), %rsi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x30f0
jmp 0x3aa4
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movzwl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jge 0x3b69
movl -0xc(%rbp), %edi
leaq -0x38(%rbp), %rsi
movl $0x20, %edx
xorl %ecx, %ecx
callq 0x30f0
jmp 0x3acf
movl -0xc(%rbp), %edi
leaq -0x3c(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x30f0
jmp 0x3ae4
movq 0x104e5(%rip), %rdi     # 0x13fd0
leaq -0x38(%rbp), %rsi
callq 0x3280
movq %rax, -0x60(%rbp)
jmp 0x3afa
movq -0x60(%rbp), %rdi
leaq 0x959a(%rip), %rsi      # 0xd09f
callq 0x32d0
movq %rax, -0x68(%rbp)
jmp 0x3b10
movq -0x68(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x3390
movq %rax, -0x70(%rbp)
jmp 0x3b22
movq -0x70(%rbp), %rdi
leaq 0x9540(%rip), %rsi      # 0xd06d
callq 0x32d0
jmp 0x3b34
movl -0x50(%rbp), %eax
movzwl -0x12(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x3b59
movq 0x10487(%rip), %rdi     # 0x13fd0
leaq 0x9524(%rip), %rsi      # 0xd074
callq 0x32d0
jmp 0x3b57
jmp 0x3b59
jmp 0x3b5b
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3aab
movq 0x10460(%rip), %rdi     # 0x13fd0
movq 0x10441(%rip), %rsi     # 0x13fb8
callq 0x3310
jmp 0x3b7e
movl -0xc(%rbp), %edi
callq 0x3510
jmp 0x3b88
leaq -0x38(%rbp), %rdi
callq 0x31d0
xorl %eax, %eax
addq $0x70, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x34a0
nopw %cs:(%rax,%rax)