// File: /workspace/github_c_makefile_star2_O1/zenkj[P]luajit-arm64/src/./../dynasm/dasm_x86.h
// Function: dasm_link
int dasm_link(Dst_DECL, size_t *szp)
{
  dasm_State *D = Dst_REF;
  int secnum;
  int ofs = 0;

#ifdef DASM_CHECKS
  *szp = 0;
  if (D->status != DASM_S_OK) return D->status;
  {
    int pc;
    for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
      if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
  }
#endif

  { /* Handle globals not defined in this translation unit. */
    int idx;
    for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) {
      int n = D->lglabels[idx];
      /* Undefined label: Collapse rel chain and replace with marker (< 0). */
      while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
    }
  }

  /* Combine all code sections. No support for data sections (yet). */
  for (secnum = 0; secnum < D->maxsection; secnum++) {
    dasm_Section *sec = D->sections + secnum;
    int *b = sec->rbuf;
    int pos = DASM_SEC2POS(secnum);
    int lastpos = sec->pos;

    while (pos != lastpos) {
      dasm_ActList p = D->actionlist + b[pos++];
      while (1) {
	int op, action = *p++;
	switch (action) {
	case DASM_REL_LG: p++; op = p[-3]; goto rel_pc;
	case DASM_REL_PC: op = p[-2]; rel_pc: {
	  int shrink = op == 0xe9 ? 3 : ((op&0xf0) == 0x80 ? 4 : 0);
	  if (shrink) {  /* Shrinkable branch opcode? */
	    int lofs, lpos = b[pos];
	    if (lpos < 0) goto noshrink;  /* Ext global? */
	    lofs = *DASM_POS2PTR(D, lpos);
	    if (lpos > pos) {  /* Fwd label: add cumulative section offsets. */
	      int i;
	      for (i = secnum; i < DASM_POS2SEC(lpos); i++)
		lofs += D->sections[i].ofs;
	    } else {
	      lofs -= ofs;  /* Bkwd label: unfix offset. */
	    }
	    lofs -= b[pos+1];  /* Short branch ok? */
	    if (lofs >= -128-shrink && lofs <= 127) ofs -= shrink;  /* Yes. */
	    else { noshrink: shrink = 0; }  /* No, cannot shrink op. */
	  }
	  b[pos+1] = shrink;
	  pos += 2;
	  break;
	}
	case DASM_SPACE: case DASM_IMM_LG: case DASM_VREG: p++;
	case DASM_DISP: case DASM_IMM_S: case DASM_IMM_B: case DASM_IMM_W:
	case DASM_IMM_D: case DASM_IMM_WB: case DASM_IMM_DB:
	case DASM_SETLABEL: case DASM_REL_A: case DASM_IMM_PC: pos++; break;
	case DASM_LABEL_LG: p++;
	case DASM_LABEL_PC: b[pos++] += ofs; break; /* Fix label offset. */
	case DASM_ALIGN: ofs -= (b[pos++]+ofs)&*p++; break; /* Adjust ofs. */
	case DASM_EXTERN: p += 2; break;
	case DASM_ESC: p++; break;
	case DASM_MARK: break;
	case DASM_SECTION: case DASM_STOP: goto stop;
	}
      }
      stop: (void)0;
    }
    ofs += sec->ofs;  /* Next section starts right after current section. */
  }

  D->codesize = ofs;  /* Total size of all code sections */
  *szp = ofs;
  return DASM_S_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq (%rdi), %r14
