// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpl $0x0, 0x60(%rax)
je 0xce04
movb $0x0, -0x1(%rbp)
jmp 0xd198
movq -0x88(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0xcfaa
movq -0x88(%rbp), %rax
movl 0x14(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cmpl $0x0, %eax
jne 0xce42
movq -0x88(%rbp), %rax
movswl 0x10(%rax), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
jne 0xcfaa
movq -0x88(%rbp), %rax
movl 0x14(%rax), %ecx
movl %ecx, 0x60(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0xd490
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
callq 0xd4c0
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0xd550
testb $0x1, %al
jne 0xce87
jmp 0xcf1b
leaq -0x30(%rbp), %rdi
callq 0xd590
movq %rax, %rcx
movq -0x88(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, -0x3c(%rbp)
movl 0x60(%rax), %eax
cmpl -0x3c(%rbp), %eax
jae 0xcf0b
movq -0x88(%rbp), %rcx
movl 0x60(%rcx), %eax
addl 0x8(%rcx), %eax
cmpl -0x3c(%rbp), %eax
jbe 0xcf0b
movq -0x88(%rbp), %rdi
movq 0x4a101(%rip), %rax     # 0x56fc8
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movswl 0x1c(%rdi), %eax
movl %eax, -0x8c(%rbp)
addq $0x28, %rdi
callq 0x6160
movq -0x98(%rbp), %rdi
movl -0x8c(%rbp), %edx
movq %rax, %rcx
leaq 0x2d30c(%rip), %rsi     # 0x3a207
movb $0x0, %al
callq 0x6450
movb $0x0, -0x1(%rbp)
jmp 0xd198
jmp 0xcf0d
leaq -0x30(%rbp), %rdi
callq 0xd5b0
jmp 0xce71
movq -0x88(%rbp), %rax
movq -0x18(%rbp), %rdi
movl 0x60(%rax), %eax
movl %eax, -0x40(%rbp)
leaq -0x40(%rbp), %rsi
callq 0xd420
movq -0x88(%rbp), %rcx
movq -0x18(%rbp), %rdi
movl 0x60(%rcx), %eax
addl 0x8(%rcx), %eax
movl %eax, -0x44(%rbp)
leaq -0x44(%rbp), %rsi
callq 0xd420
movq -0x88(%rbp), %rax
movl 0x60(%rax), %eax
movl %eax, -0x9c(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xd5d0
movq %rax, %rcx
movl -0x9c(%rbp), %eax
cmpl (%rcx), %eax
jae 0xcfa1
movq -0x88(%rbp), %rax
movl 0x60(%rax), %eax
movl %eax, -0xa0(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xd5d0
movl -0xa0(%rbp), %ecx
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0xd198
testb $0x1, -0x19(%rbp)
je 0xd194
movq -0x18(%rbp), %rdi
callq 0xd490
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rdi
callq 0xd4c0
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xd550
testb $0x1, %al
jne 0xcfe4
jmp 0xd14b
leaq -0x50(%rbp), %rdi
callq 0xd590
movq %rax, %rcx
movq -0x88(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, -0x5c(%rbp)
movswl 0x10(%rax), %eax
andl $0x1f, %eax
cmpl $0x12, %eax
je 0xd020
movl -0x5c(%rbp), %eax
andl $0xff0000, %eax         # imm = 0xFF0000
cmpl $0x0, %eax
jne 0xd020
movl -0x5c(%rbp), %eax
addl $0x10000, %eax          # imm = 0x10000
movl %eax, -0x5c(%rbp)
movq -0x88(%rbp), %rax
cmpl $0x0, 0x18(%rax)
je 0xd063
movq -0x88(%rbp), %rcx
movl -0x5c(%rbp), %eax
movl 0x18(%rcx), %ecx
subl $0x1, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0xd063
movq -0x88(%rbp), %rax
movl 0x18(%rax), %ecx
addl -0x5c(%rbp), %ecx
movl %ecx, -0x5c(%rbp)
movl 0x18(%rax), %eax
subl $0x1, %eax
xorl $-0x1, %eax
andl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x88(%rbp), %rax
movswl 0xe(%rax), %eax
cmpl $0x0, %eax
je 0xd0bb
movq -0x88(%rbp), %rcx
movl -0x5c(%rbp), %eax
movswl 0xe(%rcx), %edx
subl $0x1, %edx
andl %edx, %eax
addl 0x8(%rcx), %eax
movswl 0xe(%rcx), %ecx
subl $0x1, %ecx
xorl $-0x1, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0xd0bb
movq -0x88(%rbp), %rax
movswl 0xe(%rax), %ecx
addl -0x5c(%rbp), %ecx
movl %ecx, -0x5c(%rbp)
movswl 0xe(%rax), %eax
subl $0x1, %eax
xorl $-0x1, %eax
andl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0xd5f0
movq %rax, -0x68(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xd590
movl (%rax), %eax
movl %eax, -0x6c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jae 0xd135
movq -0x88(%rbp), %rcx
movl -0x6c(%rbp), %eax
subl -0x5c(%rbp), %eax
cmpl 0x8(%rcx), %eax
jb 0xd135
movq -0x88(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl %ecx, 0x60(%rax)
movq -0x18(%rbp), %rdi
movl 0x60(%rax), %eax
movl %eax, -0x70(%rbp)
leaq -0x70(%rbp), %rsi
callq 0xd420
movq -0x88(%rbp), %rcx
movq -0x18(%rbp), %rdi
movl 0x60(%rcx), %eax
addl 0x8(%rcx), %eax
movl %eax, -0x74(%rbp)
leaq -0x74(%rbp), %rsi
callq 0xd420
movb $0x1, -0x1(%rbp)
jmp 0xd198
jmp 0xd137
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0xd5f0
movq %rax, -0x80(%rbp)
jmp 0xcfc1
movq -0x88(%rbp), %rdi
movq 0x49e6f(%rip), %rax     # 0x56fc8
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movswl 0x1c(%rdi), %eax
movl %eax, -0xa4(%rbp)
addq $0x28, %rdi
callq 0x6160
movq -0xb0(%rbp), %rdi
movl -0xa4(%rbp), %edx
movq %rax, %rcx
leaq 0x2d0aa(%rip), %rsi     # 0x3a237
movb $0x0, %al
callq 0x6450
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)