// File: /workspace/github_cpp_makefile_star2_O2/MisakiFx[P]P2PDownload/./client.hpp
// Function: P2PClient::DownloadFIle(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
bool DownloadFIle(std::string& name)
    {
      std::string host = _online_list[_host_idx];
      int64_t fsize = GetFileSize(host.c_str(), name);
      if(fsize < 0)
      {
        std::cerr << "download file " << name << " failed" << std::endl;
        return false;
      }
      int count = fsize / RANGE_SIZE;
      std::vector<boost::thread> thr_list(count + 1);
      std::vector<int> res_list(count + 1);
      int ret = true;
      int64_t rangeSize = ((int64_t)2 << 30);
      for(int64_t i = 0; i <= count; i++)
      {
        int64_t start, end, rlen;
        start = i * RANGE_SIZE;
        end = (i + 1) * RANGE_SIZE - 1;;
        if(i == count)
        {
          if(fsize % RANGE_SIZE == 0)
          {
            break;
          }
          end = fsize - 1;
        }
        rlen = end - start + 1;
        int* res = &res_list[i];
        boost::thread thr(&P2PClient::RangeDownload, this, host, name, start, end, res); 
        if(fsize >= rangeSize)
        {
          thr.join();
          if(*res == 0)
          {
            ret = false;
          }
        }
        else 
        {
          thr_list[i] = std::move(thr);
        }
      }
      if(fsize < rangeSize)
      {
        for(int i = 0; i <= count; i++)
        {
          if(i == count  && (fsize % RANGE_SIZE) == 0)
          {
            break;
          }
          thr_list[i].join();
          if(res_list[i] == 0)
          {
            ret = false;
          }
        }
      }
      if(ret == true)
      {
        std::cout << "download file " << name << " success" << std::endl;
        return true;
      }
      else
      {
        std::cout << "download file " << name << " false" << std::endl;
        return false;
      }
   //   Client client(_online_list[_host_idx].c_str(), _srv_port);
   //   std::string uri = "/list/" + name;
   //   auto rsp = client.Head(uri.c_str());
   //   if(rsp && rsp->status == 200)
   //   {
   //     //std::string realpath = "Shared/" + name;
   //     //std::ofstream file(realpath, std::ios::binary);
   //     ////打开失败
   //     //if(!file.is_open())
   //     //{
   //     //  std::cerr << "file " << realpath << " open failed" << std::endl;
   //     //  return false;
   //     //}
   //     ////全部写入文件中
   //     //file.write(&rsp->body[0], rsp->body.size());
   //     ////写入失败
   //     //if(!file.good())
   //     //{
   //     //  std::cerr << "file " << realpath << " write body error" << std::endl;
   //     //  return false;
   //     //}
   //     //file.close();
   //     //std::cout << "file " << realpath << " download success" << std::endl;
   //     if(rsp->has_header("Content-Length"))
   //     {
   //       //表示可以获取文件长度进行分块下载
   //       std::string len = rsp->get_header_value("Content-Length");
   //       int64_t content_len;
   //       std::stringstream tmp;
   //       tmp << len;
   //       tmp >> content_len;
   //       int count = content_len / RANGE_SIZE;
   //       std::vector<std::thread> thr_list(count + 1);
   //       std::vector<Download> res_list(count + 1);
   //       for(int i = 0; i <= count; i++)
   //       {
   //         int64_t start = i * RANGE_SIZE;
   //         int64_t end = (i + 1) * RANGE_SIZE;
   //         if(i == count)
   //         {
   //           if(content_len % RANGE_SIZE == 0)
   //           {
   //             break;
   //           }
   //           end = content_len - 1;
   //         }
   //         int64_t len = end - start + 1;
   //         res_list[i].SetData(host, name, start, end, len);
   //         std::thread thr(&P2PClient::RangeDownload, this, &res_list[i]); 
   //         thr_list[i] = std::move(thr);
   //       }
   //       bool ret = true;
   //       for(int i = 0; i <= count; i++)
   //       {
   //         thr_list[i].join();
   //         if(res_list[i]._res == true)
   //         {
   //           continue;
   //         }
   //         ret = false;
   //       }
   //       if(ret == false)
   //       {
   //         std::cerr << "download file " << name << " failed" << std::endl;;
   //         return false;
   //       }
   //     }
   //     else 
   //     {
   //       //无法进行分块下载
   //       //1.正常下载（有风险）
   //       //2.报错不支持
   //       std::cerr << "download file " << name << " failed" << std::endl;;
   //       return false;
   //     }
   //   }
   //   else
   //   {
   //     std::cerr << "file " << name << " download failed" << std::endl;
   //     return false;
   //   }
   //   std::cout << "download file " << name << " success" << std::endl;
   //   return true;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rsi, %r14
movq %rdi, %r15
movslq (%rdi), %rsi
shlq $0x5, %rsi
addq 0x8(%rdi), %rsi
leaq -0x138(%rbp), %rbx
movq %rbx, %rdi
callq 0x87b0
movq (%rbx), %rsi
leaq -0x118(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
callq 0x81d0
leaq -0x118(%rbp), %rsi
movq %r15, %rdi
movq %r14, %rdx
callq 0x18a26
movq %rax, %r12
leaq -0x118(%rbp), %rdi
callq 0x8aa8
testq %r12, %r12
js 0x15b9c
movl $0x6400000, %ecx        # imm = 0x6400000
movq %r12, %rax
xorl %edx, %edx
divq %rcx
movq %rdx, -0x60(%rbp)
movq %rax, -0x68(%rbp)
shlq $0x20, %rax
movabsq $0x100000000, %rbx      # imm = 0x100000000
movq %rax, %r13
addq %rax, %rbx
sarq $0x20, %rbx
leaq -0xc8(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
movq %rbx, %rsi
callq 0x18b78
movq %r12, -0x38(%rbp)
movq %r15, -0x78(%rbp)
movq %r14, -0x40(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xd8(%rbp), %rdx
movq %rbx, %rsi
callq 0x18bca
sarq $0x20, %r13
pushq $-0x1
popq %rax
movq %r13, -0x88(%rbp)
cmovnsq %r13, %rax
incq %rax
movq %rax, -0x80(%rbp)
pushq $0x1
popq %rax
movl %eax, -0x2c(%rbp)
xorl %edx, %edx
leaq -0xf8(%rbp), %rdi
leaq -0x158(%rbp), %r12
leaq -0xd8(%rbp), %r14
movq -0x38(%rbp), %rax
shrq $0x1f, %rax
movq %rax, -0x70(%rbp)
movq $0x0, -0x58(%rbp)
xorl %ebx, %ebx
xorl %r15d, %r15d
cmpq %r15, -0x80(%rbp)
movq -0x88(%rbp), %rcx
je 0x15b0d
movq %rcx, %rax
xorq %r15, %rax
orq -0x60(%rbp), %rax
je 0x15b0d
movq %rbx, -0x50(%rbp)
movq %rdx, %rbx
leaq 0x6400000(%rdx), %rax
movq %r15, -0x48(%rbp)
cmpq %r15, %rcx
movq -0x38(%rbp), %r13
movq %rax, -0x98(%rbp)
cmovneq %rax, %r13
movq -0xb0(%rbp), %r15
leaq -0x138(%rbp), %rsi
callq 0x87b0
movq %r12, %rdi
movq -0x40(%rbp), %rsi
callq 0x87b0
decq %r13
movq -0x58(%rbp), %rax
movq %r15, -0x90(%rbp)
addq %r15, %rax
subq $0x8, %rsp
movq %r14, %r15
movq %r14, %rdi
leaq 0x31be(%rip), %rsi      # 0x18c1c
xorl %edx, %edx
movq -0x78(%rbp), %rcx
leaq -0xf8(%rbp), %r14
movq %r14, %r8
movq %r12, %r9
pushq %rax
pushq %r13
pushq %rbx
callq 0x18f9e
addq $0x20, %rsp
movq %r12, %r13
movq %r12, %rdi
callq 0x8aa8
movq %r14, %rdi
callq 0x8aa8
cmpq $0x0, -0x70(%rbp)
je 0x15ac6
movq %r15, %r14
movq %r15, %rdi
callq 0x19092
movq -0x48(%rbp), %r15
movq -0x90(%rbp), %rax
movl (%rax,%r15,4), %eax
testl %eax, %eax
movl -0x2c(%rbp), %ecx
cmovel %eax, %ecx
movl %ecx, -0x2c(%rbp)
movq %r13, %r12
movq -0x50(%rbp), %rbx
jmp 0x15ae6
movq -0xc8(%rbp), %rdi
movq -0x50(%rbp), %rbx
addq %rbx, %rdi
movq %r15, %r14
movq %r15, %rsi
callq 0x190f8
movq %r13, %r12
movq -0x48(%rbp), %r15
movq %r14, %rdi
callq 0x1912e
addq $0x10, %rbx
addq $0x4, -0x58(%rbp)
incq %r15
movq -0x98(%rbp), %rdx
leaq -0xf8(%rbp), %rdi
jmp 0x159d8
cmpq $0x7fffffff, -0x38(%rbp) # imm = 0x7FFFFFFF
jg 0x15b68
movslq -0x68(%rbp), %rbx
movl %ebx, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r14, %rax
xorq %r12, %rax
orq -0x60(%rbp), %rax
sete %al
cmpq %rbx, %r12
jg 0x15b68
testb %al, %al
jne 0x15b68
movq -0xc8(%rbp), %rdi
addq %r15, %rdi
callq 0x19092
movq -0xb0(%rbp), %rax
movl (%rax,%r12,4), %eax
testl %eax, %eax
movl -0x2c(%rbp), %ecx
cmovel %eax, %ecx
movl %ecx, -0x2c(%rbp)
incq %r12
addq $0x10, %r15
jmp 0x15b24
cmpl $0x1, -0x2c(%rbp)
sete %bl
movq -0x40(%rbp), %r14
jne 0x15bd5
movq 0x2241c(%rip), %rdi     # 0x37f98
leaq 0xf22f(%rip), %rsi      # 0x24db2
callq 0x8530
movq %rax, %rdi
movq %r14, %rsi
callq 0x84b0
leaq 0xf13b(%rip), %rsi      # 0x24cd5
jmp 0x15bfa
movq 0x22445(%rip), %rdi     # 0x37fe8
leaq 0xf208(%rip), %rsi      # 0x24db2
callq 0x8530
movq %rax, %rdi
movq %r14, %rsi
callq 0x84b0
leaq 0xf200(%rip), %rsi      # 0x24dc1
movq %rax, %rdi
callq 0x8530
movq %rax, %rdi
callq 0x8250
xorl %ebx, %ebx
jmp 0x15c22
movq 0x223bc(%rip), %rdi     # 0x37f98
leaq 0xf1cf(%rip), %rsi      # 0x24db2
callq 0x8530
movq %rax, %rdi
movq %r14, %rsi
callq 0x84b0
leaq 0xf1cf(%rip), %rsi      # 0x24dc9
movq %rax, %rdi
callq 0x8530
movq %rax, %rdi
callq 0x8250
leaq -0xb0(%rbp), %rdi
callq 0x195da
leaq -0xc8(%rbp), %rdi
callq 0x19156
leaq -0x138(%rbp), %rdi
callq 0x8aa8
movl %ebx, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x15ca7
jmp 0x15c5c
movq %rax, %rbx
leaq -0x118(%rbp), %rdi
callq 0x8aa8
jmp 0x15cb3
jmp 0x15c5c
movq %rax, %rbx
jmp 0x15cb3
jmp 0x15c98
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x1912e
jmp 0x15c9b
movq %rax, %rbx
leaq -0x158(%rbp), %rdi
callq 0x8aa8
jmp 0x15c88
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x8aa8
jmp 0x15c9b
jmp 0x15c98
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x195da
leaq -0xc8(%rbp), %rdi
callq 0x19156
leaq -0x138(%rbp), %rdi
callq 0x8aa8
movq %rbx, %rdi
callq 0x8990
nop