// File: /workspace/github_c_makefile_star2_O1/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, %r12
movl %esi, -0x2c(%rbp)
movq %rdx, -0x50(%rbp)
cmpl $0x2b, %ecx
jne 0x20141
addq $0x10, %rdx
movq %rdx, -0x50(%rbp)
addl $-0x2, %esi
movl %esi, -0x2c(%rbp)
movq 0x1c8(%r12), %rbx
leaq -0x2c(%rbp), %r15
leaq -0x50(%rbp), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x20a91
movq %rax, %r13
movq %rax, %r14
shrq $0x20, %r14
movl (%r15), %eax
movq %rsp, %rdx
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x48(%rbp)
movq %rdx, %rsp
testq %rax, %rax
je 0x201c3
movq %r13, -0x40(%rbp)
movq %rbx, -0x38(%rbp)
xorl %r15d, %r15d
leaq -0x50(%rbp), %r13
movq %r15, %rbx
incq %r15
movq %r12, %rdi
leaq -0x2c(%rbp), %rsi
movq %r13, %rdx
callq 0x20a91
movq -0x48(%rbp), %rcx
movq %rax, (%rcx,%rbx,8)
cmpl $0x0, -0x2c(%rbp)
jne 0x20196
movq -0x38(%rbp), %rbx
movq -0x40(%rbp), %r13
jmp 0x201c6
xorl %r15d, %r15d
movl %r15d, %eax
movq %rax, -0x38(%rbp)
leaq (%rax,%rax,2), %rax
leaq 0x68(,%rax,4), %rsi
movl $0x1, %edi
callq 0x32f0
xorl %edx, %edx
movq %rdx, 0x20(%rax)
movabsq $-0xfffffffa, %rcx      # imm = 0xFFFFFFFF00000006
movq %rcx, 0x28(%rax)
movl $0xffffffff, 0x30(%rax) # imm = 0xFFFFFFFF
movq %rbx, 0x10(%rax)
movq %rdx, (%rax)
movq 0x18(%rbx), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq %rax, 0x18(%rbx)
movl %r15d, 0x58(%rax)
movl %r13d, 0x5c(%rax)
movl %r14d, 0x60(%rax)
testl %r15d, %r15d
movq %rax, -0x58(%rbp)
je 0x202fb
leaq 0x6c(%rax), %r13
xorl %r15d, %r15d
movq -0x48(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq (%rdx,%r15,8), %rax
movq %rax, -0x8(%r13)
movl %r14d, (%r13)
movl (%rdx,%r15,8), %r8d
cmpq %r8, 0x188(%r12)
jbe 0x20342
movl %r14d, %eax
cmpq %rax, 0x170(%r12)
jbe 0x20327
movq 0x168(%r12), %rcx
leaq (%rax,%rax,4), %rax
leaq (%rcx,%rax,8), %rbx
movl (%rbx), %eax
testl %eax, %eax
je 0x2029b
cmpl $0xa, %eax
je 0x20295
cmpl $0xb, %eax
jne 0x2035a
addq $0x8, %rbx
jmp 0x202e8
addq $0xc, %rbx
jmp 0x202e8
movq %r14, -0x40(%rbp)
movq 0x180(%r12), %rax
movq (%rax,%r8,8), %rsi
movl (%rsi), %r8d
cmpl $0x3, %r8d
jne 0x20385
movq %r12, %r14
movq %r12, %rdi
callq 0x16a3f
movl %eax, %r12d
cmpl 0x18(%rbx), %eax
jge 0x203a2
movslq %r12d, %rax
shlq $0x3, %rax
addq 0x8(%rbx), %rax
movq %rax, %rbx
movq %r14, %r12
movq -0x48(%rbp), %rdx
movq -0x38(%rbp), %rsi
movl (%rbx), %r14d
incq %r15
addq $0xc, %r13
cmpq %r15, %rsi
jne 0x2023c
movq %r12, %rdi
movl %r14d, %esi
movl $0x1, %edx
callq 0xf30d
movq %r12, %rdi
movl %eax, %esi
movq -0x58(%rbp), %rdx
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12256
leaq 0x56de(%rip), %rsi      # 0x25a0c
leaq 0x5710(%rip), %rcx      # 0x25a45
movq %r12, %rdi
movl $0x7c, %edx
movl %r14d, %r8d
jmp 0x2039b
leaq 0x5177(%rip), %rsi      # 0x254c0
leaq 0x5924(%rip), %rcx      # 0x25c74
movq %r12, %rdi
movl $0x143, %edx            # imm = 0x143
jmp 0x2039b
movq %r12, %rdi
movl %r14d, %esi
callq 0x12335
leaq 0x7663(%rip), %rsi      # 0x279cf
leaq 0x775d(%rip), %rcx      # 0x27ad0
movq %r12, %rdi
movl $0x136, %edx            # imm = 0x136
movq %rax, %r8
xorl %eax, %eax
callq 0xca73
leaq 0x7643(%rip), %rsi      # 0x279cf
leaq 0x7719(%rip), %rcx      # 0x27aac
movq %r12, %rdi
movl $0x12c, %edx            # imm = 0x12C
xorl %eax, %eax
callq 0xca73
movq %r14, %rdi
movq -0x40(%rbp), %rsi
callq 0x12335
leaq 0x761a(%rip), %rsi      # 0x279cf
leaq 0x76d4(%rip), %rcx      # 0x27a90
movq %r14, %rdi
movl $0x127, %edx            # imm = 0x127
movl %r12d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0xca73