// File: /workspace/github_cpp_makefile_star2_O1/DimitriFourny[P]unxip/unxip.cpp
// Function: main
int main(int argc, char* argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <file.xip>\n", argv[0]);
    return 1;
  }
  char* filepath = argv[1];

  FILE* file = fopen(filepath, "rb");
  if (!file) {
    fprintf(stderr, "Error: File \"%s\" can't be opened\n", filepath);
    return 1;
  }

  std::string outdir = CreateXipDirectory(filepath);

  // Check the XIP headers
  struct XipHeader header;
  memset(&header, 0, sizeof(header));
  size_t nb_read = 0;
  nb_read = fread(&header, 1, sizeof(header), file);

  if (nb_read < 4 || memcmp(&header.signature, "xar!", 4)) {
    fprintf(stderr, "Invalid XAR signature\n");
    return InvalidXipFile(file);
  }

  // Big endian to little endian
  header.header_size = __bswap_16(header.header_size);
  header.xar_version = __bswap_16(header.xar_version);
  header.toc_size_compressed = __bswap_64(header.toc_size_compressed);
  header.toc_size_uncompressed = __bswap_64(header.toc_size_uncompressed);
  header.checksum_algo = __bswap_32(header.checksum_algo);

  if (fseek(file, header.header_size, SEEK_SET)) {
    fprintf(stderr, "Header size is invalid\n");
    return InvalidXipFile(file);
  }

  if (!header.toc_size_compressed ||
      header.toc_size_compressed > MAX_TOC_SIZE ||
      header.toc_size_uncompressed > MAX_TOC_SIZE) {
    fprintf(stderr, "Table of Contents invalid size\n");
    return InvalidXipFile(file);
  }

  // Read the Table of Contents (TOC)
  uint8_t* toc_compressed = new uint8_t[header.toc_size_compressed];
  nb_read = fread(toc_compressed, 1, header.toc_size_compressed, file);
  if (nb_read < header.toc_size_compressed) {
    fprintf(stderr, "Can't read the Table of Contents\n");
    return InvalidXipFile(file);
  }

  const size_t toc_size = header.toc_size_uncompressed;
  uint8_t* toc = new uint8_t[toc_size];
  bool success =
      ZlibUncompress(toc_compressed, header.toc_size_compressed, toc, toc_size);
  if (!success) {
    fprintf(stderr, "Can't uncompress the Table of Contents\n");
    return InvalidXipFile(file);
  }
  delete[] toc_compressed;

  SaveToFile(outdir + "xip_toc.xml", toc, toc_size);

  // Parse the TOC to get the files
  auto files = ParseTableOfContents(toc, toc_size);
  std::string savepath;
  for (const auto& xar_file : files) {
    savepath = outdir + xar_file.name;
    printf("%s\t", savepath.c_str());
    fflush(stdout);

    const size_t offset =
        header.header_size + header.toc_size_compressed + xar_file.offset;
    success = SaveFileStreamToFile(savepath, file, offset, xar_file.size);

    if (success) {
      printf("done\n");
    } else {
      printf("error\n");
    }
  }

  fclose(file);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
cmpl $0x1, %edi
jle 0x2d9e
movq 0x8(%rsi), %rbx
leaq 0x15d0(%rip), %rsi      # 0x40d9
movq %rbx, %rdi
callq 0x2190
testq %rax, %rax
je 0x2da8
movq %rax, %rbp
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x2652
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movups %xmm0, 0xc(%rdi)
movaps %xmm0, (%rdi)
movl $0x1, %esi
movl $0x1c, %edx
movq %rbp, %rcx
callq 0x2210
cmpq $0x4, %rax
jb 0x2d94
cmpl $0x21726178, 0x20(%rsp) # imm = 0x21726178
jne 0x2d94
movzwl 0x24(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x24(%rsp)
rolw $0x8, 0x26(%rsp)
movq 0x28(%rsp), %r14
bswapq %r14
movq %r14, 0x28(%rsp)
movq 0x30(%rsp), %r12
bswapq %r12
movq %r12, 0x30(%rsp)
movl 0x38(%rsp), %ecx
bswapl %ecx
movl %ecx, 0x38(%rsp)
movzwl %ax, %r15d
movq %rbp, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2170
testl %eax, %eax
jne 0x2dca
leaq -0x10000(%r14), %rax
cmpq $-0xffff, %rax          # imm = 0xFFFF0001
jb 0x2dd4
cmpq $0x10000, %r12          # imm = 0x10000
jae 0x2dd4
movq %r14, %rdi
callq 0x2030
movq %rax, %rbx
movl $0x1, %esi
movq %rax, %rdi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x2210
cmpq %r14, %rax
jb 0x2dde
movq %r12, %rdi
callq 0x2030
movq %rax, %r13
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0x25ec
testb %al, %al
je 0x2de8
movq %rbx, %rdi
callq 0x21c0
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x32ac
leaq 0x1550(%rip), %rsi      # 0x419c
movq %rsp, %rdi
callq 0x2270
movq %rsp, %rbx
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x272d
movq (%rbx), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2c7f
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2140
movq %rbp, %rbx
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x29d2
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
movq 0x60(%rsp), %r12
movq 0x68(%rsp), %r13
cmpq %r13, %r12
je 0x2d63
addq %r15, %r14
leaq 0x78(%rsp), %r15
movq %rsp, %rbp
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
movq %r12, %rdx
callq 0x2f8b
movq %rbp, %rdi
movq %r15, %rsi
callq 0x21b0
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x2d04
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2140
movq (%rsp), %rsi
leaq 0x1499(%rip), %rdi      # 0x41a8
xorl %eax, %eax
callq 0x2050
movq 0x32ab(%rip), %rax      # 0x5fc8
movq (%rax), %rdi
callq 0x2180
movq 0x20(%r12), %rdx
addq %r14, %rdx
movq 0x28(%r12), %rcx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x278c
testb %al, %al
leaq 0x14aa(%rip), %rdi      # 0x41f0
leaq 0x14a9(%rip), %rax      # 0x41f6
cmovneq %rax, %rdi
callq 0x2220
addq $0x30, %r12
cmpq %r13, %r12
jne 0x2cc7
movq %rbx, %rdi
callq 0x2120
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2d86
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2140
leaq 0x60(%rsp), %rdi
callq 0x2f30
xorl %ebx, %ebx
jmp 0x2df5
movq %rbp, %rdi
callq 0x247d
jmp 0x2df0
movq %rsi, %rdi
callq 0x24c8
jmp 0x2dc3
movq 0x3231(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
leaq 0x1323(%rip), %rsi      # 0x40dc
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21e0
movl $0x1, %ebx
jmp 0x2e10
movq %rbp, %rdi
callq 0x2351
jmp 0x2df0
movq %rbp, %rdi
callq 0x2432
jmp 0x2df0
movq %rbp, %rdi
callq 0x23e7
jmp 0x2df0
movq %rbp, %rdi
callq 0x239c
movl $0x1, %ebx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2e10
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2140
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2e4c
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2e79
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2140
jmp 0x2e79
jmp 0x2e4c
jmp 0x2e4c
jmp 0x2e4c
movq %rax, %rbx
jmp 0x2e79
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2e6f
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2140
leaq 0x60(%rsp), %rdi
callq 0x2f30
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2e94
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x2240