// File: /workspace/github_c_makefile_star2_O2/cogbee[P]vmir-latest/src/vmir_bitcode_instr.c
// Function: parse_gep
static void
parse_gep(ir_unit_t *iu, unsigned int argc, const int64_t *argv, int op)
{
  ir_bb_t *ib = iu->iu_current_bb;

  if(op == FUNC_CODE_INST_GEP) {
    argv+=2;
    argc-=2;
  }

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

  ir_valuetype_t *values = alloca(argc * sizeof(ir_valuetype_t));

  int num_indicies = 0;
  while(argc > 0)
    values[num_indicies++] = instr_get_vtp(iu, &argc, &argv);

  ir_instr_gep_t *i = instr_add(ib,
                                sizeof(ir_instr_gep_t) +
                                sizeof(ir_gep_index_t) *
                                num_indicies, IR_IC_GEP);

  i->num_indicies = num_indicies;
  i->baseptr = baseptr;
  int current_type_index = baseptr.type;

  for(int n = 0; n < num_indicies; n++) {
    i->indicies[n].value = values[n];
    i->indicies[n].type = current_type_index;
    ir_value_t *index_value = value_get(iu, values[n].value);
    int element;
    int inner_type_index;
    ir_type_t *ct = type_get(iu, current_type_index);

    switch(ct->it_code) {
    case IR_TYPE_POINTER:
      inner_type_index = ct->it_pointer.pointee;
      break;

    case IR_TYPE_STRUCT:
      switch(index_value->iv_class) {
      case IR_VC_CONSTANT:
        element = value_get_const32(iu, index_value);
        if(element >= ct->it_struct.num_elements)
          parser_error(iu, "Bad index %d into struct %s",
                       element, type_str_index(iu, current_type_index));
        inner_type_index = ct->it_struct.elements[element].type;
        break;
      default:
        parser_error(iu, "Bad value class %d for struct index",
                     index_value->iv_class);
      }
      break;

    case IR_TYPE_ARRAY:
      inner_type_index = ct->it_array.element_type;
      break;

    default:
      parser_error(iu, "gep unable to index %s",
                   type_str_index(iu, current_type_index));
    }
    current_type_index = inner_type_index;
  }

  int gep_return_type = type_make_pointer(iu, current_type_index, 1);
  value_alloc_instr_ret(iu, gep_return_type, &i->super);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, -0x30(%rbp)
movl %esi, -0x34(%rbp)
movq %rdx, -0x60(%rbp)
cmpl $0x2b, %ecx
jne 0x1b791
addq $0x10, %rdx
movq %rdx, -0x60(%rbp)
addl $-0x2, %esi
movl %esi, -0x34(%rbp)
movq -0x30(%rbp), %rdi
movq 0x1c8(%rdi), %rax
movq %rax, -0x40(%rbp)
leaq -0x34(%rbp), %r13
leaq -0x60(%rbp), %rdx
movq %r13, %rsi
callq 0x1be63
movq %rax, -0x50(%rbp)
shrq $0x20, %rax
movq %rax, -0x48(%rbp)
movl (%r13), %eax
movq %rsp, %r15
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r15
movq %r15, %rsp
pushq $0x68
popq %r12
xorl %r14d, %r14d
testl %eax, %eax
je 0x1b804
leaq 0x1(%r14), %rbx
movq -0x30(%rbp), %rdi
movq %r13, %rsi
leaq -0x60(%rbp), %rdx
callq 0x1be63
movq %rax, (%r15,%r14,8)
movl -0x34(%rbp), %eax
addq $0xc, %r12
movq %rbx, %r14
jmp 0x1b7dc
pushq $0x6
popq %rdx
movq -0x40(%rbp), %rdi
movq %r12, %rsi
movq %r14, -0x40(%rbp)
callq 0x101c5
movq -0x40(%rbp), %rcx
movq %rax, %rbx
movl %ecx, 0x58(%rax)
movq -0x50(%rbp), %rax
movl %eax, 0x5c(%rbx)
movq -0x48(%rbp), %rax
movl %eax, 0x60(%rbx)
movq %rbx, -0x50(%rbp)
addq $0x6c, %rbx
xorl %r14d, %r14d
movq %r15, -0x58(%rbp)
cmpq %r14, %rcx
je 0x1b8f0
movq (%r15,%r14,8), %rsi
movq %rsi, -0x8(%rbx)
movl %eax, (%rbx)
movq -0x30(%rbp), %r12
movq %r12, %rdi
movq %rax, %r15
callq 0x102f1
movq %rax, %r13
movq %r12, %rdi
movq %r15, -0x48(%rbp)
movl %r15d, %esi
callq 0xe719
movq %rax, %r12
movl (%rax), %eax
testl %eax, %eax
je 0x1b8a1
cmpl $0xa, %eax
je 0x1b897
cmpl $0xb, %eax
movq -0x58(%rbp), %r15
movq -0x40(%rbp), %rcx
jne 0x1b91d
addq $0x8, %r12
jmp 0x1b8e0
addq $0xc, %r12
movq -0x58(%rbp), %r15
jmp 0x1b8dc
movl (%r13), %r8d
cmpl $0x3, %r8d
jne 0x1b94d
movq -0x30(%rbp), %rdi
movq %r13, %rsi
callq 0x14006
movl %eax, %r13d
cmpl 0x18(%r12), %eax
movq -0x58(%rbp), %r15
jge 0x1b96b
movslq %r13d, %rax
shlq $0x3, %rax
addq 0x8(%r12), %rax
movq %rax, %r12
movq -0x40(%rbp), %rcx
movl (%r12), %eax
incq %r14
addq $0xc, %rbx
jmp 0x1b83e
pushq $0x1
popq %rdx
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movl %eax, %esi
callq 0xe37f
movq %rbx, %rdi
movl %eax, %esi
movq -0x50(%rbp), %rdx
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10532
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
callq 0x105cd
leaq 0x7250(%rip), %rsi      # 0x22b84
leaq 0x734a(%rip), %rcx      # 0x22c85
movq %rbx, %rdi
movl $0x136, %edx            # imm = 0x136
movq %rax, %r8
xorl %eax, %eax
callq 0xbed2
leaq 0x7230(%rip), %rsi      # 0x22b84
leaq 0x7306(%rip), %rcx      # 0x22c61
movq -0x30(%rbp), %rdi
movl $0x12c, %edx            # imm = 0x12C
xorl %eax, %eax
callq 0xbed2
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
callq 0x105cd
leaq 0x7202(%rip), %rsi      # 0x22b84
leaq 0x72bc(%rip), %rcx      # 0x22c45
movq %rbx, %rdi
movl $0x127, %edx            # imm = 0x127
movl %r13d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0xbed2