// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
cmpl $0x0, 0x130(%rdi)
jle 0x4b06
movq %rdi, %r12
xorl %ecx, %ecx
movq %rdi, 0x18(%rsp)
movq 0x128(%r12), %rax
movq (%rax,%rcx,8), %rbx
movslq 0x58(%rbx), %rdi
cmpq $0x2, %rdi
jl 0x4af2
movq %rcx, 0x28(%rsp)
shlq $0x3, %rdi
callq 0x8508
movq %rax, %r15
movslq 0x58(%rbx), %rdi
shlq $0x2, %rdi
callq 0x8508
movl 0x58(%rbx), %ebp
xorl %r13d, %r13d
movl $0x0, %esi
testl %ebp, %ebp
movq %rbx, %r12
jle 0x46c1
movq 0x50(%r12), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rcx
cmpl $0x0, 0x124(%rcx)
jne 0x46c1
incq %rsi
cmpq %rsi, %rbp
jne 0x46aa
movl %ebp, %esi
movq %rax, 0x8(%rsp)
cmpl %esi, %ebp
movq %r12, 0x10(%rsp)
jle 0x4827
decl %ebp
xorl %r13d, %r13d
movq %rsi, 0x30(%rsp)
movq 0x50(%r12), %rax
movslq %ebp, %rbx
movq (%rax,%rbx,8), %r14
movl $0x80, %edi
callq 0x8508
movq %rax, (%r15,%r13,8)
movl 0x124(%r14), %ecx
movq 0x8(%rsp), %rdx
movl %ecx, (%rdx,%r13,4)
movl $0x10, %ecx
movq %rax, %rdi
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq (%r15,%r13,8), %rax
xorl %ecx, %ecx
movq %rcx, 0x50(%rax)
movq (%r15,%r13,8), %rax
movq %rcx, 0x70(%rax)
movq (%r15,%r13,8), %rax
movl %ecx, 0x78(%rax)
movl %ecx, 0x58(%rax)
movslq 0x1c(%r12), %rdi
movslq (%rdx,%r13,4), %rcx
subq %rcx, %rdi
movl %edi, 0x1c(%rax)
testb $0x1, 0x30(%r12)
je 0x4785
callq 0x8508
movq (%r15,%r13,8), %rcx
movq %rax, 0x38(%rcx)
movq (%r15,%r13,8), %rax
movq $0x0, 0x40(%rax)
movq (%r15,%r13,8), %rax
movq 0x38(%rax), %rdi
movq 0x8(%rsp), %rcx
movslq (%rcx,%r13,4), %rsi
addq 0x38(%r12), %rsi
movslq 0x1c(%rax), %rdx
callq 0x2130
movq 0x8(%rsp), %rdx
movq %r15, %rcx
movq (%r15,%r13,8), %rax
movl 0x1c(%rax), %eax
subl %eax, 0x1c(%r12)
movl 0x124(%r14), %eax
cmpl (%rdx,%r13,4), %eax
jne 0x4814
decq %rbx
movq %rcx, %r15
movl $0x0, 0x124(%r14)
movq (%r15,%r13,8), %rax
movq %rax, 0x18(%r14)
movq (%r15,%r13,8), %r12
movslq 0x58(%r12), %rax
leaq 0x1(%rax), %rcx
movl %ecx, 0x58(%r12)
movq 0x50(%r12), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x852c
movq %rax, 0x50(%r12)
movslq 0x58(%r12), %rcx
movq 0x10(%rsp), %r12
movq %r14, -0x8(%rax,%rcx,8)
movq 0x50(%r12), %rax
movq (%rax,%rbx,8), %r14
movl 0x124(%r14), %eax
decl %ebp
decq %rbx
movq 0x8(%rsp), %rcx
cmpl (%rcx,%r13,4), %eax
je 0x47a7
jmp 0x4817
movq %rcx, %r15
incq %r13
movq 0x30(%rsp), %rsi
cmpl %esi, %ebp
jge 0x46dd
movl %r13d, %ebp
movl %esi, 0x58(%r12)
testl %r13d, %r13d
movq 0x18(%rsp), %r12
jle 0x4894
leaq 0x1(%rbp), %rbx
movslq 0x130(%r12), %rax
leaq 0x1(%rax), %rcx
movq -0x10(%r15,%rbx,8), %rdx
movl %ecx, (%rdx)
movq %r12, 0x8(%rdx)
movq -0x10(%r15,%rbx,8), %r14
movl %ecx, 0x130(%r12)
movq 0x128(%r12), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x852c
movq %rax, 0x128(%r12)
movslq 0x130(%r12), %rcx
movq %r14, -0x8(%rax,%rcx,8)
decq %rbx
cmpq $0x1, %rbx
jg 0x483d
movq 0x10(%rsp), %r9
movl 0x78(%r9), %eax
testl %eax, %eax
movq %rbp, 0x20(%rsp)
jle 0x48df
movq 0x70(%r9), %rcx
movl 0x1c(%r9), %edx
xorl %ebx, %ebx
movq (%rcx,%rbx,8), %rdi
movl 0x18(%rdi), %esi
cmpl %edx, %esi
jae 0x48e1
movl 0x20(%rdi), %edi
leaq 0x586b(%rip), %r8       # 0xa130
movl (%r8,%rdi,4), %edi
addl %esi, %edi
cmpl %edi, %edx
jb 0x4b1e
incq %rbx
cmpq %rbx, %rax
jne 0x48b0
movl %eax, %ebx
jmp 0x48e1
xorl %ebx, %ebx
movq 0x8(%rsp), %r10
cmpl 0x78(%r9), %ebx
jge 0x49b0
movq 0x20(%rsp), %rax
leal -0x1(%rax), %r13d
movl %ebx, %r14d
movq 0x70(%r9), %rax
movq (%rax,%r14,8), %rbp
testl %r13d, %r13d
js 0x4930
movl 0x18(%rbp), %eax
movl %r13d, %ecx
movq (%r15,%rcx,8), %rdx
movl 0x1c(%rdx), %edx
addl (%r10,%rcx,4), %edx
cmpl %eax, %edx
jae 0x4930
leal -0x1(%r13), %ecx
testl %r13d, %r13d
movl %ecx, %r13d
jg 0x490c
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testl %r13d, %r13d
js 0x4b15
movl %r13d, %eax
movl 0x18(%rbp), %esi
movl 0x20(%rbp), %edx
subl (%r10,%rax,4), %esi
movl %esi, 0x18(%rbp)
movq (%r15,%rax,8), %rcx
leaq 0x57dc(%rip), %rdi      # 0xa130
movl (%rdi,%rdx,4), %edx
addl %esi, %edx
cmpl %edx, 0x1c(%rcx)
jb 0x4b1a
movq %rcx, 0x10(%rbp)
movq (%r15,%rax,8), %r12
movslq 0x78(%r12), %rax
leaq 0x1(%rax), %rcx
movl %ecx, 0x78(%r12)
movq 0x70(%r12), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x852c
movq 0x8(%rsp), %r10
movq %rax, 0x70(%r12)
movslq 0x78(%r12), %rcx
movq 0x10(%rsp), %r9
movq %rbp, -0x8(%rax,%rcx,8)
incq %r14
cmpl %r14d, 0x78(%r9)
jg 0x48fc
movl %ebx, 0x78(%r9)
movq 0x48(%r9), %rbx
testq %rbx, %rbx
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %r13
je 0x4a60
movl 0x20(%rbx), %eax
testl %eax, %eax
jle 0x49f4
movq 0x18(%rbx), %rcx
xorl %r14d, %r14d
movq 0x10(%rsp), %rdx
cmpq %rdx, (%rcx,%r14,8)
je 0x49ef
incq %r14
cmpq %r14, %rax
jne 0x49d9
movl %eax, %r14d
incl %r14d
jmp 0x49fa
movl $0x1, %r14d
addl %r13d, %eax
movl %eax, 0x20(%rbx)
movq 0x18(%rbx), %rdi
movslq %eax, %rsi
shlq $0x3, %rsi
callq 0x852c
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
testl %r13d, %r13d
movq 0x8(%rsp), %r10
jle 0x4a60
leaq 0x1(%r13), %rax
subq %r13, %r14
movq -0x10(%r15,%rax,8), %rcx
movq 0x18(%rbx), %rdx
movq %rcx, (%rdx,%r14,8)
decq %rax
incq %r14
cmpq $0x1, %rax
jg 0x4a47
movslq 0x180(%r12), %rax
testq %rax, %rax
jle 0x4ada
movq 0x178(%r12), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
cmpl $0x4, 0x28(%rsi)
jne 0x4ad2
movl 0x30(%rsi), %edi
movq 0x10(%rsp), %r8
cmpl (%r8), %edi
jne 0x4ad2
testq %r13, %r13
je 0x4ab7
movl 0x3c(%rsi), %r8d
xorl %edi, %edi
movq (%r15,%rdi,8), %r9
movl 0x1c(%r9), %r9d
addl (%r10,%rdi,4), %r9d
cmpl %r8d, %r9d
jge 0x4ab9
incq %rdi
cmpq %rdi, %r13
jne 0x4a99
movl %r13d, %edi
jmp 0x4ab9
xorl %edi, %edi
cmpl %r13d, %edi
je 0x4b2e
movl %edi, %edi
movq (%r15,%rdi,8), %r8
movl (%r8), %r8d
movl %r8d, 0x30(%rsi)
movl (%r10,%rdi,4), %edi
subl %edi, 0x3c(%rsi)
incq %rdx
cmpq %rax, %rdx
jne 0x4a77
movq %r15, %rdi
movq %r10, %rbx
callq 0x2030
movq %rbx, %rdi
callq 0x2030
movq 0x28(%rsp), %rcx
incq %rcx
movslq 0x130(%r12), %rax
cmpq %rax, %rcx
jl 0x4659
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x18(%rbp), %esi
jmp 0x4b31
movl 0x1c(%r9), %edx
leaq 0x5f29(%rip), %rdi      # 0xaa4e
xorl %eax, %eax
callq 0x22ef
jmp 0x4b3f
movl 0x18(%rsi), %esi
leaq 0x5f3a(%rip), %rdi      # 0xaa72
xorl %eax, %eax
callq 0x22ef
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21f0