// File: /workspace/github_c_makefile_star2_O0/cogbee[P]vmir-latest/src/vmir.c
// Function: vmir_load
int
vmir_load(ir_unit_t *iu, const uint8_t *u8, int len)
{
  bcbitstream_t bs = {0};
  bs.rdata = u8;
  bs.bytes_length = len;


  TAILQ_INIT(&iu->iu_functions_with_bodies);

  if(setjmp(iu->iu_parser_jmp)) {
    iu_cleanup(iu);
    return VMIR_ERR_LOAD_ERROR;
  }

#ifdef VMIR_VM_JIT
  jit_init(iu);
#endif

  const uint32_t magic = read_bits(&bs, 32);
  // crosss....cogbee
  read_bits(&bs, 32*5);
  iu->iu_data_ptr = 4096;
  switch(magic) {

  case 0x6d736100: // WebAssembly
    iu->iu_mode = VMIR_WASM;
    wasm_parse_module(iu, u8 + 4, u8 + len);
    break;

  case 0xdec04342: // LLVM Bitcode
  case 0x0b17c0de: // LLVM Bitcode
    // WebAssembly need memory at 0. Bitcode don't really.
    ir_parse_blocks(iu, 2, NULL, NULL, &bs);
    break;
  default:
    return VMIR_ERR_NOT_BITCODE;
  }

  free(iu->iu_text_alloc);

#ifdef VMIR_VM_JIT
  jit_seal_code(iu);
#endif
  iu->iu_heap_start = VMIR_ALIGN(iu->iu_data_ptr, 4096);
  iu->iu_stats.data_size = iu->iu_heap_start;

  vmir_heap_init(iu);

  initialize_globals(iu, iu->iu_mem);

  libc_initialize(iu);

  iu->iu_vm_funcs  = calloc(VECTOR_LEN(&iu->iu_functions), sizeof(void *));
  iu->iu_function_table = calloc(VECTOR_LEN(&iu->iu_functions), sizeof(void *));
  
  for(int i = 0; i < VECTOR_LEN(&iu->iu_functions); i++) {
    ir_function_t *f = VECTOR_ITEM(&iu->iu_functions, i);

    iu->iu_vm_funcs[i]  = f->if_vm_text;
    iu->iu_function_table[i] = f->if_ext_func;
    if(f->if_used && f->if_vm_text == NULL && f->if_ext_func == NULL) {
      vmir_log(iu, VMIR_LOG_ERROR, "....Function %s() is not defined", f->if_name);
      if(!(iu->iu_debug_flags & VMIR_DBG_IGNORE_UNRESOLVED_FUNCTIONS)) {
        parser_error(iu, "Function %s() is not defined", f->if_name);
      }
    }
  }

  run_global_ctors(iu);

  iu_cleanup(iu);
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x31f0
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0x1a8(%rax)
movq -0x10(%rbp), %rcx
addq $0x1a8, %rcx            # imm = 0x1A8
movq -0x10(%rbp), %rax
movq %rcx, 0x1b0(%rax)
movq -0x10(%rbp), %rdi
addq $0x370, %rdi            # imm = 0x370
callq 0x32b0
cmpl $0x0, %eax
je 0x17460
movq -0x10(%rbp), %rdi
callq 0x176d0
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x176bc
leaq -0x38(%rbp), %rdi
movq %rdi, -0x58(%rbp)
movl $0x20, %esi
callq 0x17a80
movq -0x58(%rbp), %rdi
movl %eax, -0x3c(%rbp)
movl $0xa0, %esi
callq 0x17a80
movq -0x10(%rbp), %rax
movl $0x1000, 0x88(%rax)     # imm = 0x1000
movl -0x3c(%rbp), %eax
movl %eax, -0x4c(%rbp)
subl $0xdec04342, %eax       # imm = 0xDEC04342
je 0x174e1
jmp 0x174a0
movl -0x4c(%rbp), %eax
subl $0xb17c0de, %eax        # imm = 0xB17C0DE
je 0x174e1
jmp 0x174ac
movl -0x4c(%rbp), %eax
subl $0x6d736100, %eax       # imm = 0x6D736100
jne 0x174fc
jmp 0x174b8
movq -0x10(%rbp), %rax
movl $0x1, 0x64(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
movq -0x18(%rbp), %rdx
movslq -0x1c(%rbp), %rax
addq %rax, %rdx
callq 0x17b90
jmp 0x17508
movq -0x10(%rbp), %rdi
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x38(%rbp), %r8
movq %rcx, %rdx
callq 0x17da0
jmp 0x17508
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x176bc
movq -0x10(%rbp), %rax
movq 0x438(%rax), %rdi
callq 0x3030
movq -0x10(%rbp), %rax
movl 0x88(%rax), %ecx
addl $0x1000, %ecx           # imm = 0x1000
subl $0x1, %ecx
andl $0xfffff000, %ecx       # imm = 0xFFFFF000
movq -0x10(%rbp), %rax
movl %ecx, 0x8c(%rax)
movq -0x10(%rbp), %rax
movl 0x8c(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x600(%rax)
movq -0x10(%rbp), %rdi
callq 0x17e80
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x17f50
movq -0x10(%rbp), %rdi
callq 0x18000
movq -0x10(%rbp), %rax
movq 0x2e0(%rax), %rdi
movl $0x8, %esi
callq 0x32c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x2e0(%rax), %rdi
movl $0x8, %esi
callq 0x32c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movl $0x0, -0x40(%rbp)
movslq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x2e0(%rcx), %rax
jae 0x176a3
movq -0x10(%rbp), %rax
movq 0x2d8(%rax), %rax
movslq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq 0x60(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x40(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movq 0x70(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x40(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movsbl 0x29(%rax), %eax
cmpl $0x0, %eax
je 0x17693
movq -0x48(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jne 0x17693
movq -0x48(%rbp), %rax
cmpq $0x0, 0x70(%rax)
jne 0x17693
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rcx
movl $0x1, %esi
leaq 0x26b58(%rip), %rdx     # 0x3e1a8
movb $0x0, %al
callq 0x4650
movq -0x10(%rbp), %rax
movl 0x100(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
jne 0x17691
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x20(%rax), %r8
leaq 0x26b4b(%rip), %rsi     # 0x3e1c9
movl $0x3c3, %edx            # imm = 0x3C3
leaq 0x26b22(%rip), %rcx     # 0x3e1ac
movb $0x0, %al
callq 0x18220
jmp 0x17693
jmp 0x17695
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x175b9
movq -0x10(%rbp), %rdi
callq 0x18350
movq -0x10(%rbp), %rdi
callq 0x176d0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)