// File: /workspace/github_cpp_makefile_star2_O1/MarkOates[P]file-downloader/file_downloader.cpp
// Function: FileDownloader::download_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
FileDownloader::FileHandle FileDownloader::download_file(std::string file_url, std::string local_filename)
{
   FileHandle file_handle(file_url, local_filename);
   std::string temp_filename = std::string(local_filename).insert(0, "~");
   CURL *curl = nullptr;
   FILE *file_pointer = nullptr;
   CURLcode res;
   curl = curl_easy_init();

   if (!curl)
   {
      file_handle.status = ERROR;
      file_handle.error = "CURL initialization error";
      return file_handle;
   }

   file_pointer = fopen(temp_filename.c_str(), "wb");
   curl_easy_setopt(curl, CURLOPT_URL, file_url.c_str());
   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, FileDownloader::write_data);
   curl_easy_setopt(curl, CURLOPT_WRITEDATA, file_pointer);
   curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
   curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_function);
   curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &file_handle);

   res = curl_easy_perform(curl);

   if (res == CURLE_OK)
   {
      long http_response_code = 0;
      curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_response_code);

      switch (http_response_code)
      {
      case 200:
         {
            // copy the temp file to the destination
            std::ifstream src(temp_filename, std::ios::binary);
            std::ofstream dst(local_filename, std::ios::binary);
            dst << src.rdbuf();

            file_handle.status = DOWNLOADED;
            file_handle.percentage = 1.0;
         }
         break;
      case 404:
      default:
         file_handle.status = ERROR;
         file_handle.error = std::to_string(http_response_code);
         break;
      }
   }
   else
   {
      file_handle.status = ERROR;
      file_handle.error = curl_easy_strerror(res);
   }

   // erase the temp file
   remove(temp_filename.c_str());

   // cleanup the curl session
   curl_easy_cleanup(curl);
   fclose(file_pointer);

   return file_handle;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x478, %rsp            # imm = 0x478
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
leaq -0x88(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%rsi), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq -0x98(%rbp), %rdi
callq 0x40e6
leaq -0x68(%rbp), %r14
movq %r14, -0x10(%r14)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq -0x78(%rbp), %rdi
callq 0x40e6
leaq -0x98(%rbp), %rsi
leaq -0x78(%rbp), %rdx
movq %rbx, %rdi
callq 0x394c
movq -0x78(%rbp), %rdi
cmpq %r14, %rdi
je 0x3bfd
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x3080
movq -0x98(%rbp), %rdi
cmpq %r15, %rdi
je 0x3c18
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x3080
leaq -0x290(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq -0x2a0(%rbp), %rdi
callq 0x40e6
leaq 0x73c2(%rip), %rcx      # 0xb004
leaq -0x2a0(%rbp), %rdi
movl $0x1, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x30d0
leaq -0x40(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x50(%rbp), %rdi
callq 0x40e6
movq -0x2a0(%rbp), %rdi
cmpq %r15, %rdi
je 0x3c8e
movq -0x290(%rbp), %rsi
incq %rsi
callq 0x3080
callq 0x32d0
movq %rax, %r14
testq %rax, %rax
je 0x3d6d
movq -0x50(%rbp), %rdi
leaq 0x7376(%rip), %rsi      # 0xb020
callq 0x3350
movq %rax, -0x30(%rbp)
movq (%r13), %rdx
movq %r14, %rdi
movl $0x2712, %esi           # imm = 0x2712
xorl %eax, %eax
callq 0x3260
leaq 0x351(%rip), %rdx       # 0x401e
movq %r14, %rdi
movl $0x4e2b, %esi           # imm = 0x4E2B
xorl %eax, %eax
callq 0x3260
movq %r14, %rdi
movl $0x2711, %esi           # imm = 0x2711
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x3260
movq %r14, %rdi
movl $0x2b, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x3260
leaq 0x321(%rip), %rdx       # 0x4028
movq %r14, %rdi
movl $0x4e58, %esi           # imm = 0x4E58
xorl %eax, %eax
callq 0x3260
movq %r14, %rdi
movl $0x2749, %esi           # imm = 0x2749
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3260
movq %r14, %rdi
callq 0x31f0
testl %eax, %eax
je 0x3d95
movl $0x3, 0x4(%rbx)
movl %eax, %edi
callq 0x3170
movq %rax, %r15
leaq 0x58(%rbx), %r12
movq 0x60(%rbx), %r13
movq %rax, %rdi
callq 0x3150
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %r15, %rcx
movq %rax, %r8
callq 0x3180
jmp 0x3f2a
movl $0x3, 0x4(%rbx)
leaq 0x58(%rbx), %rdi
movq 0x60(%rbx), %rdx
leaq 0x7283(%rip), %rcx      # 0xb006
movl $0x19, %r8d
xorl %esi, %esi
callq 0x3180
jmp 0x3f44
leaq -0x58(%rbp), %rdx
movq $0x0, (%rdx)
movq %r14, %rdi
movl $0x200002, %esi         # imm = 0x200002
xorl %eax, %eax
callq 0x32b0
movq -0x58(%rbp), %r13
cmpq $0xc8, %r13
jne 0x3e51
leaq -0x2a0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movl $0x4, %edx
callq 0x32f0
leaq -0x4a0(%rbp), %rdi
movq %r12, %rsi
movl $0x4, %edx
callq 0x30b0
leaq -0x290(%rbp), %rsi
leaq -0x4a0(%rbp), %rdi
callq 0x30e0
movl $0x2, 0x4(%rbx)
movl $0x3f800000, 0xc(%rbx)  # imm = 0x3F800000
movq 0xb1b7(%rip), %rax      # 0xefc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x498(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x4a0(%rbp,%rcx)
callq 0x31b0
leaq -0x3a8(%rbp), %rdi
callq 0x3210
leaq -0x2a0(%rbp), %rdi
callq 0x3030
jmp 0x3f2a
movq %r15, %r8
movl $0x3, 0x4(%rbx)
movq %r13, %r12
negq %r12
cmovsq %r13, %r12
movl $0x1, %r15d
cmpq $0xa, %r12
jb 0x3ec5
movl $0x4, %r15d
movabsq $0x346dc5d63886594b, %rsi # imm = 0x346DC5D63886594B
movq %r12, %rcx
cmpq $0x63, %rcx
jbe 0x3ebc
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x3ec2
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x3ec5
movq %rcx, %rax
mulq %rsi
shrq $0xb, %rdx
addl $0x4, %r15d
cmpq $0x1869f, %rcx          # imm = 0x1869F
movq %rdx, %rcx
ja 0x3e84
addl $-0x3, %r15d
jmp 0x3ec5
addl $-0x2, %r15d
jmp 0x3ec5
decl %r15d
shrq $0x3f, %r13
leal (%r15,%r13), %esi
leaq -0x2a0(%rbp), %rdi
movq %r8, (%rdi)
movl $0x2d, %edx
callq 0x31a0
leaq -0x2a0(%rbp), %rax
addq (%rax), %r13
movq %r13, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x4066
leaq 0x58(%rbx), %rdi
leaq -0x2a0(%rbp), %r15
movq %r15, %rsi
callq 0x3190
movq (%r15), %rdi
leaq -0x290(%rbp), %rax
cmpq %rax, %rdi
je 0x3f2a
movq -0x290(%rbp), %rsi
incq %rsi
callq 0x3080
movq -0x50(%rbp), %rdi
callq 0x30f0
movq %r14, %rdi
callq 0x3090
movq -0x30(%rbp), %rdi
callq 0x3160
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x3f5d
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x3080
movq %rbx, %rax
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
leaq -0x4a0(%rbp), %rdi
callq 0x3230
jmp 0x3f86
movq %rax, %r12
leaq -0x2a0(%rbp), %rdi
callq 0x3030
jmp 0x3ff4
jmp 0x3ff1
jmp 0x3ff1
jmp 0x3ff1
movq %rax, %r12
jmp 0x400d
movq %rax, %r12
movq -0x78(%rbp), %rdi
cmpq %r14, %rdi
je 0x3fbc
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x3080
jmp 0x3fbc
movq %rax, %r12
movq -0x98(%rbp), %rdi
cmpq %r15, %rdi
je 0x4015
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x3080
jmp 0x4015
movq %rax, %r12
movq -0x2a0(%rbp), %rdi
cmpq %r15, %rdi
je 0x400d
movq -0x290(%rbp), %rsi
jmp 0x4005
movq %rax, %r12
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x400d
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x3080
movq %rbx, %rdi
callq 0x3a26
movq %r12, %rdi
callq 0x31d0
nop