// File: bchu7796[P]p2p-file-sharing/peers.cpp
// Function: client::download_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, chunk)
int client::download_file(std::string file_name, int index, struct chunk chunk_instance){
    int sock_fd;
    char buffer[1024];
    struct sockaddr_in peer_address;
    int selected_peer_index = rand() % chunk_instance.peers.size();

    
    //initialize address structure
    bzero(&peer_address, sizeof(peer_address));
    peer_address.sin_family = PF_INET;
    peer_address.sin_addr.s_addr = chunk_instance.peers[selected_peer_index].address.s_addr;
    peer_address.sin_port = htons(chunk_instance.peers[selected_peer_index].port);
    std::cout<<"LIST"<<std::endl;
    std::cout<<"peer: "<< inet_ntoa(chunk_instance.peers[selected_peer_index].address)\
    << ":" << chunk_instance.peers[selected_peer_index].port << " chunk: " << index << std::endl; 

    //create socket
    if((sock_fd = socket(AF_INET, SOCK_STREAM , 0)) < 0){
        perror("Failed to create socket"); 
        exit(0);
    }

    //connect to server
    if(connect(sock_fd,(struct sockaddr*) &peer_address, sizeof(peer_address)) < 0){
        perror("Failed to connect"); 
        exit(0);
    }

    send(sock_fd, &file_name, sizeof(file_name), 0);
    send(sock_fd, &index, sizeof(index), 0);
    recv(sock_fd, &buffer, sizeof(buffer), 0);

    close(sock_fd);

    /*TODO
        WRITE TO A FILE FROM CHUNK's OFFSET.
    */

    std::ofstream download;
    std::stringstream ss;
    std::string index_str;
    int chunk_size = CHUNK_SIZE;

    ss << index;
    ss >> index_str;
    mtx.lock();
    download.open(file_name + index_str);
    //download.seekp(chunk_size * index, std::ios::beg);
    download << buffer;
    download.close();
    mtx.unlock();

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x860, %rsp            # imm = 0x860
movq %rsi, -0x850(%rbp)
movq %rcx, -0x838(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
callq 0x30a0
movq -0x838(%rbp), %rdi
cltq
movq %rax, -0x848(%rbp)
addq $0x8, %rdi
callq 0x62b0
movq %rax, %rcx
movq -0x848(%rbp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0x444(%rbp)
leaq -0x440(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x31a0
movq -0x838(%rbp), %rdi
movw $0x2, -0x440(%rbp)
addq $0x8, %rdi
movslq -0x444(%rbp), %rsi
callq 0x63a0
movq -0x838(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x43c(%rbp)
addq $0x8, %rdi
movslq -0x444(%rbp), %rsi
callq 0x63a0
movzwl 0x4(%rax), %edi
callq 0x3050
movw %ax, -0x43e(%rbp)
movq 0xf88a(%rip), %rdi      # 0x13fd0
leaq 0x8940(%rip), %rsi      # 0xd08d
callq 0x32d0
movq %rax, %rdi
movq 0xf85c(%rip), %rsi      # 0x13fb8
callq 0x3310
movq 0xf868(%rip), %rdi      # 0x13fd0
leaq 0x8923(%rip), %rsi      # 0xd092
callq 0x32d0
movq -0x838(%rbp), %rdi
movq %rax, -0x840(%rbp)
addq $0x8, %rdi
movslq -0x444(%rbp), %rsi
callq 0x63a0
movl (%rax), %eax
movl %eax, -0x448(%rbp)
movl -0x448(%rbp), %edi
callq 0x3140
movq -0x840(%rbp), %rdi
movq %rax, %rsi
callq 0x32d0
movq %rax, %rdi
leaq 0x8974(%rip), %rsi      # 0xd132
callq 0x32d0
movq -0x838(%rbp), %rdi
movq %rax, -0x830(%rbp)
addq $0x8, %rdi
movslq -0x444(%rbp), %rsi
callq 0x63a0
movq -0x830(%rbp), %rdi
movzwl 0x4(%rax), %esi
callq 0x3450
movq %rax, %rdi
leaq 0x889e(%rip), %rsi      # 0xd099
callq 0x32d0
movq %rax, %rdi
movl -0x14(%rbp), %esi
callq 0x3480
movq %rax, %rdi
movq 0xf7a3(%rip), %rsi      # 0x13fb8
callq 0x3310
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3080
movl %eax, -0x24(%rbp)
cmpl $0x0, %eax
jge 0x4846
leaq 0x87e1(%rip), %rdi      # 0xd01b
callq 0x3250
xorl %edi, %edi
callq 0x3340
movl -0x24(%rbp), %edi
leaq -0x440(%rbp), %rsi
movl $0x10, %edx
callq 0x3240
cmpl $0x0, %eax
jge 0x4872
leaq 0x87cd(%rip), %rdi      # 0xd033
callq 0x3250
xorl %edi, %edi
callq 0x3340
movq -0x850(%rbp), %rsi
movl -0x24(%rbp), %edi
movl $0x20, %edx
xorl %ecx, %ecx
movl %ecx, -0x854(%rbp)
callq 0x3180
movl -0x854(%rbp), %ecx
movl -0x24(%rbp), %edi
leaq -0x14(%rbp), %rsi
movl $0x4, %edx
callq 0x3180
movl -0x854(%rbp), %ecx
movl -0x24(%rbp), %edi
leaq -0x430(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x30f0
movl -0x24(%rbp), %edi
callq 0x3510
leaq -0x648(%rbp), %rdi
callq 0x3470
leaq -0x7d0(%rbp), %rdi
callq 0x3200
jmp 0x48e1
leaq -0x800(%rbp), %rdi
callq 0x33c0
movl $0x400, -0x804(%rbp)    # imm = 0x400
leaq -0x7c0(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x3480
jmp 0x4908
leaq -0x7d0(%rbp), %rdi
leaq -0x800(%rbp), %rsi
callq 0x3380
jmp 0x491d
leaq 0xf994(%rip), %rdi      # 0x142b8
callq 0x63c0
jmp 0x492b
movq -0x850(%rbp), %rsi
leaq -0x828(%rbp), %rdi
leaq -0x800(%rbp), %rdx
callq 0x63f0
jmp 0x4947
leaq -0x648(%rbp), %rdi
leaq -0x828(%rbp), %rsi
movl $0x10, %edx
callq 0x3170
jmp 0x4961
leaq -0x828(%rbp), %rdi
callq 0x31d0
leaq -0x648(%rbp), %rdi
leaq -0x430(%rbp), %rsi
callq 0x32d0
jmp 0x4982
leaq -0x648(%rbp), %rdi
callq 0x33d0
jmp 0x4990
leaq 0xf921(%rip), %rdi      # 0x142b8
callq 0x6470
jmp 0x499e
leaq -0x800(%rbp), %rdi
callq 0x31d0
leaq -0x7d0(%rbp), %rdi
callq 0x3210
leaq -0x648(%rbp), %rdi
callq 0x3400
xorl %eax, %eax
addq $0x860, %rsp            # imm = 0x860
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x7d8(%rbp)
movl %eax, -0x7dc(%rbp)
jmp 0x4a2b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x7d8(%rbp)
movl %eax, -0x7dc(%rbp)
jmp 0x4a13
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x7d8(%rbp)
movl %eax, -0x7dc(%rbp)
leaq -0x828(%rbp), %rdi
callq 0x31d0
leaq -0x800(%rbp), %rdi
callq 0x31d0
leaq -0x7d0(%rbp), %rdi
callq 0x3210
leaq -0x648(%rbp), %rdi
callq 0x3400
movq -0x7d8(%rbp), %rdi
callq 0x34a0
nopw %cs:(%rax,%rax)