// File: /workspace/github_c_makefile_star2_O0/mrkite[P]regs/src/dsk.cc
// Function: doFile(unsigned char, unsigned char, unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned short, std::shared_ptr<TheHandle>)
static uint16_t doFile(uint8_t track, uint8_t sector, uint8_t type,
                       std::string name, uint16_t length, Handle h) {
  auto data = std::vector<uint8_t>((length + 1) * 256);
  auto listTrack = track;
  auto listSector = sector;
  while (listTrack) {
    h->seek(listTrack * 16 * 256 + listSector * 256 + 1);
    listTrack = h->r8();
    listSector = h->r8();
    h->skip(2);
    auto offset = h->r16();
    h->skip(5);
    std::vector<uint8_t> pairs;
    for (auto i = 0; i < 0x7a * 2; i++) {
      pairs.push_back(h->r8());
    }
    for (auto i = 0; i < pairs.size(); i += 2) {
      if (pairs[i] == 0) {
        continue;
      }
      h->seek(pairs[i] * 256 * 16 + pairs[i + 1] * 256);
      auto v = h->readBytes(256);
      std::copy(v.begin(), v.end(), data.begin() + offset);
      offset += 256;
    }
  }

  switch (type) {
    case 0x00:  // text, run until null
      for (auto i = 0; i < data.size(); i++) {
        if (data[i] == '\0') {
          data.resize(i);
          break;
        }
      }
      break;
    case 0x01:  // int basic
    case 0x02:  // applesoft basic
      {
        auto wrap = TheHandle::createFromArray(data);
        auto len = wrap->r16();
        data = wrap->readBytes(len);
      }
      break;
    case 0x04:  // binary
      {
        auto wrap = TheHandle::createFromArray(data);
        auto meta = wrap->r16();
        if (name.empty()) {  // we just want the meta data
          return meta;
        }
        auto len = wrap->r16();
        data = wrap->readBytes(len);
      }
      break;
  }
  std::ofstream f(name, std::ios::out | std::ios::binary | std::ios::trunc);
  if (!f.is_open()) {
    std::cerr << "Failed to create " << name << std::endl;
    return -1;
  }
  f.write((const char *)&data[0], data.size());
  f.close();
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3d0, %rsp            # imm = 0x3D0
movq %r9, -0x348(%rbp)
movq %rcx, -0x340(%rbp)
movw %r8w, %ax
movb %dil, -0x3(%rbp)
movb %sil, -0x4(%rbp)
movb %dl, -0x5(%rbp)
movq %rcx, -0x10(%rbp)
movw %ax, -0x12(%rbp)
movq %r9, -0x20(%rbp)
movzwl -0x12(%rbp), %eax
shll $0x8, %eax
addl $0x100, %eax            # imm = 0x100
movq %rax, -0x338(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x330(%rbp)
callq 0x4f90
movq -0x338(%rbp), %rsi
movq -0x330(%rbp), %rdx
leaq -0x38(%rbp), %rdi
callq 0x4fb0
jmp 0x4220
leaq -0x39(%rbp), %rdi
callq 0x5020
movb -0x3(%rbp), %al
movb %al, -0x4d(%rbp)
movb -0x4(%rbp), %al
movb %al, -0x4e(%rbp)
cmpb $0x0, -0x4d(%rbp)
je 0x4561
movq -0x348(%rbp), %rdi
callq 0x4aa0
movq %rax, %rdi
movzbl -0x4d(%rbp), %edx
shll $0xc, %edx
movzbl -0x4e(%rbp), %eax
shll $0x8, %eax
movl %eax, %ecx
movl %edx, %eax
leal 0x1(%rax,%rcx), %eax
movl %eax, %esi
callq 0x7380
jmp 0x426d
movq -0x348(%rbp), %rdi
callq 0x4aa0
movq %rax, %rdi
callq 0x7130
movb %al, -0x349(%rbp)
jmp 0x4289
movq -0x348(%rbp), %rdi
movb -0x349(%rbp), %al
movb %al, -0x4d(%rbp)
callq 0x4aa0
movq %rax, %rdi
callq 0x7130
movb %al, -0x34a(%rbp)
jmp 0x42ae
movq -0x348(%rbp), %rdi
movb -0x34a(%rbp), %al
movb %al, -0x4e(%rbp)
callq 0x4aa0
movq %rax, %rdi
movl $0x2, %esi
callq 0x73a0
jmp 0x42d2
movq -0x348(%rbp), %rdi
callq 0x4aa0
movq %rax, %rdi
callq 0x70e0
movw %ax, -0x34c(%rbp)
jmp 0x42ef
movq -0x348(%rbp), %rdi
movw -0x34c(%rbp), %ax
movw %ax, -0x50(%rbp)
callq 0x4aa0
movq %rax, %rdi
movl $0x5, %esi
callq 0x73a0
jmp 0x4315
leaq -0x68(%rbp), %rdi
callq 0x5040
movl $0x0, -0x6c(%rbp)
cmpl $0xf4, -0x6c(%rbp)
jge 0x43ab
movq -0x348(%rbp), %rdi
callq 0x4aa0
movq %rax, %rdi
callq 0x7130
movb %al, -0x34d(%rbp)
jmp 0x434a
movb -0x34d(%rbp), %al
movb %al, -0x6d(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x6d(%rbp), %rsi
callq 0x5060
jmp 0x4362
jmp 0x4364
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x4325
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x5020
jmp 0x493b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4932
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4553
movl $0x0, -0x74(%rbp)
movslq -0x74(%rbp), %rax
movq %rax, -0x358(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x5090
movq %rax, %rcx
movq -0x358(%rbp), %rax
cmpq %rcx, %rax
jae 0x4545
movslq -0x74(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x50b0
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x43f3
jmp 0x451d
movq -0x348(%rbp), %rdi
callq 0x4aa0
movq %rax, -0x368(%rbp)
movslq -0x74(%rbp), %rsi
leaq -0x68(%rbp), %rdi
movq %rdi, -0x370(%rbp)
callq 0x50b0
movq -0x370(%rbp), %rdi
movzbl (%rax), %eax
shll $0xc, %eax
movl %eax, -0x35c(%rbp)
movl -0x74(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x50b0
movq -0x368(%rbp), %rdi
movq %rax, %rcx
movl -0x35c(%rbp), %eax
movzbl (%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, %esi
callq 0x7380
jmp 0x445b
movq -0x348(%rbp), %rdi
callq 0x4aa0
movq %rax, %rsi
leaq -0x90(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x72c0
jmp 0x447d
leaq -0x90(%rbp), %rdi
movq %rdi, -0x380(%rbp)
callq 0x5140
movq -0x380(%rbp), %rdi
movq %rax, -0x98(%rbp)
callq 0x5170
movq %rax, -0xa0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x5140
movq %rax, -0xb0(%rbp)
movzwl -0x50(%rbp), %eax
movl %eax, %esi
leaq -0xb0(%rbp), %rdi
callq 0x51a0
movq %rax, -0xa8(%rbp)
movq -0x98(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
callq 0x50d0
movq %rax, -0x378(%rbp)
jmp 0x44f6
movq -0x378(%rbp), %rax
movq %rax, -0xb8(%rbp)
movzwl -0x50(%rbp), %eax
addl $0x100, %eax            # imm = 0x100
movw %ax, -0x50(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x51e0
movl -0x74(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x74(%rbp)
jmp 0x43b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x51e0
jmp 0x4553
leaq -0x68(%rbp), %rdi
callq 0x51e0
jmp 0x4235
leaq -0x68(%rbp), %rdi
callq 0x51e0
jmp 0x4932
movzbl -0x5(%rbp), %eax
movl %eax, -0x384(%rbp)
testl %eax, %eax
je 0x4598
jmp 0x4571
movl -0x384(%rbp), %eax
decl %eax
subl $0x2, %eax
jb 0x460c
jmp 0x4584
movl -0x384(%rbp), %eax
subl $0x4, %eax
je 0x46b7
jmp 0x47d0
movl $0x0, -0xbc(%rbp)
movslq -0xbc(%rbp), %rax
movq %rax, -0x390(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x5090
movq %rax, %rcx
movq -0x390(%rbp), %rax
cmpq %rcx, %rax
jae 0x4607
movslq -0xbc(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x50b0
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x45f4
movslq -0xbc(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x5240
jmp 0x45f2
jmp 0x4607
jmp 0x45f6
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x45a2
jmp 0x47d0
leaq -0xd0(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x6c80
jmp 0x461e
leaq -0xd0(%rbp), %rdi
callq 0x4aa0
movq %rax, %rdi
callq 0x70e0
movw %ax, -0x392(%rbp)
jmp 0x463b
movw -0x392(%rbp), %ax
movw %ax, -0xd2(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x4aa0
movq %rax, %rsi
movzwl -0xd2(%rbp), %edx
leaq -0xf0(%rbp), %rdi
callq 0x72c0
jmp 0x466d
leaq -0x38(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x52d0
leaq -0xf0(%rbp), %rdi
callq 0x51e0
leaq -0xd0(%rbp), %rdi
callq 0x4af0
jmp 0x47d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x4af0
jmp 0x4932
leaq -0x100(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x6c80
jmp 0x46c9
leaq -0x100(%rbp), %rdi
callq 0x4aa0
movq %rax, %rdi
callq 0x70e0
movw %ax, -0x394(%rbp)
jmp 0x46e6
movq -0x340(%rbp), %rdi
movw -0x394(%rbp), %ax
movw %ax, -0x102(%rbp)
callq 0x3380
testb $0x1, %al
jne 0x4706
jmp 0x473d
movw -0x102(%rbp), %ax
movw %ax, -0x2(%rbp)
movl $0x1, -0x108(%rbp)
jmp 0x47b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x4af0
jmp 0x4932
leaq -0x100(%rbp), %rdi
callq 0x4aa0
movq %rax, %rdi
callq 0x70e0
movw %ax, -0x396(%rbp)
jmp 0x475a
movw -0x396(%rbp), %ax
movw %ax, -0x10a(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x4aa0
movq %rax, %rsi
movzwl -0x10a(%rbp), %edx
leaq -0x128(%rbp), %rdi
callq 0x72c0
jmp 0x478c
leaq -0x38(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x52d0
leaq -0x128(%rbp), %rdi
callq 0x51e0
movl $0x0, -0x108(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x4af0
movl -0x108(%rbp), %eax
testl %eax, %eax
jne 0x491c
jmp 0x47ce
jmp 0x47d0
movl $0x10, %edi
movl $0x4, %esi
callq 0x5300
movl %eax, -0x39c(%rbp)
jmp 0x47e7
movl -0x39c(%rbp), %edi
movl $0x20, %esi
callq 0x5300
movl %eax, -0x3a0(%rbp)
jmp 0x47ff
movl -0x3a0(%rbp), %edx
movq -0x340(%rbp), %rsi
leaq -0x328(%rbp), %rdi
callq 0x3410
jmp 0x481a
leaq -0x328(%rbp), %rdi
callq 0x3150
movb %al, -0x3a1(%rbp)
jmp 0x482e
movb -0x3a1(%rbp), %al
testb $0x1, %al
jne 0x48b3
jmp 0x483a
movq 0x77af(%rip), %rdi      # 0xbff0
leaq 0x3868(%rip), %rsi      # 0x80b0
callq 0x31b0
movq %rax, -0x3b0(%rbp)
jmp 0x4856
movq -0x340(%rbp), %rsi
movq -0x3b0(%rbp), %rdi
callq 0x31a0
movq %rax, -0x3b8(%rbp)
jmp 0x4872
movq -0x3b8(%rbp), %rdi
movq 0x7730(%rip), %rsi      # 0xbfb0
callq 0x3200
jmp 0x4887
movw $0xffff, -0x2(%rbp)     # imm = 0xFFFF
movl $0x1, -0x108(%rbp)
jmp 0x4910
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x328(%rbp), %rdi
callq 0x3310
jmp 0x4932
xorl %eax, %eax
movl %eax, %esi
leaq -0x38(%rbp), %rdi
movq %rdi, -0x3c8(%rbp)
callq 0x50b0
movq -0x3c8(%rbp), %rdi
movq %rax, -0x3c0(%rbp)
callq 0x5090
movq -0x3c0(%rbp), %rsi
movq %rax, %rdx
leaq -0x328(%rbp), %rdi
callq 0x32a0
jmp 0x48f2
leaq -0x328(%rbp), %rdi
callq 0x32f0
jmp 0x4900
movw $0x0, -0x2(%rbp)
movl $0x1, -0x108(%rbp)
leaq -0x328(%rbp), %rdi
callq 0x3310
leaq -0x38(%rbp), %rdi
callq 0x51e0
movw -0x2(%rbp), %ax
addq $0x3d0, %rsp            # imm = 0x3D0
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x51e0
movq -0x48(%rbp), %rdi
callq 0x33d0
nopw %cs:(%rax,%rax)
nop