// File: /workspace/github_c_makefile_star2_O0/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
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq 0x1c8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37cf0
subl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37cf0
movl %eax, -0x30(%rbp)
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
cmpl $0xb, -0x1c(%rbp)
jne 0x36b63
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37cf0
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37cf0
movl %eax, -0x38(%rbp)
movl -0x30(%rbp), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x36b61
movl -0xc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xc(%rbp)
movq -0x18(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x18(%rbp)
jmp 0x36b87
movl -0x30(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x36b85
movl -0xc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xc(%rbp)
movq -0x18(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x18(%rbp)
jmp 0x36b87
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37a40
movq %rax, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0x20ee0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpl $0xa, (%rax)
jne 0x36bc9
movq -0x48(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x40(%rbp)
jmp 0x36ba4
jmp 0x36bcb
movq -0x48(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xc4(%rbp)
subl $0x1, %eax
je 0x36bf5
jmp 0x36bde
movl -0xc4(%rbp), %eax
addl $-0x4, %eax
subl $0x2, %eax
jb 0x36c8d
jmp 0x36cbc
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x36c77
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0xa1bc(%rip), %rsi      # 0x40dd7
callq 0x32d0
cmpl $0x0, %eax
je 0x36c70
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0xa1b7(%rip), %rsi      # 0x40deb
callq 0x32d0
cmpl $0x0, %eax
je 0x36c70
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0xa1b0(%rip), %rsi      # 0x40dfd
callq 0x32d0
cmpl $0x0, %eax
je 0x36c70
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0xa1a5(%rip), %rsi      # 0x40e0b
callq 0x32d0
cmpl $0x0, %eax
jne 0x36c75
jmp 0x37101
jmp 0x36c77
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movl 0x24(%rax), %esi
callq 0x1ee20
movq %rax, -0x50(%rbp)
jmp 0x36cf8
movq -0x8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movl 0x24(%rax), %esi
callq 0x291c0
movq -0xd0(%rbp), %rdi
movl %eax, %esi
callq 0x1ee20
movq %rax, -0x50(%rbp)
jmp 0x36cf8
movq -0x8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x264e0
movq -0xd8(%rbp), %rdi
movq %rax, %r8
leaq 0x9fda(%rip), %rsi      # 0x40cbf
movl $0x1e1, %edx            # imm = 0x1E1
leaq 0xa126(%rip), %rcx      # 0x40e17
movb $0x0, %al
callq 0x18220
movq -0x50(%rbp), %rax
cmpl $0xc, (%rax)
je 0x36d3d
movq -0x8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x4760
movq -0xe0(%rbp), %rdi
movq %rax, %r8
leaq 0x9f95(%rip), %rsi      # 0x40cbf
movl $0x1e7, %edx            # imm = 0x1E7
leaq 0xa10c(%rip), %rcx      # 0x40e42
movb $0x0, %al
callq 0x18220
movl -0x30(%rbp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x36d86
movq -0x8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x4760
movq -0xe8(%rbp), %rdi
movq %rax, %r8
leaq 0x9f4c(%rip), %rsi      # 0x40cbf
movl $0x1ec, %edx            # imm = 0x1EC
leaq 0xa0e0(%rip), %rcx      # 0x40e5f
movb $0x0, %al
callq 0x18220
movq -0x50(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x5c(%rbp)
movl -0xc(%rbp), %eax
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
cmpl $0x0, -0xc(%rbp)
jbe 0x36e6c
movl -0x6c(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jl 0x36e0b
movq -0x68(%rbp), %rax
movq %rax, -0xf0(%rbp)
movslq -0x6c(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37a40
movq -0xf8(%rbp), %rcx
movq %rax, %rdx
movq -0xf0(%rbp), %rax
movq %rdx, -0x74(%rbp)
movq -0x74(%rbp), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x36e5e
movq -0x68(%rbp), %rax
movq %rax, -0x100(%rbp)
movslq -0x6c(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x6c(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37c50
movq -0x108(%rbp), %rcx
movq %rax, %rdx
movq -0x100(%rbp), %rax
movq %rdx, -0x7c(%rbp)
movq -0x7c(%rbp), %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x36db3
movq -0x28(%rbp), %rdi
movslq -0x6c(%rbp), %rax
imulq $0xc, %rax, %rsi
addq $0x70, %rsi
movl -0x1c(%rbp), %edx
callq 0x20c10
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, 0x58(%rax)
movl -0x34(%rbp), %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x64(%rax)
movl -0x38(%rbp), %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x68(%rax)
movl -0x6c(%rbp), %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x6c(%rax)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jge 0x36f32
movq -0x88(%rbp), %rax
addq $0x70, %rax
movslq -0x8c(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq -0x68(%rbp), %rcx
movslq -0x8c(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, (%rax)
movq -0x88(%rbp), %rax
addq $0x70, %rax
movslq -0x8c(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x36ecb
movl -0x2c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpq 0x158(%rcx), %rax
jae 0x370c5
movq -0x8(%rbp), %rax
movq 0x150(%rax), %rax
movl -0x2c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movq -0x98(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x370c3
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x9c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
cmpl $-0x1, 0x14(%rax)
jne 0x36fad
jmp 0x36fad
movq -0xa8(%rbp), %rax
cmpl $0x0, 0x14(%rax)
jne 0x36fbf
jmp 0x370ad
movq -0xa8(%rbp), %rax
movl 0x14(%rax), %eax
subl $0x1, %eax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %eax
movq -0x88(%rbp), %rcx
cmpl 0x6c(%rcx), %eax
jge 0x370ab
movq -0xa8(%rbp), %rax
movq 0x18(%rax), %rax
andq $0x8, %rax
cmpq $0x0, %rax
je 0x370a9
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rax
addq $0x70, %rax
movslq -0xac(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %esi
callq 0x1ee20
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
cmpl $0xb, (%rax)
je 0x37078
movq -0x8(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x8(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x4760
movq -0x110(%rbp), %rdi
movq %rax, %r8
leaq 0x9c5a(%rip), %rsi      # 0x40cbf
movl $0x21d, %edx            # imm = 0x21D
leaq 0x9e10(%rip), %rcx      # 0x40e81
movb $0x0, %al
callq 0x18220
movq -0x8(%rbp), %rdi
movq -0xb8(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x1ecd0
movl %eax, %ecx
movq -0x88(%rbp), %rax
addq $0x70, %rax
movslq -0xac(%rbp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
jmp 0x370ab
jmp 0x370ad
jmp 0x370af
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x36f6c
jmp 0x370c5
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl 0x10(%rax), %esi
callq 0x1ee20
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x370ea
jmp 0x37101
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl 0x10(%rax), %esi
movq -0x88(%rbp), %rdx
callq 0x22410
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)