// File: /workspace/github_cpp_makefile_star2_O1/bchu7796[P]p2p-file-sharing/peers.cpp
// Function: client::download(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int client::download(std::string file_name){
    client myclient;
    std::vector<chunk> chunks;
    std::vector<int> indexes;
    int file_length;

    location_request(file_name, chunks, file_length);
    read_log(file_name, chunks, file_length);

    //pick four chunk to download.
    indexes = select_chunk(chunks);
    
    while(!indexes.empty()){
        for(int i=0; i < indexes.size(); i++){
            //download_file(file_name, indexes[i], chunks[indexes[i]]);
            std::thread download_thread(&client::download_file, myclient,file_name, indexes[i], chunks[indexes[i]]);
            download_thread.detach();
            write_log(file_name, chunks);
            chunks[indexes[i]].is_possessed = true;
            register_chunk(file_name, indexes[i]);
        }
        indexes  = select_chunk(chunks);
    }

    for(int i = 0; i < chunks.size(); i++){
        std::cout<<"chunk"<<i<<":"<<std::endl;
        for(int j = 0; j < chunks[i].peers.size(); j++){
            std::cout<<"ip:"<<inet_ntoa(chunks[i].peers[j].address)<<":"<<chunks[i].peers[j].port<<std::endl;
        }
    }

    combine_file(file_name, chunks.size());

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rsi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
leaq 0x100(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rsi), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
movq $0x0, 0x8(%rsp)
leaq 0xf0(%rsp), %rdi
movq $0x0, (%rsp)
callq 0x620c
leaq 0xf0(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0xac(%rsp), %rcx
callq 0x3b9e
movq 0xf0(%rsp), %rdi
cmpq %r14, %rdi
je 0x4e88
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x3300
leaq 0xe0(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
movq $0x0, 0x8(%rsp)
leaq 0xd0(%rsp), %rdi
movq $0x0, (%rsp)
callq 0x620c
leaq 0xd0(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x40dc
movq 0xd0(%rsp), %rdi
cmpq %r14, %rdi
je 0x4eeb
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x3300
leaq 0x110(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x5de2
leaq 0x40(%rsp), %rdi
leaq 0x110(%rsp), %rdx
callq 0x4474
movq 0x40(%rsp), %r13
movq 0x48(%rsp), %r14
movq 0x50(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x5e6c
cmpq %r14, %r13
je 0x5133
movq %r15, (%rsp)
leaq 0x140(%rsp), %r12
leaq 0x88(%rsp), %r15
movq %r13, 0x10(%rsp)
subq %r13, %r14
je 0x5098
sarq $0x2, %r14
cmpq $0x1, %r14
adcq $0x0, %r14
movq 0x10(%rsp), %rbp
leaq -0xa0f(%rip), %rax      # 0x4570
movq %rax, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movslq (%rbp), %r9
shlq $0x5, %r9
addq 0x20(%rsp), %r9
leaq 0x60(%rsp), %r13
movq %r13, %rdi
leaq 0x40(%rsp), %rsi
leaq 0x1f(%rsp), %rdx
movq %rbx, %rcx
movq %rbp, %r8
callq 0x5eba
movq %r13, %rdi
callq 0x3470
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x68(%rsp), %rdi
callq 0x620c
movq %r12, %rdi
leaq 0x20(%rsp), %rsi
callq 0x5de2
leaq 0x68(%rsp), %rsi
movq %r12, %rdx
callq 0x3f88
movq %r12, %rdi
callq 0x5e6c
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x501b
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3300
movslq (%rbp), %rax
movq 0x20(%rsp), %rcx
shlq $0x5, %rax
movb $0x1, 0x4(%rcx,%rax)
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x620c
movl (%rbp), %edx
movq %r15, %rsi
callq 0x4c2c
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x507f
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3300
cmpq $0x0, 0x60(%rsp)
jne 0x546c
addq $0x4, %rbp
decq %r14
jne 0x4f78
leaq 0x128(%rsp), %r14
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x5de2
leaq 0x40(%rsp), %rdi
movq %r14, %rdx
callq 0x4474
movq 0x40(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x48(%rsp), %r14
movq 0x50(%rsp), %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x50f5
movq (%rsp), %rsi
subq %rdi, %rsi
callq 0x3300
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x510c
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3300
leaq 0x128(%rsp), %rdi
callq 0x5e6c
movq %rbp, %rax
movq %rbp, (%rsp)
movq 0x8(%rsp), %rbp
movq %rbp, %r13
cmpq %r14, %rbp
jne 0x4f59
jmp 0x5139
movq %r13, %rbp
movq %r15, %rax
movq %rax, (%rsp)
movq %rbp, 0x8(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x52cd
xorl %r12d, %r12d
movq 0x5e74(%rip), %r13      # 0xafd0
leaq 0x2fe4(%rip), %rbp      # 0x8147
movl $0x5, %edx
movq %r13, %rdi
leaq 0x2f5d(%rip), %rsi      # 0x80cf
callq 0x3330
movq %r13, %rdi
movl %r12d, %esi
callq 0x3460
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3330
movq (%r14), %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
movq 0x20(%rsp), %rax
movq %r12, 0x10(%rsp)
shlq $0x5, %r12
movq 0x10(%rax,%r12), %rcx
cmpq 0x8(%rax,%r12), %rcx
je 0x52ae
xorl %r15d, %r15d
movl $0x3, %edx
movq %r13, %rdi
leaq 0x2ee9(%rip), %rsi      # 0x80d5
callq 0x3330
movq 0x20(%rsp), %rax
movq 0x8(%rax,%r12), %rax
movl (%rax,%r15,8), %edi
callq 0x3130
testq %rax, %rax
je 0x5224
movq %rax, %r14
movq %rax, %rdi
callq 0x3120
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x3330
jmp 0x523d
movq (%r13), %rax
movq -0x18(%rax), %rax
leaq (%rax,%r13), %rdi
movl 0x20(%r13,%rax), %esi
orl $0x1, %esi
callq 0x3440
movl $0x1, %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3330
movq 0x20(%rsp), %rax
movq 0x8(%rax,%r12), %rax
movzwl 0x4(%rax,%r15,8), %esi
movq %r13, %rdi
callq 0x31c0
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
incq %r15
movq 0x20(%rsp), %rax
movq 0x10(%rax,%r12), %rcx
subq 0x8(%rax,%r12), %rcx
sarq $0x3, %rcx
cmpq %r15, %rcx
ja 0x51dd
movq 0x10(%rsp), %r12
incq %r12
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
sarq $0x5, %rax
cmpq %r12, %rax
ja 0x5163
leaq 0xc0(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0xb0(%rsp), %rdi
callq 0x620c
movq 0x28(%rsp), %rdx
subq 0x20(%rsp), %rdx
shrq $0x5, %rdx
leaq 0xb0(%rsp), %rsi
callq 0x4940
movq 0xb0(%rsp), %rdi
cmpq %r14, %rdi
je 0x5328
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3300
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x533e
movq (%rsp), %rsi
subq %rdi, %rsi
callq 0x3300
leaq 0x20(%rsp), %rdi
callq 0x5e6c
xorl %eax, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
cmpq %r14, %rdi
je 0x5474
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3300
jmp 0x5474
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x5e6c
jmp 0x53d6
movq %rax, %rbx
jmp 0x53d6
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
cmpq %r14, %rdi
je 0x53d6
movq 0xe0(%rsp), %rsi
jmp 0x53ce
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %r14, %rdi
je 0x53d6
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x3300
xorl %edi, %edi
movq $0x0, (%rsp)
jmp 0x5479
jmp 0x5471
jmp 0x5400
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x5e6c
jmp 0x5465
jmp 0x5471
movq %rax, %rbx
jmp 0x5465
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x5e6c
jmp 0x543c
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x545d
movq 0x98(%rsp), %rsi
jmp 0x5450
movq %rax, %rbx
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x545d
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3300
jmp 0x545d
movq %rax, %rbx
cmpq $0x0, 0x60(%rsp)
jne 0x546c
movq 0x10(%rsp), %rdi
jmp 0x5479
callq 0x30a0
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x548a
movq (%rsp), %rsi
subq %rdi, %rsi
callq 0x3300
leaq 0x20(%rsp), %rdi
callq 0x5e6c
movq %rbx, %rdi
callq 0x3480