// File: /workspace/github_c_makefile_star2_O1/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 0x4530
testl %ebp, %ebp
je 0x40ea
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 0x4cf2
movq (%r14), %rdi
callq 0x4bc4
movl %eax, 0x3c(%rsp)
movq (%r14), %rdi
callq 0x4bc4
movl %eax, %r13d
movq (%r14), %rdi
movl $0x2, %esi
callq 0x4cfc
movq (%r14), %rdi
callq 0x4ba2
movl %eax, %ebp
movq (%r14), %rdi
movl $0x5, %esi
callq 0x4cfc
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movl $0xf4, %ebx
movq (%r14), %rdi
callq 0x4bc4
movb %al, 0x18(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x45f4
decl %ebx
jne 0x401e
movq 0x40(%rsp), %rdi
cmpq %rdi, 0x48(%rsp)
je 0x40cb
xorl %ebx, %ebx
movzbl (%rdi,%rbx), %ecx
testq %rcx, %rcx
je 0x40b5
movq (%r14), %rax
shll $0xc, %ecx
movzbl 0x1(%rdi,%rbx), %esi
shll $0x8, %esi
addq %rcx, %rsi
movq %rax, %rdi
callq 0x4cf2
movq (%r14), %rsi
movq %r12, %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x4cbc
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
subq %rsi, %rdx
je 0x4098
movzwl %bp, %edi
addq (%rsp), %rdi
callq 0x32b0
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x40af
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x31b0
addl $0x100, %ebp            # imm = 0x100
addq $0x2, %rbx
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rax
subq %rdi, %rax
cmpq %rbx, %rax
ja 0x404b
testq %rdi, %rdi
je 0x40dd
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x31b0
movl 0x3c(%rsp), %ebp
testb %bpl, %bpl
jne 0x3fb0
movzbl 0x38(%rsp), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jb 0x413c
cmpl $0x4, %eax
je 0x416c
testl %eax, %eax
movq 0x30(%rsp), %r14
jne 0x420f
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
je 0x420f
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %esi, %esi
cmpb $0x0, (%rax,%rsi)
je 0x431e
incq %rsi
cmpq %rsi, %rcx
jne 0x4125
jmp 0x420f
leaq 0x18(%rsp), %rdi
movq %rsp, %rsi
callq 0x49ba
movq 0x18(%rsp), %rdi
callq 0x4ba2
movq 0x18(%rsp), %rsi
movzwl %ax, %edx
leaq 0x40(%rsp), %rdi
callq 0x4cbc
movq 0x30(%rsp), %r14
jmp 0x41af
leaq 0x18(%rsp), %rdi
movq %rsp, %rsi
callq 0x49ba
movq 0x30(%rsp), %r14
movq 0x18(%rsp), %rdi
callq 0x4ba2
cmpq $0x0, 0x8(%r14)
je 0x430b
movq 0x18(%rsp), %rdi
callq 0x4ba2
movq 0x18(%rsp), %rsi
movzwl %ax, %edx
leaq 0x40(%rsp), %rdi
callq 0x4cbc
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x40(%rsp), %rax
movq %rax, (%rsp)
movups 0x48(%rsp), %xmm0
movups %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
testq %rdi, %rdi
je 0x41e9
subq %rdi, %rsi
callq 0x31b0
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x4200
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x31b0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x420f
callq 0x48b4
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
movl $0x34, %edx
callq 0x3350
leaq 0xb0(%rsp), %rdi
callq 0x3060
testb %al, %al
je 0x4256
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
subq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x3220
leaq 0x40(%rsp), %rdi
callq 0x3260
xorl %ebx, %ebx
jmp 0x42af
movq 0x3d93(%rip), %rdi      # 0x7ff0
leaq 0xe68(%rip), %rsi       # 0x50cc
movl $0x11, %edx
callq 0x31f0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x3d74(%rip), %rdi      # 0x7ff0
callq 0x31f0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3180
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movw $0xffff, %bx            # imm = 0xFFFF
movq %rax, %rdi
callq 0x3120
movq 0x3d02(%rip), %rax      # 0x7fb8
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 0x3150
leaq 0x138(%rsp), %rdi
callq 0x30b0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x42f7
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31b0
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 0x42e1
callq 0x48b4
jmp 0x42e1
movq %rsp, %rdi
callq 0x4590
jmp 0x420f
jmp 0x4360
jmp 0x4337
jmp 0x4337
jmp 0x4337
jmp 0x4360
jmp 0x4337
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4383
callq 0x48b4
jmp 0x4383
jmp 0x4360
jmp 0x4360
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x3280
jmp 0x4383
jmp 0x4360
movq %rax, %rbx
jmp 0x4383
jmp 0x4369
jmp 0x4369
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x4383
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x31b0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4399
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31b0
movq %rbx, %rdi
callq 0x3320
nop