// File: /workspace/github_c_makefile_star2_O3/deplinenoise[P]vlink/t_ataritos.c
// Function: tos_writerelocs
void tos_writerelocs(struct GlobalVars *gv,FILE *f,
                     struct LinkedSection **sections)
{
  const char *fn = "tos_writerelocs(): ";
  int i;
  struct Reloc *rel;
  struct RelocInsert *ri;
  unsigned long lastoffs = 0;

  for (i=0; i<3; i++) {
    if (sections[i]) {
      sort_relocs(&sections[i]->relocs);
      for (rel=(struct Reloc *)sections[i]->relocs.first;
           rel->n.next!=NULL; rel=(struct Reloc *)rel->n.next) {
        if (ri = rel->insert) {
          if (rel->rtype!=R_ABS || ri->bpos!=0 || ri->bsiz!=32) {
            error(32,fff_ataritos.tname,reloc_name[rel->rtype],
                  (int)ri->bpos,(int)ri->bsiz,ri->mask,
                  sections[i]->name,rel->offset);
            continue;
          }
        }
        else
          continue;

        if (!lastoffs) {
          /* first relocation offset is 32 bits, the rest are bytes! */
          fwrite32be(f,sections[i]->base + rel->offset);
        }
        else {
          long diff = (sections[i]->base + rel->offset) - lastoffs;

          if (diff < 0) {
            ierror("%snegative offset difference: "
                   "%s(0x%08lx)+0x%08lx - 0x%08lx",fn,sections[i]->name,
                   sections[i]->base,rel->offset,lastoffs);
          }
          while (diff > 254) {
            fwrite8(f,1);
            diff -= 254;
          }
          fwrite8(f,(uint8_t)diff);
        }
        lastoffs = sections[i]->base + rel->offset;
      }
    }
  }

  if (!lastoffs) {
    /* not a single relocation written - write 0-word */
    fwrite32be(f,0);
  }
  else
    fwrite8(f,0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %rbx
leaq 0x112aa(%rip), %rbp     # 0x2f2f0
xorl %r15d, %r15d
xorl %r13d, %r13d
movq (%r14,%r15,8), %rdi
testq %rdi, %rdi
je 0x1e18b
subq $-0x80, %rdi
callq 0xb846
movq (%r14,%r15,8), %rax
movq 0x80(%rax), %r12
cmpq $0x0, (%r12)
je 0x1e18b
movq 0x30(%r12), %rax
testq %rax, %rax
je 0x1e182
movzbl 0x38(%r12), %ecx
cmpq $0x1, %rcx
sete %dl
movzwl 0x8(%rax), %edi
testw %di, %di
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x1e0ff
cmpw $0x20, 0xa(%rax)
jne 0x1e0fd
movq (%r14,%r15,8), %rax
movq 0x40(%rax), %rcx
movq 0x20(%r12), %r8
testq %r13, %r13
je 0x1e136
movq %rcx, %rbp
subq %r13, %rbp
addq %r8, %rbp
js 0x1e146
cmpq $0xff, %rbp
jb 0x1e162
movq %rbp, %r13
movq %rbx, %rdi
movl $0x1, %esi
callq 0x6c8f
leaq -0xfe(%r13), %rbp
cmpq $0x1fc, %r13            # imm = 0x1FC
movq %rbp, %r13
ja 0x1e0db
jmp 0x1e162
xorl %edi, %edi
movq 0x123a2(%rip), %rsi     # 0x304a8
movq (%rbp,%rcx,8), %rdx
movzwl %di, %ecx
movzwl 0xa(%rax), %r8d
movq 0x10(%rax), %r9
movq (%r14,%r15,8), %r10
movl $0x20, %edi
xorl %eax, %eax
pushq 0x20(%r12)
pushq 0x18(%r10)
callq 0x6f14
addq $0x10, %rsp
jmp 0x1e182
addl %ecx, %r8d
movq %rbx, %rdi
movl %r8d, %esi
callq 0x6be8
jmp 0x1e175
movq 0x18(%rax), %rdx
leaq 0x780b(%rip), %rdi      # 0x2595c
leaq 0x77f0(%rip), %rsi      # 0x25948
movq %r13, %r9
xorl %eax, %eax
callq 0x52b0
movzbl %bpl, %esi
movq %rbx, %rdi
callq 0x6c8f
leaq 0x1117b(%rip), %rbp     # 0x2f2f0
movq (%r14,%r15,8), %rax
movq 0x20(%r12), %r13
addq 0x40(%rax), %r13
movq (%r12), %r12
jmp 0x1e06d
incq %r15
cmpq $0x3, %r15
jne 0x1e04c
movq %rbx, %rdi
xorl %esi, %esi
addq $0x8, %rsp
testq %r13, %r13
je 0x1e1b5
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6c8f
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6be8