// File: /workspace/github_c_makefile_star2_O1/deplinenoise[P]vlink/t_aout.c
// Function: aout_addrelocs
uint32_t aout_addrelocs(struct GlobalVars *gv,struct LinkedSection **ls,
                      int sec,struct list *rlst,
                      uint32_t (*getrinfo)(struct GlobalVars *,struct Reloc *,
                                          bool,const char *,uint32_t),
                      int be)
/* creates a.out relocations for a single section (.text or .data) */
{
  struct Reloc *rel;
  uint32_t rtabsize=0,rinfo;
  lword a;

  if (ls[sec]) {
    if (fff[gv->dest_format]->flags & AOUT_JAGRELOC) {
      /* Output is for Atari Jaguar, so search for possible MOVEI RISC
         relocations and flag them by the internal type R_AOUT_MOVEI. */
      detect_movei_relocs((struct Reloc *)ls[sec]->relocs.first);
      detect_movei_relocs((struct Reloc *)ls[sec]->xrefs.first);
    }

    /* relocations */
    for (rel=(struct Reloc *)ls[sec]->relocs.first; rel->n.next!=NULL;
         rel=(struct Reloc *)rel->n.next) {
      int rsec = aout_sectindex(ls,rel->relocsect.lnk);

      if (rel->flags & RELF_INTERNAL)
        continue;  /* internal relocations will never be exported */

      /* fix addend for a.out */
      a = (lword)ls[rsec]->base + rel->addend;
      /* @@@ calculation for other relocs: baserel,jmptab,load-relative? */

      if (rel->rtype == R_AOUT_MOVEI)
        a = sign_extend(SWAP16(a),32);

      if ((rinfo = getrinfo(gv,rel,FALSE,ls[sec]->name,rel->offset)) != ~0) {
        aout_addreloclist(rlst,rel->offset,(uint32_t)sectype[rsec],rinfo,be);
        writesection(gv,ls[sec]->data+rel->offset,rel,a);
        rtabsize += sizeof(struct relocation_info);
      }
    }

    /* external references */
    for (rel=(struct Reloc *)ls[sec]->xrefs.first; rel->n.next!=NULL;
         rel=(struct Reloc *)rel->n.next) {
      uint32_t symidx;

      if (rel->flags & RELF_INTERNAL)
        continue;  /* internal relocations will never be exported */
      if ((symidx = aout_findsym(rel->xrefname,be)) == -1)
        symidx = aout_addsym(rel->xrefname,0,0,0,N_UNDF|N_EXT,0,be);

      /* fix addend for a.out */
      if (rel->rtype == R_PC)
        a = rel->addend - (lword)(ls[sec]->base + rel->offset);
      else
        a = rel->addend;
      /* @@@ calculation for other relocs: baserel,jmptab,load-relative? */

      if (rel->rtype == R_AOUT_MOVEI)
        a = sign_extend(SWAP16(a),32);

      if ((rinfo = getrinfo(gv,rel,TRUE,ls[sec]->name,rel->offset)) != ~0) {
        aout_addreloclist(rlst,rel->offset,symidx,rinfo,be);
        writesection(gv,ls[sec]->data+rel->offset,rel,a);
        rtabsize += sizeof(struct relocation_info);
      }
    }
  }

  return (rtabsize);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, 0x14(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movslq %edx, %rbx
movq (%rsi,%rbx,8), %rax
testq %rax, %rax
je 0x1cce6
movq %rsi, %r12
movq %rdi, %rbp
movzbl 0x50(%rdi), %ecx
leaq 0x125bb(%rip), %rdx     # 0x2f180
movq (%rdx,%rcx,8), %rcx
testb $0x1, 0xaf(%rcx)
je 0x1cbee
movq 0x80(%rax), %rdi
callq 0x1ce41
movq (%r12,%rbx,8), %rax
movq 0x98(%rax), %rdi
callq 0x1ce41
movq %rbx, 0x18(%rsp)
movq (%r12,%rbx,8), %rax
movq 0x80(%rax), %r15
cmpq $0x0, (%r15)
je 0x1ccf3
movl $0x0, 0x10(%rsp)
movq 0x18(%rsp), %r13
movq 0x18(%r15), %rax
xorl %ebx, %ebx
cmpq %rax, (%r12,%rbx,8)
je 0x1cc2f
incq %rbx
cmpq $0x3, %rbx
jne 0x1cc1b
movl $0x3, %ebx
cmpl $0x3, %ebx
jb 0x1cc46
movq 0x18(%rax), %rsi
leaq 0x7f88(%rip), %rdi      # 0x24bc7
xorl %eax, %eax
callq 0x52b0
testb $0x10, 0x39(%r15)
jne 0x1ccd7
movslq %ebx, %r14
movq (%r12,%r14,8), %rax
movq 0x28(%r15), %rbx
addq 0x40(%rax), %rbx
cmpb $0x11, 0x38(%r15)
jne 0x1cc7a
roll $0x10, %ebx
movq %rbx, %rdi
movl $0x20, %esi
callq 0x6f17
movq %rax, %rbx
movq (%r12,%r13,8), %rax
movq 0x18(%rax), %rcx
movl 0x20(%r15), %r8d
movq %rbp, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x28(%rsp)
cmpl $-0x1, %eax
je 0x1ccd7
movl 0x20(%r15), %esi
leaq 0x7c6e(%rip), %rcx      # 0x24910
movzbl (%r14,%rcx), %edx
movq 0x20(%rsp), %rdi
movl %eax, %ecx
movl 0x14(%rsp), %r8d
callq 0x1cef5
movq (%r12,%r13,8), %rax
movq 0x78(%rax), %rsi
addq 0x20(%r15), %rsi
movq %rbp, %rdi
movq %r15, %rdx
movq %rbx, %rcx
callq 0xb5b0
addl $0x8, 0x10(%rsp)
movq (%r15), %r15
cmpq $0x0, (%r15)
jne 0x1cc15
jmp 0x1ccfb
movl $0x0, 0x10(%rsp)
jmp 0x1ce2e
movl $0x0, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq (%r12,%rax,8), %rax
movq 0x98(%rax), %r15
cmpq $0x0, (%r15)
je 0x1ce2e
movq %rbp, 0x30(%rsp)
movq 0x30(%rsp), %rbp
testb $0x10, 0x39(%r15)
jne 0x1ce21
movq 0x10(%r15), %r14
movq 0x16e0b(%rip), %rbx     # 0x33b40
movq %r14, %rdi
callq 0x6e09
movzwl %ax, %eax
movq (%rbx,%rax,8), %r13
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %r13, %r13
je 0x1cd68
movq 0x18(%r13), %rsi
movq %r14, %rdi
callq 0x5160
testl %eax, %eax
je 0x1cd64
movq 0x10(%r13), %r13
jmp 0x1cd49
movl 0x2c(%r13), %ebx
cmpl $-0x1, %ebx
jne 0x1cd8e
movq 0x10(%r15), %rdi
movl 0x14(%rsp), %eax
movl %eax, (%rsp)
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0x1ca08
movl %eax, %ebx
movb 0x38(%r15), %al
movq 0x28(%r15), %r14
cmpb $0x2, %al
jne 0x1cdae
movq 0x18(%rsp), %rcx
movq (%r12,%rcx,8), %rcx
movq 0x40(%rcx), %rcx
addq 0x20(%r15), %rcx
subq %rcx, %r14
cmpb $0x11, %al
jne 0x1cdc6
roll $0x10, %r14d
movq %r14, %rdi
movl $0x20, %esi
callq 0x6f17
movq %rax, %r14
movq 0x18(%rsp), %r13
movq (%r12,%r13,8), %rax
movq 0x18(%rax), %rcx
movl 0x20(%r15), %r8d
movq %rbp, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq *0x28(%rsp)
cmpl $-0x1, %eax
je 0x1ce21
movl 0x20(%r15), %esi
movq 0x20(%rsp), %rdi
movl %ebx, %edx
movl %eax, %ecx
movl 0x14(%rsp), %r8d
callq 0x1cef5
movq (%r12,%r13,8), %rax
movq 0x78(%rax), %rsi
addq 0x20(%r15), %rsi
movq %rbp, %rdi
movq %r15, %rdx
movq %r14, %rcx
callq 0xb5b0
addl $0x8, 0x10(%rsp)
movq (%r15), %r15
cmpq $0x0, (%r15)
jne 0x1cd1f
movl 0x10(%rsp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq