// File: /workspace/github_c_makefile_star2_O3/katlogic[P]ljx/src/host/buildvm_asm.c
// Function: emit_asm
void emit_asm(BuildCtx *ctx)
{
  int i, rel;

  fprintf(ctx->fp, "\t.file \"buildvm_%s.dasc\"\n", ctx->dasm_arch);
#if LJ_ARCH_PPC64
  fprintf(ctx->fp, "\t.abiversion 2\n");
#endif
  fprintf(ctx->fp, "\t.text\n");
  emit_asm_align(ctx, 4);

#if LJ_TARGET_PS3
  emit_asm_label(ctx, ctx->beginsym, ctx->codesz, 0);
#else
  emit_asm_label(ctx, ctx->beginsym, 0, 0);
#endif
  if (ctx->mode != BUILD_machasm)
    fprintf(ctx->fp, ".Lbegin:\n");

#if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND
  /* This should really be moved into buildvm_arm.dasc. */
#if LJ_ARCH_HASFPU
  fprintf(ctx->fp,
	  ".fnstart\n"
	  ".save {r5, r6, r7, r8, r9, r10, r11, lr}\n"
	  ".vsave {d8-d15}\n"
	  ".save {r4}\n"
	  ".pad #28\n");
#else
  fprintf(ctx->fp,
	  ".fnstart\n"
	  ".save {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"
	  ".pad #28\n");
#endif
#endif
#if LJ_TARGET_MIPS
  fprintf(ctx->fp, ".set nomips16\n.abicalls\n.set noreorder\n.set nomacro\n");
#endif

  for (i = rel = 0; i < ctx->nsym; i++) {
    int32_t ofs = ctx->sym[i].ofs;
    int32_t next = ctx->sym[i+1].ofs;
#if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND && LJ_HASFFI
    if (!strcmp(ctx->sym[i].name, "lj_vm_ffi_call"))
      fprintf(ctx->fp,
	      ".globl lj_err_unwind_arm\n"
	      ".personality lj_err_unwind_arm\n"
	      ".fnend\n"
	      ".fnstart\n"
	      ".save {r4, r5, r11, lr}\n"
	      ".setfp r11, sp\n");
#endif
    emit_asm_label(ctx, ctx->sym[i].name, next - ofs, 1);
    while (rel < ctx->nreloc && ctx->reloc[rel].ofs <= next) {
      BuildReloc *r = &ctx->reloc[rel];
      int n = r->ofs - ofs;
#if LJ_TARGET_X86ORX64
      if (r->type != 0 &&
	  (ctx->mode == BUILD_elfasm || ctx->mode == BUILD_machasm)) {
	emit_asm_reloc_text(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);
      } else {
	emit_asm_bytes(ctx, ctx->code+ofs, n);
	emit_asm_reloc(ctx, r->type, ctx->relocsym[r->sym]);
      }
      ofs += n+4;
#else
      emit_asm_wordreloc(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);
      ofs += n;
#endif
      rel++;
    }
#if LJ_TARGET_X86ORX64
    emit_asm_bytes(ctx, ctx->code+ofs, next-ofs);
#else
    emit_asm_words(ctx, ctx->code+ofs, next-ofs);
#endif
  }

#if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND
  fprintf(ctx->fp,
#if !LJ_HASFFI
	  ".globl lj_err_unwind_arm\n"
	  ".personality lj_err_unwind_arm\n"
#endif
	  ".fnend\n");
#endif

  fprintf(ctx->fp, "\n");
  switch (ctx->mode) {
  case BUILD_elfasm:
#if !(LJ_TARGET_PS3 || LJ_TARGET_PSVITA)
    fprintf(ctx->fp, "\t.section .note.GNU-stack,\"\"," ELFASM_PX "progbits\n");
#endif
#if LJ_TARGET_PPC && !LJ_TARGET_PS3
    /* Hard-float ABI. */
    fprintf(ctx->fp, "\t.gnu_attribute 4, 1\n");
#endif
    /* fallthrough */
  case BUILD_coffasm:
    fprintf(ctx->fp, "\t.ident \"%s\"\n", ctx->dasm_ident);
    break;
  case BUILD_machasm:
    fprintf(ctx->fp,
      "\t.cstring\n"
      "\t.ascii \"%s\\0\"\n", ctx->dasm_ident);
    break;
  default:
    break;
  }
  fprintf(ctx->fp, "\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
movq 0x90(%rbx), %rdx
leaq 0x8731(%rip), %rsi      # 0x11ff8
xorl %eax, %eax
callq 0x5130
movq 0x10(%rbx), %rcx
leaq 0x8739(%rip), %rdi      # 0x12012
movl $0x7, %esi
movl $0x1, %edx
callq 0x51d0
movl 0x8(%rbx), %eax
cmpq $0x2, %rax
ja 0x990f
leaq 0x8910(%rip), %rcx      # 0x12208
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
movq 0x10(%rbx), %rdi
movl $0x4, %edx
xorl %eax, %eax
callq 0x5130
movq 0x70(%rbx), %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x9bf2
cmpl $0x2, 0x8(%rbx)
je 0x993f
movq 0x10(%rbx), %rcx
leaq 0x86ea(%rip), %rdi      # 0x1201a
movl $0x9, %esi
movl $0x1, %edx
callq 0x51d0
cmpl $0x0, 0x40(%rbx)
jle 0x9b70
leaq 0xa0(%rbx), %rax
movq %rax, 0x8(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x58(%rbx), %rax
movq %r14, %rcx
shlq $0x4, %rcx
movl 0x8(%rax,%rcx), %r13d
incq %r14
movq %r14, %rdx
shlq $0x4, %rdx
movl 0x8(%rax,%rdx), %ebp
movq (%rax,%rcx), %rsi
movl %ebp, 0x4(%rsp)
subl %r13d, %ebp
movq %rbx, %rdi
movl %ebp, %edx
movl $0x1, %ecx
callq 0x9bf2
cmpl 0x44(%rbx), %r15d
jge 0x9b52
movq %r14, 0x10(%rsp)
movslq %r15d, %r15
leaq (%r15,%r15,2), %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,4), %r12
movl -0x8(%r12), %r14d
cmpl 0x4(%rsp), %r14d
jg 0x9b40
movl %r14d, %edx
subl %r13d, %edx
cmpl $0x0, (%r12)
movslq %r13d, %rsi
je 0x9a21
testl $0xfffffffd, 0x8(%rbx)  # imm = 0xFFFFFFFD
jne 0x9a21
movq 0x60(%rbx), %rax
movslq -0x4(%r12), %rcx
movq (%rax,%rcx,8), %r13
testl %edx, %edx
jle 0x9bea
addq 0x28(%rbx), %rsi
leal -0x1(%rdx), %eax
movl %eax, %ecx
movzbl (%rsi,%rcx), %ecx
cmpl $0xe8, %ecx
je 0x9a8c
cmpl $0xe9, %ecx
jne 0x9a95
leaq 0x86fe(%rip), %rbp      # 0x1211a
jmp 0x9ad2
addq 0x28(%rbx), %rsi
movq %rbx, %rdi
callq 0x9cb9
movl (%r12), %ebp
movq 0x60(%rbx), %rax
movslq -0x4(%r12), %rcx
movq (%rax,%rcx,8), %r13
movl 0x8(%rbx), %eax
cmpl $0x1, %eax
je 0x9a50
testl %eax, %eax
je 0x9a65
movq 0x10(%rbx), %rdi
jmp 0x9a76
movq 0x10(%rbx), %rdi
leaq 0x8681(%rip), %rsi      # 0x120dc
movq %r13, %rdx
xorl %eax, %eax
callq 0x5130
movq 0x10(%rbx), %rdi
testl %ebp, %ebp
je 0x9a76
leaq 0x873a(%rip), %rsi      # 0x121ae
jmp 0x9a7d
leaq 0x8740(%rip), %rsi      # 0x121bd
movq %r13, %rdx
xorl %eax, %eax
callq 0x5130
jmp 0x9b23
leaq 0x6ac5(%rip), %rbp      # 0x10558
jmp 0x9ad2
testb %cl, %cl
jns 0x9bea
cmpl $0x1, %edx
je 0x9bea
cmpb $-0x71, %cl
ja 0x9bea
movl %edx, %eax
cmpb $0xf, -0x2(%rsi,%rax)
jne 0x9bea
andl $0x7f, %ecx
leaq 0x8702(%rip), %rax      # 0x121c8
movslq (%rax,%rcx,4), %rbp
addq %rax, %rbp
addl $-0x2, %edx
movl %edx, %eax
movq %rbx, %rdi
movl %eax, %edx
callq 0x9cb9
xorl %eax, %eax
cmpb $0x5f, (%r13)
sete %al
cmpb $0x6c, (%r13,%rax)
jne 0x9afe
cmpb $0x6a, 0x1(%r13,%rax)
jne 0x9afe
cmpb $0x5f, 0x2(%r13,%rax)
je 0x9b0b
cmpl $0x0, 0x8(%rbx)
leaq 0x864a(%rip), %rsi      # 0x12153
je 0x9b12
leaq 0x864d(%rip), %rsi      # 0x1215f
movq 0x10(%rbx), %rdi
movq %rbp, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x5130
addl $0x4, %r14d
incq %r15
movslq 0x44(%rbx), %rax
addq $0xc, %r12
movl %r14d, %r13d
cmpq %rax, %r15
jl 0x99b2
jmp 0x9b43
movl %r13d, %r14d
movl 0x4(%rsp), %ebp
subl %r14d, %ebp
movl %r14d, %r13d
movq 0x10(%rsp), %r14
movslq %r13d, %rsi
addq 0x28(%rbx), %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x9cb9
movslq 0x40(%rbx), %rax
cmpq %rax, %r14
jl 0x995b
movq 0x10(%rbx), %rsi
movl $0xa, %edi
callq 0x50d0
movl 0x8(%rbx), %eax
cmpl $0x2, %eax
je 0x9bb2
leaq 0x84bf(%rip), %r14      # 0x1204c
cmpl $0x1, %eax
je 0x9bb9
testl %eax, %eax
jne 0x9bce
movq 0x10(%rbx), %rcx
leaq 0x8483(%rip), %rdi      # 0x12024
movl $0x27, %esi
movl $0x1, %edx
callq 0x51d0
jmp 0x9bb9
leaq 0x84a1(%rip), %r14      # 0x1205a
movq 0x10(%rbx), %rdi
movq 0x88(%rbx), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x5130
movq 0x10(%rbx), %rsi
movl $0xa, %edi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x50d0
movq %r13, %rdi
callq 0x52fb