// File: /workspace/github_cpp_makefile_star2_O3/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, 0x18(%rsp)
leaq 0xf0(%rsp), %rdi
movq $0x0, (%rsp)
callq 0x6204
leaq 0xf0(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0xac(%rsp), %rcx
callq 0x3ba0
movq 0xf0(%rsp), %rdi
cmpq %r14, %rdi
je 0x4e8e
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, 0x18(%rsp)
leaq 0xd0(%rsp), %rdi
movq $0x0, (%rsp)
callq 0x6204
leaq 0xd0(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x40e4
movq 0xd0(%rsp), %rdi
cmpq %r14, %rdi
je 0x4ef1
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x3300
leaq 0x110(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x5e06
leaq 0x40(%rsp), %rdi
leaq 0x110(%rsp), %rdx
callq 0x4472
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %r14
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x5e90
movq %r15, %rax
cmpq %r14, %r15
je 0x5137
leaq 0x140(%rsp), %r12
leaq 0x88(%rsp), %rbp
movq %rax, %r15
movq (%rsp), %rax
movq %rax, 0x58(%rsp)
movq %r15, 0x8(%rsp)
subq %r15, %r14
je 0x50ab
sarq $0x2, %r14
cmpq $0x1, %r14
adcq $0x0, %r14
movq 0x8(%rsp), %r15
leaq -0xa20(%rip), %rax      # 0x4574
movq %rax, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movslq (%r15), %r9
shlq $0x5, %r9
addq 0x20(%rsp), %r9
leaq 0x60(%rsp), %r13
movq %r13, %rdi
leaq 0x40(%rsp), %rsi
leaq 0x17(%rsp), %rdx
movq %rbx, %rcx
movq %r15, %r8
callq 0x5ee4
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 0x6204
movq %r12, %rdi
leaq 0x20(%rsp), %rsi
callq 0x5e06
leaq 0x68(%rsp), %rsi
movq %r12, %rdx
callq 0x3f8a
movq %r12, %rdi
callq 0x5e90
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x502f
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3300
movslq (%r15), %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 %rbp, %rdi
callq 0x6204
movl (%r15), %edx
movq %rbp, %rsi
callq 0x4c32
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x5092
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3300
cmpq $0x0, 0x60(%rsp)
jne 0x5481
addq $0x4, %r15
decq %r14
jne 0x4f8d
leaq 0x128(%rsp), %r14
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x5e06
leaq 0x40(%rsp), %rdi
movq %r14, %rdx
callq 0x4472
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %r14
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x511f
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x3300
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x511f
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3300
leaq 0x128(%rsp), %rdi
callq 0x5e90
cmpq %r14, %r15
jne 0x4f65
jmp 0x513a
movq %rax, %r15
movq %r15, 0x18(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x52c8
xorl %r12d, %r12d
movq 0x5e77(%rip), %r13      # 0xafd0
leaq 0x2fe7(%rip), %rbp      # 0x8147
movl $0x5, %edx
movq %r13, %rdi
leaq 0x2f60(%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, 0x8(%rsp)
shlq $0x5, %r12
movq 0x10(%rax,%r12), %rcx
cmpq 0x8(%rax,%r12), %rcx
je 0x52ab
xorl %r15d, %r15d
movl $0x3, %edx
movq %r13, %rdi
leaq 0x2eec(%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 0x5221
movq %rax, %r14
movq %rax, %rdi
callq 0x3120
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x3330
jmp 0x523a
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 0x51da
movq 0x8(%rsp), %r12
incq %r12
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
cmpq %r12, %rcx
ja 0x5160
leaq 0xc0(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0xb0(%rsp), %rdi
callq 0x6204
movq 0x28(%rsp), %rdx
subq 0x20(%rsp), %rdx
shrq $0x5, %rdx
leaq 0xb0(%rsp), %rsi
callq 0x4946
movq 0xb0(%rsp), %rdi
cmpq %r14, %rdi
je 0x5323
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3300
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x5339
movq (%rsp), %rsi
subq %rdi, %rsi
callq 0x3300
leaq 0x20(%rsp), %rdi
callq 0x5e90
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 0x5489
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3300
jmp 0x5489
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x5e90
jmp 0x54a2
movq %rax, %rbx
jmp 0x54a2
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
cmpq %r14, %rdi
je 0x54a2
movq 0xe0(%rsp), %rsi
jmp 0x53d7
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %r14, %rdi
je 0x54a2
movq 0x100(%rsp), %rsi
incq %rsi
jmp 0x549d
jmp 0x5486
movq %rax, %rbx
jmp 0x53f9
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x5e90
movq 0x8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rax
movq %rax, (%rsp)
jmp 0x5489
jmp 0x5486
movq %rax, %rbx
jmp 0x5475
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x5e90
jmp 0x544c
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x546d
movq 0x98(%rsp), %rsi
jmp 0x5460
movq %rax, %rbx
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x546d
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3300
jmp 0x546d
movq %rax, %rbx
cmpq $0x0, 0x60(%rsp)
jne 0x5481
movq 0x58(%rsp), %rsi
movq 0x8(%rsp), %rdi
jmp 0x549a
callq 0x30a0
movq %rax, %rbx
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x54a2
movq %rax, %rdi
movq (%rsp), %rsi
subq %rdi, %rsi
callq 0x3300
leaq 0x20(%rsp), %rdi
callq 0x5e90
movq %rbx, %rdi
callq 0x3480