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

  unsigned int typeid = instr_get_uint(iu, &argc, &argv);
  ir_valuetype_t value = instr_get_value(iu, &argc, &argv, typeid);
  unsigned int defblock = instr_get_uint(iu, &argc, &argv);
  int paths = argc / 2;

  ir_instr_switch_t *i =
    instr_add(ib, sizeof(ir_instr_switch_t) +
              sizeof(ir_instr_path_t) * paths, IR_IC_SWITCH);

  i->value = value;
  i->defblock = defblock;
  i->num_paths = paths;

  const int width = type_bitwidth(iu, type_get(iu, typeid));
  const uint64_t mask = width == 64 ? ~1ULL : (1ULL << width) - 1;
  for(int n = 0; n < paths; n++) {
    int val = instr_get_uint(iu, &argc, &argv);
    i->paths[n].block = instr_get_uint(iu, &argc, &argv);
    ir_value_t *iv = value_get(iu, val);

    if(iv->iv_class != IR_VC_CONSTANT)
      parser_error(iu, "Switch on non-constant value");
    if(iv->iv_type != typeid)
      parser_error(iu, "Type mismatch for switch/case values");
    i->paths[n].v64 = value_get_const64(iu, iv) & mask;
  }
  qsort(i->paths, paths, sizeof(ir_instr_path_t), switch_sort64);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x1c8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37cf0
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %ecx
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37c50
movq %rax, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37cf0
movl %eax, -0x30(%rbp)
movl -0xc(%rbp), %eax
shrl %eax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rsi
shlq $0x4, %rsi
addq $0x68, %rsi
movl $0xc, %edx
callq 0x20c10
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x2c(%rbp), %rcx
movq %rcx, 0x58(%rax)
movl -0x30(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x60(%rax)
movl -0x34(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x64(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x1ee20
movq -0x68(%rbp), %rdi
movq %rax, %rsi
callq 0x32f30
movl %eax, -0x44(%rbp)
cmpl $0x40, -0x44(%rbp)
jne 0x371ee
movq $-0x2, %rax
movq %rax, -0x70(%rbp)
jmp 0x37203
movl -0x44(%rbp), %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
subq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x372f4
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37cf0
movl %eax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x37cf0
movl %eax, %ecx
movq -0x40(%rbp), %rax
addq $0x68, %rax
movslq -0x54(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movl -0x58(%rbp), %esi
callq 0x20ee0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpl $0x3, (%rax)
je 0x37292
movq -0x8(%rbp), %rdi
leaq 0x9a40(%rip), %rsi      # 0x40cbf
movl $0x28f, %edx            # imm = 0x28F
leaq 0x9c16(%rip), %rcx      # 0x40ea1
movb $0x0, %al
callq 0x18220
movq -0x60(%rbp), %rax
movl 0x24(%rax), %eax
cmpl -0x24(%rbp), %eax
je 0x372bc
movq -0x8(%rbp), %rdi
leaq 0x9a16(%rip), %rsi      # 0x40cbf
movl $0x291, %edx            # imm = 0x291
leaq 0x9c09(%rip), %rcx      # 0x40ebe
movb $0x0, %al
callq 0x18220
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x322d0
movq %rax, %rcx
andq -0x50(%rbp), %rcx
movq -0x40(%rbp), %rax
addq $0x68, %rax
movslq -0x54(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x37212
movq -0x40(%rbp), %rdi
addq $0x68, %rdi
movslq -0x34(%rbp), %rsi
movl $0x10, %edx
leaq 0xb44(%rip), %rcx       # 0x37e50
callq 0x30d0
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)