// File: /workspace/github_c_makefile_star2_O3/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, %r14d
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x12(%rsp), %rdi
movw $0x0, -0x2(%rdi)
movq 0x48(%r15), %r12
movl $0x26, %edx
movq %r12, %rsi
callq 0x5060
movq %r12, %rdi
callq 0x50e0
leal 0x2(%rax), %edx
cmpq $0x26, %rax
movl $0x28, %ecx
cmovbl %edx, %ecx
leaq 0x10(%rsp), %rdx
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1ea87
movq 0x1c0(%r15), %rdi
callq 0x1e849
testq %rax, %rax
je 0x1ea58
movq %rax, %r12
leaq 0x71ba(%rip), %r13      # 0x25abc
xorl %ebx, %ebx
movq %r14, 0x40(%rsp)
cmpq $0x0, 0x48(%r12)
je 0x1e921
testb $0x1, 0x21(%r12)
je 0x1e921
testb $0x1, 0x24(%r12)
je 0x1e93a
movq 0x1c0(%r15), %rdi
callq 0x1e849
movq %rax, %r12
testq %rax, %rax
jne 0x1e909
jmp 0x1ea58
testb $0x1, %bl
jne 0x1e95a
movq 0xa0(%r15), %rdi
testq %rdi, %rdi
je 0x1e95a
movq 0x38(%r12), %rdx
movq %r13, %rsi
xorl %eax, %eax
callq 0x5180
movq %r15, %rdi
movq %r12, %rsi
callq 0xb6e6
movq 0x50(%r12), %r13
testq %r13, %r13
je 0x1ea37
movq 0x38(%r12), %rbx
movq 0x78(%r12), %rbp
cmpq $0x20, %r13
movl $0x20, %r12d
cmovbq %r13, %r12
cmpl $0x4, %r14d
je 0x1e9c4
cmpl $0x3, %r14d
jne 0x1e9ea
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
movl $0x2, %esi
leaq 0x10(%rsp), %r14
jmp 0x1ea0f
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movl %ebx, %esi
callq 0x693d
leaq 0x14(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x51a0
movl $0x3, %esi
jmp 0x1ea0f
movzwl %bx, %esi
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x6935
leaq 0x12(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x51a0
movl $0x1, %esi
movq 0x40(%rsp), %rax
leal (%r12,%rax), %ecx
movq 0x8(%rsp), %rdi
movq %r14, %rdx
movq %rax, %r14
callq 0x1ea87
addq %r12, %rbp
addq %r12, %rbx
subq %r12, %r13
jne 0x1e97d
movq 0x1c0(%r15), %rdi
callq 0x1e849
movq %rax, %r12
movb $0x1, %bl
testq %rax, %rax
leaq 0x706a(%rip), %r13      # 0x25abc
jne 0x1e909
leaq 0x10(%rsp), %rdx
movl $0x0, (%rdx)
movl $0xb, %esi
subl %r14d, %esi
movq 0x8(%rsp), %rdi
movl %r14d, %ecx
callq 0x1ea87
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq