// File: /workspace/github_c_makefile_star2_O3/negge[P]xlink/src/xlink.c
// Function: xlink_module_split_segments
void xlink_module_split_segments(xlink_module *mod) {
  int i, j, k, n, m;
  for (i = 0; i < mod->nsegments; i++) {
    xlink_segment *seg;
    seg = mod->segments[i];
    if (seg->npublics > 1) {
      int *offsets;
      xlink_segment **segs;
      /* Allocate space for the worst case, a segment for every public */
      segs = xlink_malloc(seg->npublics*sizeof(xlink_segment *));
      offsets = xlink_malloc(seg->npublics*sizeof(int));
      /* All publics with offset == 0 stay in the existing segment */
      for (n = 0; n < seg->npublics && seg->publics[n]->offset == 0; n++);
      /* All publics with offset > 0 are split into their own segment */
      for (m = 0, j = seg->npublics - 1; j > n - 1; m++) {
        xlink_public *pub;
        pub = seg->publics[j];
        segs[m] = xlink_malloc(sizeof(xlink_segment));
        offsets[m] = pub->offset;
        *segs[m] = *seg;
        segs[m]->publics = NULL;
        segs[m]->relocs = NULL;
        segs[m]->npublics = segs[m]->nrelocs = 0;
        segs[m]->length = seg->length - offsets[m];
        if (seg->info & SEG_HAS_DATA) {
          segs[m]->data = xlink_malloc(segs[m]->length);
          segs[m]->mask = NULL;
          memcpy(segs[m]->data, &seg->data[offsets[m]], segs[m]->length);
        }
        seg->length -= segs[m]->length;
        /* Add all publics with the same offset to this new segment */
        for (; pub->offset == offsets[m]; j--, pub = seg->publics[j]) {
          pub->offset = 0;
          pub->segment = segs[m];
          xlink_segment_add_public(segs[m], pub);
        }
      }
      seg->npublics = n;
      /* Add the new segments to the module in the original order */
      for (j = m; j-- > 0; ) {
        XLINK_LIST_ADD(module, segment, mod, segs[j]);
      }
      /* All relocs with offset < seg->length stay in the existing segment */
      n = 0;
      for (; n < seg->nrelocs && seg->relocs[n]->offset < seg->length; n++) {
        xlink_reloc *rel;
        rel = seg->relocs[n];
        XLINK_ERROR(seg->length < rel->offset + OMF_FIXUP_SIZE[rel->location],
         ("FIXUP offset %i past segment end %i", rel->offset, seg->length));
      }
      /* Move all relocs with offset >= seg->length to the right segment */
      for (k = m - 1, j = n; j < seg->nrelocs; j++) {
        xlink_reloc *rel;
        rel = seg->relocs[j];
        for (; k >= 0 && offsets[k] + segs[k]->length < rel->offset; k--);
        XLINK_ERROR(k < 0,
         ("Could not find segment for reloc offset %i", rel->offset));
        rel->offset -= offsets[k];
        XLINK_ERROR(
         segs[k]->length < rel->offset + OMF_FIXUP_SIZE[rel->location],
         ("FIXUP offset %i past segment end %i", rel->offset, seg->length));
        rel->segment = segs[k];
        xlink_segment_add_reloc(segs[k], rel);
      }
      seg->nrelocs = n;
      /* If seg was part of a group, insert new segments into that group */
      if (seg->group != NULL) {
        xlink_group *grp;
        grp = seg->group;
        for (n = 0; n < grp->nsegments && grp->segments[n] != seg; n++);
        n++;
        grp->nsegments += m;
        grp->segments =
         xlink_realloc(grp->segments, grp->nsegments*sizeof(xlink_segment *));
        memmove(&grp->segments[n + m], &grp->segments[n],
         (grp->nsegments - (n + m))*sizeof(xlink_segment *));
        for (j = m; j-- > 0; ) {
          grp->segments[n + m - (j + 1)] = segs[j];
        }
      }
      /* If any relocs pointed into the segment, set their index and offset */
      for (j = 0; j < mod->nrelocs; j++) {
        xlink_reloc *rel;
        rel = mod->relocs[j];
        if (rel->target == OMF_TARGET_SEG && rel->target_idx == seg->index ) {
          k = 0;
          while (k < m && offsets[k] + segs[k]->length < rel->addend.offset) {
            k++;
          }
          XLINK_ERROR(k == m,
           ("Could not find segment for reloc offset %i", rel->offset));
          rel->target_idx = segs[k]->index;
          rel->addend.offset -= offsets[k];
        }
      }
      free(segs);
      free(offsets);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl 0x130(%rdi), %eax
testl %eax, %eax
jle 0x4b11
movq %rdi, %rbp
xorl %edx, %edx
movq %rdi, 0x28(%rsp)
movq 0x128(%rbp), %rcx
movq (%rcx,%rdx,8), %rbx
movslq 0x58(%rbx), %rdi
cmpq $0x2, %rdi
jl 0x4ae8
movq %rdx, 0x40(%rsp)
shlq $0x3, %rdi
callq 0x8520
movq %rax, %r15
movslq 0x58(%rbx), %rdi
shlq $0x2, %rdi
callq 0x8520
movq %rax, %r12
movl 0x58(%rbx), %r13d
movq $0x0, 0x20(%rsp)
movl $0x0, %edx
testl %r13d, %r13d
movq %rbx, 0x18(%rsp)
jle 0x4893
movq 0x50(%rbx), %rax
xorl %edx, %edx
movq (%rax,%rdx,8), %rcx
cmpl $0x0, 0x124(%rcx)
jne 0x469e
incq %rdx
cmpq %rdx, %r13
jne 0x4681
movl %r13d, %edx
jmp 0x4893
cmpl %edx, %r13d
jle 0x4893
movq %r12, 0x8(%rsp)
decl %r13d
movl $0x1, %eax
xorl %r12d, %r12d
movq %rdx, 0x10(%rsp)
movq %r15, 0x38(%rsp)
movq %rax, 0x30(%rsp)
movq 0x50(%rbx), %rax
movslq %r13d, %r14
movq (%rax,%r14,8), %rbp
movl $0x80, %edi
callq 0x8520
movq %rax, (%r15,%r12,8)
movl 0x124(%rbp), %ecx
movq 0x8(%rsp), %r8
movl %ecx, (%r8,%r12,4)
movl $0x10, %ecx
movq %rax, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq (%r15,%r12,8), %rax
xorl %ecx, %ecx
movq %rcx, 0x50(%rax)
movq (%r15,%r12,8), %rax
movq %rcx, 0x70(%rax)
movq (%r15,%r12,8), %rax
movl %ecx, 0x78(%rax)
movl %ecx, 0x58(%rax)
movslq 0x1c(%rbx), %rdi
movslq (%r8,%r12,4), %rcx
subq %rcx, %rdi
movl %edi, 0x1c(%rax)
testb $0x1, 0x30(%rbx)
je 0x476f
callq 0x8520
movq (%r15,%r12,8), %rcx
movq %rax, 0x38(%rcx)
movq (%r15,%r12,8), %rax
movq $0x0, 0x40(%rax)
movq (%r15,%r12,8), %rax
movq 0x38(%rax), %rdi
movq 0x8(%rsp), %rcx
movslq (%rcx,%r12,4), %rsi
addq 0x38(%rbx), %rsi
movslq 0x1c(%rax), %rdx
callq 0x2140
movq 0x8(%rsp), %r8
movq (%r15,%r12,8), %rax
movl 0x1c(%rax), %edi
subl %edi, 0x1c(%rbx)
movl 0x124(%rbp), %eax
movq %r12, %rcx
cmpl (%r8,%r12,4), %eax
jne 0x47ed
decq %r14
movq %rcx, %r12
movq 0x8(%rsp), %r15
movl $0x0, 0x124(%rbp)
movq 0x38(%rsp), %rax
movq (%rax,%r12,8), %rbx
movq %rbx, 0x18(%rbp)
movslq 0x58(%rbx), %rax
leaq 0x1(%rax), %rcx
movl %ecx, 0x58(%rbx)
movq 0x50(%rbx), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x8540
movq %rax, 0x50(%rbx)
movslq 0x58(%rbx), %rcx
movq 0x18(%rsp), %rbx
movq %rbp, -0x8(%rax,%rcx,8)
movq 0x50(%rbx), %rax
movq (%rax,%r14,8), %rbp
movl 0x124(%rbp), %eax
decl %r13d
decq %r14
cmpl (%r15,%r12,4), %eax
je 0x478c
jmp 0x47f0
movq %rcx, %r12
incq %r12
movq 0x30(%rsp), %r14
leal 0x1(%r14), %eax
movq 0x10(%rsp), %rdx
cmpl %edx, %r13d
movq 0x38(%rsp), %r15
jge 0x46c1
movl %edx, 0x58(%rbx)
testl %r12d, %r12d
setg %al
movq %r12, 0x20(%rsp)
jle 0x4af9
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rbp
movl 0x130(%rbp), %ecx
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
incq %r14
leal 0x1(%rcx), %eax
movq -0x10(%r15,%r14,8), %rdx
movl %eax, (%rdx)
movq %rbp, 0x8(%rdx)
movq -0x10(%r15,%r14,8), %rbx
movl %eax, 0x130(%rbp)
movq 0x128(%rbp), %rdi
movslq %ecx, %rax
leaq 0x8(,%rax,8), %rsi
callq 0x8540
movq %rax, 0x128(%rbp)
movslq 0x130(%rbp), %rcx
movq %rbx, -0x8(%rax,%rcx,8)
decq %r14
cmpq $0x1, %r14
ja 0x483d
movq 0x18(%rsp), %rbx
jmp 0x4b07
movl %edx, 0x58(%rbx)
movq $0x0, 0x10(%rsp)
movl 0x78(%rbx), %eax
testl %eax, %eax
jle 0x48e8
movq 0x70(%rbx), %rcx
movl 0x1c(%rbx), %edx
xorl %r9d, %r9d
movq 0x20(%rsp), %r13
movq (%rcx,%r9,8), %rdi
movl 0x18(%rdi), %esi
cmpl %edx, %esi
jae 0x48f0
movl 0x20(%rdi), %edi
leaq 0x5866(%rip), %r8       # 0xa130
movl (%r8,%rdi,4), %edi
addl %esi, %edi
cmpl %edi, %edx
jb 0x4b3e
incq %r9
cmpq %r9, %rax
jne 0x48b5
movl %eax, %r9d
jmp 0x49af
xorl %r9d, %r9d
movq 0x20(%rsp), %r13
cmpl %eax, %r9d
jge 0x49af
decl %r13d
movq %r9, 0x30(%rsp)
movl %r9d, %r14d
movq 0x70(%rbx), %rax
movq (%rax,%r14,8), %rbp
movl 0x18(%rbp), %esi
testl %r13d, %r13d
js 0x4b23
movl %r13d, %eax
movl (%r12,%rax,4), %edi
movq (%r15,%rax,8), %rcx
movl 0x1c(%rcx), %edx
leal (%rdx,%rdi), %r8d
cmpl %esi, %r8d
jae 0x4940
leal -0x1(%r13), %eax
testl %r13d, %r13d
movl %eax, %r13d
jg 0x4918
jmp 0x4b23
subl %edi, %esi
movl %esi, 0x18(%rbp)
movl 0x20(%rbp), %edi
leaq 0x57e1(%rip), %r8       # 0xa130
movl (%r8,%rdi,4), %edi
addl %esi, %edi
cmpl %edi, %edx
jb 0x4b3b
movq %rcx, 0x10(%rbp)
movq (%r15,%rax,8), %rbx
movslq 0x78(%rbx), %rax
leaq 0x1(%rax), %rcx
movl %ecx, 0x78(%rbx)
movq 0x70(%rbx), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x8540
movq %rax, 0x70(%rbx)
movslq 0x78(%rbx), %rcx
movq 0x18(%rsp), %rbx
movq %rbp, -0x8(%rax,%rcx,8)
incq %r14
cmpl %r14d, 0x78(%rbx)
jg 0x4904
movq 0x28(%rsp), %rbp
movq 0x20(%rsp), %r13
movq 0x30(%rsp), %r9
movl %r9d, 0x78(%rbx)
movq 0x48(%rbx), %rbx
testq %rbx, %rbx
je 0x4a50
movl 0x20(%rbx), %eax
movq 0x18(%rbx), %rdi
testl %eax, %eax
jle 0x49e9
xorl %r14d, %r14d
movq 0x18(%rsp), %rcx
cmpq %rcx, (%rdi,%r14,8)
je 0x49e4
incq %r14
cmpq %r14, %rax
jne 0x49ce
movl %eax, %r14d
incl %r14d
jmp 0x49ef
movl $0x1, %r14d
addl %r13d, %eax
movl %eax, 0x20(%rbx)
movslq %eax, %rsi
shlq $0x3, %rsi
callq 0x8540
movq %rax, 0x18(%rbx)
leal (%r14,%r13), %ecx
leaq (%rax,%rcx,8), %rdi
movl %r14d, %edx
leaq (%rax,%rdx,8), %rsi
movslq 0x20(%rbx), %rdx
movslq %ecx, %r14
subq %r14, %rdx
shlq $0x3, %rdx
callq 0x21a0
cmpb $0x0, 0x10(%rsp)
je 0x4a50
movl %r13d, %eax
subq %rax, %r14
incq %rax
movq -0x10(%r15,%rax,8), %rcx
movq 0x18(%rbx), %rdx
movq %rcx, (%rdx,%r14,8)
decq %rax
incq %r14
cmpq $0x1, %rax
ja 0x4a37
movslq 0x180(%rbp), %rax
testq %rax, %rax
movq 0x18(%rsp), %r11
jle 0x4acd
movq 0x178(%rbp), %rcx
movl %r13d, %edx
xorl %esi, %esi
movq (%rcx,%rsi,8), %rdi
cmpl $0x4, 0x28(%rdi)
jne 0x4ac5
movl 0x30(%rdi), %r8d
cmpl (%r11), %r8d
jne 0x4ac5
testl %r13d, %r13d
je 0x4aa7
movl 0x3c(%rdi), %r9d
xorl %r8d, %r8d
movq (%r15,%r8,8), %r10
movl 0x1c(%r10), %r10d
addl (%r12,%r8,4), %r10d
cmpl %r9d, %r10d
jge 0x4aaa
incq %r8
cmpq %r8, %rdx
jne 0x4a8c
jmp 0x4b20
xorl %r8d, %r8d
cmpl %r13d, %r8d
je 0x4b20
movl %r8d, %r8d
movq (%r15,%r8,8), %r9
movl (%r9), %r9d
movl %r9d, 0x30(%rdi)
movl (%r12,%r8,4), %r8d
subl %r8d, 0x3c(%rdi)
incq %rsi
cmpq %rax, %rsi
jne 0x4a6d
movq %r15, %rdi
callq 0x2030
movq %r12, %rdi
callq 0x2030
movl 0x130(%rbp), %eax
movq 0x40(%rsp), %rdx
incq %rdx
movslq %eax, %rcx
cmpq %rcx, %rdx
jl 0x4621
jmp 0x4b11
movq $0x0, 0x10(%rsp)
movq 0x28(%rsp), %rbp
movq 0x8(%rsp), %r12
jmp 0x489f
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x18(%rdi), %esi
leaq 0x5f48(%rip), %rdi      # 0xaa72
xorl %eax, %eax
callq 0x22ef
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21f0
movl 0x1c(%rbx), %edx
leaq 0x5f09(%rip), %rdi      # 0xaa4e
xorl %eax, %eax
callq 0x22ef
jmp 0x4b31