// File: /workspace/github_c_makefile_star2_O1/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 0x11116(%rip), %rbp     # 0x2f2f0
xorl %r15d, %r15d
xorl %r13d, %r13d
movq (%r14,%r15,8), %rdi
testq %rdi, %rdi
je 0x1e30f
subq $-0x80, %rdi
callq 0xb926
movq (%r14,%r15,8), %rax
movq 0x80(%rax), %r12
cmpq $0x0, (%r12)
je 0x1e30f
movq 0x30(%r12), %rax
testq %rax, %rax
je 0x1e2d2
movzbl 0x38(%r12), %ecx
cmpq $0x1, %rcx
jne 0x1e29c
cmpw $0x0, 0x8(%rax)
jne 0x1e29c
cmpw $0x20, 0xa(%rax)
jne 0x1e29c
movq (%r14,%r15,8), %rax
movq 0x40(%rax), %rcx
movq 0x20(%r12), %r8
testq %r13, %r13
je 0x1e2db
movq %rcx, %rbp
subq %r13, %rbp
addq %r8, %rbp
jns 0x1e271
movq 0x18(%rax), %rdx
leaq 0x771c(%rip), %rdi      # 0x2597c
leaq 0x7701(%rip), %rsi      # 0x25968
movq %r13, %r9
xorl %eax, %eax
callq 0x52b0
cmpq $0xff, %rbp
jl 0x1e2eb
movq %rbx, %rdi
movl $0x1, %esi
callq 0x6d04
leaq -0xfe(%rbp), %rax
cmpq $0x1fc, %rbp            # imm = 0x1FC
movq %rax, %rbp
jg 0x1e27a
jmp 0x1e2ee
movq 0x12205(%rip), %rsi     # 0x304a8
movq (%rbp,%rcx,8), %rdx
movzwl 0x8(%rax), %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 0x6f84
addq $0x10, %rsp
movq (%r12), %r12
jmp 0x1e201
addl %ecx, %r8d
movq %rbx, %rdi
movl %r8d, %esi
callq 0x6c5d
jmp 0x1e300
movq %rbp, %rax
movzbl %al, %esi
movq %rbx, %rdi
callq 0x6d04
leaq 0x10ff0(%rip), %rbp     # 0x2f2f0
movq (%r14,%r15,8), %rax
movq 0x20(%r12), %r13
addq 0x40(%rax), %r13
jmp 0x1e2d2
incq %r15
cmpq $0x3, %r15
jne 0x1e1e0
movq %rbx, %rdi
xorl %esi, %esi
addq $0x8, %rsp
testq %r13, %r13
je 0x1e339
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6d04
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6c5d