// File: /workspace/github_c_makefile_star2_O3/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 0x6be8
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6be8
cmpq $0x0, (%r12)
je 0x12252
movl 0x1d8(%r14), %esi
movq %rbx, %rdi
callq 0x6be8
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6be8
movl 0x1d8(%r14), %esi
decl %esi
movq %rbx, %rdi
callq 0x6be8
movq (%r12), %rax
testq %rax, %rax
je 0x11f25
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 0x6be8
incl %ebp
movq (%r13), %rax
movq %r13, %r12
testq %rax, %rax
jne 0x11ef1
movl 0x1d8(%r14), %edx
cmpl %edx, %ebp
je 0x11f40
leaq 0x113ab(%rip), %rdi     # 0x232e2
movl %ebp, %esi
xorl %eax, %eax
callq 0x52b0
movq 0x1c0(%r14), %r12
movq (%r12), %rax
testq %rax, %rax
je 0x12243
leaq 0x1140a(%rip), %r13     # 0x23365
movq %r12, %rbp
xorl %ecx, %ecx
movb %cl, 0x21952(%rip)      # 0x338b8
movb %cl, 0x21950(%rip)      # 0x338bc
movzbl 0x20(%r12), %esi
cmpl $0x3, %esi
movq %rax, (%rsp)
je 0x11fb1
cmpl $0x2, %esi
je 0x11f9b
cmpl $0x1, %esi
jne 0x11fc7
movq %rbx, %rdi
movl $0x3e9, %esi            # imm = 0x3E9
callq 0x6be8
leaq 0x1137f(%rip), %rcx     # 0x23318
jmp 0x11fdc
movq %rbx, %rdi
movl $0x3ea, %esi            # imm = 0x3EA
callq 0x6be8
leaq 0x1136e(%rip), %rcx     # 0x2331d
jmp 0x11fdc
movq %rbx, %rdi
movl $0x3eb, %esi            # imm = 0x3EB
callq 0x6be8
leaq 0x1103b(%rip), %rcx     # 0x23000
jmp 0x11fdc
leaq 0x11354(%rip), %rdi     # 0x23322
xorl %eax, %eax
callq 0x52b0
leaq 0x13cbf(%rip), %rcx     # 0x25c9b
testq %r15, %r15
je 0x12026
movq 0xb0(%rbp), %r12
movl 0x48(%rbp), %edx
addl $0x3, %edx
andl $-0x4, %edx
movq %r15, %rdi
leaq 0x1134c(%rip), %rsi     # 0x23347
xorl %eax, %eax
callq 0x5180
cmpq $0x0, (%r12)
je 0x12026
movq 0x20(%r12), %rdx
movl 0x30(%r12), %ecx
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x5180
movq (%r12), %r12
jmp 0x12002
movq %r14, %rdi
movq %rbp, %rsi
callq 0x135be
testb $0x2, 0x21(%rbp)
jne 0x1206d
movq 0x50(%rbp), %rsi
addq $0x3, %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
callq 0x6be8
movq 0x50(%rbp), %rdx
movq 0x78(%rbp), %rsi
movq %rbx, %rdi
callq 0x6ba5
movl 0x50(%rbp), %edx
movq %rbx, %rdi
movl $0x2, %esi
callq 0x6ce0
jmp 0x12081
movq 0x48(%rbp), %rsi
addq $0x3, %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
callq 0x6be8
movq (%rsp), %r12
movq 0x80(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x3ec, %ecx            # imm = 0x3EC
movl $0x1, %r8d
movl $0x20, %r9d
callq 0x135fc
movq %rbp, %rdi
callq 0x1397c
movq 0xb0(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x2, %ecx
movl $0x2, %r8d
xorl %r9d, %r9d
callq 0x139f0
movq 0xb0(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x2, %ecx
movl $0x1, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x139f0
movq 0xb0(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x2, %ecx
movl $0x4, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x139f0
movq 0xb0(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
movl $0x2, %r8d
xorl %r9d, %r9d
callq 0x139f0
movq 0xb0(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
movl $0x1, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x139f0
movq 0xb0(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
movl $0x4, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x139f0
movq 0xb0(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x3, %ecx
movl $0x2, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x139f0
movq 0xb0(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x3, %ecx
movl $0x1, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x139f0
movq 0xb0(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x3, %ecx
movl $0x4, %r8d
movl $0x100, %r9d            # imm = 0x100
callq 0x139f0
cmpb $0x1, 0x216cc(%rip)     # 0x338b8
jne 0x121f8
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6be8
movq %rbp, %rdi
callq 0x13b8e
cmpb $0x0, 0x6c(%r14)
jne 0x12229
movq %rbp, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0xb3c5
testl %eax, %eax
je 0x12229
movq 0x60(%rbp), %rsi
movq %rbx, %rdi
callq 0x13c0a
movq %rbx, %rdi
movl $0x3f2, %esi            # imm = 0x3F2
callq 0x6be8
movq (%r12), %rax
testq %rax, %rax
jne 0x11f5b
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movl $0x1, %esi
callq 0x6be8
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6be8
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6be8
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6be8
movq %rbx, %rdi
movl $0x3e9, %esi            # imm = 0x3E9
callq 0x6be8
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6be8
movq %rbx, %rdi
movl $0x3f2, %esi            # imm = 0x3F2
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6be8