// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x41f8, %rsp           # imm = 0x41F8
movq %rdx, 0x38(%rsp)
movq %rdi, %rbx
leaq 0x70(%rsp), %rdi
movq %rcx, 0x18(%rdi)
movq %r8, 0x20(%rdi)
movq %r9, 0x28(%rdi)
testb %al, %al
je 0x3c3c
movaps %xmm0, 0xa0(%rsp)
movaps %xmm1, 0xb0(%rsp)
movaps %xmm2, 0xc0(%rsp)
movaps %xmm3, 0xd0(%rsp)
movaps %xmm4, 0xe0(%rsp)
movaps %xmm5, 0xf0(%rsp)
movaps %xmm6, 0x100(%rsp)
movaps %xmm7, 0x110(%rsp)
movq 0x168(%rbx), %rax
movq %rsi, 0x8(%rsp)
movl 0x10(%rsi), %ecx
movabsq $0x3000000018, %rdx     # imm = 0x3000000018
movq %rdx, 0x10(%rsp)
leaq 0x4230(%rsp), %rdx
movq %rdx, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
imulq $0x28, %rcx, %rcx
leaq (%rax,%rcx), %r12
addq $0x8, %r12
movslq 0xc(%r12), %r15
movl %r15d, %eax
leal (,%rax,4), %ebp
movslq %ebp, %rax
addq %rsp, %rax
addq $0x1f0, %rax            # imm = 0x1F0
movq %rax, 0x30(%rsp)
xorl %eax, %eax
movl %eax, %r14d
cmpq %r15, %r14
jge 0x3d57
movq (%r12), %rax
movslq (%rax,%r14,4), %rax
imulq $0x28, %rax, %r13
addq 0x168(%rbx), %r13
movq %r13, %rdi
callq 0x3f94
movl %eax, %ecx
leal -0x3(%rcx), %edx
movslq %ebp, %rcx
cmpl $0x3, %edx
jb 0x3cdc
cmpl $0x6, %eax
je 0x3d1b
cmpl $0xb, %eax
jne 0x3e02
movl 0x10(%rsp), %eax
cmpq $0x28, %rax
ja 0x3cf9
movl %eax, %edx
leaq 0x70(%rsp), %rsi
addq %rsi, %rax
addl $0x8, %edx
movl %edx, 0x10(%rsp)
jmp 0x3d07
movq 0x18(%rsp), %rax
leaq 0x8(%rax), %rdx
movq %rdx, 0x18(%rsp)
addq $-0x4, %rcx
movl (%rax), %eax
movl %eax, 0x1f0(%rsp,%rcx)
movl %ecx, %ebp
incq %r14
jmp 0x3c9d
movl 0x10(%rsp), %eax
cmpq $0x28, %rax
ja 0x3d38
movl %eax, %edx
leaq 0x70(%rsp), %rsi
addq %rsi, %rax
addl $0x8, %edx
movl %edx, 0x10(%rsp)
jmp 0x3d46
movq 0x18(%rsp), %rax
leaq 0x8(%rax), %rdx
movq %rdx, 0x18(%rsp)
addq $-0x8, %rcx
movq (%rax), %rax
movq %rax, 0x1f0(%rsp,%rcx)
jmp 0x3d14
movl 0xa8(%rbx), %ebp
testl %ebp, %ebp
je 0x3d6f
andl $0x0, 0xa8(%rbx)
movq 0x8(%rsp), %r12
jmp 0x3d90
movl 0xa0(%rbx), %esi
xorl %eax, %eax
movl %eax, %edx
movq %rbx, %rdi
callq 0x40d8
movl %eax, %ebp
testl %eax, %eax
movq 0x8(%rsp), %r12
je 0x3e33
movl %ebp, 0x4(%rsp)
movq 0x38(%rbx), %r14
leaq 0x120(%rsp), %rdi
movq %rdi, 0x38(%rbx)
callq 0x32e0
movl %eax, %r15d
movl %ebp, %ecx
testl %eax, %eax
jne 0x3e76
movq 0x38(%rsp), %rax
testq %rax, %rax
leaq 0x68(%rsp), %rdx
cmovneq %rax, %rdx
movq %rbx, 0x40(%rsp)
movl %ebp, 0x48(%rsp)
movq %r12, 0x50(%rsp)
andq $0x0, 0x58(%rsp)
leaq 0x4(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x70(%r12), %rax
testq %rax, %rax
je 0x3e53
movq 0x20(%rbx), %rcx
movq %rdx, %rdi
movq 0x30(%rsp), %rsi
movq %rbx, %rdx
callq *%rax
jmp 0x3e67
movq %rbx, %rdi
movq %r13, %rsi
callq 0x408f
movq 0x8(%rsp), %rcx
movq 0x20(%rcx), %r9
movl %r14d, %ecx
leaq 0x1c1c0(%rip), %rdx     # 0x1ffe0
xorl %esi, %esi
movq %rbx, %rdi
movq %rax, %r8
movl %esi, %eax
callq 0x3fc2
pushq $0x8
jmp 0x3e4d
movq 0x20(%r12), %rcx
leaq 0x1c1d1(%rip), %rdx     # 0x20010
xorl %esi, %esi
movl %esi, %eax
movq %rbx, %rdi
callq 0x3fc2
pushq $0x9
popq %rax
jmp 0x3f82
movq 0x60(%r12), %rdi
leaq 0x40(%rsp), %rcx
movq 0x30(%rsp), %rsi
callq 0x410d
cmpl $0x1, %eax
pushq $0x6
popq %r15
cmovnel %eax, %r15d
movl 0x4(%rsp), %ecx
movl %ecx, %r8d
movq %r14, 0x38(%rbx)
subl %ebp, %r8d
cmpl 0x88(%r12), %r8d
jbe 0x3ee5
movl %r8d, 0x88(%r12)
movl 0xa0(%rbx), %r9d
cmpl %r9d, %r8d
jbe 0x3eb9
movq 0x20(%r12), %rcx
leaq 0x1c199(%rip), %rdx     # 0x20043
xorl %eax, %eax
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x3fc2
jmp 0x3ee5
movl 0x608(%rbx), %eax
cmpl %r8d, %eax
cmovbel %r8d, %eax
movl %eax, 0x608(%rbx)
movq 0x20(%r12), %rcx
leaq 0x1c191(%rip), %rdx     # 0x20069
xorl %eax, %eax
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x3fc2
cmpl $0x0, 0xa8(%rbx)
je 0x3efa
movq %rbx, %rdi
movl %ebp, %esi
callq 0x9cee
jmp 0x3f00
movl %ebp, 0xa8(%rbx)
movl %r15d, %eax
decl %eax
cmpl $0x6, %eax
ja 0x3f7f
leaq 0x1a41f(%rip), %rcx     # 0x1e330
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x40(%rbx), %ecx
leaq 0x1c15f(%rip), %rdx     # 0x20083
xorl %eax, %eax
pushq $0x2
popq %rsi
jmp 0x3f39
movl 0x40(%rbx), %ecx
leaq 0x1c19b(%rip), %rdx     # 0x200d0
xorl %esi, %esi
movl %esi, %eax
movq %rbx, %rdi
callq 0x3fc2
jmp 0x3f7f
leaq 0x1c15a(%rip), %rdx     # 0x200a4
jmp 0x3f72
leaq 0x1c169(%rip), %rdx     # 0x200bc
xorl %esi, %esi
movl %esi, %eax
jmp 0x3f77
leaq 0x1c135(%rip), %rdx     # 0x20095
jmp 0x3f72
leaq 0x1c177(%rip), %rdx     # 0x200e0
jmp 0x3f72
leaq 0x1c181(%rip), %rdx     # 0x200f3
xorl %eax, %eax
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x3fc2
movl %r15d, %eax
addq $0x41f8, %rsp           # imm = 0x41F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq