// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %r9, %r14
movq %rcx, 0x30(%rsp)
movl %edx, 0x38(%rsp)
movl %esi, %r13d
movl %edi, %ebp
shll $0x8, %r8d
leaq 0x100(%r8), %rsi
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
callq 0x34f2
testl %ebp, %ebp
je 0x30a8
leaq 0x40(%rsp), %r15
leaq 0x18(%rsp), %r12
movq (%r14), %rdi
movzbl %bpl, %eax
shll $0xc, %eax
movzbl %r13b, %ecx
shll $0x8, %ecx
leaq (%rax,%rcx), %rsi
incq %rsi
callq 0x3cb6
movq (%r14), %rdi
callq 0x3b88
movl %eax, 0x3c(%rsp)
movq (%r14), %rdi
callq 0x3b88
movl %eax, %r13d
movq (%r14), %rdi
movl $0x2, %esi
callq 0x3cc0
movq (%r14), %rdi
callq 0x3b66
movl %eax, %ebp
movq (%r14), %rdi
movl $0x5, %esi
callq 0x3cc0
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movl $0xf4, %ebx
movq (%r14), %rdi
callq 0x3b88
movb %al, 0x18(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x35b6
decl %ebx
jne 0x2fcc
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rax
cmpq %rdi, %rax
je 0x3089
xorl %ebx, %ebx
movzbl (%rdi,%rbx), %ecx
testq %rcx, %rcx
je 0x3076
movq (%r14), %rax
shll $0xc, %ecx
movzbl 0x1(%rdi,%rbx), %esi
shll $0x8, %esi
addq %rcx, %rsi
movq %rax, %rdi
callq 0x3cb6
movq (%r14), %rsi
movq %r12, %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x3c80
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rdx
subq %rax, %rdx
je 0x3051
movzwl %bp, %edi
addq (%rsp), %rdi
movq %rax, %rsi
callq 0x22b0
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x3066
movq 0x28(%rsp), %rsi
subq %rax, %rsi
movq %rax, %rdi
callq 0x21b0
addl $0x100, %ebp            # imm = 0x100
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rax
addq $0x2, %rbx
movq %rax, %rcx
subq %rdi, %rcx
cmpq %rbx, %rcx
ja 0x2ffc
testq %rdi, %rdi
movl 0x3c(%rsp), %ebp
je 0x309f
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
testb %bpl, %bpl
jne 0x2f5e
movzbl 0x38(%rsp), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jb 0x30fa
cmpl $0x4, %eax
je 0x312a
testl %eax, %eax
movq 0x30(%rsp), %r14
jne 0x31cd
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
je 0x31cd
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %esi, %esi
cmpb $0x0, (%rax,%rsi)
je 0x32dc
incq %rsi
cmpq %rsi, %rcx
jne 0x30e3
jmp 0x31cd
leaq 0x18(%rsp), %rdi
movq %rsp, %rsi
callq 0x397e
movq 0x18(%rsp), %rdi
callq 0x3b66
movq 0x18(%rsp), %rsi
movzwl %ax, %edx
leaq 0x40(%rsp), %rdi
callq 0x3c80
movq 0x30(%rsp), %r14
jmp 0x316d
leaq 0x18(%rsp), %rdi
movq %rsp, %rsi
callq 0x397e
movq 0x30(%rsp), %r14
movq 0x18(%rsp), %rdi
callq 0x3b66
cmpq $0x0, 0x8(%r14)
je 0x32c9
movq 0x18(%rsp), %rdi
callq 0x3b66
movq 0x18(%rsp), %rsi
movzwl %ax, %edx
leaq 0x40(%rsp), %rdi
callq 0x3c80
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
movaps 0x40(%rsp), %xmm0
movaps %xmm0, (%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
testq %rdi, %rdi
je 0x31be
subq %rdi, %rsi
callq 0x21b0
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x31be
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x31cd
callq 0x3876
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
movl $0x34, %edx
callq 0x2350
leaq 0xb0(%rsp), %rdi
callq 0x2060
testb %al, %al
je 0x3214
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
subq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x2220
leaq 0x40(%rsp), %rdi
callq 0x2260
xorl %ebx, %ebx
jmp 0x326d
movq 0x3dcd(%rip), %rdi      # 0x6fe8
leaq 0xeaa(%rip), %rsi       # 0x40cc
movl $0x11, %edx
callq 0x21f0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x3dae(%rip), %rdi      # 0x6fe8
callq 0x21f0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movw $0xffff, %bx            # imm = 0xFFFF
movq %rax, %rdi
callq 0x2120
movq 0x3d3c(%rip), %rax      # 0x6fb0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x48(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x40(%rsp,%rcx)
callq 0x2150
leaq 0x138(%rsp), %rdi
callq 0x20b0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x32b5
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
movl %ebx, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ebx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x329f
callq 0x3876
jmp 0x329f
movq %rsp, %rdi
callq 0x3552
jmp 0x31cd
jmp 0x331e
jmp 0x32f5
jmp 0x32f5
jmp 0x32f5
jmp 0x331e
jmp 0x32f5
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3341
callq 0x3876
jmp 0x3341
jmp 0x331e
jmp 0x331e
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2280
jmp 0x3341
jmp 0x331e
movq %rax, %rbx
jmp 0x3341
jmp 0x3327
jmp 0x3327
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x3341
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x3357
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2320
nop