// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq 0x98(%rdi), %r15
movq %rdi, 0x8(%rsp)
movq 0x1c0(%rdi), %r12
movq %rsi, %rdi
movl $0x3f3, %esi            # imm = 0x3F3
callq 0x6856
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6856
cmpq $0x0, (%r12)
je 0x1147f
movq 0x8(%rsp), %r14
movl 0x1d8(%r14), %esi
movq %rbx, %rdi
callq 0x6856
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6856
movl 0x1d8(%r14), %esi
decl %esi
movq %rbx, %rdi
callq 0x6856
movq (%r12), %r13
testq %r13, %r13
je 0x1118a
movzbl 0x21(%r12), %eax
andl $-0x40, %eax
shll $0x18, %eax
movq 0x48(%r12), %rsi
addq $0x3, %rsi
shrq $0x2, %rsi
orl %eax, %esi
movq %rbx, %rdi
callq 0x6856
incl %ebp
movq %r13, %r12
jmp 0x11157
movq 0x8(%rsp), %rax
movl 0x1d8(%rax), %edx
cmpl %ebp, %edx
je 0x111a9
leaq 0x10152(%rip), %rdi     # 0x212f2
movl %ebp, %esi
xorl %eax, %eax
callq 0x6b2c
movq 0x8(%rsp), %rax
movq 0x1c0(%rax), %r13
leaq 0x101b9(%rip), %r14     # 0x21375
movq 0x8(%rsp), %rbp
movq (%r13), %rcx
testq %rcx, %rcx
je 0x11470
xorl %eax, %eax
movb %al, 0x22ad2(%rip)      # 0x33ca8
movb %al, 0x22ad0(%rip)      # 0x33cac
movzbl 0x20(%r13), %esi
cmpl $0x3, %esi
movq %rcx, 0x10(%rsp)
je 0x11221
cmpl $0x2, %esi
je 0x1120b
cmpl $0x1, %esi
jne 0x11237
movq %rbx, %rdi
movl $0x3e9, %esi            # imm = 0x3E9
callq 0x6856
leaq 0x1011f(%rip), %rcx     # 0x21328
jmp 0x1124c
movq %rbx, %rdi
movl $0x3ea, %esi            # imm = 0x3EA
callq 0x6856
leaq 0x1010e(%rip), %rcx     # 0x2132d
jmp 0x1124c
movq %rbx, %rdi
movl $0x3eb, %esi            # imm = 0x3EB
callq 0x6856
leaq 0xfddb(%rip), %rcx      # 0x21010
jmp 0x1124c
leaq 0x100f4(%rip), %rdi     # 0x21332
xorl %eax, %eax
callq 0x6b2c
leaq 0x1266f(%rip), %rcx     # 0x238bb
testq %r15, %r15
je 0x11297
movq 0xb0(%r13), %r12
movl 0x48(%r13), %edx
addl $0x3, %edx
andl $-0x4, %edx
movq %r15, %rdi
leaq 0x100eb(%rip), %rsi     # 0x21357
xorl %eax, %eax
callq 0x5180
cmpq $0x0, (%r12)
je 0x11297
movq 0x20(%r12), %rdx
movl 0x30(%r12), %ecx
movq %r15, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x5180
movq (%r12), %r12
jmp 0x11273
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x12599
testb $0x2, 0x21(%r13)
jne 0x112e4
movq 0x50(%r13), %rsi
addq $0x3, %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
callq 0x6856
movq 0x50(%r13), %rdx
movq 0x78(%r13), %rsi
movq %rbx, %rdi
callq 0x6817
movl 0x50(%r13), %edx
movq %rbx, %rdi
pushq $0x2
popq %r12
movl %r12d, %esi
callq 0x693c
jmp 0x112fc
movq 0x48(%r13), %rsi
addq $0x3, %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
callq 0x6856
pushq $0x2
popq %r12
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl $0x3ec, %ecx            # imm = 0x3EC
pushq $0x1
popq %r14
movl %r14d, %r8d
pushq $0x20
popq %r9
callq 0x125d3
movq %r13, %rdi
callq 0x128ff
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl %r12d, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x12969
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl %r12d, %ecx
movl %r14d, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x12969
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl %r12d, %ecx
pushq $0x4
popq %r8
movl $0x100, %r9d            # imm = 0x100
callq 0x12969
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl %r14d, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x12969
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl %r14d, %ecx
movl %r14d, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x12969
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl %r14d, %ecx
pushq $0x4
popq %r8
movl $0x100, %r9d            # imm = 0x100
callq 0x12969
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %rbx, %r14
pushq $0x3
popq %rbx
movl %ebx, %ecx
movl %r12d, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x12969
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %ebx, %ecx
pushq $0x1
popq %r8
movl $0x100, %r9d            # imm = 0x100
callq 0x12969
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %ebx, %ecx
movq %r14, %rbx
pushq $0x4
popq %r8
movl $0x100, %r9d            # imm = 0x100
callq 0x12969
cmpb $0x1, 0x22891(%rip)     # 0x33ca8
jne 0x11423
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6856
movq %r13, %rdi
callq 0x12adb
movq 0x8(%rsp), %rax
cmpb $0x0, 0x6c(%rax)
leaq 0xff3a(%rip), %r14      # 0x21375
jne 0x11459
movq %r13, %rdi
pushq $0x1
popq %rdx
movl %edx, %esi
callq 0xae5e
testl %eax, %eax
je 0x11459
movq %rbx, %rdi
movq %r13, %rsi
callq 0x12b49
movq %rbx, %rdi
movl $0x3f2, %esi            # imm = 0x3F2
callq 0x6856
movq 0x10(%rsp), %r13
jmp 0x111c1
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x6856
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6856
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6856
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6856
movq %rbx, %rdi
movl $0x3e9, %esi            # imm = 0x3E9
callq 0x6856
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6856
movq %rbx, %rdi
movl $0x3f2, %esi            # imm = 0x3F2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6856