// File: /workspace/github_c_makefile_star2_O2/cogbee[P]vmir-latest/src/vmir_bitcode_instr.c
// Function: parse_call_or_invoke
static void
parse_call_or_invoke(ir_unit_t *iu, unsigned int argc, const int64_t *argv,
                     int ii_class)
{
  // http://llvm.org/docs/LangRef.html#call-instruction
  // http://llvm.org/docs/LangRef.html#invoke-instruction
  ir_bb_t *ib = iu->iu_current_bb;

  unsigned int attribute_set = instr_get_uint(iu, &argc, &argv) - 1;
  int cc            = instr_get_uint(iu, &argc, &argv);



  int normal_dest            = -1;
  int unwind_dest            = -1;

  if(ii_class == IR_IC_INVOKE) {
    normal_dest = instr_get_uint(iu, &argc, &argv);
    unwind_dest = instr_get_uint(iu, &argc, &argv);

    if(cc & 0x2000) {
      argc--;
      argv++;
    }

  } else {
    if(cc & 0x8000) {
      argc--;
      argv++;
    }

  }

  ir_valuetype_t fnidx = instr_get_vtp(iu, &argc, &argv);

  const ir_value_t *fn;
  const ir_type_t *fnty = NULL;

  while(1) {
    fn = value_get(iu, fnidx.value);
    if(fn->iv_class == IR_VC_ALIAS) {
      fnidx.value = fn->iv_reg;
      continue;
    }
    break;
  }

  switch(fn->iv_class) {
  case IR_VC_FUNCTION:
    {
      const ir_function_t *f = fn->iv_func;
      // What I added cogbee......
      if (f->if_name) {
        
        // Some functions that have no effect for us, drop them here
        if(!strcmp(f->if_name, "llvm.lifetime.start") ||
           !strcmp(f->if_name, "llvm.lifetime.end") ||
           !strcmp(f->if_name, "llvm.prefetch") ||
           !strcmp(f->if_name, "llvm.va_end")) {
          return;
        }
      }
    }
    fnty = type_get(iu, fn->iv_type);
    break;

  case IR_VC_TEMPORARY:
  case IR_VC_REGFRAME:
    fnty = type_get(iu, type_get_pointee(iu, fn->iv_type));
    break;
  default:
    parser_error(iu, "Function call via value '%s' not supported",
                 value_str(iu, fn));
    break;
  }

  if(fnty->it_code != IR_TYPE_FUNCTION)
    parser_error(iu, "Call to non-function type %s",
                 type_str(iu, fnty));

  if(cc & (1 << 14))
    // MustTail
    parser_error(iu, "Can't handle must-tail call to %s",
                 type_str(iu, fnty));

  int function_args = fnty->it_function.num_parameters;

  ir_valuetype_t *args = alloca(argc * sizeof(ir_valuetype_t));
  int n = 0;

  while(argc > 0) {

    if(n >= function_args) {
      // Vararg, so type not know, encoded as valuetypepair
      args[n] = instr_get_vtp(iu, &argc, &argv);
    } else {
      // Just the value
      args[n] = instr_get_value(iu, &argc, &argv,
                                fnty->it_function.parameters[n]);
    }
    n++;
  }
  ir_instr_call_t *i =
    instr_add(ib, sizeof(ir_instr_call_t) +
              sizeof(ir_instr_arg_t) * n, ii_class);

  i->callee = fnidx;
  i->normal_dest = normal_dest;
  i->unwind_dest = unwind_dest;
  i->argc = n;


  for(int j = 0; j < n; j++) {
    i->argv[j].value = args[j];
    i->argv[j].copy_size = 0;
  }

  if(attribute_set < VECTOR_LEN(&iu->iu_attrsets)) {
    const ir_attrset_t *ias = &VECTOR_ITEM(&iu->iu_attrsets, attribute_set);
    for(int k = 0; k < ias->ias_size; k++) {
      const ir_attr_t *ia = ias->ias_list[k];
      if(ia->ia_index == -1) {
        // Function attributes
      } if(ia->ia_index == 0) {
        // Return value attributes
      } else {
        int arg = ia->ia_index - 1;
        if(arg < i->argc) {
          if(ia->ia_flags & (1ULL << ATTR_KIND_BY_VAL)) {
            ir_type_t *ty = type_get(iu, i->argv[arg].value.type);
            if(ty->it_code != IR_TYPE_POINTER) {
              parser_error(iu, "Copy-by-value on non-pointer %s",
                           type_str(iu, ty));
            }
            i->argv[arg].copy_size = type_sizeof(iu, ty->it_pointer.pointee);
          }
        }
      }
    }
  }

  const ir_type_t *rety = type_get(iu, fnty->it_function.return_type);

  if(rety->it_code == IR_TYPE_VOID)
    return;

  value_alloc_instr_ret(iu, fnty->it_function.return_type, &i->super);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, %r12d
movq %rdi, %rbx
leaq -0x2c(%rbp), %r14
movl %esi, (%r14)
leaq -0x48(%rbp), %r15
movq %rdx, (%r15)
movq 0x1c8(%rdi), %rax
movq %rax, -0x80(%rbp)
movq %r14, %rsi
movq %r15, %rdx
callq 0x1bff8
movl %eax, %r13d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1bff8
movl %eax, %r15d
movl %r12d, -0x58(%rbp)
cmpl $0xb, %r12d
jne 0x1ba2e
leaq -0x48(%rbp), %r12
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1bff8
movl %eax, -0x3c(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1bff8
btl $0xd, %r15d
movl %eax, -0x4c(%rbp)
jae 0x1ba45
decl -0x2c(%rbp)
addq $0x8, -0x48(%rbp)
jmp 0x1ba45
pushq $-0x1
popq %rax
testw %r15w, %r15w
movl %eax, -0x4c(%rbp)
jns 0x1ba42
decl -0x2c(%rbp)
addq $0x8, -0x48(%rbp)
movl %eax, -0x3c(%rbp)
decl %r13d
leaq -0x2c(%rbp), %rsi
leaq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x1be63
movq %rax, %r14
shrq $0x20, %rax
movq %rax, -0x78(%rbp)
movq %rbx, %rdi
movl %r14d, %esi
callq 0x102f1
movq %rax, %r12
movl (%rax), %eax
cmpl $0xa, %eax
jne 0x1ba7f
movl 0x30(%r12), %r14d
jmp 0x1ba63
leal -0x4(%rax), %ecx
cmpl $0x2, %ecx
movq %r14, -0x70(%rbp)
jae 0x1bc62
movl 0x24(%r12), %esi
movq %rbx, %rdi
callq 0x14b6e
movl %eax, %esi
movq %rbx, %rdi
callq 0xe719
cmpl $0xc, (%rax)
jne 0x1bd1d
movl %r13d, -0x5c(%rbp)
movq %rbx, -0x38(%rbp)
btl $0xe, %r15d
jb 0x1bd40
movq %rax, -0x68(%rbp)
movslq 0x14(%rax), %r15
leaq -0x2c(%rbp), %rax
movl (%rax), %eax
movq %rsp, %rbx
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rbx
movq %rbx, %rsp
pushq $0x70
popq %r14
xorl %r13d, %r13d
leaq -0x48(%rbp), %r12
testl %eax, %eax
je 0x1bb37
cmpq %r15, %r13
jge 0x1bb17
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax,%r13,4), %ecx
movq -0x38(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
movq %r12, %rdx
callq 0x1bfba
jmp 0x1bb27
movq -0x38(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
movq %r12, %rdx
callq 0x1be63
movq %rax, (%rbx,%r13,8)
incq %r13
movl -0x2c(%rbp), %eax
addq $0xc, %r14
jmp 0x1baf0
movq -0x80(%rbp), %rdi
movq %r14, %rsi
movl -0x58(%rbp), %edx
callq 0x101c5
movq %rax, %r15
movq -0x70(%rbp), %rax
movl %eax, 0x58(%r15)
movq -0x78(%rbp), %rax
movl %eax, 0x5c(%r15)
movl -0x3c(%rbp), %eax
movl %eax, 0x64(%r15)
movl -0x4c(%rbp), %eax
movl %eax, 0x68(%r15)
movl %r13d, 0x6c(%r15)
leaq 0x70(%r15), %rsi
movq %r15, %rax
addq $0x78, %rax
xorl %ecx, %ecx
cmpq %rcx, %r13
je 0x1bb91
movq (%rbx,%rcx,8), %rdx
movq %rdx, -0x8(%rax)
andl $0x0, (%rax)
incq %rcx
addq $0xc, %rax
jmp 0x1bb78
movl -0x5c(%rbp), %ebx
movq -0x38(%rbp), %rax
cmpq %rbx, 0x158(%rax)
jbe 0x1bc1d
shlq $0x4, %rbx
movq -0x38(%rbp), %rax
addq 0x150(%rax), %rbx
xorl %r12d, %r12d
movq %r15, -0x58(%rbp)
movq -0x38(%rbp), %r14
movslq (%rbx), %rax
cmpq %rax, %r12
jge 0x1bc1d
movq 0x8(%rbx), %rax
movq (%rax,%r12,8), %rcx
movl 0x14(%rcx), %eax
testl %eax, %eax
je 0x1bc18
decl %eax
cmpl 0x6c(%r15), %eax
jge 0x1bc18
testb $0x8, 0x18(%rcx)
je 0x1bc18
cltq
imulq $0xc, %rax, %r13
movq %rsi, %r15
movl 0x4(%rsi,%r13), %esi
movq %r14, %rdi
callq 0xe719
cmpl $0xb, (%rax)
jne 0x1bcfa
addq %r15, %r13
movl 0x8(%rax), %esi
movq %r14, %rdi
callq 0xe68d
movl %eax, 0x8(%r13)
movq %r15, %rsi
movq -0x58(%rbp), %r15
incq %r12
jmp 0x1bbbb
movq -0x68(%rbp), %r14
movl 0x10(%r14), %esi
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq 0xe719
cmpl $0x1, (%rax)
jne 0x1bc45
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x10(%r14), %esi
movq %rbx, %rdi
movq %r15, %rdx
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10532
cmpl $0x1, %eax
jne 0x1bccf
movq 0x30(%r12), %rax
movq 0x20(%rax), %r14
testq %r14, %r14
je 0x1bcc5
leaq 0x7020(%rip), %rsi      # 0x22c9c
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0x1bc36
leaq 0x7021(%rip), %rsi      # 0x22cb0
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0x1bc36
leaq 0x7020(%rip), %rsi      # 0x22cc2
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0x1bc36
leaq 0x701b(%rip), %rsi      # 0x22cd0
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0x1bc36
movl 0x24(%r12), %esi
jmp 0x1ba9e
movq %rbx, %rdi
movq %r12, %rsi
callq 0x14155
leaq 0x6ea3(%rip), %rsi      # 0x22b84
leaq 0x6ff4(%rip), %rcx      # 0x22cdc
movq %rbx, %rdi
movl $0x1e1, %edx            # imm = 0x1E1
movq %rax, %r8
xorl %eax, %eax
callq 0xbed2
movq %r14, %rdi
movq %rax, %rsi
callq 0x408f
leaq 0x6e78(%rip), %rsi      # 0x22b84
leaq 0x7033(%rip), %rcx      # 0x22d46
movq %r14, %rdi
movl $0x21d, %edx            # imm = 0x21D
jmp 0x1bcf0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x408f
leaq 0x6e55(%rip), %rsi      # 0x22b84
leaq 0x6fd1(%rip), %rcx      # 0x22d07
movq %rbx, %rdi
movl $0x1e7, %edx            # imm = 0x1E7
jmp 0x1bcf0
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x408f
leaq 0x6e2e(%rip), %rsi      # 0x22b84
leaq 0x6fc7(%rip), %rcx      # 0x22d24
movq %rbx, %rdi
movl $0x1ec, %edx            # imm = 0x1EC
jmp 0x1bcf0