// File: /workspace/github_c_makefile_star2_O3/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 $0x41e8, %rsp           # imm = 0x41E8
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x60(%rsp), %rdx
movq %rcx, 0x18(%rdx)
movq %r8, 0x20(%rdx)
movq %r9, 0x28(%rdx)
testb %al, %al
je 0x3ced
movaps %xmm0, 0x90(%rsp)
movaps %xmm1, 0xa0(%rsp)
movaps %xmm2, 0xb0(%rsp)
movaps %xmm3, 0xc0(%rsp)
movaps %xmm4, 0xd0(%rsp)
movaps %xmm5, 0xe0(%rsp)
movaps %xmm6, 0xf0(%rsp)
movaps %xmm7, 0x100(%rsp)
movq 0x168(%rdi), %rcx
movl 0x10(%rsi), %eax
movabsq $0x3000000018, %rsi     # imm = 0x3000000018
movq %rsi, 0x10(%rsp)
leaq 0x4220(%rsp), %rsi
movq %rsi, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
leaq (%rax,%rax,4), %rdx
movslq 0x14(%rcx,%rdx,8), %rax
movl %eax, %esi
leal (,%rsi,4), %ebp
testq %rax, %rax
jle 0x3e34
leaq (%rcx,%rdx,8), %rsi
addq $0x8, %rsi
movq 0x168(%rbx), %rcx
movq 0x20(%rsp), %rdx
movq (%rsi), %rdi
movl 0x10(%rsp), %r11d
movq 0x18(%rsp), %r10
xorl %esi, %esi
movl %esi, %r12d
leaq 0x1f5d0(%rip), %r8      # 0x23330
movl %ebp, %r9d
movslq (%rdi,%r12,4), %rsi
leaq (%rsi,%rsi,4), %rsi
leaq (%rcx,%rsi,8), %rsi
movl (%rsi), %r13d
addl $-0x3, %r13d
cmpl $0x8, %r13d
ja 0x3f14
movslq (%r8,%r13,4), %r13
addq %r8, %r13
jmpq *%r13
cmpl $0x28, %r11d
ja 0x3daa
movl %r11d, %esi
movq %rsi, %r13
addq %rdx, %r13
addl $0x8, %r11d
movl %r11d, 0x10(%rsp)
movq %r10, %rsi
movq %r13, %r10
jmp 0x3db3
leaq 0x8(%r10), %rsi
movq %rsi, 0x18(%rsp)
movslq %r9d, %r9
addq $-0x4, %r9
movl (%r10), %r10d
movl %r10d, 0x1e0(%rsp,%r9)
jmp 0x3e16
movl 0x8(%rsi), %r13d
cmpl $0x21, %r13d
jl 0x3d8a
cmpl $0x41, %r13d
jae 0x3f14
cmpl $0x28, %r11d
ja 0x3dfb
movl %r11d, %esi
movq %rsi, %r13
addq %rdx, %r13
addl $0x8, %r11d
movl %r11d, 0x10(%rsp)
movq %r10, %rsi
movq %r13, %r10
jmp 0x3e04
leaq 0x8(%r10), %rsi
movq %rsi, 0x18(%rsp)
movslq %r9d, %r9
addq $-0x8, %r9
movq (%r10), %r10
movq %r10, 0x1e0(%rsp,%r9)
movl %r9d, %r10d
movl %r10d, %r13d
movq %rsi, %r10
movq %r12, %rsi
incq %rsi
movq %rsi, %r12
movl %r13d, %r9d
cmpq %rax, %rsi
jb 0x3d63
movl 0xa8(%rbx), %eax
testl %eax, %eax
je 0x3ed0
movl $0x0, 0xa8(%rbx)
movl %eax, %r12d
movl %r12d, 0xc(%rsp)
movq 0x38(%rbx), %r13
leaq 0x110(%rsp), %rdi
movq %rdi, 0x38(%rbx)
callq 0x32e0
movl %eax, %edx
movl %r12d, %ecx
testl %eax, %eax
jne 0x3f60
movslq %ebp, %rax
leaq (%rsp,%rax), %rsi
addq $0x1e0, %rsi            # imm = 0x1E0
testq %r15, %r15
leaq 0x58(%rsp), %rdx
cmovneq %r15, %rdx
movq %rbx, 0x30(%rsp)
movl %r12d, 0x38(%rsp)
movq %r14, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
leaq 0xc(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x70(%r14), %rax
testq %rax, %rax
je 0x3f43
movq 0x20(%rbx), %rcx
movq %rdx, %rdi
movq %rbx, %rdx
callq *%rax
jmp 0x3f51
movl 0xa0(%rbx), %esi
movq 0x90(%rbx), %rdi
callq 0xa0d0
testq %rax, %rax
je 0x3ef3
subl 0x20(%rbx), %eax
movl %eax, %r12d
jne 0x3e4f
movq 0x20(%r14), %rcx
leaq 0x20fe2(%rip), %rdx     # 0x24ee0
xorl %esi, %esi
movl %esi, %eax
movq %rbx, %rdi
callq 0x4091
movl $0x9, %eax
jmp 0x407f
movq %rbx, %rdi
callq 0x415e
movq 0x20(%r14), %r9
movl %r12d, %ecx
leaq 0x20f86(%rip), %rdx     # 0x24eb0
xorl %esi, %esi
movq %rbx, %rdi
movq %rax, %r8
movl %esi, %eax
callq 0x4091
movl $0x8, %eax
jmp 0x407f
movq 0x60(%r14), %rdi
leaq 0x30(%rsp), %rcx
callq 0x41e6
cmpl $0x1, %eax
movl $0x6, %edx
cmovnel %eax, %edx
movl 0xc(%rsp), %ecx
movl %edx, %ebp
movl %ecx, %r8d
movq %r13, 0x38(%rbx)
subl %r12d, %r8d
cmpl 0x88(%r14), %r8d
jbe 0x3fd1
movl %r8d, 0x88(%r14)
movl 0xa0(%rbx), %r9d
cmpl %r9d, %r8d
jbe 0x3fa4
movq 0x20(%r14), %rcx
leaq 0x20f80(%rip), %rdx     # 0x24f13
xorl %eax, %eax
movq %rbx, %rdi
movl $0x1, %esi
callq 0x4091
jmp 0x3fd1
movl 0x608(%rbx), %eax
cmpl %r8d, %eax
cmovbel %r8d, %eax
movl %eax, 0x608(%rbx)
movq 0x20(%r14), %rcx
leaq 0x20f77(%rip), %rdx     # 0x24f39
xorl %eax, %eax
movq %rbx, %rdi
movl $0x3, %esi
callq 0x4091
cmpl $0x0, 0xa8(%rbx)
je 0x3fee
movl %r12d, %r12d
movl %r12d, %esi
addq 0x20(%rbx), %rsi
movq %rbx, %rdi
callq 0xa151
jmp 0x3ff5
movl %r12d, 0xa8(%rbx)
movl %ebp, %eax
decl %eax
cmpl $0x6, %eax
ja 0x407d
leaq 0x1f34f(%rip), %rcx     # 0x23354
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x40(%rbx), %ecx
leaq 0x20f3b(%rip), %rdx     # 0x24f53
xorl %eax, %eax
movq %rbx, %rdi
movl $0x2, %esi
jmp 0x4035
movl 0x40(%rbx), %ecx
leaq 0x20f72(%rip), %rdx     # 0x24fa0
xorl %esi, %esi
movl %esi, %eax
movq %rbx, %rdi
callq 0x4091
jmp 0x407d
leaq 0x20f31(%rip), %rdx     # 0x24f74
jmp 0x406e
leaq 0x20f40(%rip), %rdx     # 0x24f8c
xorl %esi, %esi
movl %esi, %eax
movq %rbx, %rdi
jmp 0x4078
leaq 0x20f09(%rip), %rdx     # 0x24f65
jmp 0x406e
leaq 0x20f4b(%rip), %rdx     # 0x24fb0
jmp 0x406e
leaq 0x20f55(%rip), %rdx     # 0x24fc3
xorl %eax, %eax
movq %rbx, %rdi
movl $0x1, %esi
callq 0x4091
movl %ebp, %eax
addq $0x41e8, %rsp           # imm = 0x41E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq