// File: /workspace/github_c_makefile_star2_O0/negge[P]xlink/src/xlink.c
// Function: xlink_segment_apply_relocations
void xlink_segment_apply_relocations(xlink_segment *segment) {
  int i;
  for (i = 1; i <= segment->nrelocs; i++) {
    xlink_reloc *rel;
    unsigned char *data;
    int offset;
    int target;
    rel = xlink_segment_get_reloc(segment, i);
    data = segment->data + rel->offset;
    offset = segment->start + rel->offset;
    switch (rel->target) {
      case OMF_TARGET_EXT : {
        xlink_public *pub;
        pub = xlink_module_get_extern(segment->module, rel->target_idx)->public;
        target = pub->offset;
        if (pub->segment != NULL) {
          target += pub->segment->start;
        }
        break;
      }
      case OMF_TARGET_SEG : {
        xlink_segment *seg;
        seg = xlink_module_get_segment(segment->module, rel->target_idx);
        target = seg->start;
        break;
      }
      default : {
        XLINK_ERROR(1, ("Unsupported target T%i", rel->target));
      }
    }
    switch (rel->location) {
      case OMF_LOCATION_16BIT : {
        if (rel->mode == OMF_MODE_EXPLICIT) {
          *(uint16_t *)data = target + rel->addend.offset;
        }
        else {
          *(uint16_t *)data = target + rel->addend.offset - offset - 2;
        }
        break;
      }
      case OMF_LOCATION_32BIT : {
        if (rel->mode == OMF_MODE_EXPLICIT) {
          *(uint32_t *)data = target + rel->addend.offset;
        }
        else {
          *(uint32_t *)data = target + rel->addend.offset - offset - 4;
        }
        break;
      }
      default : {
        XLINK_ERROR(1, ("Unsupported location %i", rel->location));
      }
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1, -0xc(%rbp)
movl -0xc(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x78(%rcx), %eax
jg 0x4b62
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x2630
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0x18(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movl 0x7c(%rax), %eax
movq -0x18(%rbp), %rcx
movl 0x18(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x3c(%rbp)
subl $0x4, %eax
je 0x4a61
jmp 0x4a0d
movl -0x3c(%rbp), %eax
subl $0x6, %eax
jne 0x4a85
jmp 0x4a17
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rax
movl 0x30(%rax), %esi
callq 0x39c0
movq 0x118(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x124(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x4a5f
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x7c(%rax), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x4aa6
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rax
movl 0x30(%rax), %esi
callq 0x3440
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl 0x7c(%rax), %eax
movl %eax, -0x28(%rbp)
jmp 0x4aa6
jmp 0x4a87
movq -0x18(%rbp), %rax
movl 0x28(%rax), %esi
leaq 0xa004(%rip), %rdi      # 0xea99
movb $0x0, %al
callq 0xad90
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21c0
movq -0x18(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x40(%rbp)
subl $0x1, %eax
je 0x4ac1
jmp 0x4ab7
movl -0x40(%rbp), %eax
subl $0x9, %eax
je 0x4afd
jmp 0x4b31
movq -0x18(%rbp), %rax
cmpl $0x1, 0x1c(%rax)
jne 0x4ae1
movl -0x28(%rbp), %eax
movq -0x18(%rbp), %rcx
addl 0x3c(%rcx), %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
jmp 0x4afb
movl -0x28(%rbp), %eax
movq -0x18(%rbp), %rcx
addl 0x3c(%rcx), %eax
subl -0x24(%rbp), %eax
subl $0x2, %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
jmp 0x4b52
movq -0x18(%rbp), %rax
cmpl $0x1, 0x1c(%rax)
jne 0x4b19
movl -0x28(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x3c(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
jmp 0x4b2f
movl -0x28(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x3c(%rax), %ecx
subl -0x24(%rbp), %ecx
subl $0x4, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
jmp 0x4b52
jmp 0x4b33
movq -0x18(%rbp), %rax
movl 0x20(%rax), %esi
leaq 0x9cd3(%rip), %rdi      # 0xe814
movb $0x0, %al
callq 0xad90
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21c0
jmp 0x4b54
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x49b3
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)