// File: /workspace/github_c_makefile_star2_O2/deplinenoise[P]vlink/t_aoutmint.c
// Function: aoutmint_writeexec
static void aoutmint_writeexec(struct GlobalVars *gv,FILE *f)
/* creates an a.out-MiNT executable file */
{
  const int be = _BIG_ENDIAN_;
  uint8_t jmp_entry_code[] = { 0x20,0x3a,0x00,0x1a,0x4e,0xfb,0x08,0xfa };
  struct LinkedSection *sections[3];
  unsigned long secsizes[2];
  struct mint_exec me;
  long tparel_offset,tparel_size;
  struct nlist32 *stksize;

  aout_initwrite(gv,sections);
  if (sections[0] == NULL)  /* this requires a .text section! */
    error(97,fff[gv->dest_format]->tname,TEXTNAME);

  memset(&me,0,sizeof(struct mint_exec));  /* init header with zero */
  text_data_bss_gaps(sections);  /* calculate gap size between sections */
  secsizes[0] = sections[0]->size + sections[0]->gapsize;
  secsizes[1] = sections[1] ? sections[1]->filesize : 0;

  /* init TOS header */
  write16be(me.tos.ph_branch,0x601a);
  write32be(me.tos.ph_tlen,secsizes[0]+TEXT_OFFSET);
  write32be(me.tos.ph_dlen,secsizes[1]);
  write32be(me.tos.ph_blen,(sections[2]?sections[2]->size:0) +
                           (sections[1]?sections[1]->gapsize:0));
  write32be(me.tos.ph_magic,0x4d694e54);  /* "MiNT" */
  write32be(me.tos.ph_flags,gv->tosflags);  /* Atari memory flags */
  write16be(me.tos.ph_abs,0);  /* includes relocations */

  aout_addsymlist(gv,sections,BIND_GLOBAL,0,be);
  aout_addsymlist(gv,sections,BIND_WEAK,0,be);
  aout_addsymlist(gv,sections,BIND_LOCAL,0,be);
  aout_debugsyms(gv,be);
  calc_relocs(gv,sections[0]);
  calc_relocs(gv,sections[1]);

  /* The Atari symbol table size is the sum of a.out symbols and strings,
     which is now known. */
  write32be(me.tos.ph_slen,aoutsymlist.nextindex * sizeof(struct nlist32) +
            aoutstrlist.nextoffset);

  /* set jmp_entry to  move.l  a_entry(pc),d0
                       jmp     (-6,pc,d0.l)   */
  memcpy(me.jmp_entry,jmp_entry_code,sizeof(jmp_entry_code));

  /* init a.out NMAGIC header */
  SETMIDMAG(&me.aout,NMAGIC,0,0);
  write32be(me.aout.a_text,sections[0]->size+sections[0]->gapsize);
  write32be(me.aout.a_data,sections[1]?sections[1]->filesize:0);
  write32be(me.aout.a_bss,(sections[2]?sections[2]->size:0) +
                          (sections[1]?sections[1]->gapsize:0));
  write32be(me.aout.a_syms,aoutsymlist.nextindex*sizeof(struct nlist32));
  write32be(me.aout.a_entry,TEXT_OFFSET);

  /* save offset to __stksize when symbol is present */
  if (stksize = find_aout_sym("__stksize")) {
    write32be(me.stkpos,
              read32be(&stksize->n_value)+sizeof(me)-TEXT_OFFSET);
  }

  /* write a.out-MiNT header (256 bytes) */
  fwritex(f,&me,sizeof(me));

  /* write sections */
  fwritex(f,sections[0]->data,sections[0]->size);
  fwritegap(f,sections[0]->gapsize);
  if (sections[1])
    fwritex(f,sections[1]->data,sections[1]->filesize);

  /* write a.out symbols */
  aout_writesymbols(f);
  aout_writestrings(f,be);

  /* write TPA relocs */
  tparel_offset = ftell(f);
  tos_writerelocs(gv,f,sections);
  tparel_size = ftell(f) - tparel_offset;

  /* we have to patch tparel_pos and tparel_size in the header, as we
     didn't know about the size of the TPA relocs table before */
  fseek(f,offsetof(struct mint_exec,tparel_pos),SEEK_SET);
  fwrite32be(f,tparel_offset);
  fwrite32be(f,tparel_size);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, %rbx
movq %rdi, %r14
movq %rsp, %r15
movq %r15, %rsi
callq 0x1acd9
cmpq $0x0, (%r15)
jne 0x1c0c7
movzbl 0x50(%r14), %eax
leaq 0x130e1(%rip), %rcx     # 0x2f190
movq (%rcx,%rax,8), %rax
movq (%rax), %rsi
leaq 0x44e3(%rip), %rdx      # 0x205a0
pushq $0x61
popq %rdi
xorl %eax, %eax
callq 0x6c19
leaq 0x18(%rsp), %rdi
xorl %ebp, %ebp
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x5120
movq %rsp, %r15
movq %r15, %rdi
callq 0xc3b1
movq (%r15), %rcx
movq 0x8(%r15), %rax
movq 0x58(%rcx), %r15
addq 0x48(%rcx), %r15
testq %rax, %rax
je 0x1c0fc
movl 0x50(%rax), %ebp
leaq 0x18(%rsp), %rdi
movl $0x601a, %esi           # imm = 0x601A
callq 0x6613
leaq 0x1a(%rsp), %rdi
addl $0xe4, %r15d
movl %r15d, %esi
callq 0x661b
leaq 0x1e(%rsp), %rdi
movl %ebp, %esi
callq 0x661b
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x1c13b
movq 0x48(%rax), %rax
jmp 0x1c13d
xorl %eax, %eax
leaq 0x22(%rsp), %rdi
movq 0x8(%rsp), %rcx
xorl %r15d, %r15d
testq %rcx, %rcx
je 0x1c155
movq 0x58(%rcx), %rsi
jmp 0x1c157
xorl %esi, %esi
addl %eax, %esi
callq 0x661b
leaq 0x2a(%rsp), %rdi
movl $0x4d694e54, %esi       # imm = 0x4D694E54
callq 0x661b
leaq 0x2e(%rsp), %rdi
movl 0x108(%r14), %esi
callq 0x661b
leaq 0x32(%rsp), %rdi
xorl %esi, %esi
callq 0x6613
movq %rsp, %r12
pushq $0x1
popq %rbp
movq %r14, %rdi
movq %r12, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
movl %ebp, %r8d
callq 0x1ad58
pushq $0x2
popq %rdx
movq %r14, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
movl %ebp, %r8d
callq 0x1ad58
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl %ebp, %r8d
callq 0x1ad58
movq %r14, %rdi
movl %ebp, %esi
callq 0x1afd8
movq (%r12), %rsi
movq %r14, %rdi
callq 0xb170
movq 0x8(%r12), %rsi
movq %r14, %rdi
callq 0xb170
leaq 0x17d27(%rip), %rbp     # 0x33f18
imull $0xc, 0x20(%rbp), %esi
leaq 0x26(%rsp), %r13
leaq 0x17cef(%rip), %rax     # 0x33ef0
addl 0x20(%rax), %esi
movq %r13, %rdi
callq 0x661b
movabsq $-0x5f704b1e5ffc5e0, %rax # imm = 0xFA08FB4E1A003A20
movq %rax, 0xe(%r13)
leaq 0x3c(%rsp), %rdi
movl $0x108, %esi            # imm = 0x108
callq 0x661b
leaq 0x40(%rsp), %rdi
movq (%r12), %rax
movl 0x58(%rax), %esi
addl 0x48(%rax), %esi
callq 0x661b
leaq 0x44(%rsp), %rdi
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x1c250
movl 0x50(%rax), %r15d
movl %r15d, %esi
callq 0x661b
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x1c268
movq 0x48(%rax), %rax
jmp 0x1c26a
xorl %eax, %eax
leaq 0x48(%rsp), %rdi
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
je 0x1c27f
movq 0x58(%rcx), %rsi
jmp 0x1c281
xorl %esi, %esi
addl %eax, %esi
callq 0x661b
leaq 0x4c(%rsp), %rdi
imull $0xc, 0x20(%rbp), %esi
callq 0x661b
leaq 0x50(%rsp), %rdi
movl $0xe4, %esi
callq 0x661b
movq 0x18(%rbp), %r12
leaq 0x6e75(%rip), %r15      # 0x23125
movq %r15, %rdi
callq 0x69ee
movzwl %ax, %eax
leaq (%r12,%rax,8), %r12
movq (%r12), %r12
testq %r12, %r12
je 0x1c2fb
movq 0x18(%r12), %rsi
movq %r15, %rdi
callq 0x5160
testl %eax, %eax
je 0x1c2df
addq $0x10, %r12
jmp 0x1c2bf
leaq 0x64(%rsp), %r15
addq $0x28, %r12
movq %r12, %rdi
callq 0x6607
leal 0x1c(%rax), %esi
movq %r15, %rdi
callq 0x661b
leaq 0x18(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
callq 0x6817
movq (%rsp), %rax
movq 0x48(%rax), %rdx
movq 0x78(%rax), %rsi
movq %rbx, %rdi
callq 0x6817
movq (%rsp), %rax
movq 0x58(%rax), %rsi
movq %rbx, %rdi
callq 0x698d
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x1c34b
movq 0x50(%rax), %rdx
movq 0x78(%rax), %rsi
movq %rbx, %rdi
callq 0x6817
movq %rbx, %rdi
callq 0x1b827
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x1b861
movq %rbx, %rdi
callq 0x5190
movq %rax, %r15
movq %rsp, %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1c6d9
movq %rbx, %rdi
callq 0x5190
movq %rax, %r14
subl %r15d, %r14d
pushq $0x44
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x5200
movq %rbx, %rdi
movl %r15d, %esi
callq 0x6856
movq %rbx, %rdi
movl %r14d, %esi
callq 0x6856
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop