// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
movq 0x98(%rdi), %r15
movq 0x1c0(%rdi), %r12
movq %rsi, %rdi
movl $0x3f3, %esi            # imm = 0x3F3
callq 0x6c5d
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6c5d
cmpq $0x0, (%r12)
je 0x123cc
movl 0x1d8(%r14), %esi
movq %rbx, %rdi
callq 0x6c5d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6c5d
movl 0x1d8(%r14), %esi
decl %esi
movq %rbx, %rdi
callq 0x6c5d
movq (%r12), %rax
testq %rax, %rax
je 0x120c8
xorl %ebp, %ebp
movq %rax, %r13
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 0x6c5d
incl %ebp
movq (%r13), %rax
movq %r13, %r12
testq %rax, %rax
jne 0x12094
movl 0x1d8(%r14), %edx
cmpl %edx, %ebp
je 0x120e3
leaq 0x11228(%rip), %rdi     # 0x23302
movl %ebp, %esi
xorl %eax, %eax
callq 0x52b0
movq 0x1c0(%r14), %r12
movq (%r12), %rax
testq %rax, %rax
je 0x123bd
leaq 0x11287(%rip), %r13     # 0x23385
movq %r12, %rbp
xorl %ecx, %ecx
movb %cl, 0x217af(%rip)      # 0x338b8
movb %cl, 0x217ad(%rip)      # 0x338bc
movzbl 0x20(%r12), %esi
cmpl $0x3, %esi
movq %rax, (%rsp)
je 0x12154
cmpl $0x2, %esi
je 0x1213e
cmpl $0x1, %esi
jne 0x1216a
movq %rbx, %rdi
movl $0x3e9, %esi            # imm = 0x3E9
callq 0x6c5d
leaq 0x111fc(%rip), %rcx     # 0x23338
jmp 0x1217f
movq %rbx, %rdi
movl $0x3ea, %esi            # imm = 0x3EA
callq 0x6c5d
leaq 0x111eb(%rip), %rcx     # 0x2333d
jmp 0x1217f
movq %rbx, %rdi
movl $0x3eb, %esi            # imm = 0x3EB
callq 0x6c5d
leaq 0x10eb8(%rip), %rcx     # 0x23020
jmp 0x1217f
leaq 0x111d1(%rip), %rdi     # 0x23342
xorl %eax, %eax
callq 0x52b0
leaq 0x13b3c(%rip), %rcx     # 0x25cbb
testq %r15, %r15
je 0x121c9
movq 0xb0(%rbp), %r12
movl 0x48(%rbp), %edx
addl $0x3, %edx
andl $-0x4, %edx
movq %r15, %rdi
leaq 0x111c9(%rip), %rsi     # 0x23367
xorl %eax, %eax
callq 0x5180
cmpq $0x0, (%r12)
je 0x121c9
movq 0x20(%r12), %rdx
movl 0x30(%r12), %ecx
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x5180
movq (%r12), %r12
jmp 0x121a5
movq %r14, %rdi
movq %rbp, %rsi
callq 0x13708
testb $0x2, 0x21(%rbp)
jne 0x12210
movq 0x50(%rbp), %rsi
addq $0x3, %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
callq 0x6c5d
movq 0x50(%rbp), %rdx
movq 0x78(%rbp), %rsi
movq %rbx, %rdi
callq 0x6c1a
movl 0x50(%rbp), %edx
movq %rbx, %rdi
movl $0x2, %esi
callq 0x6d55
jmp 0x12224
movq 0x48(%rbp), %rsi
addq $0x3, %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
callq 0x6c5d
movq (%rsp), %r12
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl $0x3ec, %ecx            # imm = 0x3EC
movl $0x1, %r8d
movl $0x20, %r9d
callq 0x13746
movq %rbp, %rdi
callq 0x13aef
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl $0x2, %ecx
movl $0x2, %r8d
xorl %r9d, %r9d
callq 0x13b63
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl $0x2, %ecx
movl $0x1, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x13b63
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl $0x2, %ecx
movl $0x4, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x13b63
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl $0x1, %ecx
movl $0x2, %r8d
xorl %r9d, %r9d
callq 0x13b63
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl $0x1, %ecx
movl $0x1, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x13b63
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl $0x1, %ecx
movl $0x4, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x13b63
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl $0x3, %ecx
movl $0x2, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x13b63
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl $0x3, %ecx
movl $0x1, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x13b63
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl $0x3, %ecx
movl $0x4, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x13b63
cmpb $0x1, 0x21551(%rip)     # 0x338b8
jne 0x12373
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6c5d
movq %rbp, %rdi
callq 0x13cfc
cmpb $0x0, 0x6c(%r14)
jne 0x123a3
movq %rbp, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0xb488
testl %eax, %eax
je 0x123a3
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x13d78
movq %rbx, %rdi
movl $0x3f2, %esi            # imm = 0x3F2
callq 0x6c5d
movq (%r12), %rax
testq %rax, %rax
jne 0x120fe
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movl $0x1, %esi
callq 0x6c5d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6c5d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6c5d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6c5d
movq %rbx, %rdi
movl $0x3e9, %esi            # imm = 0x3E9
callq 0x6c5d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6c5d
movq %rbx, %rdi
movl $0x3f2, %esi            # imm = 0x3F2
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6c5d