// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
testl %esi, %esi
jle 0x209cf
cmpl $0x1, %esi
je 0x209cf
movq 0x1c8(%rdi), %r9
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
addq (%rdx), %rbx
leaq 0x10(%rdx), %rax
movq %rax, -0x40(%rbp)
leal -0x2(%rsi), %r8d
movl %r8d, -0x2c(%rbp)
movl 0x8(%rdx), %r13d
cmpl $0xb, %ecx
movl %ecx, -0x54(%rbp)
movq %r9, -0x70(%rbp)
jne 0x20609
testl %r8d, %r8d
je 0x209cf
cmpl $0x3, %esi
je 0x209cf
movl (%rax), %r8d
leaq 0x20(%rdx), %rax
movq %rax, -0x40(%rbp)
leal -0x4(%rsi), %eax
movl %eax, -0x2c(%rbp)
movl 0x18(%rdx), %eax
btl $0xd, %r13d
movl %eax, -0x48(%rbp)
movl %r8d, -0x44(%rbp)
jae 0x20634
addl $-0x5, %esi
movl %esi, -0x2c(%rbp)
addq $0x28, %rdx
movq %rdx, -0x40(%rbp)
jmp 0x20634
testw %r13w, %r13w
js 0x20618
movl $0xffffffff, -0x48(%rbp) # imm = 0xFFFFFFFF
jmp 0x2062d
movl $0xffffffff, -0x48(%rbp) # imm = 0xFFFFFFFF
addl $-0x3, %esi
movl %esi, -0x2c(%rbp)
addq $0x18, %rdx
movq %rdx, -0x40(%rbp)
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
leaq -0x2c(%rbp), %rsi
leaq -0x40(%rbp), %rdx
movq %rdi, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x20bc1
movq -0x38(%rbp), %rdi
movq %rax, %r12
movq 0x188(%rdi), %rax
movl %r12d, %r8d
cmpq %r8, %rax
jbe 0x20a23
movq %r12, %rsi
shrq $0x20, %rsi
movq 0x180(%rdi), %rcx
movq (%rcx,%r8,8), %r15
movl (%r15), %edx
cmpl $0xa, %edx
jne 0x206a1
movl 0x30(%r15), %r8d
movl %r8d, %r12d
cmpq %r8, %rax
ja 0x20671
leaq 0x4ee0(%rip), %rsi      # 0x25570
leaq 0x568d(%rip), %rcx      # 0x25d24
movl $0x143, %edx            # imm = 0x143
jmp 0x209fc
leal -0x4(%rdx), %eax
cmpl $0x2, %eax
movq %rsi, -0x68(%rbp)
jae 0x20791
movl 0x24(%r15), %esi
movq -0x38(%rbp), %rdi
callq 0x17764
movq -0x38(%rbp), %rdi
movl %eax, %r8d
cmpq %r8, 0x170(%rdi)
jbe 0x209e9
movq 0x168(%rdi), %rax
leaq (%r8,%r8,4), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
cmpl $0xc, (%rax)
jne 0x20a2b
btl $0xe, %r13d
jb 0x20a52
movq %rbx, -0x60(%rbp)
movl -0x2c(%rbp), %eax
movq %rsp, %r15
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r15
movq %r15, %rsp
testq %rax, %rax
je 0x20810
movq -0x50(%rbp), %rcx
movslq 0x14(%rcx), %r13
xorl %ebx, %ebx
leaq -0x40(%rbp), %r14
cmpq %r13, %rbx
jge 0x2076a
testl %eax, %eax
jle 0x209ba
movq -0x50(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x40(%rbp), %rdx
leaq 0x8(%rdx), %rsi
movl (%rcx,%rbx,4), %ecx
movq %rsi, -0x40(%rbp)
decl %eax
movl %eax, -0x2c(%rbp)
movl 0x198(%rdi), %esi
subl (%rdx), %esi
shlq $0x20, %rcx
orq %rcx, %rsi
movq %rsi, (%r15,%rbx,8)
jmp 0x20785
movq -0x38(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
movq %r14, %rdx
callq 0x20bc1
movq -0x38(%rbp), %rdi
movq %rax, (%r15,%rbx,8)
movl -0x2c(%rbp), %eax
incq %rbx
testl %eax, %eax
jne 0x2072c
jmp 0x20812
cmpl $0x1, %edx
jne 0x2098f
movq 0x30(%r15), %rax
movq 0x20(%rax), %r14
testq %r14, %r14
je 0x20807
leaq 0x7383(%rip), %rsi      # 0x27b31
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0x20967
leaq 0x7380(%rip), %rsi      # 0x27b45
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0x20967
leaq 0x737b(%rip), %rsi      # 0x27b57
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0x20967
leaq 0x7372(%rip), %rsi      # 0x27b65
movq %r14, %rdi
callq 0x3300
movq -0x38(%rbp), %rdi
testl %eax, %eax
je 0x20967
movl 0x24(%r15), %r8d
jmp 0x206c5
xorl %ebx, %ebx
movl %ebx, %r14d
leaq (%r14,%r14,2), %rax
leaq 0x70(,%rax,4), %rsi
movl $0x1, %edi
callq 0x32f0
movq %rax, %r13
movl -0x54(%rbp), %eax
movl %eax, 0x28(%r13)
movq $-0x1, 0x2c(%r13)
movq -0x70(%rbp), %rcx
movq %rcx, 0x10(%r13)
movq 0x18(%rcx), %rax
movq %rax, 0x8(%r13)
movq %r13, (%rax)
movq %r13, 0x18(%rcx)
movl %r12d, 0x58(%r13)
movq -0x68(%rbp), %rax
movl %eax, 0x5c(%r13)
movl -0x44(%rbp), %eax
movl %eax, 0x64(%r13)
movl -0x48(%rbp), %eax
movl %eax, 0x68(%r13)
movl %ebx, 0x6c(%r13)
testl %ebx, %ebx
je 0x20896
leaq 0x78(%r13), %rax
xorl %ecx, %ecx
movq (%r15,%rcx,8), %rdx
movq %rdx, -0x8(%rax)
movl $0x0, (%rax)
incq %rcx
addq $0xc, %rax
cmpq %rcx, %r14
jne 0x2087c
movl -0x60(%rbp), %ecx
movq -0x38(%rbp), %rdi
cmpq %rcx, 0x158(%rdi)
jbe 0x20941
movq 0x150(%rdi), %rbx
shlq $0x4, %rcx
movl (%rbx,%rcx), %eax
testl %eax, %eax
jle 0x20941
addq %rcx, %rbx
movq %r13, %r14
addq $0x70, %r14
xorl %r15d, %r15d
movq 0x8(%rbx), %rcx
movq (%rcx,%r15,8), %rdx
movl 0x14(%rdx), %ecx
testl %ecx, %ecx
je 0x20936
decl %ecx
cmpl 0x6c(%r13), %ecx
jge 0x20936
testb $0x8, 0x18(%rdx)
je 0x20936
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl 0x4(%r14,%rax,4), %r8d
cmpq %r8, 0x170(%rdi)
jbe 0x209e9
movq 0x168(%rdi), %rcx
leaq (%r8,%r8,4), %rdx
leaq (%rcx,%rdx,8), %rsi
cmpl $0xb, (%rsi)
jne 0x20a03
leaq (%r14,%rax,4), %r12
movl 0x8(%rsi), %esi
movq -0x38(%rbp), %rdi
callq 0xf5f0
movq -0x38(%rbp), %rdi
movl %eax, 0x8(%r12)
movl (%rbx), %eax
incq %r15
movslq %eax, %rcx
cmpq %rcx, %r15
jl 0x208cd
movq -0x50(%rbp), %rax
movl 0x10(%rax), %r8d
cmpq %r8, 0x170(%rdi)
jbe 0x209e9
movq 0x168(%rdi), %rax
leaq (%r8,%r8,4), %rcx
cmpl $0x1, (%rax,%rcx,8)
jne 0x20976
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r8d, %esi
movq %r13, %rdx
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12027
movq %rdi, %rbx
movq %r15, %rsi
callq 0x16be1
leaq 0x7078(%rip), %rsi      # 0x27a19
leaq 0x71c9(%rip), %rcx      # 0x27b71
movq %rbx, %rdi
movl $0x1e1, %edx            # imm = 0x1E1
movq %rax, %r8
xorl %eax, %eax
callq 0xca98
leaq 0x7058(%rip), %rsi      # 0x27a19
leaq 0x70ee(%rip), %rcx      # 0x27ab6
movl $0x3c, %edx
jmp 0x209e2
leaq 0x7043(%rip), %rsi      # 0x27a19
leaq 0x70ec(%rip), %rcx      # 0x27ac9
movl $0x69, %edx
xorl %eax, %eax
callq 0xca98
leaq 0x50cc(%rip), %rsi      # 0x25abc
leaq 0x50fe(%rip), %rcx      # 0x25af5
movl $0x7c, %edx
xorl %eax, %eax
callq 0xca98
movq %rdi, %rbx
callq 0x415e
leaq 0x7007(%rip), %rsi      # 0x27a19
leaq 0x71c2(%rip), %rcx      # 0x27bdb
movq %rbx, %rdi
movl $0x21d, %edx            # imm = 0x21D
jmp 0x209b0
movl %r12d, %r8d
jmp 0x20689
movq %rdi, %rbx
movq -0x50(%rbp), %rsi
callq 0x415e
leaq 0x6fdb(%rip), %rsi      # 0x27a19
leaq 0x7157(%rip), %rcx      # 0x27b9c
movq %rbx, %rdi
movl $0x1e7, %edx            # imm = 0x1E7
jmp 0x209b0
movq %rdi, %rbx
movq -0x50(%rbp), %rsi
callq 0x415e
leaq 0x6fb4(%rip), %rsi      # 0x27a19
leaq 0x714d(%rip), %rcx      # 0x27bb9
movq %rbx, %rdi
movl $0x1ec, %edx            # imm = 0x1EC
jmp 0x209b0