// File: /workspace/github_c_makefile_star2_O0/zenkj[P]luajit-arm64/src/host/buildvm.c
// Function: build_code
static int build_code(BuildCtx *ctx)
{
  int status;
  int i;

  /* Initialize DynASM structures. */
  ctx->nglob = GLOB__MAX;
  ctx->glob = (void **)malloc(ctx->nglob*sizeof(void *));
  memset(ctx->glob, 0, ctx->nglob*sizeof(void *));
  ctx->nreloc = 0;

  ctx->globnames = globnames;
  ctx->relocsym = (const char **)malloc(NRELOCSYM*sizeof(const char *));
  ctx->nrelocsym = 0;
  for (i = 0; i < (int)NRELOCSYM; i++) relocmap[i] = -1;

  ctx->dasm_ident = DASM_IDENT;
  ctx->dasm_arch = DASM_ARCH;

  dasm_init(Dst, DASM_MAXSECTION);
  dasm_setupglobal(Dst, ctx->glob, ctx->nglob);
  dasm_setup(Dst, build_actionlist);

  /* Call arch-specific backend to emit the code. */
  ctx->npc = build_backend(ctx);

  /* Finalize the code. */
  (void)dasm_checkstep(Dst, -1);
  if ((status = dasm_link(Dst, &ctx->codesz))) return status;
  ctx->code = (uint8_t *)malloc(ctx->codesz);
  if ((status = dasm_encode(Dst, (void *)ctx->code))) return status;

  /* Allocate symbol table and bytecode offsets. */
  ctx->beginsym = sym_decorate(ctx, "", LABEL_PREFIX "vm_asm_begin");
  ctx->sym = (BuildSym *)malloc((ctx->npc+ctx->nglob+1)*sizeof(BuildSym));
  ctx->nsym = 0;
  ctx->bc_ofs = (int32_t *)malloc(ctx->npc*sizeof(int32_t));

  /* Collect the opcodes (PC labels). */
  for (i = 0; i < ctx->npc; i++) {
    int32_t ofs = dasm_getpclabel(Dst, i);
    if (ofs < 0) return 0x22000000|i;
    ctx->bc_ofs[i] = ofs;
    if ((LJ_HASJIT ||
	 !(i == BC_JFORI || i == BC_JFORL || i == BC_JITERL || i == BC_JLOOP ||
	   i == BC_IFORL || i == BC_IITERL || i == BC_ILOOP)) &&
	(LJ_HASFFI || i != BC_KCDATA))
      sym_insert(ctx, ofs, LABEL_PREFIX_BC, bc_names[i]);
  }

  /* Collect the globals (named labels). */
  for (i = 0; i < ctx->nglob; i++) {
    const char *gl = globnames[i];
    int len = (int)strlen(gl);
    if (!ctx->glob[i]) {
      fprintf(stderr, "Error: undefined global %s\n", gl);
      exit(2);
    }
    /* Skip the _Z symbols. */
    if (!(len >= 2 && gl[len-2] == '_' && gl[len-1] == 'Z'))
      sym_insert(ctx, (int32_t)((uint8_t *)(ctx->glob[i]) - ctx->code),
		 LABEL_PREFIX, globnames[i]);
  }

  /* Close the address range. */
  sym_insert(ctx, (int32_t)ctx->codesz, "", "");
  ctx->nsym--;

  dasm_free(Dst);

  return 0;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq 0x28(%rsp), %rax
movl $0xa3, 0x3c(%rax)
movq 0x28(%rsp), %rax
movslq 0x3c(%rax), %rdi
shlq $0x3, %rdi
callq 0x5130
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x50(%rax)
movq 0x28(%rsp), %rax
movq 0x50(%rax), %rdi
movq 0x28(%rsp), %rax
movslq 0x3c(%rax), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x50c0
movq 0x28(%rsp), %rax
movl $0x0, 0x44(%rax)
movq 0x28(%rsp), %rax
leaq 0x11bc8(%rip), %rcx     # 0x195e0
movq %rcx, 0x78(%rax)
movl $0x128, %edi            # imm = 0x128
callq 0x5130
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x60(%rax)
movq 0x28(%rsp), %rax
movl $0x0, 0x48(%rax)
movl $0x0, 0x20(%rsp)
cmpl $0x25, 0x20(%rsp)
jge 0x7a6d
movslq 0x20(%rsp), %rcx
leaq 0x126b7(%rip), %rax     # 0x1a110
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x7a46
movq 0x28(%rsp), %rax
leaq 0xcbe6(%rip), %rcx      # 0x1465f
movq %rcx, 0x80(%rax)
movq 0x28(%rsp), %rax
leaq 0xcbe0(%rip), %rcx      # 0x1466c
movq %rcx, 0x88(%rax)
movq 0x28(%rsp), %rdi
movl $0x2, %esi
callq 0x52d0
movq 0x28(%rsp), %rdi
movq 0x28(%rsp), %rax
movq 0x50(%rax), %rsi
movq 0x28(%rsp), %rax
movl 0x3c(%rax), %edx
callq 0x5580
movq 0x28(%rsp), %rdi
leaq 0x8817(%rip), %rsi      # 0x102e0
callq 0x5750
movq 0x28(%rsp), %rdi
callq 0x8730
movl %eax, %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x38(%rax)
movq 0x28(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x7410
movq 0x28(%rsp), %rdi
movq 0x28(%rsp), %rsi
addq $0x30, %rsi
callq 0x6570
movl %eax, 0x24(%rsp)
cmpl $0x0, %eax
je 0x7b1a
movl 0x24(%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x7da0
movq 0x28(%rsp), %rax
movq 0x30(%rax), %rdi
callq 0x5130
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x28(%rsp), %rdi
movq 0x28(%rsp), %rax
movq 0x28(%rax), %rsi
callq 0x6a00
movl %eax, 0x24(%rsp)
cmpl $0x0, %eax
je 0x7b5d
movl 0x24(%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x7da0
movq 0x28(%rsp), %rdi
leaq 0xdef5(%rip), %rsi      # 0x15a5e
leaq 0xcb00(%rip), %rdx      # 0x14670
callq 0x8510
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x70(%rax)
movq 0x28(%rsp), %rax
movl 0x38(%rax), %eax
movq 0x28(%rsp), %rcx
addl 0x3c(%rcx), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x4, %rdi
callq 0x5130
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x58(%rax)
movq 0x28(%rsp), %rax
movl $0x0, 0x40(%rax)
movq 0x28(%rsp), %rax
movslq 0x38(%rax), %rdi
shlq $0x2, %rdi
callq 0x5130
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x68(%rax)
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
movq 0x28(%rsp), %rcx
cmpl 0x38(%rcx), %eax
jge 0x7c5e
movq 0x28(%rsp), %rdi
movl 0x20(%rsp), %esi
callq 0x7370
movl %eax, 0x1c(%rsp)
cmpl $0x0, 0x1c(%rsp)
jge 0x7c17
movl 0x20(%rsp), %eax
orl $0x22000000, %eax       # imm = 0x22000000
movl %eax, 0x34(%rsp)
jmp 0x7da0
movl 0x1c(%rsp), %edx
movq 0x28(%rsp), %rax
movq 0x68(%rax), %rax
movslq 0x20(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movq 0x28(%rsp), %rdi
movl 0x1c(%rsp), %esi
movslq 0x20(%rsp), %rcx
leaq 0x10dff(%rip), %rax     # 0x18a40
movq (%rax,%rcx,8), %rcx
leaq 0xca34(%rip), %rdx      # 0x14680
callq 0x87a0
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x7bde
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
movq 0x28(%rsp), %rcx
cmpl 0x3c(%rcx), %eax
jge 0x7d61
movslq 0x20(%rsp), %rcx
leaq 0x1195c(%rip), %rax     # 0x195e0
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rdi
callq 0x50a0
movl %eax, 0xc(%rsp)
movq 0x28(%rsp), %rax
movq 0x50(%rax), %rax
movslq 0x20(%rsp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x7cd7
movq 0x12341(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
movq 0x10(%rsp), %rdx
leaq 0xc9c1(%rip), %rsi      # 0x14687
movb $0x0, %al
callq 0x5110
movl $0x2, %edi
callq 0x5190
cmpl $0x2, 0xc(%rsp)
jl 0x7d0e
movq 0x10(%rsp), %rax
movl 0xc(%rsp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5f, %eax
jne 0x7d0e
movq 0x10(%rsp), %rax
movl 0xc(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5a, %eax
je 0x7d4f
movq 0x28(%rsp), %rdi
movq 0x28(%rsp), %rax
movq 0x50(%rax), %rax
movslq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x28(%rsp), %rcx
movq 0x28(%rcx), %rcx
subq %rcx, %rax
movl %eax, %esi
movslq 0x20(%rsp), %rcx
leaq 0x118a1(%rip), %rax     # 0x195e0
movq (%rax,%rcx,8), %rcx
leaq 0xc959(%rip), %rdx      # 0x146a3
callq 0x87a0
jmp 0x7d51
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x7c66
movq 0x28(%rsp), %rdi
movq 0x28(%rsp), %rax
movq 0x30(%rax), %rax
movl %eax, %esi
leaq 0xdce6(%rip), %rcx      # 0x15a5e
movq %rcx, %rdx
callq 0x87a0
movq 0x28(%rsp), %rax
movl 0x40(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x40(%rax)
movq 0x28(%rsp), %rdi
callq 0x54c0
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopl (%rax)