// File: /workspace/github_c_makefile_star2_O0/deplinenoise[P]vlink/t_amigahunk.c
// Function: writeexec
static void writeexec(struct GlobalVars *gv,FILE *f)
/* creates a target-amigahunk executable file (which is relocatable) */
{
  struct LinkedSection *ls = (struct LinkedSection *)gv->lnksec.first;
  struct LinkedSection *nextls;
  FILE *hunk_map = gv->map_file_newstyle;
  int i=0;

  fwrite32be(f,HUNK_HEADER);
  fwrite32be(f,0);  /* resident libraries no longer supp. since OS2.0 */

  if (ls->n.next == NULL) {
    /* special case: no sections, create dummy section */
    fwrite32be(f,1);
    fwrite32be(f,0);
    fwrite32be(f,0);
    fwrite32be(f,0);
    fwrite32be(f,HUNK_CODE);
    fwrite32be(f,0);
    fwrite32be(f,HUNK_END);
    return;
  }

  fwrite32be(f,gv->nsecs);  /* number of sections - no overlay support! @@@ */
  fwrite32be(f,0);
  fwrite32be(f,gv->nsecs-1);

  /* write section size specifiers */
  while (nextls = (struct LinkedSection *)ls->n.next) {
    fwrite32be(f,((ls->flags&(SF_FAST|SF_CHIP))<<24) | ((ls->size+3)>>2));
    ls = nextls;
    i++;
  }
  if (i != gv->nsecs)
    ierror("writeexec(): %d sections in list, but it should be %d",
           i,gv->nsecs);

  /* section loop */
  ls = (struct LinkedSection *)gv->lnksec.first;
  while (nextls = (struct LinkedSection *)ls->n.next) {
    const char *hunk_typename = "";
    exthunk = symhunk = FALSE;

    switch (ls->type) {  /* section type */
      case ST_CODE:
        fwrite32be(f,HUNK_CODE);
        hunk_typename = "CODE";
        break;
      case ST_DATA:
        fwrite32be(f,HUNK_DATA);
        hunk_typename = "DATA";
        break;
      case ST_UDATA:
        fwrite32be(f,HUNK_BSS);
        hunk_typename = "BSS";
        break;
      default:
        ierror("writeexec(): Illegal section type %u",ls->type);
        break;
    }

    if (hunk_map) {
      uint32_t offset = 0;
      struct Symbol *sym = (struct Symbol *)ls->symbols.first;
      fprintf(hunk_map, "SECTION size=%d type=HUNK_%s\n", (uint32_t) ((ls->size+3) & ~3), hunk_typename);
      while (sym->n.next) {
        fprintf(hunk_map, " %s %d\n", sym->name, (uint32_t) sym->value);
        sym = (struct Symbol*) sym->n.next;
      }
    }

    fix_reloc_addends(gv,ls);
    if (ls->flags & SF_UNINITIALIZED) {
      fwrite32be(f,(ls->size+3)>>2);  /* bss - size only */
    }
    else {
      fwrite32be(f,(ls->filesize+3)>>2);  /* initialized section size */
      fwritex(f,ls->data,ls->filesize);   /* write section contents */
      fwrite_align(f,2,ls->filesize);
    }

    /* relocation hunks */
    reloc_hunk(gv,f,ls,HUNK_ABSRELOC32,R_ABS,32);
    unsupp_relocs(ls);  /* print unsupported relocations */

    /* symbol table */
    ext_defs(gv,f,ls,SYMB_GLOBAL,SYM_RELOC,EXT_SYMB);
    ext_defs(gv,f,ls,SYMB_GLOBAL,SYM_ABS,EXT_IGNORE);
    ext_defs(gv,f,ls,SYMB_GLOBAL,SYM_INDIR,EXT_IGNORE);
    ext_defs(gv,f,ls,SYMB_LOCAL,SYM_RELOC,EXT_SYMB);
    ext_defs(gv,f,ls,SYMB_LOCAL,SYM_ABS,EXT_IGNORE);
    ext_defs(gv,f,ls,SYMB_LOCAL,SYM_INDIR,EXT_IGNORE);
    ext_defs(gv,f,ls,SYMB_WEAK,SYM_RELOC,EXT_IGNORE);
    ext_defs(gv,f,ls,SYMB_WEAK,SYM_ABS,EXT_IGNORE);
    ext_defs(gv,f,ls,SYMB_WEAK,SYM_INDIR,EXT_IGNORE);
    if (symhunk)
      fwrite32be(f,0);  /* close HUNK_SYMBOL block, if required */
    unsupp_symbols(ls);  /* print unsupported symbol definitions */

    /* line debug hunks */
    if (gv->strip_symbols < STRIP_DEBUG) {
      if (checktargetext(ls,TGEXT_AMIGAOS,SUBID_LINE))
        linedebug_hunks(gv,f,ls);
    }

    fwrite32be(f,HUNK_END);  /* end of this section */
    ls = nextls;
  }
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq 0x50(%rsp), %rax
movq 0x1c0(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x50(%rsp), %rax
movq 0x98(%rax), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movq 0x48(%rsp), %rdi
movl $0x3f3, %esi            # imm = 0x3F3
callq 0x84d0
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x84d0
movq 0x40(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x1c1d0
movq 0x48(%rsp), %rdi
movl $0x1, %esi
callq 0x84d0
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x84d0
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x84d0
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x84d0
movq 0x48(%rsp), %rdi
movl $0x3e9, %esi            # imm = 0x3E9
callq 0x84d0
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x84d0
movq 0x48(%rsp), %rdi
movl $0x3f2, %esi            # imm = 0x3F2
callq 0x84d0
jmp 0x1c682
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rax
movl 0x1d8(%rax), %esi
callq 0x84d0
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x84d0
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rax
movl 0x1d8(%rax), %esi
subl $0x1, %esi
callq 0x84d0
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x1c266
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rax
movzbl 0x21(%rax), %eax
andl $0xc0, %eax
shll $0x18, %eax
cltq
movq 0x40(%rsp), %rcx
movq 0x48(%rcx), %rcx
addq $0x3, %rcx
shrq $0x2, %rcx
orq %rcx, %rax
movl %eax, %esi
callq 0x84d0
movq 0x38(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x1c209
movl 0x2c(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl 0x1d8(%rcx), %eax
je 0x1c294
movl 0x2c(%rsp), %esi
movq 0x50(%rsp), %rax
movl 0x1d8(%rax), %edx
leaq 0x19e69(%rip), %rdi     # 0x360f6
movb $0x0, %al
callq 0x8ae0
movq 0x50(%rsp), %rax
movq 0x1c0(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x1c682
leaq 0x1c0a4(%rip), %rax     # 0x38367
movq %rax, 0x20(%rsp)
movl $0x0, 0x289b6(%rip)     # 0x44c88
movl $0x0, 0x289b0(%rip)     # 0x44c8c
movq 0x40(%rsp), %rax
movzbl 0x20(%rax), %eax
movl %eax, 0xc(%rsp)
subl $0x1, %eax
je 0x1c306
jmp 0x1c2f0
movl 0xc(%rsp), %eax
subl $0x2, %eax
je 0x1c323
jmp 0x1c2fb
movl 0xc(%rsp), %eax
subl $0x3, %eax
je 0x1c340
jmp 0x1c35d
movq 0x48(%rsp), %rdi
movl $0x3e9, %esi            # imm = 0x3E9
callq 0x84d0
leaq 0x19e10(%rip), %rax     # 0x3612c
movq %rax, 0x20(%rsp)
jmp 0x1c374
movq 0x48(%rsp), %rdi
movl $0x3ea, %esi            # imm = 0x3EA
callq 0x84d0
leaq 0x19df8(%rip), %rax     # 0x36131
movq %rax, 0x20(%rsp)
jmp 0x1c374
movq 0x48(%rsp), %rdi
movl $0x3eb, %esi            # imm = 0x3EB
callq 0x84d0
leaq 0x19abe(%rip), %rax     # 0x35e14
movq %rax, 0x20(%rsp)
jmp 0x1c374
movq 0x40(%rsp), %rax
movzbl 0x20(%rax), %esi
leaq 0x19dc9(%rip), %rdi     # 0x36136
movb $0x0, %al
callq 0x8ae0
cmpq $0x0, 0x30(%rsp)
je 0x1c407
movl $0x0, 0x1c(%rsp)
movq 0x40(%rsp), %rax
movq 0xb0(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x30(%rsp), %rdi
movq 0x40(%rsp), %rax
movq 0x48(%rax), %rax
addq $0x3, %rax
andq $-0x4, %rax
movl %eax, %edx
movq 0x20(%rsp), %rcx
leaq 0x19d9e(%rip), %rsi     # 0x3615b
movb $0x0, %al
callq 0x5150
movq 0x10(%rsp), %rax
cmpq $0x0, (%rax)
je 0x1c405
movq 0x30(%rsp), %rdi
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rdx
movq 0x10(%rsp), %rax
movq 0x30(%rax), %rax
movl %eax, %ecx
leaq 0x19d8a(%rip), %rsi     # 0x36179
movb $0x0, %al
callq 0x5150
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x1c3c4
jmp 0x1c407
movq 0x50(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x1de90
movq 0x40(%rsp), %rax
movzbl 0x21(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x1c446
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rax
movq 0x48(%rax), %rax
addq $0x3, %rax
shrq $0x2, %rax
movl %eax, %esi
callq 0x84d0
jmp 0x1c499
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rax
addq $0x3, %rax
shrq $0x2, %rax
movl %eax, %esi
callq 0x84d0
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rax
movq 0x78(%rax), %rsi
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rdx
callq 0x8470
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rax
movl %eax, %edx
movl $0x2, %esi
callq 0x8640
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x3ec, %ecx            # imm = 0x3EC
movl $0x1, %r8d
movl $0x20, %r9d
callq 0x1df70
movq 0x40(%rsp), %rdi
callq 0x1e520
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x2, %r8d
xorl %r9d, %r9d
movl %r8d, %ecx
callq 0x1ebc0
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x2, %ecx
movl $0x1, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x1ebc0
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x2, %ecx
movl $0x4, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x1ebc0
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x1, %ecx
movl $0x2, %r8d
xorl %r9d, %r9d
callq 0x1ebc0
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x1, %r8d
movl $0x100, %r9d            # imm = 0x100
movl %r8d, %ecx
callq 0x1ebc0
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x1, %ecx
movl $0x4, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x1ebc0
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x3, %ecx
movl $0x2, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x1ebc0
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x3, %ecx
movl $0x1, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x1ebc0
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x3, %ecx
movl $0x4, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x1ebc0
cmpl $0x0, 0x28676(%rip)     # 0x44c88
je 0x1c620
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x84d0
movq 0x40(%rsp), %rdi
callq 0x1ede0
movq 0x50(%rsp), %rax
movzbl 0x6c(%rax), %eax
cmpl $0x1, %eax
jge 0x1c664
movq 0x40(%rsp), %rdi
movl $0x1, %edx
movl %edx, %esi
callq 0x10df0
cmpl $0x0, %eax
je 0x1c662
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x1ee70
jmp 0x1c664
movq 0x48(%rsp), %rdi
movl $0x3f2, %esi            # imm = 0x3F2
callq 0x84d0
movq 0x38(%rsp), %rax
movq %rax, 0x40(%rsp)
jmp 0x1c2a5
addq $0x58, %rsp
retq
nopw (%rax,%rax)