movq $0x0, (%rsi)
movl 0x4c(%r14), %eax
testl %eax, %eax
jne 0x6204
movq %rsi, %r11
movq 0x28(%r14), %rcx
testq %rcx, %rcx
setne %dil
je 0x5fd3
movq 0x20(%r14), %rdx
movl $0x22000000, %eax       # imm = 0x22000000
cmpl $0x0, (%rdx)
jg 0x5fd3
xorl %eax, %eax
movl $0x4, %esi
cmpq %rcx, %rsi
setb %dil
jae 0x5fd3
incl %eax
cmpl $0x0, (%rdx,%rsi)
leaq 0x4(%rsi), %rsi
jle 0x5fb7
orl $0x22000000, %eax       # imm = 0x22000000
jmp 0x5fd3
testb %dil, %dil
jne 0x6204
movq 0x18(%r14), %rax
cmpq $0x29, %rax
jb 0x6032
movq 0x10(%r14), %rcx
leaq 0x50(%r14), %rdx
addq $-0x29, %rax
shrq $0x2, %rax
addq $0xa, %rax
movl $0xa, %esi
movl (%rcx,%rsi,4), %edi
testl %edi, %edi
jle 0x6026
movl %esi, %r8d
negl %r8d
movl %edi, %r9d
shrl $0x18, %edi
leaq (%rdi,%rdi,4), %rdi
movq (%rdx,%rdi,8), %r10
movl (%r10,%r9,4), %edi
movl %r8d, (%r10,%r9,4)
testl %edi, %edi
jg 0x600c
leaq 0x1(%rsi), %rdi
cmpq %rax, %rsi
movq %rdi, %rsi
jne 0x5fff
xorl %eax, %eax
cmpl $0x0, 0x48(%r14)
jle 0x61fa
movq %r11, -0x18(%rsp)
leaq 0x50(%r14), %rdx
leaq 0x70(%r14), %rax
movq %rax, -0x20(%rsp)
leaq 0x6014(%rip), %r10      # 0xc06c
movl $0x3, %r11d
xorl %ebx, %ebx
xorl %r8d, %r8d
movq %r14, -0x10(%rsp)
leaq (%rbx,%rbx,4), %rax
movl %ebx, %ebp
shll $0x18, %ebp
movl 0x18(%rdx,%rax,8), %r15d
cmpl %r15d, %ebp
movq %rax, -0x8(%rsp)
je 0x61c3
movq (%rdx,%rax,8), %r12
movq 0x8(%r14), %r13
movslq %ebp, %rax
incl %ebp
movslq (%r12,%rax,4), %rsi
addq %r13, %rsi
leaq 0x1(%rsi), %rcx
movzbl (%rsi), %eax
addl $0xffffff17, %eax       # imm = 0xFFFFFF17
cmpl $0x16, %eax
ja 0x60bb
movslq (%r10,%rax,4), %rax
addq %r10, %rax
jmpq *%rax
addq $0x2, %rsi
movq %rsi, %rcx
incl %ebp
movb $0x1, %r14b
movq %rcx, %rsi
testb %r14b, %r14b
jne 0x6098
jmp 0x61ba
xorl %r14d, %r14d
jmp 0x60be
addq $0x2, %rsi
movq %rsi, %rcx
movslq %ebp, %rax
incl %ebp
addl %r8d, (%r12,%rax,4)
jmp 0x60bb
addq $0x3, %rsi
jmp 0x6176
leaq 0x2(%rsi), %rcx
movb -0x1(%rsi), %al
movl %eax, %edi
andb $-0x10, %dil
xorl %esi, %esi
negb %dil
seto %sil
shll $0x2, %esi
cmpb $-0x17, %al
cmovel %r11d, %esi
xorl %eax, %eax
testl %esi, %esi
je 0x61aa
movslq %ebp, %r14
movl (%r12,%r14,4), %edi
testl %edi, %edi
js 0x61aa
movl %edi, %r9d
shrl $0x18, %r9d
leaq (%r9,%r9,4), %rax
movq (%rdx,%rax,8), %rax
movl (%rax,%rdi,4), %eax
cmpl %ebp, %edi
jle 0x6181
movl %r9d, %edi
cmpq %rdi, %rbx
jae 0x6184
movq -0x20(%rsp), %r9
addl (%r9), %eax
addq $0x28, %r9
decq %rdi
cmpq %rdi, %rbx
jne 0x6146
jmp 0x6184
addq $0x2, %rsi
jmp 0x6176
movslq %ebp, %rax
incl %ebp
movl (%r12,%rax,4), %eax
addl %r8d, %eax
movzbl 0x1(%rsi), %ecx
addq $0x2, %rsi
andl %eax, %ecx
subl %ecx, %r8d
movb $0x1, %r14b
movq %rsi, %rcx
jmp 0x60be
subl %r8d, %eax
subl 0x4(%r12,%r14,4), %eax
movl $0xffffff80, %edi       # imm = 0xFFFFFF80
subl %esi, %edi
cmpl $0x80, %eax
movl $0x0, %r9d
cmovgel %r9d, %esi
cmpl %edi, %eax
cmovll %r9d, %esi
subl %esi, %r8d
movl %esi, %eax
movslq %ebp, %rsi
movl %eax, 0x4(%r12,%rsi,4)
addl $0x2, %ebp
jmp 0x60bb
cmpl %r15d, %ebp
jne 0x608c
movq -0x8(%rsp), %rax
leaq (%rdx,%rax,8), %rax
movslq 0x20(%rax), %rax
movslq %r8d, %r8
addq %rax, %r8
incq %rbx
movq -0x10(%rsp), %r14
movslq 0x48(%r14), %rax
addq $0x28, -0x20(%rsp)
cmpq %rax, %rbx
jl 0x6068
movq -0x18(%rsp), %r11
xorl %eax, %eax
jmp 0x61fd
xorl %r8d, %r8d
movq %r8, 0x40(%r14)
movq %r8, (%r11)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq