// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdi, 0x1c0(%rsp)
movq %rsi, 0x1b8(%rsp)
movl $0x1, 0x1b4(%rsp)
movq 0x8643(%rip), %rax      # 0x37d3c
movq %rax, 0x1ac(%rsp)
movq 0x1c0(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x2d100
cmpq $0x0, 0x190(%rsp)
jne 0x2f750
movq 0x1c0(%rsp), %rax
movzbl 0x50(%rax), %eax
movl %eax, %ecx
leaq 0x102fa(%rip), %rax     # 0x3fa30
movq (%rax,%rcx,8), %rax
movq (%rax), %rsi
movl $0x61, %edi
leaq 0x5e57(%rip), %rdx      # 0x355a0
movb $0x0, %al
callq 0x8c00
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x5100
leaq 0x190(%rsp), %rdi
callq 0x13560
movq 0x190(%rsp), %rax
movq 0x48(%rax), %rax
movq 0x190(%rsp), %rcx
addq 0x58(%rcx), %rax
movq %rax, 0x180(%rsp)
cmpq $0x0, 0x198(%rsp)
je 0x2f7af
movq 0x198(%rsp), %rax
movq 0x50(%rax), %rax
movq %rax, 0x60(%rsp)
jmp 0x2f7b8
xorl %eax, %eax
movq %rax, 0x60(%rsp)
jmp 0x2f7b8
movq 0x60(%rsp), %rax
movq %rax, 0x188(%rsp)
leaq 0x80(%rsp), %rdi
movl $0x601a, %esi           # imm = 0x601A
callq 0x7ae0
leaq 0x80(%rsp), %rdi
addq $0x2, %rdi
movq 0x180(%rsp), %rax
addq $0xe4, %rax
movl %eax, %esi
callq 0x7b30
leaq 0x80(%rsp), %rdi
addq $0x6, %rdi
movq 0x188(%rsp), %rax
movl %eax, %esi
callq 0x7b30
leaq 0x80(%rsp), %rax
addq $0xa, %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x1a0(%rsp)
je 0x2f842
movq 0x1a0(%rsp), %rax
movq 0x48(%rax), %rax
movq %rax, 0x50(%rsp)
jmp 0x2f84b
xorl %eax, %eax
movq %rax, 0x50(%rsp)
jmp 0x2f84b
movq 0x50(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x198(%rsp)
je 0x2f873
movq 0x198(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x40(%rsp)
jmp 0x2f87c
xorl %eax, %eax
movq %rax, 0x40(%rsp)
jmp 0x2f87c
movq 0x58(%rsp), %rdi
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
addq %rcx, %rax
movl %eax, %esi
callq 0x7b30
leaq 0x80(%rsp), %rdi
addq $0x12, %rdi
movl $0x4d694e54, %esi       # imm = 0x4D694E54
callq 0x7b30
leaq 0x80(%rsp), %rdi
addq $0x16, %rdi
movq 0x1c0(%rsp), %rax
movl 0x108(%rax), %esi
callq 0x7b30
leaq 0x80(%rsp), %rdi
addq $0x1a, %rdi
xorl %esi, %esi
callq 0x7ae0
movq 0x1c0(%rsp), %rdi
leaq 0x190(%rsp), %rsi
movl $0x1, %r8d
xorl %ecx, %ecx
movl %r8d, %edx
callq 0x2d190
movq 0x1c0(%rsp), %rdi
leaq 0x190(%rsp), %rsi
movl $0x2, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x2d190
movq 0x1c0(%rsp), %rdi
leaq 0x190(%rsp), %rsi
xorl %ecx, %ecx
movl $0x1, %r8d
movl %ecx, %edx
callq 0x2d190
movq 0x1c0(%rsp), %rdi
movl $0x1, %esi
callq 0x2d600
movq 0x1c0(%rsp), %rdi
movq 0x190(%rsp), %rsi
callq 0x114f0
movq 0x1c0(%rsp), %rdi
movq 0x198(%rsp), %rsi
callq 0x114f0
leaq 0x80(%rsp), %rdi
addq $0xe, %rdi
leaq 0x1556b(%rip), %rax     # 0x44ef8
movl 0x20(%rax), %eax
imulq $0xc, %rax, %rax
leaq 0x15535(%rip), %rcx     # 0x44ed0
movl 0x20(%rcx), %ecx
addq %rcx, %rax
movl %eax, %esi
callq 0x7b30
movq 0x1ac(%rsp), %rax
movq %rax, 0x9c(%rsp)
leaq 0x80(%rsp), %rdi
addq $0x24, %rdi
movl $0x108, %esi            # imm = 0x108
callq 0x7b30
leaq 0x80(%rsp), %rdi
addq $0x24, %rdi
addq $0x4, %rdi
movq 0x190(%rsp), %rax
movq 0x48(%rax), %rax
movq 0x190(%rsp), %rcx
addq 0x58(%rcx), %rax
movl %eax, %esi
callq 0x7b30
leaq 0x80(%rsp), %rax
addq $0x24, %rax
addq $0x8, %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x198(%rsp)
je 0x2fa30
movq 0x198(%rsp), %rax
movq 0x50(%rax), %rax
movq %rax, 0x30(%rsp)
jmp 0x2fa39
xorl %eax, %eax
movq %rax, 0x30(%rsp)
jmp 0x2fa39
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rax
movl %eax, %esi
callq 0x7b30
leaq 0x80(%rsp), %rax
addq $0x24, %rax
addq $0xc, %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x1a0(%rsp)
je 0x2fa7d
movq 0x1a0(%rsp), %rax
movq 0x48(%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x2fa86
xorl %eax, %eax
movq %rax, 0x20(%rsp)
jmp 0x2fa86
movq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x198(%rsp)
je 0x2faae
movq 0x198(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x2fab7
xorl %eax, %eax
movq %rax, 0x10(%rsp)
jmp 0x2fab7
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
addq %rcx, %rax
movl %eax, %esi
callq 0x7b30
leaq 0x80(%rsp), %rdi
addq $0x24, %rdi
addq $0x10, %rdi
leaq 0x15411(%rip), %rax     # 0x44ef8
movl 0x20(%rax), %eax
imulq $0xc, %rax, %rax
movl %eax, %esi
callq 0x7b30
leaq 0x80(%rsp), %rdi
addq $0x24, %rdi
addq $0x14, %rdi
movl $0xe4, %esi
callq 0x7b30
leaq 0x801f(%rip), %rdi      # 0x37b35
callq 0x2fc90
movq %rax, 0x68(%rsp)
cmpq $0x0, %rax
je 0x2fb5f
leaq 0x80(%rsp), %rax
addq $0x4c, %rax
movq %rax, 0x8(%rsp)
movq 0x68(%rsp), %rdi
addq $0x8, %rdi
callq 0x7a00
movq 0x8(%rsp), %rdi
movl %eax, %eax
addq $0x100, %rax            # imm = 0x100
subq $0xe4, %rax
movl %eax, %esi
callq 0x7b30
movq 0x1b8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x8470
movq 0x1b8(%rsp), %rdi
movq 0x190(%rsp), %rax
movq 0x78(%rax), %rsi
movq 0x190(%rsp), %rax
movq 0x48(%rax), %rdx
callq 0x8470
movq 0x1b8(%rsp), %rdi
movq 0x190(%rsp), %rax
movq 0x58(%rax), %rsi
callq 0x86c0
cmpq $0x0, 0x198(%rsp)
je 0x2fbe7
movq 0x1b8(%rsp), %rdi
movq 0x198(%rsp), %rax
movq 0x78(%rax), %rsi
movq 0x198(%rsp), %rax
movq 0x50(%rax), %rdx
callq 0x8470
movq 0x1b8(%rsp), %rdi
callq 0x2e490
movq 0x1b8(%rsp), %rdi
movl $0x1, %esi
callq 0x2e4d0
movq 0x1b8(%rsp), %rdi
callq 0x5160
movq %rax, 0x78(%rsp)
movq 0x1c0(%rsp), %rdi
movq 0x1b8(%rsp), %rsi
leaq 0x190(%rsp), %rdx
callq 0x2fff0
movq 0x1b8(%rsp), %rdi
callq 0x5160
subq 0x78(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x1b8(%rsp), %rdi
movl $0x44, %esi
xorl %edx, %edx
callq 0x51d0
movq 0x1b8(%rsp), %rdi
movq 0x78(%rsp), %rax
movl %eax, %esi
callq 0x84d0
movq 0x1b8(%rsp), %rdi
movq 0x70(%rsp), %rax
movl %eax, %esi
callq 0x84d0
addq $0x1c8, %rsp            # imm = 0x1C8
retq