// File: /workspace/github_c_makefile_star2_O3/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 0x98ae
movq (%rbx), %rdi
cmpq $0x0, (%rdi)
je 0x710e
leaq 0x40(%rsp), %r13
leaq 0x10(%rbx), %rax
movq %rax, 0x70(%rsp)
xorl %r12d, %r12d
leaq 0x78(%rsp), %r15
movq %r13, 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 0x98ae
movq (%rbx), %rdi
callq 0x96e8
movl %eax, 0x8(%rsp)
movq (%rbx), %rdi
callq 0x96e8
movl %eax, 0xc(%rsp)
movq (%rbx), %rdi
callq 0x96e8
movl %eax, 0x10(%rsp)
movq (%rbx), %rdi
callq 0x9780
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x9780
movb %al, 0x14(%rsp)
movq (%rbx), %rdi
callq 0x9780
movb %al, 0x15(%rsp)
movq (%rbx), %rdi
callq 0x9780
movl %eax, %r14d
movq (%rbx), %rdi
callq 0x96e8
movw %ax, 0x16(%rsp)
movq (%rbx), %rdi
callq 0x975e
movw %ax, 0x18(%rsp)
movq (%rbx), %rdi
movl $0x2, %esi
callq 0x98b8
movq (%rbx), %rdi
callq 0x96e8
movl %eax, 0x1c(%rsp)
movq (%rbx), %rdi
callq 0x96e8
movl %eax, 0x20(%rsp)
movq (%rbx), %rdi
movl $0x2, %esi
callq 0x98b8
movq (%rbx), %rdi
callq 0x975e
movw %ax, 0x24(%rsp)
movq (%rbx), %rdi
callq 0x96e8
movb %bpl, 0x7(%rsp)
movq %r15, %rbp
movl %eax, 0x28(%rsp)
movq (%rbx), %rdi
callq 0x975e
movl %eax, %r15d
movq (%rbx), %rdi
callq 0x975e
movl %eax, %r13d
cmpb $0x0, 0x14(%rsp)
movzwl %r15w, %eax
je 0x6fff
leaq (%r12,%rax), %r15
addq $0xa, %r15
xorl %eax, %eax
jmp 0x7030
movq (%rbx), %rdi
leaq (%r12,%rax), %r15
addq $0xa, %r15
movq %r15, %rsi
callq 0x98ae
movq (%rbx), %rdi
callq 0x9780
testb %al, %al
movzbl %al, %eax
movl $0xa, %ecx
cmovel %ecx, %eax
movb %al, 0x14(%rsp)
movl $0x1, %eax
addq %rax, %r15
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x98ae
movq (%rbx), %rsi
movsbl 0x14(%rsp), %edx
movq %rbp, %r15
movq %rbp, %rdi
callq 0x980a
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x5280
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x7080
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x5230
movzwl %r13w, %eax
addq %r12, %rax
movq %rax, 0x50(%rsp)
movl 0x8(%rsp), %r15d
cmpb $0x1, %r14b
jne 0x70b7
shll $0x9, %r15d
movl %r15d, 0x8(%rsp)
movzbl 0x7(%rsp), %eax
movl %eax, %ecx
shll $0x8, %ecx
orl %eax, %ecx
andl $0xe01f, %ecx           # imm = 0xE01F
movw %cx, 0x18(%rsp)
movl $0x0, 0x68(%rsp)
movq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7d18
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x70dd
callq 0x671a
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %r13
cmpq %r13, %rdi
je 0x70f9
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5230
movslq %r15d, %rax
addq %rax, %r12
movq (%rbx), %rdi
cmpq (%rdi), %r12
movq %rbp, %r15
jb 0x6eed
movb $0x1, %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x712e
jmp 0x712e
jmp 0x712e
jmp 0x712e
jmp 0x712e
jmp 0x712e
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x7d42
movq %rbx, %rdi
callq 0x53e0
nop