// File: /workspace/github_c_makefile_star2_O2/deplinenoise[P]vlink/t_rawbin.c
// Function: srec_write
static void srec_write(struct GlobalVars *gv,FILE *f,int addrsize)
{
  bool firstsec = TRUE;
  struct LinkedSection *ls;
  unsigned long len,addr;
  uint8_t *p,buf[MAXSREC+8];

  /* write header */
  buf[0] = buf[1] = 0;
  strncpy(&buf[2],gv->dest_name,MAXSREC+6);
  SRecOut(f,0,buf,(strlen(gv->dest_name)<(MAXSREC+6)) ?
          strlen(gv->dest_name)+2 : MAXSREC+8);

  while (ls = get_next_section(gv)) {
    if (ls->size == 0 || !(ls->flags & SF_ALLOC) || (ls->ld_flags & LSF_NOLOAD))
      continue;  /* ignore empty sections */

    if (firstsec) {
      if (gv->trace_file)
        fprintf(gv->trace_file,"Base address = 0x%08lx.\n",ls->copybase);
      firstsec = FALSE;
    }

    /* resolve all relocations and write section contents */
    calc_relocs(gv,ls);

    for (p=ls->data,addr=ls->copybase,len=ls->filesize; len>0; ) {
      int nbytes = (len>MAXSREC) ? MAXSREC : len;

      switch (addrsize) {
        case 2:
          write16be(buf,(uint16_t)addr);
          memcpy(buf+2,p,nbytes);
          SRecOut(f,1,buf,2+nbytes);
          break;
        case 3:
          buf[0] = (uint8_t)((addr>>16)&0xff);
          buf[1] = (uint8_t)((addr>>8)&0xff);
          buf[2] = (uint8_t)(addr&0xff);
          memcpy(buf+3,p,nbytes);
          SRecOut(f,2,buf,3+nbytes);
          break;
        case 4:
          write32be(buf,(uint32_t)addr);
          memcpy(buf+4,p,nbytes);
          SRecOut(f,3,buf,4+nbytes);
          break;
        default:
          ierror("srec_write(): Illegal SRec-type: %d",addrsize);
          nbytes = len;
          break;
      }
      p += nbytes;
      addr += nbytes;
      len -= nbytes;
    }
  }

  /* write trailer */
  memset(buf,0,4);
  SRecOut(f,11-addrsize,buf,addrsize);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %r13d
movq %rsi, %r14
movq %rdi, %r15
leaq 0x12(%rsp), %rdi
andw $0x0, -0x2(%rdi)
movq 0x48(%r15), %r12
pushq $0x26
popq %rdx
movq %r12, %rsi
callq 0x5060
movq %r12, %rdi
callq 0x50e0
leal 0x2(%rax), %edx
cmpq $0x26, %rax
pushq $0x28
popq %rcx
cmovbl %edx, %ecx
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdx
movq %r14, 0x8(%rsp)
movq %r14, %rdi
xorl %esi, %esi
callq 0x1d045
leaq 0x67f6(%rip), %r14      # 0x236e0
movq %r13, 0x40(%rsp)
movq %r15, %rdi
callq 0x1ce4b
testq %rax, %rax
je 0x1d01b
movq %rax, %r12
cmpq $0x0, 0x48(%rax)
je 0x1ceef
testb $0x1, 0x21(%r12)
je 0x1ceef
testb $0x1, 0x24(%r12)
jne 0x1ceef
testb $0x1, %bl
jne 0x1cf3a
movq 0xa0(%r15), %rdi
testq %rdi, %rdi
je 0x1cf3a
movq 0x38(%r12), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x5180
movq %r15, %rdi
movq %r12, %rsi
callq 0xb170
movq 0x78(%r12), %rbp
movq 0x38(%r12), %rbx
movq 0x50(%r12), %r14
testq %r14, %r14
je 0x1d00d
cmpq $0x20, %r14
pushq $0x20
popq %r12
cmovbq %r14, %r12
cmpl $0x4, %r13d
je 0x1cfa0
cmpl $0x3, %r13d
jne 0x1cfc3
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x10(%rsp)
movb %bh, 0x11(%rsp)
movb %bl, 0x12(%rsp)
leaq 0x13(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x51a0
pushq $0x2
popq %rsi
leaq 0x10(%rsp), %r13
jmp 0x1cfe6
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movl %ebx, %esi
callq 0x661b
leaq 0x14(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x51a0
pushq $0x3
jmp 0x1cfe5
movzwl %bx, %esi
leaq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0x6613
leaq 0x12(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x51a0
pushq $0x1
popq %rsi
movq 0x40(%rsp), %rax
leal (%r12,%rax), %ecx
movq 0x8(%rsp), %rdi
movq %r13, %rdx
movq %rax, %r13
callq 0x1d045
addq %r12, %rbp
addq %r12, %rbx
subq %r12, %r14
jmp 0x1cf54
movb $0x1, %bl
leaq 0x66ca(%rip), %r14      # 0x236e0
jmp 0x1ceef
leaq 0x10(%rsp), %rdx
andl $0x0, (%rdx)
pushq $0xb
popq %rsi
subl %r13d, %esi
movq 0x8(%rsp), %rdi
movl %r13d, %ecx
callq 0x1d045
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq