// File: /workspace/github_c_makefile_star2_O2/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, 0x38(%rsp)
movl %edx, 0x30(%rsp)
movl %esi, %r12d
movl %edi, %ebp
shll $0x8, %r8d
leaq 0x100(%r8), %rsi
leaq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %r15
movq %r15, %rdx
callq 0x3082
movq %rsp, %rbx
testb %bpl, %bpl
je 0x2cc4
movq (%r14), %rdi
movzbl %bpl, %eax
shll $0xc, %eax
movzbl %r12b, %ecx
shll $0x8, %ecx
leaq (%rax,%rcx), %rsi
incq %rsi
callq 0x385c
movq (%r14), %rdi
callq 0x375a
movl %eax, 0x34(%rsp)
movq (%r14), %rdi
callq 0x375a
movl %eax, %ebp
movq (%r14), %rdi
pushq $0x2
popq %rsi
callq 0x3866
movq (%r14), %rdi
callq 0x3738
movl %eax, %r13d
movq (%r14), %rdi
pushq $0x5
popq %rsi
callq 0x3866
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movl $0xf4, %r12d
subl $0x1, %r12d
jb 0x2c39
movq (%r14), %rdi
callq 0x375a
movb %al, (%rsp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x31ca
jmp 0x2c1b
xorl %r12d, %r12d
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
subq %rax, %rcx
cmpq %r12, %rcx
jbe 0x2cb0
movzbl (%rax,%r12), %ecx
testq %rcx, %rcx
je 0x2caa
movq (%r14), %rdi
shll $0xc, %ecx
movzbl 0x1(%rax,%r12), %esi
shll $0x8, %esi
addq %rcx, %rsi
callq 0x385c
movq (%r14), %rsi
movq %rbx, %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x3826
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
subq %rsi, %rdx
je 0x2c9b
movzwl %r13w, %edi
addq 0x18(%rsp), %rdi
callq 0x2290
addl $0x100, %r13d           # imm = 0x100
movq %rbx, %rdi
callq 0x3150
addq $0x2, %r12
jmp 0x2c3c
movq %r15, %rdi
callq 0x3150
movl %ebp, %r12d
movl 0x34(%rsp), %ebp
jmp 0x2baa
movzbl 0x30(%rsp), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jb 0x2d1e
cmpl $0x4, %eax
movq 0x38(%rsp), %r14
je 0x2d4c
testl %eax, %eax
jne 0x2dab
movq 0x18(%rsp), %rax
movq %rax, %rcx
subq 0x20(%rsp), %rcx
pushq $-0x1
popq %rdx
leaq (%rcx,%rdx), %rsi
cmpq $-0x1, %rsi
je 0x2dab
leaq 0x1(%rdx), %rsi
cmpb $0x0, 0x1(%rax,%rdx)
movq %rsi, %rdx
jne 0x2cf3
leaq 0x18(%rsp), %rdi
callq 0x30d4
jmp 0x2dab
movq %rsp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x355c
movq 0x38(%rsp), %r14
movq (%rsp), %rdi
callq 0x3738
movq (%rsp), %rsi
movzwl %ax, %edx
leaq 0x40(%rsp), %rdi
callq 0x3826
jmp 0x2d87
movq %rsp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x355c
movq (%rsp), %rdi
callq 0x3738
cmpq $0x0, 0x8(%r14)
je 0x2e43
movq (%rsp), %rdi
callq 0x3738
movq (%rsp), %rsi
movzwl %ax, %edx
leaq 0x40(%rsp), %rdi
callq 0x3826
leaq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
callq 0x33d0
movq %rbx, %rdi
callq 0x3150
leaq 0x8(%rsp), %rdi
callq 0x3456
leaq 0x40(%rsp), %rdi
pushq $0x34
popq %rdx
movq %r14, %rsi
callq 0x2330
leaq 0xb0(%rsp), %rdi
callq 0x2050
testb %al, %al
je 0x2df1
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
subq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x21f0
leaq 0x40(%rsp), %rdi
callq 0x2240
xorl %ebx, %ebx
jmp 0x2e1b
movq 0x41f8(%rip), %rdi      # 0x6ff0
leaq 0x12cd(%rip), %rsi      # 0x40cc
callq 0x2160
movq %rax, %rdi
movq %r14, %rsi
callq 0x2150
movw $0xffff, %bx            # imm = 0xFFFF
movq %rax, %rdi
callq 0x20e0
leaq 0x40(%rsp), %rdi
callq 0x2260
leaq 0x18(%rsp), %rdi
callq 0x3150
movl %ebx, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ebx
leaq 0x8(%rsp), %rdi
callq 0x3456
jmp 0x2e25
jmp 0x2e81
jmp 0x2e5d
jmp 0x2e5d
jmp 0x2e5d
jmp 0x2e81
jmp 0x2e5d
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x3456
jmp 0x2e97
jmp 0x2e81
jmp 0x2e81
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2260
jmp 0x2e97
jmp 0x2e81
movq %rax, %rbx
jmp 0x2e97
jmp 0x2e8a
jmp 0x2e8a
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x3150
leaq 0x18(%rsp), %rdi
callq 0x3150
movq %rbx, %rdi
callq 0x2300
nop