// File: /workspace/github_c_makefile_star2_O1/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, 0x28(%rsp)
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 0x3c93
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), %rax
movl 0x10(%rsi), %ecx
movabsq $0x3000000018, %rsi     # imm = 0x3000000018
movq %rsi, 0x10(%rsp)
leaq 0x4220(%rsp), %rsi
movq %rsi, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
leaq (%rcx,%rcx,4), %rcx
movl 0x14(%rax,%rcx,8), %edx
movl %edx, %esi
leal (,%rsi,4), %esi
movl %esi, 0x8(%rsp)
testl %edx, %edx
jle 0x3e17
leaq (%rax,%rcx,8), %r15
addq $0x8, %r15
xorl %eax, %eax
movl %eax, %r12d
movl 0x8(%rsp), %r13d
movq 0x168(%rbx), %rax
movq (%r15), %rcx
movslq (%rcx,%r12,4), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rsi
movl (%rsi), %eax
cmpl $0x7, %eax
jne 0x3d35
movl 0x8(%rsi), %ecx
movl $0x3, %edx
cmpl $0x9, %ecx
jl 0x3d37
movl $0x4, %edx
cmpl $0x11, %ecx
jl 0x3d37
movl $0x5, %edx
cmpl $0x21, %ecx
jl 0x3d37
movl $0x6, %edx
cmpl $0x41, %ecx
jl 0x3d37
movl %eax, %edx
movl %edx, %eax
addl $-0x3, %eax
cmpl $0x3, %eax
jb 0x3d4b
cmpl $0x6, %edx
je 0x3dac
cmpl $0xb, %edx
jne 0x3dc6
movl 0x10(%rsp), %eax
cmpq $0x28, %rax
ja 0x3d65
movl %eax, %ecx
addq 0x20(%rsp), %rax
addl $0x8, %ecx
movl %ecx, 0x10(%rsp)
jmp 0x3d73
movq 0x18(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movslq %r13d, %rcx
addq $-0x4, %rcx
movl (%rax), %eax
movl %eax, 0x1e0(%rsp,%rcx)
movl %ecx, %eax
movb $0x1, %bpl
testb %bpl, %bpl
je 0x3e2f
movq %r12, %rcx
incq %rcx
movslq 0xc(%r15), %rdx
movq %rcx, %r12
movl %eax, %r13d
cmpq %rdx, %rcx
jl 0x3ced
jmp 0x3e17
movl 0x10(%rsp), %eax
cmpq $0x28, %rax
ja 0x3df2
movl %eax, %ecx
addq 0x20(%rsp), %rax
addl $0x8, %ecx
movl %ecx, 0x10(%rsp)
jmp 0x3e00
movq %rbx, %rdi
callq 0x4112
movq 0x20(%r14), %r9
movl %r12d, %ecx
xorl %esi, %esi
movl %esi, %ebp
movq %rbx, %rdi
leaq 0x2101d(%rip), %rdx     # 0x24e00
movq %rax, %r8
movl %esi, %eax
callq 0x4045
movl %r13d, %eax
jmp 0x3d88
movq 0x18(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movslq %r13d, %rcx
addq $-0x8, %rcx
movq (%rax), %rax
movq %rax, 0x1e0(%rsp,%rcx)
jmp 0x3d83
movl 0xa8(%rbx), %r15d
testl %r15d, %r15d
je 0x3e39
movl $0x0, 0xa8(%rbx)
jmp 0x3e5a
movl $0x8, %eax
jmp 0x4033
movl 0xa0(%rbx), %esi
movq %rbx, %rdi
callq 0xa084
testq %rax, %rax
je 0x3e51
subl 0x20(%rbx), %eax
jmp 0x3e53
xorl %eax, %eax
movl %eax, %r15d
testl %eax, %eax
je 0x3ed8
movl %r15d, 0xc(%rsp)
movq 0x38(%rbx), %r12
leaq 0x110(%rsp), %rdi
movq %rdi, 0x38(%rbx)
callq 0x32e0
movl %eax, %ebp
testl %eax, %eax
jne 0x3f14
movslq 0x8(%rsp), %rax
leaq (%rsp,%rax), %rsi
addq $0x1e0, %rsi            # imm = 0x1E0
movq 0x28(%rsp), %rax
testq %rax, %rax
leaq 0x58(%rsp), %rdx
cmovneq %rax, %rdx
movq %rbx, 0x30(%rsp)
movl %r15d, 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 0x3ef9
movq 0x20(%rbx), %rcx
movq %rdx, %rdi
movq %rbx, %rdx
callq *%rax
jmp 0x3f07
movq 0x20(%r14), %rcx
leaq 0x20f4d(%rip), %rdx     # 0x24e30
xorl %esi, %esi
movl %esi, %eax
movq %rbx, %rdi
callq 0x4045
movl $0x9, %eax
jmp 0x4033
movq 0x60(%r14), %rdi
leaq 0x30(%rsp), %rcx
callq 0x4193
cmpl $0x1, %eax
movl $0x6, %ecx
cmovnel %eax, %ecx
movl %ecx, %ebp
movq %r12, 0x38(%rbx)
movl 0xc(%rsp), %r8d
subl %r15d, %r8d
cmpl 0x88(%r14), %r8d
jbe 0x3f85
movl %r8d, 0x88(%r14)
movl 0xa0(%rbx), %r9d
cmpl %r9d, %r8d
jbe 0x3f58
movq 0x20(%r14), %rcx
leaq 0x20f1c(%rip), %rdx     # 0x24e63
xorl %eax, %eax
movq %rbx, %rdi
movl $0x1, %esi
callq 0x4045
jmp 0x3f85
movl 0x608(%rbx), %eax
cmpl %r8d, %eax
cmovbel %r8d, %eax
movl %eax, 0x608(%rbx)
movq 0x20(%r14), %rcx
leaq 0x20f13(%rip), %rdx     # 0x24e89
xorl %eax, %eax
movq %rbx, %rdi
movl $0x3, %esi
callq 0x4045
cmpl $0x0, 0xa8(%rbx)
je 0x3fa2
movl %r15d, %r15d
movl %r15d, %esi
addq 0x20(%rbx), %rsi
movq %rbx, %rdi
callq 0xa10c
jmp 0x3fa9
movl %r15d, 0xa8(%rbx)
movl %ebp, %eax
decl %eax
cmpl $0x6, %eax
ja 0x4031
leaq 0x1f377(%rip), %rcx     # 0x23330
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x40(%rbx), %ecx
leaq 0x20ed7(%rip), %rdx     # 0x24ea3
xorl %eax, %eax
movq %rbx, %rdi
movl $0x2, %esi
jmp 0x3fe9
movl 0x40(%rbx), %ecx
leaq 0x20f0e(%rip), %rdx     # 0x24ef0
xorl %esi, %esi
movl %esi, %eax
movq %rbx, %rdi
callq 0x4045
jmp 0x4031
leaq 0x20ecd(%rip), %rdx     # 0x24ec4
jmp 0x4022
leaq 0x20edc(%rip), %rdx     # 0x24edc
xorl %esi, %esi
movl %esi, %eax
movq %rbx, %rdi
jmp 0x402c
leaq 0x20ea5(%rip), %rdx     # 0x24eb5
jmp 0x4022
leaq 0x20ee7(%rip), %rdx     # 0x24f00
jmp 0x4022
leaq 0x20ef1(%rip), %rdx     # 0x24f13
xorl %eax, %eax
movq %rbx, %rdi
movl $0x1, %esi
callq 0x4045
movl %ebp, %eax
addq $0x41e8, %rsp           # imm = 0x41E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq