// File: /workspace/github_c_makefile_star2_O2/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 %ebp, %ebp
xorl %esi, %esi
callq 0x8a68
leaq 0x10(%rbx), %rax
movq %rax, 0x70(%rsp)
pushq $0x2
popq %r14
leaq 0x78(%rsp), %r13
movq (%rbx), %rdi
cmpq (%rdi), %rbp
jae 0x6bff
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
andq $0x0, 0x38(%rsp)
movb $0x0, 0x40(%rsp)
leaq 0x30(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rax)
movq %rbp, %rsi
callq 0x8a68
movq (%rbx), %rdi
callq 0x88ce
movl %eax, 0x8(%rsp)
movq (%rbx), %rdi
callq 0x88ce
movl %eax, 0xc(%rsp)
movq (%rbx), %rdi
callq 0x88ce
movl %eax, 0x10(%rsp)
movq (%rbx), %rdi
callq 0x8966
movl %eax, %r15d
movq (%rbx), %rdi
callq 0x8966
movb %al, 0x14(%rsp)
movq (%rbx), %rdi
callq 0x8966
movb %al, 0x15(%rsp)
movq (%rbx), %rdi
callq 0x8966
movl %eax, %r12d
movq (%rbx), %rdi
callq 0x88ce
movw %ax, 0x16(%rsp)
movq (%rbx), %rdi
callq 0x8944
movw %ax, 0x18(%rsp)
movq (%rbx), %rdi
movq %r14, %rsi
callq 0x8a72
movq (%rbx), %rdi
callq 0x88ce
movl %eax, 0x1c(%rsp)
movq (%rbx), %rdi
callq 0x88ce
movl %eax, 0x20(%rsp)
movq (%rbx), %rdi
movq %r14, %rsi
callq 0x8a72
movq (%rbx), %rdi
callq 0x8944
movw %ax, 0x24(%rsp)
movq (%rbx), %rdi
callq 0x88ce
movl %r12d, %r14d
movb %r15b, 0x7(%rsp)
movq %rbp, %r12
movl %eax, 0x28(%rsp)
movq (%rbx), %rdi
callq 0x8944
movl %eax, %ebp
movq (%rbx), %rdi
callq 0x8944
movl %eax, %r15d
cmpb $0x0, 0x14(%rsp)
movzwl %bp, %eax
je 0x6b38
leaq (%r12,%rax), %rbp
addq $0xa, %rbp
xorl %eax, %eax
jmp 0x6b65
movq (%rbx), %rdi
leaq (%r12,%rax), %rbp
addq $0xa, %rbp
movq %rbp, %rsi
callq 0x8a68
movq (%rbx), %rdi
callq 0x8966
testb %al, %al
movzbl %al, %eax
pushq $0xa
popq %rcx
cmovel %ecx, %eax
movb %al, 0x14(%rsp)
pushq $0x1
popq %rax
addq %rax, %rbp
movq (%rbx), %rdi
movq %rbp, %rsi
callq 0x8a68
movq (%rbx), %rsi
movsbl 0x14(%rsp), %edx
movq %r13, %rbp
movq %r13, %rdi
callq 0x89d8
leaq 0x30(%rsp), %rdi
movq %rbp, %rsi
callq 0x5250
movq %rbp, %rdi
callq 0x5140
movzwl %r15w, %eax
addq %r12, %rax
movq %rax, 0x50(%rsp)
movl 0x8(%rsp), %ebp
cmpb $0x1, %r14b
jne 0x6bcf
shll $0x9, %ebp
movl %ebp, 0x8(%rsp)
movzbl 0x7(%rsp), %eax
movl %eax, %ecx
shll $0x8, %ecx
orl %eax, %ecx
andl $0xe01f, %ecx           # imm = 0xE01F
movw %cx, 0x18(%rsp)
andl $0x0, 0x68(%rsp)
movq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x75cc
movslq %ebp, %rax
movq %r12, %rbp
addq %rax, %rbp
leaq 0x8(%rsp), %rdi
callq 0x645a
pushq $0x2
popq %r14
jmp 0x6a1a
movb $0x1, %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6c1f
jmp 0x6c1f
jmp 0x6c1f
jmp 0x6c1f
jmp 0x6c1f
jmp 0x6c1f
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x645a
movq %rbx, %rdi
callq 0x53c0