// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x7e0, %rsp            # imm = 0x7E0
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %rbx
movl %edx, 0x4(%rsp)
callq 0x3080
cltq
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
xorl %edx, %edx
divq %rsi
andq $0x0, 0x10(%rsp)
movw $0x2, 0x8(%rsp)
movslq %edx, %r12
movl (%rcx,%r12,8), %eax
movl %eax, 0xc(%rsp)
movzwl 0x4(%rcx,%r12,8), %eax
rolw $0x8, %ax
movw %ax, 0xa(%rsp)
movq 0x6f3c(%rip), %r15      # 0xafd0
leaq 0x2ff2(%rip), %rsi      # 0x708d
movq %r15, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3150
leaq 0x2fe0(%rip), %rsi      # 0x7092
movq %r15, %rdi
callq 0x32a0
movq %rax, %r15
movq 0x8(%r14), %rax
movl (%rax,%r12,8), %edi
callq 0x3100
movq %r15, %rdi
movq %rax, %rsi
callq 0x32a0
leaq 0x3056(%rip), %rsi      # 0x7132
movq %rax, %rdi
callq 0x32a0
movq 0x8(%r14), %rcx
movzwl 0x4(%rcx,%r12,8), %esi
movq %rax, %rdi
callq 0x33f0
leaq 0x2f9c(%rip), %rsi      # 0x7099
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
movl %ebp, %esi
callq 0x3420
movq %rax, %rdi
callq 0x3150
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x3060
testl %eax, %eax
js 0x4265
movl %eax, %ebp
leaq 0x8(%rsp), %rsi
pushq $0x10
popq %rdx
movl %eax, %edi
callq 0x3210
testl %eax, %eax
js 0x426e
pushq $0x20
popq %rdx
movl %ebp, %edi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x3160
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x3160
leaq 0x3e0(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x30d0
movl %ebp, %edi
callq 0x34c0
leaq 0x1e0(%rsp), %rdi
callq 0x3410
leaq 0x58(%rsp), %rdi
callq 0x31d0
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x68(%rsp), %rdi
movl 0x4(%rsp), %esi
callq 0x3420
leaq 0x58(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x3330
leaq 0x70c2(%rip), %rdi      # 0xb290
callq 0x511c
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rdx
movq %rbx, %rsi
callq 0x512f
leaq 0x1e0(%rsp), %rdi
leaq 0x38(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x3140
leaq 0x38(%rsp), %rdi
callq 0x3110
leaq 0x1e0(%rsp), %rdi
leaq 0x3e0(%rsp), %rsi
callq 0x32a0
leaq 0x1e0(%rsp), %rdi
callq 0x3370
leaq 0x7063(%rip), %rdi      # 0xb290
callq 0x31f0
leaq 0x18(%rsp), %rdi
callq 0x3110
leaq 0x58(%rsp), %rdi
callq 0x31e0
leaq 0x1e0(%rsp), %rdi
callq 0x33a0
xorl %eax, %eax
addq $0x7e0, %rsp            # imm = 0x7E0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2daf(%rip), %rdi      # 0x701b
jmp 0x4275
leaq 0x2dbe(%rip), %rdi      # 0x7033
callq 0x3220
xorl %edi, %edi
callq 0x3300
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3110
jmp 0x429a
jmp 0x4297
movq %rax, %rbx
jmp 0x42ae
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x3110
leaq 0x58(%rsp), %rdi
callq 0x31e0
leaq 0x1e0(%rsp), %rdi
callq 0x33a0
movq %rbx, %rdi
callq 0x3440
nop