// File: /workspace/github_c_makefile_star2_O2/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
movq %rdi, %r15
xorl %ecx, %ecx
movq %rdi, 0x20(%rsp)
movslq 0x130(%r15), %rax
cmpq %rax, %rcx
jge 0x4505
movq 0x128(%r15), %rax
movq (%rax,%rcx,8), %rax
movslq 0x58(%rax), %rdi
cmpq $0x2, %rdi
jl 0x44fd
movq %rcx, 0x30(%rsp)
shlq $0x3, %rdi
movq %rax, 0x8(%rsp)
callq 0x75d9
movq %rax, %r12
movq 0x8(%rsp), %rax
movslq 0x58(%rax), %rdi
shlq $0x2, %rdi
callq 0x75d9
movq 0x8(%rsp), %r10
movq %rax, %r13
movl 0x58(%r10), %ebx
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
movq %rax, 0x18(%rsp)
xorl %eax, %eax
cmpq %rax, 0x18(%rsp)
je 0x41a2
movq 0x50(%r10), %rcx
movq (%rcx,%rax,8), %rcx
cmpl $0x0, 0x124(%rcx)
jne 0x419d
incq %rax
jmp 0x4180
movq %rax, 0x18(%rsp)
decl %ebx
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpl 0x18(%rsp), %ebx
movq %rbp, 0x28(%rsp)
jl 0x42b9
movq 0x50(%r10), %rax
movslq %ebx, %r15
movq (%rax,%r15,8), %rbp
movl $0x80, %edi
callq 0x75d9
movq 0x8(%rsp), %r10
movq %rax, (%r12,%r14,8)
movl 0x124(%rbp), %ecx
movl %ecx, (%r13,%r14,4)
pushq $0x10
popq %rcx
movq %rax, %rdi
movq %r10, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq (%r12,%r14,8), %rax
andq $0x0, 0x50(%rax)
movq (%r12,%r14,8), %rax
andq $0x0, 0x70(%rax)
movq (%r12,%r14,8), %rax
andl $0x0, 0x78(%rax)
andl $0x0, 0x58(%rax)
movslq 0x1c(%r10), %rdi
movslq (%r13,%r14,4), %rcx
subq %rcx, %rdi
movl %edi, 0x1c(%rax)
testb $0x1, 0x30(%r10)
je 0x4269
callq 0x75d9
movq (%r12,%r14,8), %rcx
movq %rax, 0x38(%rcx)
movq (%r12,%r14,8), %rax
andq $0x0, 0x40(%rax)
movq (%r12,%r14,8), %rax
movq 0x38(%rax), %rdi
movslq (%r13,%r14,4), %rsi
movq 0x8(%rsp), %rcx
addq 0x38(%rcx), %rsi
movslq 0x1c(%rax), %rdx
callq 0x2140
movq 0x8(%rsp), %r10
movq (%r12,%r14,8), %rax
movl 0x1c(%rax), %edi
subl %edi, 0x1c(%r10)
decq %r15
movl 0x124(%rbp), %eax
cmpl (%r13,%r14,4), %eax
jne 0x42a5
andl $0x0, 0x124(%rbp)
movq (%r12,%r14,8), %rdi
movq %rdi, 0x18(%rbp)
movq %rbp, %rsi
callq 0x234b
movq 0x8(%rsp), %r10
movq 0x50(%r10), %rax
movq (%rax,%r15,8), %rbp
decl %ebx
jmp 0x426d
incq %r14
movq 0x28(%rsp), %rbp
decq %rbp
incl 0x14(%rsp)
jmp 0x41b0
movq 0x18(%rsp), %rax
movl %eax, 0x58(%r10)
movq %r14, %rax
movq 0x20(%rsp), %r15
testq %rax, %rax
jle 0x42fe
leaq -0x1(%rax), %rbx
movl 0x130(%r15), %ecx
incl %ecx
movq -0x8(%r12,%rax,8), %rdx
movl %ecx, (%rdx)
movq %r15, 0x8(%rdx)
movq -0x8(%r12,%rax,8), %rsi
movq %r15, %rdi
callq 0x2baa
movq 0x8(%rsp), %r10
movq %rbx, %rax
jmp 0x42ca
movl 0x78(%r10), %eax
testl %eax, %eax
movl $0x0, %ebx
cmovgl %eax, %ebx
xorl %ebp, %ebp
cmpq %rbp, %rbx
je 0x4342
movq 0x70(%r10), %rcx
movq (%rcx,%rbp,8), %rcx
movl 0x18(%rcx), %esi
movl 0x1c(%r10), %edx
cmpl %edx, %esi
jae 0x4347
movl 0x20(%rcx), %ecx
leaq 0x4e00(%rip), %rdi      # 0x9130
movl (%rdi,%rcx,4), %ecx
addl %esi, %ecx
cmpl %ecx, %edx
jb 0x453c
incq %rbp
jmp 0x430e
movq %rbx, %rbp
jmp 0x4349
movl %ebp, %ebx
leal -0x1(%r14), %r15d
cltq
cmpq %rax, %rbp
jge 0x43c1
movq 0x70(%r10), %rax
movq (%rax,%rbp,8), %rsi
testl %r15d, %r15d
js 0x4514
movl %r15d, %ecx
movl (%r13,%rcx,4), %r8d
movq (%r12,%rcx,8), %rdx
movl 0x1c(%rdx), %edi
leal (%rdi,%r8), %r9d
movl 0x18(%rsi), %eax
cmpl %eax, %r9d
jae 0x4385
decl %r15d
jmp 0x435c
subl %r8d, %eax
movl %eax, 0x18(%rsi)
movl 0x20(%rsi), %r8d
leaq 0x4d9a(%rip), %r9       # 0x9130
movl (%r9,%r8,4), %r8d
addl %eax, %r8d
cmpl %r8d, %edi
jb 0x452d
movq %rdx, 0x10(%rsi)
movq (%r12,%rcx,8), %rdi
callq 0x2473
movq 0x8(%rsp), %r10
incq %rbp
movl 0x78(%r10), %eax
jmp 0x434d
movl %ebx, 0x78(%r10)
movq 0x48(%r10), %rbx
testq %rbx, %rbx
movq 0x20(%rsp), %r15
je 0x447a
movl 0x20(%rbx), %eax
testl %eax, %eax
movl $0x0, %r15d
cmovgl %eax, %r15d
movq 0x18(%rbx), %rdi
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x43ff
cmpq %r10, (%rdi,%rcx,8)
je 0x43fc
incq %rcx
jmp 0x43ec
movl %ecx, %r15d
leal (%r15,%r14), %ebp
addl %r14d, %eax
movl %eax, 0x20(%rbx)
movslq %eax, %rsi
shlq $0x3, %rsi
callq 0x7608
movq %rax, 0x18(%rbx)
leal (%r15,%r14), %ecx
incl %ecx
leaq 0x8(%rax,%rbp,8), %rdi
movl %r15d, %edx
leaq (%rax,%rdx,8), %rsi
addq $0x8, %rsi
movslq 0x20(%rbx), %rdx
movslq %ecx, %rax
subq %rax, %rdx
shlq $0x3, %rdx
callq 0x21a0
movl 0x14(%rsp), %eax
addl %r15d, %eax
cltq
movq 0x28(%rsp), %rsi
addq %rax, %rsi
movq %r14, %rax
movq 0x20(%rsp), %r15
movq 0x8(%rsp), %r10
testq %rax, %rax
jle 0x447a
movq -0x8(%r12,%rax,8), %rcx
decq %rax
movq 0x18(%rbx), %rdx
movq %rcx, (%rdx,%rsi,8)
incq %rsi
jmp 0x4460
movl 0x180(%r15), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
cmpq %rax, %rcx
je 0x44e8
movq 0x178(%r15), %rdx
movq (%rdx,%rcx,8), %rdx
cmpl $0x4, 0x28(%rdx)
jne 0x44e3
movl 0x30(%rdx), %esi
cmpl (%r10), %esi
jne 0x44e3
xorl %esi, %esi
cmpq %rsi, %r14
je 0x44c8
movq (%r12,%rsi,8), %rdi
movl 0x1c(%rdi), %edi
addl (%r13,%rsi,4), %edi
cmpl 0x3c(%rdx), %edi
jge 0x44cb
incq %rsi
jmp 0x44ad
movl %r14d, %esi
cmpl %r14d, %esi
je 0x454c
movl %esi, %esi
movq (%r12,%rsi,8), %rdi
movl (%rdi), %edi
movl %edi, 0x30(%rdx)
movl (%r13,%rsi,4), %esi
subl %esi, 0x3c(%rdx)
incq %rcx
jmp 0x448d
movq %r12, %rdi
callq 0x2030
movq %r13, %rdi
callq 0x2030
movq 0x30(%rsp), %rcx
incq %rcx
jmp 0x4112
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x18(%rsi), %esi
leaq 0x5554(%rip), %rdi      # 0x9a72
xorl %eax, %eax
callq 0x7514
pushq $-0x1
popq %rdi
callq 0x21f0
movl 0x1c(%r10), %edx
leaq 0x5516(%rip), %rdi      # 0x9a4e
movl %eax, %esi
jmp 0x4543
leaq 0x550b(%rip), %rdi      # 0x9a4e
xorl %eax, %eax
callq 0x7514
jmp 0x4525
movl 0x18(%rdx), %esi
jmp 0x4517