// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
leaq 0x7d43(%rip), %rax      # 0x37d4d
movq %rax, 0x38(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x0, 0x34(%rsp)
cmpl $0x3, 0x34(%rsp)
jge 0x30266
movq 0x40(%rsp), %rax
movslq 0x34(%rsp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x30254
movq 0x40(%rsp), %rax
movslq 0x34(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
addq $0x80, %rdi
callq 0x11780
movq 0x40(%rsp), %rax
movslq 0x34(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x80(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
cmpq $0x0, (%rax)
je 0x30252
movq 0x28(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x30135
movq 0x28(%rsp), %rax
movzbl 0x38(%rax), %eax
cmpl $0x1, %eax
jne 0x300c5
movq 0x20(%rsp), %rax
movzwl 0x8(%rax), %eax
cmpl $0x0, %eax
jne 0x300c5
movq 0x20(%rsp), %rax
movzwl 0xa(%rax), %eax
cmpl $0x20, %eax
je 0x30133
movq 0x117a4(%rip), %rsi     # 0x41870
movq 0x28(%rsp), %rax
movzbl 0x38(%rax), %eax
movl %eax, %ecx
leaq 0xfac2(%rip), %rax      # 0x3fba0
movq (%rax,%rcx,8), %rdx
movq 0x20(%rsp), %rax
movzwl 0x8(%rax), %ecx
movq 0x20(%rsp), %rax
movzwl 0xa(%rax), %r8d
movq 0x20(%rsp), %rax
movq 0x10(%rax), %r9
movq 0x40(%rsp), %rax
movslq 0x34(%rsp), %rdi
movq (%rax,%rdi,8), %rax
movq 0x18(%rax), %r10
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rax
movl $0x20, %edi
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x8c00
jmp 0x30240
jmp 0x3013a
jmp 0x30240
cmpq $0x0, 0x18(%rsp)
jne 0x3016e
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rax
movslq 0x34(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x40(%rax), %rax
movq 0x28(%rsp), %rcx
addq 0x20(%rcx), %rax
movl %eax, %esi
callq 0x84d0
jmp 0x30220
movq 0x40(%rsp), %rax
movslq 0x34(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x40(%rax), %rax
movq 0x28(%rsp), %rcx
addq 0x20(%rcx), %rax
subq 0x18(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jge 0x301e0
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rax
movslq 0x34(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x18(%rax), %rdx
movq 0x40(%rsp), %rax
movslq 0x34(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x40(%rax), %rcx
movq 0x28(%rsp), %rax
movq 0x20(%rax), %r8
movq 0x18(%rsp), %r9
leaq 0x7b88(%rip), %rdi      # 0x37d61
movb $0x0, %al
callq 0x8ae0
jmp 0x301e2
cmpq $0xfe, 0x10(%rsp)
jle 0x3020e
movq 0x48(%rsp), %rdi
movl $0x1, %esi
callq 0x85e0
movq 0x10(%rsp), %rax
subq $0xfe, %rax
movq %rax, 0x10(%rsp)
jmp 0x301e2
movq 0x48(%rsp), %rdi
movq 0x10(%rsp), %rax
movzbl %al, %esi
callq 0x85e0
movq 0x40(%rsp), %rax
movslq 0x34(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x40(%rax), %rax
movq 0x28(%rsp), %rcx
addq 0x20(%rcx), %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x28(%rsp)
jmp 0x30074
jmp 0x30254
jmp 0x30256
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x30020
cmpq $0x0, 0x18(%rsp)
jne 0x3027c
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x84d0
jmp 0x30288
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x85e0
addq $0x58, %rsp
retq
nopl (%rax)