// File: /workspace/github_c_makefile_star2_O1/mrkite[P]regs/src/omf.cc
// Function: OMF::loadSegments()
bool OMF::loadSegments() {
  handle->seek(0);
  for (uint64_t ofs = 0; ofs < handle->length;) {
    Segment seg;
    handle->seek(ofs);
    seg.bytecnt = handle->r32();
    seg.resspc = handle->r32();
    seg.length = handle->r32();
    auto kind = handle->r8();
    seg.lablen = handle->r8();
    seg.numlen = handle->r8();
    auto version = handle->r8();
    seg.banksize = handle->r32();
    seg.kind = handle->r16();
    handle->skip(2);  // undefined
    seg.org = handle->r32();
    seg.align = handle->r32();
    handle->skip(2);  // byte ordering
    seg.segnum = handle->r16();
    seg.entry = handle->r32();
    auto dispname = handle->r16();
    auto dispdata = handle->r16();
    auto skip = 0;
    if (seg.lablen == 0) {
      skip = 1;
      handle->seek(ofs + dispname + 0xa);
      seg.lablen = handle->r8();
      if (seg.lablen == 0) {
        seg.lablen = 0xa;
      }
    }
    // there are 2 names, one is always 10 bytes at dispname,
    // this is the "loadname" and it specifies the name by the linker
    // it is followed by the segname, which is the actual segment name.

    // check if load name is valid
    handle->seek(ofs + dispname + 0xa + skip);
    seg.name = handle->read(seg.lablen);
    seg.offset = ofs + dispdata;
    if (version == 1) {  // convert to v2
      seg.bytecnt *= 512;
      seg.kind = (kind & 0xf1f) | ((kind & 0xe0) << 8);
    }
    seg.mapped = 0;
    ofs += seg.bytecnt;
    segments.push_back(seg);
  }
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
xorl %esi, %esi
callq 0x9aca
movq (%rbx), %rdi
cmpq $0x0, (%rdi)
je 0x7130
leaq 0x40(%rsp), %r15
leaq 0x10(%rbx), %rax
movq %rax, 0x70(%rsp)
xorl %r12d, %r12d
movq %r15, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movb $0x0, 0x40(%rsp)
leaq 0x30(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rax)
movq %r12, %rsi
callq 0x9aca
movq (%rbx), %rdi
callq 0x9904
movl %eax, 0x8(%rsp)
movq (%rbx), %rdi
callq 0x9904
movl %eax, 0xc(%rsp)
movq (%rbx), %rdi
callq 0x9904
movl %eax, 0x10(%rsp)
movq (%rbx), %rdi
callq 0x999c
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x999c
movb %al, 0x14(%rsp)
movq (%rbx), %rdi
callq 0x999c
movb %al, 0x15(%rsp)
movq (%rbx), %rdi
callq 0x999c
movl %eax, %r14d
movq (%rbx), %rdi
callq 0x9904
movw %ax, 0x16(%rsp)
movq (%rbx), %rdi
callq 0x997a
movw %ax, 0x18(%rsp)
movq (%rbx), %rdi
movl $0x2, %esi
callq 0x9ad4
movq (%rbx), %rdi
callq 0x9904
movl %eax, 0x1c(%rsp)
movq (%rbx), %rdi
callq 0x9904
movl %eax, 0x20(%rsp)
movq (%rbx), %rdi
movl $0x2, %esi
callq 0x9ad4
movq (%rbx), %rdi
callq 0x997a
movw %ax, 0x24(%rsp)
movq (%rbx), %rdi
callq 0x9904
movl %eax, 0x28(%rsp)
movq (%rbx), %rdi
callq 0x997a
movl %eax, %r15d
movq (%rbx), %rdi
callq 0x997a
movl %eax, %r13d
cmpb $0x0, 0x14(%rsp)
movzwl %r15w, %r15d
je 0x7028
xorl %eax, %eax
jmp 0x7056
movq (%rbx), %rdi
leaq (%r12,%r15), %rsi
addq $0xa, %rsi
callq 0x9aca
movq (%rbx), %rdi
callq 0x999c
testb %al, %al
movzbl %al, %eax
movl $0xa, %ecx
cmovel %ecx, %eax
movb %al, 0x14(%rsp)
movl $0x1, %eax
movq (%rbx), %rdi
addq %r12, %r15
leaq (%rax,%r15), %rsi
addq $0xa, %rsi
callq 0x9aca
movq (%rbx), %rsi
movsbl 0x14(%rsp), %edx
leaq 0x78(%rsp), %r15
movq %r15, %rdi
callq 0x9a26
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x5280
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x70ad
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x5230
movzwl %r13w, %eax
addq %r12, %rax
movq %rax, 0x50(%rsp)
cmpb $0x1, %r14b
jne 0x70da
shll $0x9, 0x8(%rsp)
movzbl %bpl, %eax
movl %eax, %ecx
shll $0x8, %ecx
orl %eax, %ecx
andl $0xe01f, %ecx           # imm = 0xE01F
movw %cx, 0x18(%rsp)
movl $0x0, 0x68(%rsp)
movslq 0x8(%rsp), %r14
movq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7ea8
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x7105
callq 0x6752
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %r15
cmpq %r15, %rdi
je 0x7121
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5230
addq %r14, %r12
movq (%rbx), %rdi
cmpq (%rdi), %r12
jb 0x6f26
movb $0x1, %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7150
jmp 0x7150
jmp 0x7150
jmp 0x7150
jmp 0x7150
jmp 0x7150
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x7ed2
movq %rbx, %rdi
callq 0x53e0
nop