// File: /workspace/github_cpp_makefile_star2_O3/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 $0x7e8, %rsp            # imm = 0x7E8
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %rbx
movl %edx, 0x4(%rsp)
callq 0x3090
cltq
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
xorl %edx, %edx
divq %rsi
movq $0x0, 0x10(%rsp)
movw $0x2, 0x8(%rsp)
movslq %edx, %r13
movl (%rcx,%r13,8), %eax
movl %eax, 0xc(%rsp)
movzwl 0x4(%rcx,%r13,8), %eax
rolw $0x8, %ax
movw %ax, 0xa(%rsp)
movq 0x69f3(%rip), %r15      # 0xafd0
leaq 0x3abe(%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 0x3a88(%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 0x465d
movq %rax, %r12
movq %rax, %rdi
callq 0x3120
movq 0x6980(%rip), %rdi      # 0xafd0
movq %r12, %rsi
movq %rax, %rdx
callq 0x3330
jmp 0x4675
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x3440
movq 0x6954(%rip), %r15      # 0xafd0
leaq 0x3ac4(%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 0x3a02(%rip), %rsi      # 0x80ae
movl $0x8, %edx
movq %rax, %rdi
callq 0x3330
movq %r14, %rdi
movl %ebp, %esi
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 0x48c0
movl %eax, %ebp
leaq 0x8(%rsp), %rsi
movl %eax, %edi
movl $0x10, %edx
callq 0x3240
testl %eax, %eax
js 0x48c5
movl $0x20, %edx
movl %ebp, %edi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x3160
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x3160
leaq 0x3e0(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x30e0
movl %ebp, %edi
callq 0x3500
leaq 0x1e0(%rsp), %rdi
callq 0x3450
leaq 0x58(%rsp), %rdi
callq 0x31e0
leaq 0x28(%rsp), %r14
movq %r14, -0x10(%r14)
movq $0x0, -0x8(%r14)
movb $0x0, (%r14)
leaq 0x68(%rsp), %rdi
movl 0x4(%rsp), %esi
callq 0x3460
leaq 0x58(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x3370
leaq 0x6afe(%rip), %rdi      # 0xb2b0
callq 0x33c0
testl %eax, %eax
jne 0x48ca
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rdx
movq %rbx, %rsi
callq 0x5dac
leaq 0x1e0(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movl $0x10, %edx
callq 0x3150
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4803
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3300
leaq 0x3e0(%rsp), %rbx
movq %rbx, %rdi
callq 0x3120
leaq 0x1e0(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x3330
leaq 0x1e0(%rsp), %rdi
callq 0x3390
leaq 0x6a76(%rip), %rdi      # 0xb2b0
callq 0x3210
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x4856
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3300
movq 0x674b(%rip), %rsi      # 0xafa8
leaq 0x58(%rsp), %rdi
callq 0x3200
leaq 0xd8(%rsp), %rdi
callq 0x30f0
movq 0x6745(%rip), %rax      # 0xafc0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x1e8(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x1e0(%rsp,%rcx)
callq 0x3260
leaq 0x2d8(%rsp), %rdi
callq 0x30f0
xorl %eax, %eax
addq $0x7e8, %rsp            # imm = 0x7E8
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 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x48fb
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3300
jmp 0x48fb
jmp 0x48f8
movq %rax, %rbx
jmp 0x4930
movq %rax, %rbx
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x4912
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3300
movq 0x668f(%rip), %rsi      # 0xafa8
leaq 0x58(%rsp), %rdi
callq 0x3200
leaq 0xd8(%rsp), %rdi
callq 0x30f0
leaq 0x1e0(%rsp), %rdi
callq 0x33d0
movq %rbx, %rdi
callq 0x3480
nop