// File: /workspace/github_c_makefile_star2_O2/mrkite[P]regs/src/omf.cc
// Function: Segment::map(std::vector<unsigned int, std::allocator<unsigned int>>&, bool)
bool Segment::map(std::vector<uint32_t> &memory, bool force) {
  if (mapped) {
    return false;
  }
  if (org && ((org & 0xffff) != 0 || (kind & 0x800) == 0)) {
    mapped = org;
    // verif we're not overlapping
    for (auto m : memory) {
      if (mapped < m && mapped + length > m) {
        fprintf(stderr, "Segment %d (%s) collides with another segment!\n", segnum, name.c_str());
        return false;
      }
    }
    memory.push_back(mapped);
    memory.push_back(mapped + length);
    if (mapped < memory[0]) {  // below the min?
      memory[0] = mapped;
    }
    return true;
  }
  if (force) {
    for (auto it = memory.begin(); it != memory.end(); it++) {
      auto base = *it;
      // skip special?
      if ((kind & 0x1f) != 0x12 && (base & 0xff0000) == 0) {
        base += 0x10000;  // not in bank 0
      }
      // are we aligned?
      if (align != 0 && (base & (align - 1)) != 0) {
        base += align;
        base &= ~(align - 1);
      }
      // does it cross bank boundaries?
      if (banksize != 0 &&
          (((base & (banksize - 1)) + length) & ~(banksize - 1)) != 0) {
        base += banksize;
        base &= ~(banksize - 1);
      }
      // does it fit?!
      it++;
      uint32_t end = *it;
      if (base < end && end - base >= length) {
        mapped = base;
        memory.push_back(mapped);
        memory.push_back(mapped + length);
        return true;
      }
    }
    fprintf(stderr, "Segment %d (%s) doesn't fit in memory\n", segnum, name.c_str());
  }
  return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x0, 0x60(%rdi)
je 0x7404
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %r14
movq %rdi, %rbx
movl 0x14(%rdi), %eax
testl %eax, %eax
je 0x7460
testw %ax, %ax
jne 0x741c
testb $0x8, 0x11(%rbx)
jne 0x7460
movl %eax, 0x60(%rbx)
movq (%r14), %rcx
movq 0x8(%r14), %rdx
movl 0x8(%rbx), %esi
addl %eax, %esi
cmpq %rdx, %rcx
je 0x7539
movl (%rcx), %edi
addq $0x4, %rcx
cmpl %edi, %eax
jae 0x742b
cmpl %edi, %esi
jbe 0x742b
movq 0x18b7f(%rip), %rax     # 0x1ffc8
movq (%rax), %rdi
movswl 0x1c(%rbx), %edx
movq 0x28(%rbx), %rcx
leaq 0xcde8(%rip), %rsi      # 0x14243
jmp 0x7592
testb %dl, %dl
je 0x73f1
movq (%r14), %rax
movq 0x8(%r14), %rcx
movzwl 0x10(%rbx), %edx
andl $0x1f, %edx
movl 0x8(%rbx), %esi
movl 0x18(%rbx), %edi
leal -0x1(%rdi), %r8d
movl %edi, %r9d
negl %r9d
movswl 0xe(%rbx), %r10d
leal -0x1(%r10), %r11d
movl %r10d, %ebp
negl %ebp
cmpq %rcx, %rax
je 0x7579
movl (%rax), %r15d
movl %r15d, %r13d
orl $0x10000, %r13d         # imm = 0x10000
testl $0xff0000, %r15d        # imm = 0xFF0000
cmovnel %r15d, %r13d
cmpw $0x12, %dx
cmovel %r15d, %r13d
leal (%rdi,%r13), %r12d
andl %r9d, %r12d
testl %r8d, %r13d
cmovel %r13d, %r12d
testl %edi, %edi
cmovel %r13d, %r12d
testw %r10w, %r10w
je 0x74e7
movl %r11d, %r15d
andl %r12d, %r15d
addl %esi, %r15d
testl %ebp, %r15d
je 0x74e7
addl %r10d, %r12d
andl %ebp, %r12d
movl 0x4(%rax), %r15d
subl %r12d, %r15d
setbe %r13b
cmpl %esi, %r15d
setb %r15b
orb %r13b, %r15b
addq $0x8, %rax
testb %r15b, %r15b
jne 0x7490
movl %r12d, 0x60(%rbx)
leaq 0x4(%rsp), %r15
movl %r12d, (%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x8146
movl 0x8(%rbx), %eax
addl 0x60(%rbx), %eax
movl %eax, (%r15)
leaq 0x4(%rsp), %rsi
movq %r14, %rdi
callq 0x8146
movb $0x1, %bl
jmp 0x73f3
leaq 0x4(%rsp), %r15
movl %eax, (%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x8146
movl 0x8(%rbx), %eax
addl 0x60(%rbx), %eax
movl %eax, (%r15)
leaq 0x4(%rsp), %rsi
movq %r14, %rdi
callq 0x8146
movl 0x60(%rbx), %eax
movq (%r14), %rcx
movb $0x1, %bl
cmpl (%rcx), %eax
jae 0x73f3
movl %eax, (%rcx)
jmp 0x73f3
movq 0x18a48(%rip), %rax     # 0x1ffc8
movq (%rax), %rdi
movswl 0x1c(%rbx), %edx
movq 0x28(%rbx), %rcx
leaq 0xcce1(%rip), %rsi      # 0x14273
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x5300
jmp 0x73f3