// File: /workspace/github_c_makefile_star2_O1/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
movl %esi, -0x2c(%rbp)
movq %rdx, -0x40(%rbp)
testl %esi, %esi
jle 0x20851
movq 0x1c8(%rdi), %r8
leaq 0x8(%rdx), %rax
movq %rax, -0x40(%rbp)
leal -0x1(%rsi), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x1, %esi
je 0x20851
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
addq (%rdx), %rbx
leaq 0x10(%rdx), %rax
movq %rax, -0x40(%rbp)
leal -0x2(%rsi), %eax
movl %eax, -0x2c(%rbp)
movl 0x8(%rdx), %r13d
cmpl $0xb, %ecx
movl %ecx, -0x54(%rbp)
movq %r8, -0x70(%rbp)
jne 0x20488
cmpl $0x2, %esi
jle 0x20851
leaq 0x18(%rdx), %rax
movq %rax, -0x40(%rbp)
leal -0x3(%rsi), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x3, %esi
je 0x20851
movl 0x10(%rdx), %r9d
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 %r9d, -0x44(%rbp)
jae 0x204b3
addl $-0x5, %esi
movl %esi, -0x2c(%rbp)
addq $0x28, %rdx
movq %rdx, -0x40(%rbp)
jmp 0x204b3
testw %r13w, %r13w
js 0x20497
movl $0xffffffff, -0x48(%rbp) # imm = 0xFFFFFFFF
jmp 0x204ac
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 0x20a91
movq -0x38(%rbp), %rdi
movq %rax, %r15
movq 0x188(%rdi), %rax
movl %r15d, %r8d
cmpq %r8, %rax
jbe 0x208a5
movq %r15, %rsi
shrq $0x20, %rsi
movq 0x180(%rdi), %rcx
movq (%rcx,%r8,8), %r12
movl (%r12), %edx
cmpl $0xa, %edx
jne 0x20522
movl 0x30(%r12), %r8d
movl %r8d, %r15d
cmpq %r8, %rax
ja 0x204f0
leaq 0x4faf(%rip), %rsi      # 0x254c0
leaq 0x575c(%rip), %rcx      # 0x25c74
movl $0x143, %edx            # imm = 0x143
jmp 0x2087e
leal -0x4(%rdx), %eax
cmpl $0x2, %eax
movq %rsi, -0x68(%rbp)
jae 0x2060f
movl 0x24(%r12), %esi
movq -0x38(%rbp), %rdi
callq 0x1784b
movq -0x38(%rbp), %rdi
movl %eax, %r8d
cmpq %r8, 0x170(%rdi)
jbe 0x2086b
movq 0x168(%rdi), %rax
leaq (%r8,%r8,4), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
cmpl $0xc, (%rax)
jne 0x208ad
btl $0xe, %r13d
jb 0x208d4
movq %rbx, -0x60(%rbp)
movl -0x2c(%rbp), %eax
movq %rsp, %r12
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r12
movq %r12, %rsp
testq %rax, %rax
je 0x20690
movq -0x50(%rbp), %rcx
movslq 0x14(%rcx), %r13
xorl %ebx, %ebx
leaq -0x40(%rbp), %r14
cmpq %r13, %rbx
jge 0x205e8
testl %eax, %eax
jle 0x2083c
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), %eax
subl (%rdx), %eax
shlq $0x20, %rcx
orq %rcx, %rax
jmp 0x205fc
movq -0x38(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
movq %r14, %rdx
callq 0x20a91
movq -0x38(%rbp), %rdi
movq %rax, (%r12,%rbx,8)
incq %rbx
movl -0x2c(%rbp), %eax
testl %eax, %eax
jne 0x205ae
jmp 0x20692
cmpl $0x1, %edx
jne 0x20811
movq 0x30(%r12), %rax
movq 0x20(%rax), %r14
testq %r14, %r14
je 0x20686
leaq 0x74ba(%rip), %rsi      # 0x27ae7
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0x207e9
leaq 0x74b7(%rip), %rsi      # 0x27afb
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0x207e9
leaq 0x74b2(%rip), %rsi      # 0x27b0d
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0x207e9
leaq 0x74a9(%rip), %rsi      # 0x27b1b
movq %r14, %rdi
callq 0x3300
movq -0x38(%rbp), %rdi
testl %eax, %eax
je 0x207e9
movl 0x24(%r12), %r8d
jmp 0x20547
xorl %ebx, %ebx
movl %ebx, %r14d
leaq (%r14,%r14,2), %rax
leaq 0x70(,%rax,4), %rsi
movl $0x1, %edi
callq 0x32f0
movq %rax, %r13
xorl %eax, %eax
movq %rax, 0x20(%r13)
movl -0x54(%rbp), %ecx
movl %ecx, 0x28(%r13)
movq $-0x1, 0x2c(%r13)
movq -0x70(%rbp), %rcx
movq %rcx, 0x10(%r13)
movq %rax, (%r13)
movq 0x18(%rcx), %rax
movq %rax, 0x8(%r13)
movq %r13, (%rax)
movq %r13, 0x18(%rcx)
movl %r15d, 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 0x20720
leaq 0x78(%r13), %rax
xorl %ecx, %ecx
movq (%r12,%rcx,8), %rdx
movq %rdx, -0x8(%rax)
movl $0x0, (%rax)
incq %rcx
addq $0xc, %rax
cmpq %rcx, %r14
jne 0x20706
movl -0x60(%rbp), %eax
movq -0x38(%rbp), %rdi
cmpq %rax, 0x158(%rdi)
jbe 0x207c3
movq 0x150(%rdi), %rbx
shlq $0x4, %rax
cmpl $0x0, (%rbx,%rax)
jle 0x207c3
addq %rax, %rbx
movq %r13, %r14
addq $0x70, %r14
xorl %r15d, %r15d
movq 0x8(%rbx), %rax
movq (%rax,%r15,8), %rcx
movl 0x14(%rcx), %eax
testl %eax, %eax
je 0x207b8
decl %eax
cmpl 0x6c(%r13), %eax
jge 0x207b8
testb $0x8, 0x18(%rcx)
je 0x207b8
cltq
leaq (%rax,%rax,2), %rax
movl 0x4(%r14,%rax,4), %r8d
cmpq %r8, 0x170(%rdi)
jbe 0x2086b
movq 0x168(%rdi), %rcx
leaq (%r8,%r8,4), %rdx
leaq (%rcx,%rdx,8), %rsi
cmpl $0xb, (%rsi)
jne 0x20885
leaq (%r14,%rax,4), %r12
movl 0x8(%rsi), %esi
movq -0x38(%rbp), %rdi
callq 0xf67c
movq -0x38(%rbp), %rdi
movl %eax, 0x8(%r12)
incq %r15
movslq (%rbx), %rax
cmpq %rax, %r15
jl 0x20752
movq -0x50(%rbp), %rax
movl 0x10(%rax), %r8d
cmpq %r8, 0x170(%rdi)
jbe 0x2086b
movq 0x168(%rdi), %rax
leaq (%r8,%r8,4), %rcx
cmpl $0x1, (%rax,%rcx,8)
jne 0x207f8
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 0x12256
movq %rdi, %rbx
movq %r12, %rsi
callq 0x16bfb
leaq 0x71ac(%rip), %rsi      # 0x279cf
leaq 0x72fd(%rip), %rcx      # 0x27b27
movq %rbx, %rdi
movl $0x1e1, %edx            # imm = 0x1E1
movq %rax, %r8
xorl %eax, %eax
callq 0xca73
leaq 0x718c(%rip), %rsi      # 0x279cf
leaq 0x7222(%rip), %rcx      # 0x27a6c
movl $0x3c, %edx
jmp 0x20864
leaq 0x7177(%rip), %rsi      # 0x279cf
leaq 0x7220(%rip), %rcx      # 0x27a7f
movl $0x69, %edx
xorl %eax, %eax
callq 0xca73
leaq 0x519a(%rip), %rsi      # 0x25a0c
leaq 0x51cc(%rip), %rcx      # 0x25a45
movl $0x7c, %edx
xorl %eax, %eax
callq 0xca73
movq %rdi, %rbx
callq 0x4112
leaq 0x713b(%rip), %rsi      # 0x279cf
leaq 0x72f6(%rip), %rcx      # 0x27b91
movq %rbx, %rdi
movl $0x21d, %edx            # imm = 0x21D
jmp 0x20832
movl %r15d, %r8d
jmp 0x2050a
movq %rdi, %rbx
movq -0x50(%rbp), %rsi
callq 0x4112
leaq 0x710f(%rip), %rsi      # 0x279cf
leaq 0x728b(%rip), %rcx      # 0x27b52
movq %rbx, %rdi
movl $0x1e7, %edx            # imm = 0x1E7
jmp 0x20832
movq %rdi, %rbx
movq -0x50(%rbp), %rsi
callq 0x4112
leaq 0x70e8(%rip), %rsi      # 0x279cf
leaq 0x7281(%rip), %rcx      # 0x27b6f
movq %rbx, %rdi
movl $0x1ec, %edx            # imm = 0x1EC
jmp 0x20832