// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movl %r9d, 0x14(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movslq %edx, %rbp
movq (%rsi,%rbp,8), %rax
testq %rax, %rax
je 0x1cb53
movq %rsi, %r12
movq %rdi, %r13
movzbl 0x50(%rdi), %ecx
leaq 0x12742(%rip), %rdx     # 0x2f180
movq (%rdx,%rcx,8), %rcx
testb $0x1, 0xaf(%rcx)
je 0x1ca6b
movq 0x80(%rax), %rdi
callq 0x1cc85
movq (%r12,%rbp,8), %rax
movq 0x98(%rax), %rdi
callq 0x1cc85
movq (%r12,%rbp,8), %rax
movq 0x80(%rax), %r15
cmpq $0x0, (%r15)
je 0x1cb6e
movl $0x0, 0x10(%rsp)
movq 0x18(%r15), %rax
xorl %r14d, %r14d
cmpq %rax, (%r12,%r14,8)
je 0x1cab2
incq %r14
cmpq $0x3, %r14
jne 0x1ca8b
movq 0x18(%rax), %rsi
leaq 0x8102(%rip), %rdi      # 0x24ba7
xorl %eax, %eax
callq 0x52b0
movl $0x3, %r14d
testb $0x10, 0x39(%r15)
jne 0x1cb40
movq (%r12,%r14,8), %rax
movq 0x28(%r15), %rbx
addq 0x40(%rax), %rbx
cmpb $0x11, 0x38(%r15)
jne 0x1cae3
roll $0x10, %ebx
movq %rbx, %rdi
movl $0x20, %esi
callq 0x6ea5
movq %rax, %rbx
movq (%r12,%rbp,8), %rax
movq 0x18(%rax), %rcx
movl 0x20(%r15), %r8d
movq %r13, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rsp)
cmpl $-0x1, %eax
je 0x1cb40
movl 0x20(%r15), %esi
leaq 0x7de5(%rip), %rcx      # 0x248f0
movzbl (%r14,%rcx), %edx
movq 0x18(%rsp), %rdi
movl %eax, %ecx
movl 0x14(%rsp), %r8d
callq 0x1cd39
movq (%r12,%rbp,8), %rax
movq 0x78(%rax), %rsi
addq 0x20(%r15), %rsi
movq %r13, %rdi
movq %r15, %rdx
movq %rbx, %rcx
callq 0xb4ed
addl $0x8, 0x10(%rsp)
movq (%r15), %r15
cmpq $0x0, (%r15)
jne 0x1ca84
movq (%r12,%rbp,8), %rax
jmp 0x1cb76
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0x10(%rsp)
movq 0x98(%rax), %r15
cmpq $0x0, (%r15)
je 0x1cb5b
testb $0x10, 0x39(%r15)
jne 0x1cc7d
movq 0x10(%r15), %rbx
movq 0x16fa7(%rip), %r14     # 0x33b40
movq %rbx, %rdi
callq 0x6d94
movzwl %ax, %eax
movq (%r14,%rax,8), %r14
testq %r14, %r14
je 0x1cbcd
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0x5160
testl %eax, %eax
je 0x1cbc3
movq 0x10(%r14), %r14
jmp 0x1cba8
movl 0x2c(%r14), %r14d
cmpl $-0x1, %r14d
jne 0x1cbef
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 0x1c881
movl %eax, %r14d
movzbl 0x38(%r15), %eax
movq 0x28(%r15), %rbx
cmpl $0x11, %eax
je 0x1cc13
cmpl $0x2, %eax
jne 0x1cc26
movq (%r12,%rbp,8), %rax
movq 0x40(%rax), %rax
addq 0x20(%r15), %rax
subq %rax, %rbx
jmp 0x1cc26
roll $0x10, %ebx
movq %rbx, %rdi
movl $0x20, %esi
callq 0x6ea5
movq %rax, %rbx
movq (%r12,%rbp,8), %rax
movq 0x18(%rax), %rcx
movl 0x20(%r15), %r8d
movq %r13, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq *0x20(%rsp)
cmpl $-0x1, %eax
je 0x1cc7d
movl 0x20(%r15), %esi
movq 0x18(%rsp), %rdi
movl %r14d, %edx
movl %eax, %ecx
movl 0x14(%rsp), %r8d
callq 0x1cd39
movq (%r12,%rbp,8), %rax
movq 0x78(%rax), %rsi
addq 0x20(%r15), %rsi
movq %r13, %rdi
movq %r15, %rdx
movq %rbx, %rcx
callq 0xb4ed
addl $0x8, 0x10(%rsp)
movq (%r15), %r15
jmp 0x1cb7d