// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
cmpl $0x0, 0x60(%rdi)
je 0x7c33
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %rbx
movl 0x14(%rdi), %eax
testl %eax, %eax
je 0x7cf5
testw %ax, %ax
jne 0x7c50
testb $0x8, 0x11(%rbx)
jne 0x7cf5
movl %eax, 0x60(%rbx)
movq (%rsi), %rcx
movq %rsi, %r14
movq 0x8(%rsi), %rdx
cmpq %rdx, %rcx
sete %r15b
je 0x7cd1
movl 0x8(%rbx), %esi
addl %eax, %esi
movl (%rcx), %edi
cmpl %edi, %eax
setb %r8b
cmpl %edi, %esi
seta %dil
testb %dil, %r8b
jne 0x7ca6
addq $0x4, %rcx
cmpq %rdx, %rcx
sete %r15b
je 0x7cd1
movl (%rcx), %edi
cmpl %edi, %eax
setb %r8b
cmpl %edi, %esi
seta %dil
andb %r8b, %dil
addq $0x4, %rcx
cmpb $0x1, %dil
jne 0x7c82
movq 0x19323(%rip), %rax     # 0x20fd0
movq (%rax), %rdi
movswl 0x1c(%rbx), %edx
movq 0x28(%rbx), %rcx
leaq 0xf584(%rip), %rsi      # 0x17243
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x5310
testb %r15b, %r15b
je 0x7c22
movl 0x60(%rbx), %eax
movl %eax, 0xc(%rsp)
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x7dd3
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r14)
jmp 0x7de0
testb %dl, %dl
je 0x7c20
movq (%rsi), %rax
movq %rsi, 0x10(%rsp)
movq 0x8(%rsi), %rsi
cmpq %rsi, %rax
sete %r15b
je 0x7e6c
movzwl 0x10(%rbx), %ecx
andl $0x1f, %ecx
movl 0x8(%rbx), %edx
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
movl (%rax), %r12d
movl %r12d, %r13d
orl $0x10000, %r13d         # imm = 0x10000
testl $0xff0000, %r12d        # imm = 0xFF0000
cmovnel %r12d, %r13d
cmpw $0x12, %cx
cmovel %r12d, %r13d
leal (%rdi,%r13), %r12d
andl %r9d, %r12d
testl %r8d, %r13d
cmovel %r13d, %r12d
testl %edi, %edi
cmovel %r13d, %r12d
testw %r10w, %r10w
je 0x7d89
movl %r11d, %r13d
andl %r12d, %r13d
addl %edx, %r13d
testl %ebp, %r13d
je 0x7d89
addl %r10d, %r12d
andl %ebp, %r12d
movl 0x4(%rax), %r13d
subl %r12d, %r13d
setbe %r14b
cmpl %edx, %r13d
setb %r13b
orb %r14b, %r13b
je 0x7db2
addq $0x8, %rax
cmpq %rsi, %rax
sete %r15b
jne 0x7d3b
jmp 0x7e6c
movl %r12d, 0x60(%rbx)
movl %r12d, 0xc(%rsp)
movq 0x10(%rsp), %r14
cmpq 0x10(%r14), %rsi
je 0x7e25
movl %r12d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r14)
jmp 0x7e32
leaq 0xc(%rsp), %rdx
movq %r14, %rdi
callq 0x9276
movl 0x8(%rbx), %eax
addl 0x60(%rbx), %eax
movl %eax, 0xc(%rsp)
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x7e00
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r14)
jmp 0x7e0d
leaq 0xc(%rsp), %rdx
movq %r14, %rdi
callq 0x9276
movl 0x60(%rbx), %eax
movq (%r14), %rcx
movb $0x1, %bpl
cmpl (%rcx), %eax
jae 0x7c22
movl %eax, (%rcx)
jmp 0x7c22
leaq 0xc(%rsp), %rdx
movq %r14, %rdi
callq 0x9276
movl 0x8(%rbx), %eax
addl 0x60(%rbx), %eax
movl %eax, 0xc(%rsp)
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x7e52
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r14)
jmp 0x7e5f
leaq 0xc(%rsp), %rdx
movq %r14, %rdi
callq 0x9276
movb $0x1, %bpl
testb $0x1, %r15b
je 0x7c22
movq %rbx, %rdi
callq 0x547c
jmp 0x7c20
nop