// File: /workspace/github_c_makefile_star2_O2/deplinenoise[P]vlink/linker.c
// Function: linker_write
void linker_write(struct GlobalVars *gv)
{
  struct LinkedSection *ls = (struct LinkedSection *)gv->lnksec.first;
  struct LinkedSection *firstls=NULL,*nextls;
  FILE *f;

  /* remove empty sections without symbols and relocs */
  gv->nsecs = 0;
  while (nextls = (struct LinkedSection *)ls->n.next) {
    if (firstls == NULL)
      firstls = ls;

    if (ls->size==0 && listempty(&ls->relocs)
        && !(ls->ld_flags & LSF_PRESERVE)) {
      if (listempty(&ls->symbols)) {
        remnode(&ls->n);
        if (ls == firstls)
          firstls = NULL;
        ls = nextls;
        continue;
      }
      else if (ls!=firstls && (!strcmp(ls->name,gv->common_sec_name) ||
                               !strcmp(ls->name,gv->scommon_sec_name))) {
        /* @@@ Attention! This is a big HACK!
           For the future it should be desirable to have a separate
           list for common symbols, instead of just putting them into
           the symbol list of the first section... @@@ */
        struct Symbol *sym;

        while (sym = (struct Symbol *)remhead(&ls->symbols)) {
          addtail(&firstls->symbols,&sym->n);
        }
        remnode(&ls->n);
        ls = nextls;
        continue;
      }
    }

    ls->index = gv->nsecs++;  /* reindex remaining sections */
    ls = nextls;
  }

  if (!gv->errflag) {  /* no error? */
    if (gv->trace_file)
      fprintf(gv->trace_file,"\nCreating output file %s (%s).\n",
              gv->dest_name,fff[gv->dest_format]->tname);

    /* write output file */
    if (f = fopen(gv->dest_name,"wb")) {
      if (gv->dest_sharedobj)
        fff[gv->dest_format]->writeshared(gv,f);
      else if (gv->dest_object)
        fff[gv->dest_format]->writeobject(gv,f);
      else
        fff[gv->dest_format]->writeexec(gv,f);
      fclose(f);
      if (!gv->dest_sharedobj && !gv->dest_object)
        set_exec(gv->dest_name);  /* set executable flag */
    }
    else  /* Can't create output file */
      error(29,gv->dest_name);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x1c0(%rdi), %r14
andl $0x0, 0x1d8(%rdi)
xorl %r13d, %r13d
movq (%r14), %rbp
testq %rbp, %rbp
je 0x9c9e
testq %r13, %r13
cmoveq %r14, %r13
cmpq $0x0, 0x48(%r14)
jne 0x9c2f
movq 0x80(%r14), %rax
cmpq $0x0, (%rax)
jne 0x9c2f
testb $0x2, 0x24(%r14)
jne 0x9c2f
movq 0xb0(%r14), %rax
cmpq $0x0, (%rax)
je 0x9c4a
cmpq %r13, %r14
je 0x9c2f
movq 0x18(%r14), %r15
movq 0x220(%rbx), %rsi
movq %r15, %rdi
callq 0x5160
testl %eax, %eax
je 0x9c66
movq 0x228(%rbx), %rsi
movq %r15, %rdi
callq 0x5160
testl %eax, %eax
je 0x9c66
movl 0x1d8(%rbx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x1d8(%rbx)
movl %eax, 0x10(%r14)
movq %rbp, %r14
jmp 0x9bc5
movq %r14, %rdi
callq 0x6444
cmpq %r13, %r14
movl $0x0, %eax
cmoveq %rax, %r13
movq %rbp, %r14
jmp 0x9bc5
leaq 0xb0(%r14), %r15
leaq 0xb0(%r13), %r12
movq %r15, %rdi
callq 0x642a
testq %rax, %rax
je 0x9c8e
movq %r12, %rdi
movq %rax, %rsi
callq 0x6414
jmp 0x9c74
movq %r14, %rdi
callq 0x6444
movq %rbp, %r14
jmp 0x9bc5
cmpl $0x0, 0x110(%rbx)
je 0x9cb6
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa0(%rbx), %rdi
testq %rdi, %rdi
je 0x9ce6
movq 0x48(%rbx), %rdx
movzbl 0x50(%rbx), %eax
leaq 0x254bf(%rip), %rcx     # 0x2f190
movq (%rcx,%rax,8), %rax
movq (%rax), %rcx
leaq 0x16769(%rip), %rsi     # 0x20448
xorl %eax, %eax
callq 0x5180
movq 0x48(%rbx), %rdi
leaq 0x16777(%rip), %rsi     # 0x20468
callq 0x5230
testq %rax, %rax
je 0x9d19
movq %rax, %r14
cmpl $0x0, 0x58(%rbx)
je 0x9d35
movzbl 0x50(%rbx), %eax
leaq 0x25481(%rip), %rcx     # 0x2f190
movq (%rcx,%rax,8), %rax
addq $0x78, %rax
jmp 0x9d54
movq 0x48(%rbx), %rsi
pushq $0x1d
popq %rdi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6c19
cmpl $0x0, 0x54(%rbx)
movzbl 0x50(%rbx), %eax
leaq 0x2544c(%rip), %rcx     # 0x2f190
movq (%rcx,%rax,8), %rax
je 0x9d50
addq $0x70, %rax
jmp 0x9d54
subq $-0x80, %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *(%rax)
movq %r14, %rdi
callq 0x50c0
cmpl $0x0, 0x58(%rbx)
jne 0x9ca7
cmpl $0x0, 0x54(%rbx)
jne 0x9ca7
movq 0x48(%rbx), %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa1ee