// File: /workspace/github_c_makefile_star2_O3/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:
cmpl $0x0, 0x60(%rdi)
je 0x7af3
xorl %eax, %eax
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
movl 0x14(%rdi), %eax
testl %eax, %eax
je 0x7b64
testw %ax, %ax
jne 0x7b17
testb $0x8, 0x11(%r14)
jne 0x7b64
movl %eax, 0x60(%r14)
movq (%rbx), %rcx
movq 0x8(%rbx), %rsi
cmpq %rsi, %rcx
je 0x7b44
movl 0x8(%r14), %edx
addl %eax, %edx
movl (%rcx), %edi
cmpl %edi, %eax
jae 0x7b3b
cmpl %edi, %edx
ja 0x7cd1
addq $0x4, %rcx
cmpq %rsi, %rcx
jne 0x7b2d
movl %eax, 0x4(%rsp)
movq 0x10(%rbx), %rcx
cmpq %rcx, %rsi
je 0x7c3e
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x7c53
testb %dl, %dl
je 0x7cd9
movq (%rbx), %rax
movq 0x8(%rbx), %rsi
cmpq %rsi, %rax
je 0x7c12
movzwl 0x10(%r14), %ecx
andl $0x1f, %ecx
movl 0x8(%r14), %edx
movl 0x18(%r14), %edi
leal -0x1(%rdi), %r8d
movl %edi, %r9d
negl %r9d
movswl 0xe(%r14), %r10d
leal -0x1(%r10), %r11d
movl %r10d, %ebp
negl %ebp
movl (%rax), %r15d
movl %r15d, %r12d
orl $0x10000, %r12d         # imm = 0x10000
testl $0xff0000, %r15d        # imm = 0xFF0000
cmovnel %r15d, %r12d
cmpw $0x12, %cx
cmovel %r15d, %r12d
leal (%r12,%rdi), %r15d
andl %r9d, %r15d
testl %r8d, %r12d
cmovel %r12d, %r15d
testl %edi, %edi
cmovel %r12d, %r15d
testw %r10w, %r10w
je 0x7bf2
movl %r11d, %r12d
andl %r15d, %r12d
addl %edx, %r12d
testl %ebp, %r12d
je 0x7bf2
addl %r10d, %r15d
andl %ebp, %r15d
movl 0x4(%rax), %r12d
subl %r15d, %r12d
setbe %r13b
cmpl %edx, %r12d
setb %r12b
orb %r13b, %r12b
je 0x7c1f
addq $0x8, %rax
cmpq %rsi, %rax
jne 0x7ba4
movq %r14, %rdi
callq 0x549d
jmp 0x7cd9
movl %r15d, 0x60(%r14)
movl %r15d, 0x4(%rsp)
movq 0x10(%rbx), %rax
cmpq %rax, %rsi
je 0x7c8e
movl %r15d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x7ca3
leaq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x907e
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rcx
movl 0x8(%r14), %eax
addl 0x60(%r14), %eax
movl %eax, 0x4(%rsp)
cmpq %rcx, %rsi
je 0x7c70
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x7c7d
leaq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x907e
movl 0x60(%r14), %ecx
movq (%rbx), %rdx
movb $0x1, %al
cmpl (%rdx), %ecx
jae 0x7cdb
movl %ecx, (%rdx)
jmp 0x7cdb
leaq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x907e
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rax
movl 0x8(%r14), %ecx
addl 0x60(%r14), %ecx
movl %ecx, 0x4(%rsp)
cmpq %rax, %rsi
je 0x7cc0
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x7ccd
leaq 0x4(%rsp), %rdx
movq %rbx, %rdi
callq 0x907e
movb $0x1, %al
jmp 0x7cdb
movq %r14, %rdi
callq 0x547a
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq