// File: /workspace/github_c_makefile_star2_O2/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
subq $0x18, %rsp
movq %rdx, %r14
movq %rsi, 0x8(%rsp)
leaq 0x12c0a(%rip), %r12     # 0x2f300
pushq $0x20
popq %r13
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpq $0x3, %r15
je 0x1c83a
movq (%r14,%r15,8), %rdi
testq %rdi, %rdi
je 0x1c832
movq %rbx, 0x10(%rsp)
movl $0x80, %ebx
addq %rbx, %rdi
callq 0xb2c8
movq (%r14,%r15,8), %rbp
addq %rbx, %rbp
movq (%rbp), %rbp
cmpq $0x0, (%rbp)
je 0x1c82d
movq 0x30(%rbp), %rax
testq %rax, %rax
je 0x1c72f
movzbl 0x38(%rbp), %ecx
cmpq $0x1, %rcx
sete %dl
movzwl 0x8(%rax), %edi
testw %di, %di
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x1c76f
cmpw $0x20, 0xa(%rax)
je 0x1c79f
xorl %edi, %edi
movq 0x14122(%rip), %rsi     # 0x30898
movq (%r12,%rcx,8), %rdx
movzwl %di, %ecx
movzwl 0xa(%rax), %r8d
movq 0x10(%rax), %r9
movq (%r14,%r15,8), %r10
movl %r13d, %edi
xorl %eax, %eax
pushq 0x20(%rbp)
pushq 0x18(%r10)
callq 0x6c19
popq %rax
popq %rcx
jmp 0x1c72f
movq (%r14,%r15,8), %rax
movq 0x40(%rax), %rcx
movq 0x20(%rbp), %r8
movq 0x10(%rsp), %r9
testq %r9, %r9
je 0x1c807
movq %rcx, %rbx
subq %r9, %rbx
addq %r8, %rbx
jns 0x1c7ef
movq 0x18(%rax), %rdx
leaq 0x6b7e(%rip), %rdi      # 0x23349
leaq 0x6b63(%rip), %rsi      # 0x23335
xorl %eax, %eax
callq 0x6b2c
jmp 0x1c7ef
movq 0x8(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x68f1
addq $-0xfe, %rbx
cmpq $0xff, %rbx
jge 0x1c7db
movzbl %bl, %esi
movq 0x8(%rsp), %rdi
callq 0x68f1
jmp 0x1c817
addl %ecx, %r8d
movq 0x8(%rsp), %rdi
movl %r8d, %esi
callq 0x6856
movq (%r14,%r15,8), %rax
movq 0x20(%rbp), %rcx
addq 0x40(%rax), %rcx
movq %rcx, 0x10(%rsp)
jmp 0x1c72f
movq 0x10(%rsp), %rbx
incq %r15
jmp 0x1c6ff
testq %rbx, %rbx
je 0x1c859
movq 0x8(%rsp), %rdi
xorl %esi, %esi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x68f1
movq 0x8(%rsp), %rdi
xorl %esi, %esi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6856