// File: /workspace/github_c_makefile_star2_O1/shuffle2[P]nx2elf/nx2elf.cpp
// Function: NsoFile::Load(std::filesystem::__cxx11::path const&)
bool Load(const fs::path& path) {
    auto file = File::Read(path);
    const size_t nro_offset = ALIGN_UP(sizeof(ModPointer), 0x10);
    if (file.size() >= sizeof(NsoHeader) &&
        !memcmp(&file[0], &nso_magic[0], nso_magic.size())) {
      memcpy(&header, &file[0], sizeof(header));

      // assume segments are after each other and mem offsets are aligned
      // note: there are also symbols "_start" and "end" which describe
      // the total size.
      auto& data_seg = header.segments[kData];
      size_t image_size =
          data_seg.mem_offset + data_seg.mem_size + data_seg.bss_align;
      image = std::vector<u8>(image_size);

      for (int i = 0; i < kNumSegment; i++) {
        auto& seg = header.segments[i];
        auto& file_size = header.segment_file_sizes[i];
        if (!Decompress(&image[seg.mem_offset], seg.mem_size,
                        &file[seg.file_offset], file_size)) {
          return false;
        }
      }
      file_type = kNso;
    } else if (file.size() >= nro_offset + sizeof(NroHeader) &&
               !memcmp(&file[nro_offset], &nro_magic[0], nro_magic.size())) {
      // Translate the nro header to nso, which is a superset
      auto nro = reinterpret_cast<NroHeader*>(&file[nro_offset]);
      if (nro->file_size != file.size()) {
        return false;
      }
      for (int i = 0; i < kNumSegment; i++) {
        auto& seg = header.segments[i];
        // TODO revisit once some nso with uncompressed segments is seen
        seg.mem_offset = seg.file_offset = nro->segments[i].offset;
        seg.mem_size = header.segment_file_sizes[i] = nro->segments[i].size;
        switch (i) {
        case kText:
          seg.bss_align = 0x100;
          break;
        case kRodata:
          seg.bss_align = 1;
          break;
        case kData:
          seg.bss_align = nro->bss_size;
          break;
        }
      }
      header.gnu_build_id = nro->gnu_build_id;
      header.dynstr = nro->dynstr;
      header.dynsym = nro->dynsym;

      image = std::move(file);
      file_type = kNro;
    }

    u8* mod_base = nullptr;
    ModPointer* mod_ptr = nullptr;
    if (file_type != kUnknown) {
      mod_ptr = reinterpret_cast<ModPointer*>(&image[0]);
      if (mod_ptr->magic_offset + sizeof(ModHeader) > image.size()) {
        return false;
      }
      mod_base = &image[mod_ptr->magic_offset];
    } else if (file.size() >= sizeof(ModPointer)) {
      // It's not an NSO or NRO, but still need to check for MOD
      mod_ptr = reinterpret_cast<ModPointer*>(&file[0]);
      if (mod_ptr->magic_offset + sizeof(ModHeader) > file.size()) {
        return false;
      }
      mod_base = &file[mod_ptr->magic_offset];
    } else {
      return false;
    }
    auto mod = reinterpret_cast<ModHeader*>(mod_base);
    if (memcmp(mod->magic, &mod_magic[0], mod_magic.size()))
      return false;

    if (file_type == kUnknown) {
      // Apparently there are images which are essentially NROs, but lack
      // the NRO header, for some reason. This is a pain.
      image = std::move(file);
      file_type = kMod;
    }
    /*
    fs::path dump(path);
    File::Write(dump.replace_extension(".bin"), image);
    //*/

    auto mod_get_offset = [&](s32 relative_offset) {
      auto ptr = reinterpret_cast<u8*>(mod_base + relative_offset);
      auto offset = reinterpret_cast<uintptr_t>(ptr) -
                    reinterpret_cast<uintptr_t>(&image[0]);
      return static_cast<u32>(offset);
    };

    dynamic = reinterpret_cast<Elf64_Dyn*>(mod_base + mod->dynamic_offset);
    for (auto dyn = dynamic; dyn->d_tag; dyn++) {
#define DT_ASSIGN_U64(dt, var) \
  case dt:                     \
    dyn_info.var = dyn->d_un;  \
    break;
      switch (dyn->d_tag) {
        DT_ASSIGN_U64(DT_SYMTAB, symtab);
        DT_ASSIGN_U64(DT_RELA, rela);
        DT_ASSIGN_U64(DT_RELASZ, relasz);
        DT_ASSIGN_U64(DT_JMPREL, jmprel);
        DT_ASSIGN_U64(DT_PLTRELSZ, pltrelsz);
        DT_ASSIGN_U64(DT_STRTAB, strtab);
        DT_ASSIGN_U64(DT_STRSZ, strsz);
        DT_ASSIGN_U64(DT_PLTGOT, pltgot);
        DT_ASSIGN_U64(DT_HASH, hash);
        DT_ASSIGN_U64(DT_GNU_HASH, gnu_hash);
        DT_ASSIGN_U64(DT_INIT, init);
        DT_ASSIGN_U64(DT_FINI, fini);
        DT_ASSIGN_U64(DT_INIT_ARRAY, init_array);
        DT_ASSIGN_U64(DT_INIT_ARRAYSZ, init_arraysz);
        DT_ASSIGN_U64(DT_FINI_ARRAY, fini_array);
        DT_ASSIGN_U64(DT_FINI_ARRAYSZ, fini_arraysz);
      }
#undef DT_ASSIGN_U64
    }
    if (file_type != kMod) {
      auto& text_seg = header.segments[kText];
      ResolvePlt(&image[text_seg.mem_offset], text_seg.mem_size);
    }

    if (file_type == kMod) {
      // need to manually build them ...
      DataExtent segments[kNumSegment]{};

      // XXX hacks...
      if (!ResolvePlt(&image[0], image.size())) {
        fputs("error: raw MOD requires .plt. please report this.\n", stderr);
        return false;
      }
      if (dyn_info.symtab >= dyn_info.strtab) {
        fputs(
            "error: raw MOD requires .dynstr directly after .dynsym. please "
            "report this.\n",
            stderr);
        return false;
      }
      // Need this up-front to be able to iter_dynsym
      header.dynsym.size = static_cast<u32>(dyn_info.strtab - dyn_info.symtab);
      // yet another dirty hack. relies on all sections having at least
      // one symbol pointing into them, and a section symbol existing for .data
      std::vector<u16> seen_shndx;
      iter_dynsym([&](const Elf64_Sym& sym, u32) {
        if (sym.st_shndx == SHN_UNDEF || sym.st_shndx >= SHN_LORESERVE) {
          return;
        }
        seen_shndx.push_back(sym.st_shndx);
      });
      std::sort(seen_shndx.begin(), seen_shndx.end());
      seen_shndx.erase(std::unique(seen_shndx.begin(), seen_shndx.end()),
                       seen_shndx.end());
      if (seen_shndx.size() != kNumSegment + 1) {
        fputs(
            "error: raw MOD failed to find .data in .dynsym. please report "
            "this.\n",
            stderr);
        return false;
      }
      iter_dynsym([&](const Elf64_Sym& sym, u32) {
        if (segments[kData].offset == 0 &&
            ELF64_ST_TYPE(sym.st_info) == STT_SECTION &&
            sym.st_shndx == seen_shndx[kData]) {
          segments[kData].offset = static_cast<u32>(sym.st_value);
        }
      });
      if (segments[kData].offset == 0) {
        fputs(
            "error: raw MOD failed to find .data in .dynsym. please report "
            "this.\n",
            stderr);
        return false;
      }

      segments[kText].offset = 0;
      segments[kText].size = static_cast<u32>(plt_info.addr + plt_info.size);
      segments[kRodata].offset =
          ALIGN_UP(segments[kText].offset + segments[kText].size, 0x1000);
      segments[kRodata].size =
          segments[kData].offset - segments[kRodata].offset;
      segments[kData].size =
          static_cast<u32>(image.size() - segments[kData].offset);

      header.dynstr.offset =
          static_cast<u32>(dyn_info.strtab - segments[kRodata].offset);
      header.dynstr.size = static_cast<u32>(dyn_info.strsz);
      header.dynsym.offset =
          static_cast<u32>(dyn_info.symtab - segments[kRodata].offset);

      for (int i = 0; i < kNumSegment; i++) {
        auto& seg = header.segments[i];
        seg.mem_offset = seg.file_offset = segments[i].offset;
        seg.mem_size = header.segment_file_sizes[i] = segments[i].size;
        switch (i) {
        case kText:
          seg.bss_align = 0x100;
          break;
        case kRodata:
          seg.bss_align = 1;
          break;
        case kData:
          // This is the actual size cleared by init code, but there
          // is a symbol named "end" which will be referenced and is
          // at the aligned boundary. So pad it out until there.
          // This is debatably a bug in nintendo's tools.
          seg.bss_align = mod_get_offset(mod->bss_end_offset) -
                          mod_get_offset(mod->bss_start_offset);
          seg.bss_align = ALIGN_UP(seg.bss_align, 0x1000) + 1;
          break;
        }
      }
    }

    // Kinda gross, but hopefully unique enough to avoid false positives...
    const GnuBuildId md5_build_id_needle = {
        {sizeof(GnuBuildId::owner), sizeof(GnuBuildId::build_id_md5), 3},
        {'G', 'N', 'U'}};
    const GnuBuildId sha1_build_id_needle = {
        {sizeof(GnuBuildId::owner), sizeof(GnuBuildId::build_id_sha1), 3},
        {'G', 'N', 'U'}};
    for (auto i : {kRodata, kText, kData}) {
      auto& seg = header.segments[i];
      note = reinterpret_cast<Elf64_Nhdr*>(
          memmemr(&image[seg.mem_offset], seg.mem_size, &md5_build_id_needle,
                  offsetof(GnuBuildId, build_id_md5)));
      if (note) {
        break;
      }
      note = reinterpret_cast<Elf64_Nhdr*>(
          memmemr(&image[seg.mem_offset], seg.mem_size, &sha1_build_id_needle,
                  offsetof(GnuBuildId, build_id_sha1)));
      if (note) {
        break;
      }
    }

    // In case of MOD-only file, we can only fill in build id if the section was
    // found manually
    if (file_type == kMod && note) {
      auto build_id = reinterpret_cast<const GnuBuildId*>(note);
      memcpy(header.gnu_build_id.data(), build_id->build_id_raw.data(),
             build_id->header.n_descsz);
    }

    eh_info.hdr_addr = mod_get_offset(mod->eh_start_offset);
    eh_info.hdr_size = mod_get_offset(mod->eh_end_offset) - eh_info.hdr_addr;

    return true;
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rsi, %r12
movq %rdi, %rbx
leaq -0xa0(%rbp), %r14
movl $0x0, (%r14)
callq 0x2100
movq %rax, 0x8(%r14)
movq %r12, %rdi
movq %r14, %rsi
callq 0x21f0
cmpq $-0x1, %rax
je 0x5107
leaq -0x70(%rbp), %r15
leaq -0xb0(%rbp), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x8ab8
leaq 0x1213e(%rip), %rdx     # 0x171f9
leaq -0xb0(%rbp), %rdi
movq %r12, %rsi
callq 0x4e7c
movq -0xb0(%rbp), %r12
testq %r12, %r12
je 0x511a
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rsi
subq %rdi, %rsi
movl $0x1, %edx
movq %r12, %rcx
callq 0x2250
testq %rax, %rax
je 0x512d
movq -0x70(%rbp), %rax
movq %rax, -0x40(%rbp)
movdqu -0x68(%rbp), %xmm0
movdqu %xmm0, -0x38(%rbp)
jmp 0x5131
pxor %xmm0, %xmm0
movdqa %xmm0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
jmp 0x5164
pxor %xmm0, %xmm0
movdqa %xmm0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
jmp 0x514f
leaq -0x40(%rbp), %r15
pxor %xmm0, %xmm0
movdqu %xmm0, (%r15)
movq $0x0, 0x10(%r15)
testq %r12, %r12
je 0x514f
movq %r12, %rdi
callq 0x2190
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x5164
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %r12
subq %rsi, %r12
cmpq $0x100, %r12            # imm = 0x100
jb 0x5184
cmpl $0x304f534e, (%rsi)     # imm = 0x304F534E
je 0x5247
cmpq $0x80, %r12
jb 0x52d3
cmpl $0x304f524e, 0x10(%rsi) # imm = 0x304F524E
jne 0x52d3
movl 0x18(%rsi), %r14d
xorl %r15d, %r15d
cmpq %r14, %r12
jne 0x532a
xorl %eax, %eax
movl 0x20(%rsi,%rax,2), %ecx
movl %ecx, 0x14(%rbx,%rax,4)
movl %ecx, 0x18(%rbx,%rax,4)
movl 0x24(%rsi,%rax,2), %ecx
movl %ecx, 0x64(%rbx,%rax)
movl %ecx, 0x1c(%rbx,%rax,4)
testl %r15d, %r15d
je 0x51e5
cmpl $0x2, %r15d
je 0x51e0
cmpl $0x1, %r15d
jne 0x51ee
movl $0x1, %ecx
jmp 0x51ea
movl 0x38(%rsi), %ecx
jmp 0x51ea
movl $0x100, %ecx            # imm = 0x100
movl %ecx, 0x20(%rbx,%rax,4)
incl %r15d
addq $0x4, %rax
cmpq $0xc, %rax
jne 0x51b0
movdqu 0x40(%rsi), %xmm0
movups 0x50(%rsi), %xmm1
movups %xmm1, 0x54(%rbx)
movdqu %xmm0, 0x44(%rbx)
movq 0x70(%rsi), %rax
movq %rax, 0x94(%rbx)
movq 0x78(%rsi), %rax
movq %rax, 0x9c(%rbx)
leaq 0x108(%rbx), %rdi
leaq -0x40(%rbp), %rsi
callq 0x8ca4
movl $0x2, (%rbx)
cmpq %r14, %r12
je 0x52d3
jmp 0x5327
leaq 0x4(%rbx), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x2160
movl 0x3c(%rbx), %esi
addl 0x38(%rbx), %esi
addl 0x40(%rbx), %esi
leaq -0x70(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x8ab8
leaq 0x108(%rbx), %rdi
leaq -0x70(%rbp), %r15
movq %r15, %rsi
callq 0x8ca4
movq (%r15), %rdi
testq %rdi, %rdi
je 0x5295
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
xorl %r14d, %r14d
movl 0x14(%rbx,%r14,4), %ecx
movl 0x18(%rbx,%r14,4), %esi
addq 0x108(%rbx), %rsi
movl 0x1c(%rbx,%r14,4), %edx
addq -0x40(%rbp), %rcx
movl 0x64(%rbx,%r14), %r8d
movq %rbx, %rdi
callq 0x8b22
testb %al, %al
je 0x5327
addq $0x4, %r14
cmpq $0xc, %r14
jne 0x5298
movl $0x1, (%rbx)
movl (%rbx), %eax
testl %eax, %eax
je 0x52fc
movq 0x108(%rbx), %r13
movq 0x110(%rbx), %rdx
movl 0x4(%r13), %ecx
leaq 0x1c(%rcx), %rsi
subq %r13, %rdx
cmpq %rdx, %rsi
ja 0x5327
addq %rcx, %r13
jmp 0x531d
movq -0x40(%rbp), %r13
movq -0x38(%rbp), %rcx
subq %r13, %rcx
cmpq $0x8, %rcx
jb 0x5327
movl 0x4(%r13), %edx
leaq 0x1c(%rdx), %rsi
cmpq %rcx, %rsi
ja 0x5327
addq %rdx, %r13
cmpl $0x30444f4d, (%r13)     # imm = 0x30444F4D
je 0x5354
xorl %r15d, %r15d
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x533f
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
movl %r15d, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %eax, %eax
jne 0x536e
leaq 0x108(%rbx), %rdi
leaq -0x40(%rbp), %rsi
callq 0x8ca4
movl $0x3, (%rbx)
movslq 0x4(%r13), %rax
leaq (%rax,%r13), %rcx
movq %rcx, 0x120(%rbx)
leaq 0x1a8(%rbx), %rcx
movq %rcx, -0xf0(%rbp)
leaq 0x1a0(%rbx), %rcx
movq %rcx, -0xe8(%rbp)
leaq 0x198(%rbx), %rcx
movq %rcx, -0xe0(%rbp)
leaq 0x190(%rbx), %rcx
movq %rcx, -0xd8(%rbp)
leaq 0x188(%rbx), %rcx
movq %rcx, -0xd0(%rbp)
leaq 0x180(%rbx), %rcx
movq %rcx, -0xc8(%rbp)
leaq 0x178(%rbx), %r9
leaq 0x170(%rbx), %rcx
movq %rcx, -0xc0(%rbp)
leaq 0x168(%rbx), %r11
leaq 0x160(%rbx), %r15
leaq 0x158(%rbx), %r12
leaq 0x150(%rbx), %r14
movq %r13, -0xb8(%rbp)
leaq 0x8(%r13,%rax), %r13
leaq 0x148(%rbx), %rax
leaq 0x140(%rbx), %rcx
leaq 0x138(%rbx), %rdx
leaq 0x130(%rbx), %r8
leaq 0x11c1f(%rip), %rdi     # 0x17050
jmp 0x54ac
movq %r14, %rsi
jmp 0x54a1
movq %r9, %rsi
cmpq $0x6ffffef5, %r10       # imm = 0x6FFFFEF5
je 0x54a1
jmp 0x54a8
movq -0xd0(%rbp), %rsi
jmp 0x54a1
movq -0xc8(%rbp), %rsi
jmp 0x54a1
movq -0xc0(%rbp), %rsi
jmp 0x54a1
movq %r12, %rsi
jmp 0x54a1
movq %r11, %rsi
jmp 0x54a1
movq %rdx, %rsi
jmp 0x54a1
movq %r15, %rsi
jmp 0x54a1
movq -0xd8(%rbp), %rsi
jmp 0x54a1
movq %rcx, %rsi
jmp 0x54a1
movq -0xe8(%rbp), %rsi
jmp 0x54a1
movq %rax, %rsi
jmp 0x54a1
movq -0xf0(%rbp), %rsi
jmp 0x54a1
movq -0xe0(%rbp), %rsi
movq (%r13), %r10
movq %r10, (%rsi)
addq $0x10, %r13
movq -0x8(%r13), %r10
cmpq $0x1c, %r10
ja 0x5438
movslq (%rdi,%r10,4), %r10
addq %rdi, %r10
movq %r8, %rsi
jmpq *%r10
cmpl $0x3, (%rbx)
je 0x54dd
movl 0x18(%rbx), %esi
movl 0x1c(%rbx), %edx
addq 0x108(%rbx), %rsi
movq %rbx, %rdi
callq 0x8b5e
cmpl $0x3, (%rbx)
leaq -0xa0(%rbp), %r14
movq -0xb8(%rbp), %r13
jne 0x5795
pxor %xmm0, %xmm0
movdqa %xmm0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movq 0x108(%rbx), %rsi
movq 0x110(%rbx), %rdx
subq %rsi, %rdx
movq %rbx, %rdi
callq 0x8b5e
testb %al, %al
je 0x5921
movq 0x130(%rbx), %rax
movq 0x158(%rbx), %rcx
cmpq %rax, %rcx
jbe 0x592a
subl %eax, %ecx
movl %ecx, 0xa0(%rbx)
pxor %xmm0, %xmm0
movdqa %xmm0, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
leaq -0x130(%rbp), %rsi
movdqa %xmm0, (%rsi)
movdqa %xmm0, 0x10(%rsi)
movq %r14, (%rsi)
leaq 0x3774(%rip), %rax      # 0x8cea
movq %rax, 0x18(%rsi)
leaq 0x3773(%rip), %rax      # 0x8cf4
movq %rax, 0x10(%rsi)
movq %rbx, %rdi
callq 0x8bf0
movq -0x120(%rbp), %rax
testq %rax, %rax
je 0x55aa
leaq -0x130(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0xa0(%rbp), %r15
movq -0x98(%rbp), %r12
cmpq %r12, %r15
je 0x55e9
movq %r12, %rax
subq %r15, %rax
sarq %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x8e9d
movq %r15, %rdi
movq %r12, %rsi
callq 0x8f94
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0x9264
movq -0x98(%rbp), %rcx
cmpq %rcx, %rax
je 0x5624
movq -0x98(%rbp), %rdx
movq %rdx, %rsi
subq %rcx, %rsi
addq %rsi, %rax
cmpq %rax, %rdx
je 0x5624
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
subq -0xa0(%rbp), %rax
cmpq $0x8, %rax
jne 0x590c
pxor %xmm0, %xmm0
leaq -0x110(%rbp), %rsi
movdqa %xmm0, (%rsi)
movdqa %xmm0, 0x10(%rsi)
leaq -0x70(%rbp), %rax
movq %rax, (%rsi)
movq %r14, 0x8(%rsi)
leaq 0x3c60(%rip), %rax      # 0x92c2
movq %rax, 0x18(%rsi)
leaq 0x3c87(%rip), %rax      # 0x92f4
movq %rax, 0x10(%rsi)
movq %rbx, %rdi
callq 0x8bf0
movq -0x100(%rbp), %rax
testq %rax, %rax
je 0x5696
leaq -0x110(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movl -0x60(%rbp), %eax
testl %eax, %eax
je 0x590c
movl $0x0, -0x70(%rbp)
movl 0x1b0(%rbx), %ecx
movl 0x1b8(%rbx), %edx
leal (%rdx,%rcx), %esi
movl %esi, -0x6c(%rbp)
addl %edx, %ecx
addl $0xfff, %ecx            # imm = 0xFFF
andl $0xfffff000, %ecx       # imm = 0xFFFFF000
movl %ecx, -0x68(%rbp)
movl %eax, %edx
subl %ecx, %edx
movl %edx, -0x64(%rbp)
movl 0x110(%rbx), %edx
movq 0x108(%rbx), %rsi
subl %esi, %edx
subl %eax, %edx
movl %edx, -0x5c(%rbp)
movl 0x130(%rbx), %eax
movl 0x158(%rbx), %edx
subl %ecx, %edx
movl %edx, 0x94(%rbx)
movl 0x160(%rbx), %edx
movl %edx, 0x98(%rbx)
subl %ecx, %eax
movl %eax, 0x9c(%rbx)
xorl %eax, %eax
xorl %ecx, %ecx
movl -0x70(%rbp,%rax,2), %edx
movl %edx, 0x14(%rbx,%rax,4)
movl %edx, 0x18(%rbx,%rax,4)
movl -0x6c(%rbp,%rax,2), %edx
movl %edx, 0x64(%rbx,%rax)
movl %edx, 0x1c(%rbx,%rax,4)
cmpl $0x2, %ecx
je 0x574c
cmpl $0x1, %ecx
je 0x5742
testl %ecx, %ecx
jne 0x576e
movl $0x100, 0x20(%rbx,%rax,4) # imm = 0x100
jmp 0x576e
movl $0x1, 0x20(%rbx,%rax,4)
jmp 0x576e
movl 0xc(%r13), %edx
addl %r13d, %edx
movl 0x8(%r13), %esi
addl %r13d, %esi
subl %esi, %edx
addl $0xfff, %edx            # imm = 0xFFF
andl $0xfffff000, %edx       # imm = 0xFFFFF000
incl %edx
movl %edx, 0x20(%rbx,%rax,4)
incl %ecx
addq $0x4, %rax
cmpq $0xc, %rax
jne 0x5712
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x5795
movq -0x90(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
pxor %xmm0, %xmm0
movdqa %xmm0, -0x70(%rbp)
movdqa %xmm0, -0x50(%rbp)
movdqa %xmm0, -0x60(%rbp)
movabsq $0x1000000004, %rax     # imm = 0x1000000004
movq %rax, -0x70(%rbp)
movl $0x3, %eax
movl %eax, -0x68(%rbp)
movw $0x4e47, %cx            # imm = 0x4E47
movw %cx, -0x64(%rbp)
movb $0x55, %dl
movb %dl, -0x62(%rbp)
movdqa %xmm0, -0xa0(%rbp)
movdqa %xmm0, -0x80(%rbp)
movdqa %xmm0, -0x90(%rbp)
movabsq $0x1400000004, %rsi     # imm = 0x1400000004
movq %rsi, -0xa0(%rbp)
movl %eax, -0x98(%rbp)
movw %cx, -0x94(%rbp)
movb %dl, -0x92(%rbp)
movq $0x1, -0xb0(%rbp)
movl $0x2, -0xa8(%rbp)
leaq 0x14(%rbx), %rax
movq 0x108(%rbx), %rcx
xorl %edx, %edx
movl -0xb0(%rbp,%rdx), %esi
shlq $0x4, %rsi
movl 0x4(%rax,%rsi), %edi
movl 0x8(%rax,%rsi), %esi
addq %rcx, %rdi
addq %rdi, %rsi
addq $-0x10, %rsi
cmpq %rdi, %rsi
jb 0x586b
movq %rsi, %r8
movdqu (%r8), %xmm0
pcmpeqb -0x70(%rbp), %xmm0
pmovmskb %xmm0, %r9d
cmpl $0xffff, %r9d           # imm = 0xFFFF
je 0x586e
decq %r8
cmpq %rdi, %r8
jae 0x584b
xorl %r8d, %r8d
movq %r8, 0x128(%rbx)
testq %r8, %r8
jne 0x58ba
cmpq %rdi, %rsi
jb 0x589e
movdqu (%rsi), %xmm0
pcmpeqb -0xa0(%rbp), %xmm0
pmovmskb %xmm0, %r8d
cmpl $0xffff, %r8d           # imm = 0xFFFF
je 0x58a0
decq %rsi
jmp 0x587a
xorl %esi, %esi
movq %rsi, 0x128(%rbx)
testq %rsi, %rsi
jne 0x58ba
addq $0x4, %rdx
cmpq $0xc, %rdx
jne 0x5826
cmpl $0x3, (%rbx)
jne 0x58db
movq 0x128(%rbx), %rsi
testq %rsi, %rsi
je 0x58db
leaq 0x44(%rbx), %rdi
movl 0x4(%rsi), %edx
addq $0x10, %rsi
callq 0x2160
movl 0x10(%r13), %eax
addl %r13d, %eax
movl 0x108(%rbx), %ecx
subl %ecx, %eax
movq %rax, 0x1c0(%rbx)
movl 0x14(%r13), %edx
addl %r13d, %edx
subl %ecx, %edx
subq %rax, %rdx
movq %rdx, 0x1c8(%rbx)
movb $0x1, %r15b
jmp 0x532a
leaq -0xa0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x22fc
jmp 0x5327
leaq 0x1180c(%rip), %rdi     # 0x17134
jmp 0x5931
leaq 0x11836(%rip), %rdi     # 0x17167
movq 0x156a0(%rip), %rax     # 0x1afd8
movq (%rax), %rsi
callq 0x21c0
jmp 0x5327
jmp 0x5991
jmp 0x5991
movq %rax, %rbx
movq -0x100(%rbp), %rax
testq %rax, %rax
je 0x59a0
leaq -0x110(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x59a0
jmp 0x5991
jmp 0x599d
movq %rax, %rbx
movq -0x120(%rbp), %rax
testq %rax, %rax
je 0x59a0
leaq -0x130(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x59a0
movq %rax, %rdi
callq 0x4fe1
jmp 0x59d1
jmp 0x59d1
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x59d4
movq -0x90(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
jmp 0x59d4
jmp 0x59d1
movq %rax, %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x59e9
movq -0x60(%rbp), %rsi
jmp 0x59e1
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x59e9
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2280
nop