// File: /workspace/github_c_makefile_star2_O0/cogbee[P]vmir-latest/src/vmir_vm.c
// Function: vmir_vm_function_call
int
vmir_vm_function_call(ir_unit_t *iu, ir_function_t *f, void *out, ...)
{
  va_list ap;
  const ir_type_t *it = &VECTOR_ITEM(&iu->iu_types, f->if_type);
  uint32_t u32;
  uint64_t u64;
  int argpos = 0;
  jmp_buf jb;
  uint64_t dummy;

  va_start(ap, out);

  argpos += it->it_function.num_parameters * sizeof(uint32_t);

  void *rf = alloca(4096 * sizeof(uint32_t));
  void *rfa = rf + argpos;

  for(int i = 0; i < it->it_function.num_parameters; i++) {

    const ir_type_t *arg = &VECTOR_ITEM(&iu->iu_types,
                                        it->it_function.parameters[i]);
    switch(legalize_type(arg)) {
    case IR_TYPE_INT8:
    case IR_TYPE_INT16:
    case IR_TYPE_INT32:
    case IR_TYPE_POINTER:
      argpos -= 4;
      u32 = va_arg(ap, int);
      *(uint32_t *)(rf + argpos) = u32;
      break;
    case IR_TYPE_INT64:
      argpos -= 8;
      u64 = va_arg(ap, uint64_t);
      *(uint64_t *)(rf + argpos) = u64;
      break;

    default:
      vmir_log(iu, VMIR_LOG_FAIL,
               "Unable to encode argument %d (%s) in call to %s",
               i, type_str(iu, arg), f->if_name);
      return VM_STOP_BAD_ARGUMENTS;
    }
  }

  uint32_t allocaptr;

  if(iu->iu_stack_stash) {
    allocaptr = iu->iu_stack_stash;
    iu->iu_stack_stash = 0;
  } else {
    allocaptr = vmir_mem_alloc(iu, iu->iu_asize, NULL);
    if(allocaptr == 0) {
      vmir_log(iu, VMIR_LOG_FAIL,
               "Unable allocate memory for stack when calling %s()",
               f->if_name);
      return VM_STOP_OUT_OF_MEMROY;
    }
  }

#ifndef VM_NO_STACK_FRAME
    uint32_t allocapeak = allocaptr;
#endif

  jmp_buf *prevjb = iu->iu_err_jmpbuf;
  iu->iu_err_jmpbuf = &jb;

  int r = setjmp(jb);
  if(!r) {
    if(out == NULL)
      out = &dummy;

    vm_frame_t F = {
      .iu = iu,
      .allocaptr = allocaptr,
#ifndef VM_NO_STACK_FRAME
      .func = f,
      .allocapeak = &allocapeak,
#endif
    };


    if(f->if_ext_func != NULL) {
      r = f->if_ext_func(out, rfa, iu, iu->iu_mem);
    } else {
      r = vm_exec(f->if_vm_text, rfa, out, &F);
    }

    if(r == 1)
      r = VM_STOP_UNCAUGHT_EXCEPTION;
  }
  iu->iu_err_jmpbuf = prevjb;
#ifndef VM_NO_STACK_FRAME
  uint32_t stackuse = allocapeak - allocaptr;

  if(stackuse > f->if_peak_stack_use) {
    f->if_peak_stack_use = stackuse;
    if(allocapeak - allocaptr > iu->iu_asize) {
      vmir_log(iu, VMIR_LOG_ERROR, "%s() peak stack usage: %d > avail: %d",
               f->if_name, allocapeak - allocaptr, iu->iu_asize);
    } else {
      iu->iu_stats.peak_stack_size =
        VMIR_MAX(iu->iu_stats.peak_stack_size, stackuse);
      vmir_log(iu, VMIR_LOG_DEBUG, "%s() peak stack usage: %d",
               f->if_name, allocapeak - allocaptr);
    }
  }
#endif

  if(iu->iu_stack_stash == 0) {
    iu->iu_stack_stash = allocaptr;
  } else {
    vmir_mem_free(iu, allocaptr);
  }

  switch(r) {
  case 0:
    break;
  case VM_STOP_EXIT:
    vmir_log(iu, VMIR_LOG_INFO, "exit(0x%x) called", iu->iu_exit_code);
    break;
  case VM_STOP_ABORT:
    vmir_log(iu, VMIR_LOG_ERROR, "abort() called");
    break;
  case VM_STOP_UNREACHABLE:
    vmir_log(iu, VMIR_LOG_ERROR, "Unreachable instruction");
    break;
  case VM_STOP_BAD_INSTRUCTION:
    vmir_log(iu, VMIR_LOG_FAIL, "Illegal instruction");
    break;
  case VM_STOP_BAD_FUNCTION:
    vmir_log(iu, VMIR_LOG_FAIL, "Bad function %s", iu->iu_exit_code);
    break;
  case VM_STOP_UNCAUGHT_EXCEPTION:
    vmir_log(iu, VMIR_LOG_ERROR, "Uncaught exception");
    break;
  case VM_STOP_ACCESS_VIOLATION:
    vmir_log(iu, VMIR_LOG_ERROR, "Access violation");
    break;
  }
  return r;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x42a0, %rsp           # imm = 0x42A0
testb %al, %al
je 0x3f67
movaps %xmm0, -0x4220(%rbp)
movaps %xmm1, -0x4210(%rbp)
movaps %xmm2, -0x4200(%rbp)
movaps %xmm3, -0x41f0(%rbp)
movaps %xmm4, -0x41e0(%rbp)
movaps %xmm5, -0x41d0(%rbp)
movaps %xmm6, -0x41c0(%rbp)
movaps %xmm7, -0x41b0(%rbp)
movq %r9, -0x4228(%rbp)
movq %r8, -0x4230(%rbp)
movq %rcx, -0x4238(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x168(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x5c(%rbp)
leaq -0x40(%rbp), %rax
leaq -0x4250(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x10(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl $0x30, 0x4(%rax)
movl $0x18, (%rax)
movq -0x48(%rbp), %rax
movslq 0x14(%rax), %rcx
shlq $0x2, %rcx
movslq -0x5c(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x5c(%rbp)
leaq -0x41a0(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
movslq -0x5c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x148(%rbp)
movl $0x0, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x420d
movq -0x10(%rbp), %rax
movq 0x168(%rax), %rax
movq -0x48(%rbp), %rcx
movq 0x8(%rcx), %rcx
movslq -0x14c(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rdi
callq 0x45d0
movl %eax, %ecx
movl %ecx, %eax
addl $-0x3, %eax
movl %ecx, -0x4254(%rbp)
subl $0x3, %eax
jb 0x4095
jmp 0x4073
movl -0x4254(%rbp), %eax
subl $0x6, %eax
je 0x4118
jmp 0x4084
movl -0x4254(%rbp), %eax
subl $0xb, %eax
jne 0x419c
jmp 0x4095
movl -0x5c(%rbp), %eax
subl $0x4, %eax
movl %eax, -0x5c(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0x4260(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x4258(%rbp)
cmpl $0x28, %eax
ja 0x40d9
movq -0x4260(%rbp), %rcx
movl -0x4258(%rbp), %edx
movslq %edx, %rax
addq 0x10(%rcx), %rax
addl $0x8, %edx
movl %edx, (%rcx)
movq %rax, -0x4268(%rbp)
jmp 0x40f6
movq -0x4260(%rbp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x8(%rcx)
movq %rax, -0x4268(%rbp)
movq -0x4268(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %edx
movq -0x140(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl %edx, (%rax,%rcx)
jmp 0x41f7
movl -0x5c(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x5c(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0x4278(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x426c(%rbp)
cmpl $0x28, %eax
ja 0x415c
movq -0x4278(%rbp), %rcx
movl -0x426c(%rbp), %edx
movslq %edx, %rax
addq 0x10(%rcx), %rax
addl $0x8, %edx
movl %edx, (%rcx)
movq %rax, -0x4280(%rbp)
jmp 0x4179
movq -0x4278(%rbp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x8(%rcx)
movq %rax, -0x4280(%rbp)
movq -0x4280(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdx
movq -0x140(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movq %rdx, (%rax,%rcx)
jmp 0x41f7
movq -0x10(%rbp), %rax
movq %rax, -0x4290(%rbp)
movl -0x14c(%rbp), %eax
movl %eax, -0x4284(%rbp)
movq -0x10(%rbp), %rdi
movq -0x158(%rbp), %rsi
callq 0x4760
movq -0x4290(%rbp), %rdi
movl -0x4284(%rbp), %ecx
movq %rax, %r8
movq -0x18(%rbp), %rax
movq 0x20(%rax), %r9
xorl %esi, %esi
leaq 0x39ea0(%rip), %rdx     # 0x3e084
movb $0x0, %al
callq 0x4650
movl $0x8, -0x4(%rbp)
jmp 0x45b8
jmp 0x41f9
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x4013
movq -0x10(%rbp), %rax
cmpl $0x0, 0xa8(%rax)
je 0x423a
movq -0x10(%rbp), %rax
movl 0xa8(%rax), %eax
movl %eax, -0x15c(%rbp)
movq -0x10(%rbp), %rax
movl $0x0, 0xa8(%rax)
jmp 0x428a
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0xa0(%rax), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x47c0
movl %eax, -0x15c(%rbp)
cmpl $0x0, -0x15c(%rbp)
jne 0x4288
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
xorl %esi, %esi
leaq 0x39e3f(%rip), %rdx     # 0x3e0b4
movb $0x0, %al
callq 0x4650
movl $0x9, -0x4(%rbp)
jmp 0x45b8
jmp 0x428a
movl -0x15c(%rbp), %eax
movl %eax, -0x160(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x168(%rbp)
movq -0x10(%rbp), %rax
leaq -0x130(%rbp), %rcx
movq %rcx, 0x38(%rax)
leaq -0x130(%rbp), %rdi
callq 0x32b0
movl %eax, -0x16c(%rbp)
cmpl $0x0, -0x16c(%rbp)
jne 0x438e
cmpq $0x0, -0x20(%rbp)
jne 0x42e5
leaq -0x138(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x198(%rbp)
movl -0x15c(%rbp), %eax
movl %eax, -0x190(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x188(%rbp)
movq $0x0, -0x180(%rbp)
leaq -0x160(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x70(%rax)
je 0x4354
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x20(%rbp), %rdi
movq -0x148(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
callq *%rax
movl %eax, -0x16c(%rbp)
jmp 0x4379
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x148(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq -0x198(%rbp), %rcx
callq 0x4840
movl %eax, -0x16c(%rbp)
cmpl $0x1, -0x16c(%rbp)
jne 0x438c
movl $0x6, -0x16c(%rbp)
jmp 0x438e
movq -0x168(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movl -0x160(%rbp), %eax
subl -0x15c(%rbp), %eax
movl %eax, -0x19c(%rbp)
movl -0x19c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x88(%rcx), %eax
jbe 0x4496
movl -0x19c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x88(%rax)
movl -0x160(%rbp), %eax
subl -0x15c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xa0(%rcx), %eax
jbe 0x4427
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
movl -0x160(%rbp), %r8d
subl -0x15c(%rbp), %r8d
movq -0x10(%rbp), %rax
movl 0xa0(%rax), %r9d
movl $0x1, %esi
leaq 0x39cc9(%rip), %rdx     # 0x3e0e7
movb $0x0, %al
callq 0x4650
jmp 0x4494
movq -0x10(%rbp), %rax
movl 0x608(%rax), %eax
cmpl -0x19c(%rbp), %eax
jbe 0x444b
movq -0x10(%rbp), %rax
movl 0x608(%rax), %eax
movl %eax, -0x4294(%rbp)
jmp 0x4457
movl -0x19c(%rbp), %eax
movl %eax, -0x4294(%rbp)
movl -0x4294(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x608(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
movl -0x160(%rbp), %r8d
subl -0x15c(%rbp), %r8d
movl $0x3, %esi
leaq 0x39c80(%rip), %rdx     # 0x3e10d
movb $0x0, %al
callq 0x4650
jmp 0x4496
movq -0x10(%rbp), %rax
cmpl $0x0, 0xa8(%rax)
jne 0x44b5
movl -0x15c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xa8(%rax)
jmp 0x44c4
movq -0x10(%rbp), %rdi
movl -0x15c(%rbp), %esi
callq 0x16910
movl -0x16c(%rbp), %eax
movq %rax, -0x42a0(%rbp)
subq $0x7, %rax
ja 0x45af
movq -0x42a0(%rbp), %rax
leaq 0x37ea7(%rip), %rcx     # 0x3c390
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x45af
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x40(%rax), %ecx
movl $0x2, %esi
leaq 0x39c19(%rip), %rdx     # 0x3e127
movb $0x0, %al
callq 0x4650
jmp 0x45af
movq -0x10(%rbp), %rdi
movl $0x1, %esi
leaq 0x39c0f(%rip), %rdx     # 0x3e139
movb $0x0, %al
callq 0x4650
jmp 0x45af
movq -0x10(%rbp), %rdi
movl $0x1, %esi
leaq 0x39c05(%rip), %rdx     # 0x3e148
movb $0x0, %al
callq 0x4650
jmp 0x45af
movq -0x10(%rbp), %rdi
xorl %esi, %esi
leaq 0x39c07(%rip), %rdx     # 0x3e160
movb $0x0, %al
callq 0x4650
jmp 0x45af
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x40(%rax), %ecx
xorl %esi, %esi
leaq 0x39bfe(%rip), %rdx     # 0x3e174
movb $0x0, %al
callq 0x4650
jmp 0x45af
movq -0x10(%rbp), %rdi
movl $0x1, %esi
leaq 0x39bf5(%rip), %rdx     # 0x3e184
movb $0x0, %al
callq 0x4650
jmp 0x45af
movq -0x10(%rbp), %rdi
movl $0x1, %esi
leaq 0x39bef(%rip), %rdx     # 0x3e197
movb $0x0, %al
callq 0x4650
movl -0x16c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x42a0, %rsp           # imm = 0x42A0
popq %rbp
retq
nopw %cs:(%rax,%rax)