// File: /workspace/github_c_makefile_star2_O0/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
subq $0xa0, %rsp
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)
cmpl $0x2b, -0x1c(%rbp)
jne 0x36473
movq -0x18(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x18(%rbp)
movl -0xc(%rbp), %eax
subl $0x2, %eax
movl %eax, -0xc(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37a40
movq %rax, -0x30(%rbp)
movl -0xc(%rbp), %eax
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x0, -0xc(%rbp)
jbe 0x364fa
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x3c(%rbp)
cltq
movq %rax, -0x88(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37a40
movq -0x88(%rbp), %rcx
movq %rax, %rdx
movq -0x80(%rbp), %rax
movq %rdx, -0x44(%rbp)
movq -0x44(%rbp), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x364ab
movq -0x28(%rbp), %rdi
movslq -0x3c(%rbp), %rax
imulq $0xc, %rax, %rsi
addq $0x68, %rsi
movl $0x6, %edx
callq 0x20c10
movq %rax, -0x50(%rbp)
movl -0x3c(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x58(%rax)
movq -0x50(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, 0x5c(%rax)
movl -0x2c(%rbp), %eax
movl %eax, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x366e8
movq -0x50(%rbp), %rax
movslq -0x58(%rbp), %rsi
leaq (%rsi,%rsi,2), %rcx
movq -0x38(%rbp), %rdx
movq (%rdx,%rsi,8), %rdx
movq %rdx, 0x64(%rax,%rcx,4)
movl -0x54(%rbp), %edx
movq -0x50(%rbp), %rax
movslq -0x58(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
movl %edx, 0x6c(%rax,%rcx,4)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movslq -0x58(%rbp), %rcx
movl (%rax,%rcx,8), %esi
callq 0x20ee0
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x1ee20
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x8c(%rbp)
testl %eax, %eax
je 0x365de
jmp 0x365ad
movl -0x8c(%rbp), %eax
subl $0xa, %eax
je 0x3668d
jmp 0x365be
movl -0x8c(%rbp), %eax
subl $0xb, %eax
jne 0x36699
jmp 0x365cf
movq -0x70(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x68(%rbp)
jmp 0x366d4
movq -0x60(%rbp), %rax
movl (%rax), %eax
subl $0x3, %eax
jne 0x36666
jmp 0x365eb
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x261a0
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jl 0x36652
movq -0x8(%rbp), %rax
movq %rax, -0x98(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x90(%rbp)
movq -0x8(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x22560
movq -0x98(%rbp), %rdi
movl -0x90(%rbp), %r8d
movq %rax, %r9
leaq 0xa680(%rip), %rsi      # 0x40cbf
movl $0x127, %edx            # imm = 0x127
leaq 0xa735(%rip), %rcx      # 0x40d80
movb $0x0, %al
callq 0x18220
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x64(%rbp), %rcx
movl (%rax,%rcx,8), %eax
movl %eax, -0x68(%rbp)
jmp 0x3668b
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movl (%rax), %r8d
leaq 0xa647(%rip), %rsi      # 0x40cbf
movl $0x12c, %edx            # imm = 0x12C
leaq 0xa718(%rip), %rcx      # 0x40d9c
movb $0x0, %al
callq 0x18220
jmp 0x366d4
movq -0x70(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x68(%rbp)
jmp 0x366d4
movq -0x8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x8(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x22560
movq -0xa0(%rbp), %rdi
movq %rax, %r8
leaq 0xa5fe(%rip), %rsi      # 0x40cbf
movl $0x136, %edx            # imm = 0x136
leaq 0xa6f3(%rip), %rcx      # 0x40dc0
movb $0x0, %al
callq 0x18220
movl -0x68(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x3653b
movq -0x8(%rbp), %rdi
movl -0x54(%rbp), %esi
movl $0x1, %edx
callq 0x1e6e0
movl %eax, -0x74(%rbp)
movq -0x8(%rbp), %rdi
movl -0x74(%rbp), %esi
movq -0x50(%rbp), %rdx
callq 0x22410
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)