// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x7f8, %rsp            # imm = 0x7F8
movq %rcx, %r14
movq %rsi, %rbx
movl %edx, 0xc(%rsp)
callq 0x3090
cltq
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
xorl %edx, %edx
divq %rsi
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movw $0x2, 0x10(%rsp)
movslq %edx, %r13
movl (%rcx,%r13,8), %eax
movl %eax, 0x14(%rsp)
movzwl 0x4(%rcx,%r13,8), %eax
rolw $0x8, %ax
movw %ax, 0x12(%rsp)
movq 0x69fa(%rip), %r15      # 0xafd0
leaq 0x3ac5(%rip), %rsi      # 0x80a2
movl $0x4, %edx
movq %r15, %rdi
callq 0x3330
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31d0
leaq 0x3a8f(%rip), %rsi      # 0x80a7
movl $0x6, %edx
movq %r15, %rdi
callq 0x3330
movq 0x8(%r14), %rax
movl (%rax,%r13,8), %edi
callq 0x3130
testq %rax, %rax
je 0x4656
movq %rax, %r12
movq %rax, %rdi
callq 0x3120
movq 0x6987(%rip), %rdi      # 0xafd0
movq %r12, %rsi
movq %rax, %rdx
callq 0x3330
jmp 0x466e
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x3440
movq 0x695b(%rip), %r15      # 0xafd0
leaq 0x3acb(%rip), %rsi      # 0x8147
movl $0x1, %edx
movq %r15, %rdi
callq 0x3330
movq 0x8(%r14), %rax
movzwl 0x4(%rax,%r13,8), %esi
movq %r15, %rdi
callq 0x31c0
movq %rax, %r14
leaq 0x3a09(%rip), %rsi      # 0x80ae
movl $0x8, %edx
movq %rax, %rdi
callq 0x3330
movl 0xc(%rsp), %esi
movq %r14, %rdi
callq 0x3460
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x32d0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31d0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3070
testl %eax, %eax
js 0x48bb
movl %eax, %ebp
leaq 0x10(%rsp), %rsi
movl %eax, %edi
movl $0x10, %edx
callq 0x3240
testl %eax, %eax
js 0x48c0
movl $0x20, %edx
movl %ebp, %edi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x3160
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x3160
leaq 0x3f0(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x30e0
movl %ebp, %edi
callq 0x3500
leaq 0x1f0(%rsp), %rdi
callq 0x3450
leaq 0x68(%rsp), %rdi
callq 0x31e0
leaq 0x38(%rsp), %r14
movq %r14, -0x10(%r14)
movq $0x0, -0x8(%r14)
movb $0x0, (%r14)
leaq 0x78(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x3460
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x3370
leaq 0x6b03(%rip), %rdi      # 0xb2b0
callq 0x33c0
testl %eax, %eax
jne 0x48c5
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rdx
movq %rbx, %rsi
callq 0x5d87
leaq 0x1f0(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movl $0x10, %edx
callq 0x3150
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x47fe
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3300
leaq 0x3f0(%rsp), %rbx
movq %rbx, %rdi
callq 0x3120
leaq 0x1f0(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x3330
leaq 0x1f0(%rsp), %rdi
callq 0x3390
leaq 0x6a7b(%rip), %rdi      # 0xb2b0
callq 0x3210
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0x4851
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3300
movq 0x6750(%rip), %rsi      # 0xafa8
leaq 0x68(%rsp), %rdi
callq 0x3200
leaq 0xe8(%rsp), %rdi
callq 0x30f0
movq 0x674a(%rip), %rax      # 0xafc0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x1f8(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x1f0(%rsp,%rcx)
callq 0x3260
leaq 0x2e8(%rsp), %rdi
callq 0x30f0
xorl %eax, %eax
addq $0x7f8, %rsp            # imm = 0x7F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x35ac
callq 0x3598
movl %eax, %edi
callq 0x31b0
movq %rax, %rbx
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x48f6
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3300
jmp 0x48f6
jmp 0x48f3
movq %rax, %rbx
jmp 0x492b
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0x490d
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3300
movq 0x6694(%rip), %rsi      # 0xafa8
leaq 0x68(%rsp), %rdi
callq 0x3200
leaq 0xe8(%rsp), %rdi
callq 0x30f0
leaq 0x1f0(%rsp), %rdi
callq 0x33d0
movq %rbx, %rdi
callq 0x3480