// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
cmpl $0x1, %edi
jle 0x2d91
movq 0x8(%rsi), %rbx
leaq 0x15cf(%rip), %rsi      # 0x40d9
movq %rbx, %rdi
callq 0x2190
testq %rax, %rax
je 0x2d9b
movq %rax, %rbp
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x2652
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rdi
movups %xmm0, 0xc(%rdi)
movaps %xmm0, (%rdi)
movl $0x1, %esi
movl $0x1c, %edx
movq %rbp, %rcx
callq 0x2210
cmpq $0x4, %rax
jb 0x2d87
cmpl $0x21726178, 0x60(%rsp) # imm = 0x21726178
jne 0x2d87
movzwl 0x64(%rsp), %eax
rolw $0x8, %ax
movq 0x68(%rsp), %r14
movq 0x70(%rsp), %r12
movzwl %ax, %r15d
movq %rbp, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2170
testl %eax, %eax
jne 0x2dbd
bswapq %r14
leaq -0x10000(%r14), %rax
cmpq $-0xffff, %rax          # imm = 0xFFFF0001
jb 0x2dc7
bswapq %r12
cmpq $0x10000, %r12          # imm = 0x10000
jae 0x2dc7
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 0x2dd1
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 0x2ddb
movq %rbx, %rdi
callq 0x21c0
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0x327e
leaq 0x156c(%rip), %rsi      # 0x419c
leaq 0x8(%rsp), %rdi
callq 0x2270
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x272d
movq (%rbx), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2c67
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2140
movq %rbp, %rbx
leaq 0x48(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x29d3
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
movq 0x48(%rsp), %r12
movq 0x50(%rsp), %r13
cmpq %r13, %r12
je 0x2d55
addq %r15, %r14
leaq 0x88(%rsp), %r15
leaq 0x8(%rsp), %rbp
movq %r15, %rdi
leaq 0x28(%rsp), %rsi
movq %r12, %rdx
callq 0x2f84
movq %rbp, %rdi
movq %r15, %rsi
callq 0x21b0
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2cf5
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2140
movq 0x8(%rsp), %rsi
leaq 0x14a7(%rip), %rdi      # 0x41a8
xorl %eax, %eax
callq 0x2050
movq 0x32b9(%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 0x14b8(%rip), %rdi      # 0x41f0
leaq 0x14b7(%rip), %rax      # 0x41f6
cmovneq %rax, %rdi
callq 0x2220
addq $0x30, %r12
cmpq %r13, %r12
jne 0x2cb5
movq %rbx, %rdi
callq 0x2120
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2d79
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2140
leaq 0x48(%rsp), %rdi
callq 0x2f26
xorl %ebx, %ebx
jmp 0x2de8
movq %rbp, %rdi
callq 0x247d
jmp 0x2de3
movq %rsi, %rdi
callq 0x24c8
jmp 0x2db6
movq 0x323e(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
leaq 0x1330(%rip), %rsi      # 0x40dc
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21e0
movl $0x1, %ebx
jmp 0x2e03
movq %rbp, %rdi
callq 0x2351
jmp 0x2de3
movq %rbp, %rdi
callq 0x2432
jmp 0x2de3
movq %rbp, %rdi
callq 0x23e7
jmp 0x2de3
movq %rbp, %rdi
callq 0x239c
movl $0x1, %ebx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2e03
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2140
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2e40
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2e6e
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2140
jmp 0x2e6e
jmp 0x2e40
jmp 0x2e40
jmp 0x2e40
movq %rax, %rbx
jmp 0x2e6e
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2e64
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2140
leaq 0x48(%rsp), %rdi
callq 0x2f26
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2e89
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x2240
nop