// File: 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
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movq %rsi, -0x1b0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1b8(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x6490
leaq -0x48(%rbp), %rdi
callq 0x6290
movq -0x1b0(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x3130
jmp 0x4f29
movq -0x1b8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
callq 0x3bb0
jmp 0x4f43
leaq -0x70(%rbp), %rdi
callq 0x31d0
movq -0x1b0(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3130
jmp 0x4f61
movq -0x1b8(%rbp), %rdi
movl -0x4c(%rbp), %ecx
leaq -0xa0(%rbp), %rsi
leaq -0x30(%rbp), %rdx
callq 0x41f0
jmp 0x4f7d
leaq -0xa0(%rbp), %rdi
callq 0x31d0
leaq -0xd0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x64b0
jmp 0x4f9b
movq -0x1b8(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
callq 0x4520
jmp 0x4fb7
leaq -0x48(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x65a0
leaq -0xb8(%rbp), %rdi
callq 0x6340
leaq -0xd0(%rbp), %rdi
callq 0x65d0
leaq -0x48(%rbp), %rdi
callq 0x6630
xorb $-0x1, %al
testb $0x1, %al
jne 0x4ff3
jmp 0x52e0
movl $0x0, -0xd4(%rbp)
movslq -0xd4(%rbp), %rax
movq %rax, -0x1c0(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x6670
movq %rax, %rcx
movq -0x1c0(%rbp), %rax
cmpq %rcx, %rax
jae 0x5268
leaq -0x9ae(%rip), %rax      # 0x4680
movq %rax, -0xf0(%rbp)
movq $0x0, -0xe8(%rbp)
movslq -0xd4(%rbp), %rsi
leaq -0x48(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
callq 0x6690
movq -0x1d0(%rbp), %rdi
movq %rax, -0x1c8(%rbp)
movslq -0xd4(%rbp), %rsi
callq 0x6690
movslq (%rax), %rsi
leaq -0x30(%rbp), %rdi
callq 0x6020
movq -0x1b0(%rbp), %rcx
movq -0x1c8(%rbp), %r8
movq %rax, %r9
leaq -0xe0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
leaq -0x11(%rbp), %rdx
callq 0x66b0
jmp 0x50a7
leaq -0xe0(%rbp), %rdi
callq 0x3490
jmp 0x50b5
movq -0x1b0(%rbp), %rsi
leaq -0x110(%rbp), %rdi
callq 0x3130
jmp 0x50ca
leaq -0x128(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x64b0
jmp 0x50dc
movq -0x1b8(%rbp), %rdi
leaq -0x110(%rbp), %rsi
leaq -0x128(%rbp), %rdx
callq 0x4050
jmp 0x50f8
leaq -0x128(%rbp), %rdi
callq 0x65d0
leaq -0x110(%rbp), %rdi
callq 0x31d0
movslq -0xd4(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x6690
movslq (%rax), %rsi
leaq -0x30(%rbp), %rdi
callq 0x6020
movq -0x1b0(%rbp), %rsi
movb $0x1, 0x4(%rax)
leaq -0x148(%rbp), %rdi
callq 0x3130
jmp 0x5145
movslq -0xd4(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x6690
movq -0x1b8(%rbp), %rdi
movl (%rax), %edx
leaq -0x148(%rbp), %rsi
callq 0x4d00
jmp 0x516c
leaq -0x148(%rbp), %rdi
callq 0x31d0
leaq -0xe0(%rbp), %rdi
callq 0x6790
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x4ffd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x553c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x31d0
jmp 0x553c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x31d0
jmp 0x553c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x65d0
jmp 0x553c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x5257
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x5231
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x65d0
leaq -0x110(%rbp), %rdi
callq 0x31d0
jmp 0x5257
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x31d0
leaq -0xe0(%rbp), %rdi
callq 0x6790
jmp 0x553c
leaq -0x178(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x64b0
jmp 0x527a
movq -0x1b8(%rbp), %rsi
leaq -0x160(%rbp), %rdi
leaq -0x178(%rbp), %rdx
callq 0x4520
jmp 0x5296
leaq -0x48(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x65a0
leaq -0x160(%rbp), %rdi
callq 0x6340
leaq -0x178(%rbp), %rdi
callq 0x65d0
jmp 0x4fdf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x65d0
jmp 0x553c
movl $0x0, -0x17c(%rbp)
movslq -0x17c(%rbp), %rax
movq %rax, -0x1d8(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x6000
movq %rax, %rcx
movq -0x1d8(%rbp), %rax
cmpq %rcx, %rax
jae 0x54c6
movq 0xecb5(%rip), %rdi      # 0x13fd0
leaq 0x7d98(%rip), %rsi      # 0xd0ba
callq 0x32d0
movq %rax, -0x1e0(%rbp)
jmp 0x5330
movq -0x1e0(%rbp), %rdi
movl -0x17c(%rbp), %esi
callq 0x3480
movq %rax, -0x1e8(%rbp)
jmp 0x534b
movq -0x1e8(%rbp), %rdi
leaq 0x7dd9(%rip), %rsi      # 0xd132
callq 0x32d0
movq %rax, -0x1f0(%rbp)
jmp 0x5367
movq -0x1f0(%rbp), %rdi
movq 0xec43(%rip), %rsi      # 0x13fb8
callq 0x3310
jmp 0x537c
movl $0x0, -0x180(%rbp)
movslq -0x180(%rbp), %rax
movq %rax, -0x1f8(%rbp)
movslq -0x17c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x6020
movq %rax, %rdi
addq $0x8, %rdi
callq 0x62b0
movq %rax, %rcx
movq -0x1f8(%rbp), %rax
cmpq %rcx, %rax
jae 0x54b0
movq 0xec06(%rip), %rdi      # 0x13fd0
leaq 0x7cef(%rip), %rsi      # 0xd0c0
callq 0x32d0
movq %rax, -0x200(%rbp)
jmp 0x53df
movslq -0x17c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x6020
movq %rax, %rdi
addq $0x8, %rdi
movslq -0x180(%rbp), %rsi
callq 0x63a0
movl (%rax), %eax
movl %eax, -0x184(%rbp)
movl -0x184(%rbp), %edi
callq 0x3140
movq -0x200(%rbp), %rdi
movq %rax, %rsi
callq 0x32d0
movq %rax, -0x208(%rbp)
jmp 0x542d
movq -0x208(%rbp), %rdi
leaq 0x7cf7(%rip), %rsi      # 0xd132
callq 0x32d0
movq %rax, -0x210(%rbp)
jmp 0x5449
movslq -0x17c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x6020
movq %rax, %rdi
addq $0x8, %rdi
movslq -0x180(%rbp), %rsi
callq 0x63a0
movq -0x210(%rbp), %rdi
movzwl 0x4(%rax), %esi
callq 0x3450
movq %rax, -0x218(%rbp)
jmp 0x5485
movq -0x218(%rbp), %rdi
movq 0xeb25(%rip), %rsi      # 0x13fb8
callq 0x3310
jmp 0x549a
jmp 0x549c
movl -0x180(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x180(%rbp)
jmp 0x5386
jmp 0x54b2
movl -0x17c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x17c(%rbp)
jmp 0x52ea
movq -0x1b0(%rbp), %rsi
leaq -0x1a8(%rbp), %rdi
callq 0x3130
jmp 0x54db
leaq -0x30(%rbp), %rdi
callq 0x6000
movq -0x1b8(%rbp), %rdi
movl %eax, %edx
leaq -0x1a8(%rbp), %rsi
callq 0x4a50
jmp 0x54fb
leaq -0x1a8(%rbp), %rdi
callq 0x31d0
leaq -0x48(%rbp), %rdi
callq 0x6340
leaq -0x30(%rbp), %rdi
callq 0x65d0
xorl %eax, %eax
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x31d0
leaq -0x48(%rbp), %rdi
callq 0x6340
leaq -0x30(%rbp), %rdi
callq 0x65d0
movq -0x78(%rbp), %rdi
callq 0x34a0
nopw (%rax,%rax)