// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0xd2c0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x12210
movq $0x0, -0x10(%rbp)
movq -0xb8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
callq 0xd2c0
movq %rax, %rcx
movq -0xc0(%rbp), %rax
cmpq (%rcx), %rax
jae 0xb918
leaq -0x78(%rbp), %rdi
callq 0xd2e0
movq -0xb8(%rbp), %rdi
callq 0xd2c0
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x12210
jmp 0xb510
movq -0xb8(%rbp), %rdi
callq 0xd2c0
movq %rax, %rdi
callq 0x11e90
movl %eax, -0xc4(%rbp)
jmp 0xb52c
movq -0xb8(%rbp), %rdi
movl -0xc4(%rbp), %eax
movl %eax, -0x78(%rbp)
callq 0xd2c0
movq %rax, %rdi
callq 0x11e90
movl %eax, -0xc8(%rbp)
jmp 0xb551
movq -0xb8(%rbp), %rdi
movl -0xc8(%rbp), %eax
movl %eax, -0x74(%rbp)
callq 0xd2c0
movq %rax, %rdi
callq 0x11e90
movl %eax, -0xcc(%rbp)
jmp 0xb576
movq -0xb8(%rbp), %rdi
movl -0xcc(%rbp), %eax
movl %eax, -0x70(%rbp)
callq 0xd2c0
movq %rax, %rdi
callq 0x11fc0
movb %al, -0xcd(%rbp)
jmp 0xb59b
movq -0xb8(%rbp), %rdi
movb -0xcd(%rbp), %al
movb %al, -0x85(%rbp)
callq 0xd2c0
movq %rax, %rdi
callq 0x11fc0
movb %al, -0xce(%rbp)
jmp 0xb5c3
movq -0xb8(%rbp), %rdi
movb -0xce(%rbp), %al
movb %al, -0x6c(%rbp)
callq 0xd2c0
movq %rax, %rdi
callq 0x11fc0
movb %al, -0xcf(%rbp)
jmp 0xb5e8
movq -0xb8(%rbp), %rdi
movb -0xcf(%rbp), %al
movb %al, -0x6b(%rbp)
callq 0xd2c0
movq %rax, %rdi
callq 0x11fc0
movb %al, -0xd0(%rbp)
jmp 0xb60d
movq -0xb8(%rbp), %rdi
movb -0xd0(%rbp), %al
movb %al, -0x86(%rbp)
callq 0xd2c0
movq %rax, %rdi
callq 0x11e90
movl %eax, -0xd4(%rbp)
jmp 0xb635
movq -0xb8(%rbp), %rdi
movl -0xd4(%rbp), %eax
movw %ax, -0x6a(%rbp)
callq 0xd2c0
movq %rax, %rdi
callq 0x11f70
movw %ax, -0xd6(%rbp)
jmp 0xb65c
movq -0xb8(%rbp), %rdi
movw -0xd6(%rbp), %ax
movw %ax, -0x68(%rbp)
callq 0xd2c0
movq %rax, %rdi
movl $0x2, %esi
callq 0x12230
jmp 0xb682
movq -0xb8(%rbp), %rdi
callq 0xd2c0
movq %rax, %rdi
callq 0x11e90
movl %eax, -0xdc(%rbp)
jmp 0xb69e
movq -0xb8(%rbp), %rdi
movl -0xdc(%rbp), %eax
movl %eax, -0x64(%rbp)
callq 0xd2c0
movq %rax, %rdi
callq 0x11e90
movl %eax, -0xe0(%rbp)
jmp 0xb6c3
movq -0xb8(%rbp), %rdi
movl -0xe0(%rbp), %eax
movl %eax, -0x60(%rbp)
callq 0xd2c0
movq %rax, %rdi
movl $0x2, %esi
callq 0x12230
jmp 0xb6e7
movq -0xb8(%rbp), %rdi
callq 0xd2c0
movq %rax, %rdi
callq 0x11f70
movw %ax, -0xe2(%rbp)
jmp 0xb704
movq -0xb8(%rbp), %rdi
movw -0xe2(%rbp), %ax
movw %ax, -0x5c(%rbp)
callq 0xd2c0
movq %rax, %rdi
callq 0x11e90
movl %eax, -0xe8(%rbp)
jmp 0xb72b
movq -0xb8(%rbp), %rdi
movl -0xe8(%rbp), %eax
movl %eax, -0x58(%rbp)
callq 0xd2c0
movq %rax, %rdi
callq 0x11f70
movw %ax, -0xea(%rbp)
jmp 0xb751
movq -0xb8(%rbp), %rdi
movw -0xea(%rbp), %ax
movw %ax, -0x88(%rbp)
callq 0xd2c0
movq %rax, %rdi
callq 0x11f70
movw %ax, -0xec(%rbp)
jmp 0xb77c
movw -0xec(%rbp), %ax
movw %ax, -0x8a(%rbp)
movl $0x0, -0x90(%rbp)
movsbl -0x6c(%rbp), %eax
cmpl $0x0, %eax
jne 0xb824
movq -0xb8(%rbp), %rdi
movl $0x1, -0x90(%rbp)
callq 0xd2c0
movq %rax, %rdi
movq -0x10(%rbp), %rax
movzwl -0x88(%rbp), %ecx
leaq 0xa(%rax,%rcx), %rsi
callq 0x12210
jmp 0xb7d1
movq -0xb8(%rbp), %rdi
callq 0xd2c0
movq %rax, %rdi
callq 0x11fc0
movb %al, -0xed(%rbp)
jmp 0xb7ed
movb -0xed(%rbp), %al
movb %al, -0x6c(%rbp)
movsbl -0x6c(%rbp), %eax
cmpl $0x0, %eax
jne 0xb822
movb $0xa, -0x6c(%rbp)
jmp 0xb822
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x94a0
jmp 0xb925
jmp 0xb824
movq -0xb8(%rbp), %rdi
callq 0xd2c0
movq %rax, %rdi
movq -0x10(%rbp), %rcx
movzwl -0x88(%rbp), %eax
addq %rax, %rcx
movslq -0x90(%rbp), %rax
leaq 0xa(%rax,%rcx), %rsi
callq 0x12210
jmp 0xb854
movq -0xb8(%rbp), %rdi
callq 0xd2c0
movq %rax, %rsi
movsbl -0x6c(%rbp), %edx
leaq -0xb0(%rbp), %rdi
callq 0x120a0
jmp 0xb875
leaq -0x78(%rbp), %rdi
addq $0x28, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x6390
leaq -0xb0(%rbp), %rdi
callq 0x61e0
movq -0x10(%rbp), %rax
movzwl -0x8a(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movzbl -0x86(%rbp), %eax
cmpl $0x1, %eax
jne 0xb8de
movl -0x78(%rbp), %eax
shll $0x9, %eax
movl %eax, -0x78(%rbp)
movzbl -0x85(%rbp), %eax
andl $0xf1f, %eax            # imm = 0xF1F
movzbl -0x85(%rbp), %ecx
andl $0xe0, %ecx
shll $0x8, %ecx
orl %ecx, %eax
movw %ax, -0x68(%rbp)
movq -0xb8(%rbp), %rdi
movl $0x0, -0x18(%rbp)
movslq -0x78(%rbp), %rcx
movq -0x10(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x10(%rbp)
addq $0x10, %rdi
leaq -0x78(%rbp), %rsi
callq 0xd340
jmp 0xb90a
leaq -0x78(%rbp), %rdi
callq 0x94a0
jmp 0xb4c3
movb $0x1, %al
andb $0x1, %al
addq $0xf0, %rsp
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x6540
nop