// File: /workspace/github_c_makefile_star2_O3/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 0x202f7
addq $0x10, %rdx
movq %rdx, -0x50(%rbp)
addl $-0x2, %esi
movl %esi, -0x2c(%rbp)
movq 0x1c8(%r12), %r13
leaq -0x2c(%rbp), %r15
leaq -0x50(%rbp), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x20bc1
movq %rax, %r14
movq %rax, -0x40(%rbp)
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 0x20371
movq %r13, -0x38(%rbp)
xorl %ebx, %ebx
leaq -0x50(%rbp), %r13
movq %rbx, %r15
incq %rbx
movq %r12, %rdi
leaq -0x2c(%rbp), %rsi
movq %r13, %rdx
callq 0x20bc1
movq -0x48(%rbp), %rcx
movq %rax, (%rcx,%r15,8)
cmpl $0x0, -0x2c(%rbp)
jne 0x20348
movq -0x38(%rbp), %r13
jmp 0x20373
xorl %ebx, %ebx
movl %ebx, %eax
movq %rax, -0x38(%rbp)
leaq (%rax,%rax,2), %rax
leaq 0x68(,%rax,4), %rsi
movl $0x1, %edi
callq 0x32f0
movabsq $-0xfffffffa, %rcx      # imm = 0xFFFFFFFF00000006
movq %rcx, 0x28(%rax)
movl $0xffffffff, 0x30(%rax) # imm = 0xFFFFFFFF
movq %r13, 0x10(%rax)
movq 0x18(%r13), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq %rax, 0x18(%r13)
movl %ebx, 0x58(%rax)
movq -0x40(%rbp), %rcx
movl %ecx, 0x5c(%rax)
movl %r14d, 0x60(%rax)
testl %ebx, %ebx
movq %rax, -0x58(%rbp)
je 0x2049e
leaq 0x6c(%rax), %r13
xorl %r15d, %r15d
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rdi
movq (%rsi,%r15,8), %r8
movq %r8, -0x8(%r13)
movl %r14d, (%r13)
movl %r8d, %eax
cmpq %rax, 0x188(%r12)
jbe 0x204e5
movl %r14d, %ecx
cmpq %rcx, 0x170(%r12)
jbe 0x204ca
movq 0x168(%r12), %rdx
leaq (%rcx,%rcx,4), %rcx
leaq (%rdx,%rcx,8), %rbx
movl (%rbx), %ecx
testl %ecx, %ecx
je 0x2043e
cmpl $0xa, %ecx
je 0x20438
cmpl $0xb, %ecx
jne 0x204fd
addq $0x8, %rbx
jmp 0x2048b
addq $0xc, %rbx
jmp 0x2048b
movq %r14, -0x40(%rbp)
movq 0x180(%r12), %rcx
movq (%rcx,%rax,8), %rsi
movl (%rsi), %r8d
cmpl $0x3, %r8d
jne 0x20528
movq %r12, %r14
movq %r12, %rdi
callq 0x16a31
movl %eax, %r12d
cmpl 0x18(%rbx), %eax
jge 0x20545
movslq %r12d, %rax
shlq $0x3, %rax
addq 0x8(%rbx), %rax
movq %rax, %rbx
movq %r14, %r12
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rdi
movl (%rbx), %r14d
incq %r15
addq $0xc, %r13
cmpq %r15, %rdi
jne 0x203e0
movq %r12, %rdi
movl %r14d, %esi
movl $0x1, %edx
callq 0xf297
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 0x12027
leaq 0x55eb(%rip), %rsi      # 0x25abc
leaq 0x561d(%rip), %rcx      # 0x25af5
movq %r12, %rdi
movl $0x7c, %edx
movl %r14d, %r8d
jmp 0x2053e
leaq 0x5084(%rip), %rsi      # 0x25570
leaq 0x5831(%rip), %rcx      # 0x25d24
movq %r12, %rdi
movl $0x143, %edx            # imm = 0x143
jmp 0x2053e
movq %r12, %rdi
movl %r14d, %esi
callq 0x120fa
leaq 0x750a(%rip), %rsi      # 0x27a19
leaq 0x7604(%rip), %rcx      # 0x27b1a
movq %r12, %rdi
movl $0x136, %edx            # imm = 0x136
movq %rax, %r8
xorl %eax, %eax
callq 0xca98
leaq 0x74ea(%rip), %rsi      # 0x27a19
leaq 0x75c0(%rip), %rcx      # 0x27af6
movq %r12, %rdi
movl $0x12c, %edx            # imm = 0x12C
xorl %eax, %eax
callq 0xca98
movq %r14, %rdi
movq -0x40(%rbp), %rsi
callq 0x120fa
leaq 0x74c1(%rip), %rsi      # 0x27a19
leaq 0x757b(%rip), %rcx      # 0x27ada
movq %r14, %rdi
movl $0x127, %edx            # imm = 0x127
movl %r12d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0xca98