// File: /workspace/github_c_makefile_star2_O1/LuaJIT[P]LuaJIT/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++];
      int op = 0;
      while (1) {
	int action = *p++;
	switch (action) {
	case DASM_REL_LG: p++;
	  /* fallthrough */
	case DASM_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;
	}
	  /* fallthrough */
	case DASM_SPACE: case DASM_IMM_LG: case DASM_VREG: p++;
	  /* fallthrough */
	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++;
	  /* fallthrough */
	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: op = *p++; break;
	case DASM_MARK: break;
	case DASM_SECTION: case DASM_STOP: goto stop;
	default: op = action; break;
	}
      }
      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 0x629c
movq %rsi, %r11
movq 0x28(%r14), %rcx
testq %rcx, %rcx
setne %dil
je 0x6039
movq 0x20(%r14), %rdx
movl $0x22000000, %eax       # imm = 0x22000000
cmpl $0x0, (%rdx)
jg 0x6039
xorl %eax, %eax
movl $0x4, %esi
cmpq %rcx, %rsi
setb %dil
jae 0x6039
incl %eax
cmpl $0x0, (%rdx,%rsi)
leaq 0x4(%rsi), %rsi
jle 0x601d
orl $0x22000000, %eax       # imm = 0x22000000
jmp 0x6039
testb %dil, %dil
jne 0x629c
movq 0x18(%r14), %rax
cmpq $0x29, %rax
jb 0x6098
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 0x608c
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 0x6072
leaq 0x1(%rsi), %rdi
cmpq %rax, %rsi
movq %rdi, %rsi
jne 0x6065
xorl %eax, %eax
cmpl $0x0, 0x48(%r14)
jle 0x6292
movq %r11, -0x20(%rsp)
leaq 0x50(%r14), %rdx
leaq 0x70(%r14), %rax
movq %rax, -0x28(%rsp)
leaq 0x5fbe(%rip), %r10      # 0xc07c
movl $0x3, %r11d
xorl %ebx, %ebx
xorl %r8d, %r8d
movq %r14, -0x18(%rsp)
leaq (%rbx,%rbx,4), %rax
movl %ebx, %ebp
shll $0x18, %ebp
movl 0x18(%rdx,%rax,8), %r15d
cmpl %r15d, %ebp
movq %rax, -0x10(%rsp)
je 0x625b
movq (%rdx,%rax,8), %r12
movq 0x8(%r14), %rax
movq %rax, -0x8(%rsp)
movslq %ebp, %rax
incl %ebp
movslq (%r12,%rax,4), %r14
addq -0x8(%rsp), %r14
xorl %ecx, %ecx
leaq 0x1(%r14), %rsi
movzbl (%r14), %edi
leal -0xe9(%rdi), %eax
cmpl $0x16, %eax
ja 0x61ff
movslq (%r10,%rax,4), %rax
addq %r10, %rax
jmpq *%rax
addq $0x2, %r14
movq %r14, %rsi
incl %ebp
movb $0x1, %al
movq %rsi, %r14
testb %al, %al
jne 0x6107
jmp 0x6252
xorl %eax, %eax
jmp 0x6132
addq $0x2, %r14
movq %r14, %rsi
movl %ecx, %eax
andl $0xf0, %eax
xorl %r14d, %r14d
cmpl $0x80, %eax
sete %r14b
shll $0x2, %r14d
cmpl $0xe9, %ecx
cmovel %r11d, %r14d
xorl %edi, %edi
testl %r14d, %r14d
je 0x6242
movslq %ebp, %rax
movl (%r12,%rax,4), %r9d
testl %r9d, %r9d
js 0x6242
movl %r9d, %r13d
shrl $0x18, %r13d
leaq (,%r13,4), %rdi
addq %r13, %rdi
movq (%rdx,%rdi,8), %rdi
movl (%rdi,%r9,4), %edi
cmpl %ebp, %r9d
jle 0x6216
movl %r13d, %r9d
cmpq %r9, %rbx
jae 0x6219
movq -0x28(%rsp), %r13
addl (%r13), %edi
addq $0x28, %r13
decq %r9
cmpq %r9, %rbx
jne 0x61b1
jmp 0x6219
addq $0x2, %r14
movq %r14, %rsi
movslq %ebp, %rax
incl %ebp
addl %r8d, (%r12,%rax,4)
jmp 0x6130
movzbl 0x1(%r14), %ecx
addq $0x2, %r14
jmp 0x620c
movslq %ebp, %rax
incl %ebp
movl (%r12,%rax,4), %eax
addl %r8d, %eax
movzbl 0x1(%r14), %esi
addq $0x2, %r14
andl %eax, %esi
subl %esi, %r8d
jmp 0x620c
movb $0x1, %al
movl %edi, %ecx
jmp 0x6132
addq $0x3, %r14
movb $0x1, %al
movq %r14, %rsi
jmp 0x6132
subl %r8d, %edi
subl 0x4(%r12,%rax,4), %edi
movl $0xffffff80, %eax       # imm = 0xFFFFFF80
subl %r14d, %eax
cmpl $0x80, %edi
movl $0x0, %r9d
cmovgel %r9d, %r14d
cmpl %eax, %edi
cmovll %r9d, %r14d
subl %r14d, %r8d
movl %r14d, %edi
movslq %ebp, %rax
movl %edi, 0x4(%r12,%rax,4)
addl $0x2, %ebp
jmp 0x6130
cmpl %r15d, %ebp
jne 0x60f7
movq -0x10(%rsp), %rax
leaq (%rdx,%rax,8), %rax
movslq 0x20(%rax), %rax
movslq %r8d, %r8
addq %rax, %r8
incq %rbx
movq -0x18(%rsp), %r14
movslq 0x48(%r14), %rax
addq $0x28, -0x28(%rsp)
cmpq %rax, %rbx
jl 0x60ce
movq -0x20(%rsp), %r11
xorl %eax, %eax
jmp 0x6295
xorl %r8d, %r8d
movq %r8, 0x40(%r14)
movq %r8, (%r11)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq