// File: /workspace/github_c_makefile_star2_O1/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 0x1c6bb
cmpq $0x0, (%r15)
jne 0x1db6d
movzbl 0x50(%r14), %eax
leaq 0x1162d(%rip), %rcx     # 0x2f180
movq (%rcx,%rax,8), %rax
movq (%rax), %rsi
leaq 0x4a3f(%rip), %rdx      # 0x225a0
movl $0x61, %edi
xorl %eax, %eax
callq 0x6f84
leaq 0x18(%rsp), %rdi
xorl %ebp, %ebp
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x5120
movq %rsp, %r15
movq %r15, %rdi
callq 0xcba3
movq (%r15), %rcx
movq 0x8(%r15), %rax
movq 0x58(%rcx), %r15
addq 0x48(%rcx), %r15
testq %rax, %rax
je 0x1dba2
movl 0x50(%rax), %ebp
leaq 0x18(%rsp), %rdi
movl $0x601a, %esi           # imm = 0x601A
callq 0x6969
leaq 0x1a(%rsp), %rdi
addl $0xe4, %r15d
movl %r15d, %esi
callq 0x6971
leaq 0x1e(%rsp), %rdi
movl %ebp, %esi
callq 0x6971
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x1dbe1
movq 0x48(%rax), %rax
jmp 0x1dbe3
xorl %eax, %eax
leaq 0x22(%rsp), %rdi
movq 0x8(%rsp), %rcx
xorl %r15d, %r15d
testq %rcx, %rcx
je 0x1dbfb
movq 0x58(%rcx), %rsi
jmp 0x1dbfd
xorl %esi, %esi
addl %eax, %esi
callq 0x6971
leaq 0x2a(%rsp), %rdi
movl $0x4d694e54, %esi       # imm = 0x4D694E54
callq 0x6971
leaq 0x2e(%rsp), %rdi
movl 0x108(%r14), %esi
callq 0x6971
leaq 0x32(%rsp), %rdi
xorl %esi, %esi
callq 0x6969
movq %rsp, %r12
movq %r14, %rdi
movq %r12, %rsi
movl $0x1, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x1c73d
movq %r14, %rdi
movq %r12, %rsi
movl $0x2, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x1c73d
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x1c73d
movq %r14, %rdi
movl $0x1, %esi
callq 0x1c99d
movq (%r12), %rsi
movq %r14, %rdi
callq 0xb7c8
movq 0x8(%r12), %rsi
movq %r14, %rdi
callq 0xb7c8
leaq 0x26(%rsp), %r13
leaq 0x15e7e(%rip), %rbp     # 0x33b28
movl 0x20(%rbp), %eax
leal (%rax,%rax,2), %esi
shll $0x2, %esi
leaq 0x15e46(%rip), %rax     # 0x33b00
addl 0x20(%rax), %esi
movq %r13, %rdi
callq 0x6971
movabsq $-0x5f704b1e5ffc5e0, %rax # imm = 0xFA08FB4E1A003A20
movq %rax, 0xe(%r13)
leaq 0x3c(%rsp), %rdi
movl $0x108, %esi            # imm = 0x108
callq 0x6971
leaq 0x40(%rsp), %rdi
movq (%r12), %rax
movl 0x58(%rax), %esi
addl 0x48(%rax), %esi
callq 0x6971
leaq 0x44(%rsp), %rdi
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x1dd09
movl 0x50(%rax), %r15d
movl %r15d, %esi
callq 0x6971
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x1dd21
movq 0x48(%rax), %rax
jmp 0x1dd23
xorl %eax, %eax
leaq 0x48(%rsp), %rdi
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
je 0x1dd38
movq 0x58(%rcx), %rsi
jmp 0x1dd3a
xorl %esi, %esi
addl %eax, %esi
callq 0x6971
leaq 0x4c(%rsp), %rdi
movl 0x20(%rbp), %eax
shll $0x2, %eax
leal (%rax,%rax,2), %esi
callq 0x6971
leaq 0x50(%rsp), %rdi
movl $0xe4, %esi
callq 0x6971
movq 0x18(%rbp), %r15
leaq 0x77a7(%rip), %rdi      # 0x25515
callq 0x6e09
movzwl %ax, %eax
movq (%r15,%rax,8), %r15
testq %r15, %r15
je 0x1dd9f
leaq 0x778f(%rip), %r12      # 0x25515
movq 0x18(%r15), %rsi
movq %r12, %rdi
callq 0x5160
testl %eax, %eax
je 0x1dda4
movq 0x10(%r15), %r15
testq %r15, %r15
jne 0x1dd86
xorl %r15d, %r15d
jmp 0x1dda8
addq $0x20, %r15
testq %r15, %r15
je 0x1ddc9
leaq 0x64(%rsp), %r12
addq $0x8, %r15
movq %r15, %rdi
callq 0x695d
leal 0x1c(%rax), %esi
movq %r12, %rdi
callq 0x6971
leaq 0x18(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
callq 0x6c1a
movq (%rsp), %rax
movq 0x48(%rax), %rdx
movq 0x78(%rax), %rsi
movq %rbx, %rdi
callq 0x6c1a
movq (%rsp), %rax
movq 0x58(%rax), %rsi
movq %rbx, %rdi
callq 0x6da8
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x1de19
movq 0x50(%rax), %rdx
movq 0x78(%rax), %rsi
movq %rbx, %rdi
callq 0x6c1a
movq %rbx, %rdi
callq 0x1d262
movq %rbx, %rdi
movl $0x1, %esi
callq 0x1d2aa
movq %rbx, %rdi
callq 0x5190
movq %rax, %r15
movq %rsp, %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1e1c2
movq %rbx, %rdi
callq 0x5190
movq %rax, %r14
subl %r15d, %r14d
movl $0x44, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x51f0
movq %rbx, %rdi
movl %r15d, %esi
callq 0x6c5d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x6c5d
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq