// File: /workspace/github_c_makefile_star2_O3/hsjoihs[P]c-compiler/codegen_switch.c
// Function: codegen_switch
void codegen_switch(struct PrinterState *ptr_prs,
                    const struct Statement *ptr_sta)
{
	const struct Statement sta = *ptr_sta;
	int stashed_break_label = ptr_prs->break_label_name;
	int stashed_is_inside_switch = ptr_prs->is_inside_switch;
	struct Vector /*<SourceLabelAndAssemblyLabel>*/ stashed_case_default_vec =
	    ptr_prs->case_default_vec;

	int break_label = get_new_label_name(ptr_prs);
	ptr_prs->break_label_name = break_label;
	ptr_prs->is_inside_switch = 1;
	struct Vector /*<SourceLabel>*/ vec =
	    collect_labels_visible_from_switch(sta.inner_statement);

	int default_label = -1;
	ptr_prs->case_default_vec = init_vector();
	for (int i = 0; i < vec.length; i++) {
		const struct SourceLabel *ptr_vec_i = vec.vector[i];
		if (ptr_vec_i->category == IDENT_LABEL) {
			continue;
		}

		int label = get_new_label_name(ptr_prs);
		if (ptr_vec_i->category == DEFAULT_LABEL) {
			if (default_label == -1) {
				default_label = label;
			} else {
				simple_error("duplicate `default` detected.\n");
			}
		}
		struct SourceLabelAndAssemblyLabel ll;
		ll.assembly_label = label;
		ll.source_label = *ptr_vec_i;

		struct SourceLabelAndAssemblyLabel *ptr_ll =
		    calloc(1, sizeof(struct SourceLabelAndAssemblyLabel));
		*ptr_ll = ll;
		push_vector(&ptr_prs->case_default_vec, ptr_ll);
	}

	const struct Expr expr = sta.expr1;
	print_expression(ptr_prs, &expr);
	if (size_of_basic(&expr.details.type, "expression of switch") == 1) {
		gen_extend_to_4byte();
	}

	if (default_label == -1) { /* default_label is not found */
		default_label = break_label;
	}
	gen_discard();

	for (int i = 0; i < ptr_prs->case_default_vec.length;
	     i++) { /* for each case */
		const struct SourceLabelAndAssemblyLabel *ptr_ll =
		    ptr_prs->case_default_vec.vector[i];
		if (ptr_ll->source_label.category != CASE_LABEL) {
			continue;
		}
		int constant1 = ptr_ll->source_label.case_int;
		int label1 = ptr_ll->assembly_label;
		gen_if_2nd_matches_int_jmp_4byte(constant1, label1);
	}
	gen_jump(default_label, "switch-default");

	print_statement(ptr_prs, sta.inner_statement);
	gen_label(break_label);

	ptr_prs->break_label_name = stashed_break_label;
	ptr_prs->is_inside_switch = stashed_is_inside_switch;
	ptr_prs->case_default_vec = stashed_case_default_vec;
	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %rsi, %r14
movq %rdi, %r12
leaq -0x478(%rbp), %rdi
movl $0x318, %edx            # imm = 0x318
callq 0x1100
movq 0x318(%r14), %r14
movl 0x8(%r12), %eax
movl %eax, -0x38(%rbp)
movl 0x24(%r12), %eax
movl %eax, -0x34(%rbp)
leaq 0x28(%r12), %rbx
movups 0x28(%r12), %xmm0
movaps %xmm0, -0x60(%rbp)
movq %r12, %rdi
callq 0x245c
movl %eax, -0x30(%rbp)
movl %eax, 0x8(%r12)
movl $0x1, 0x24(%r12)
movq %r14, -0x48(%rbp)
movq %r14, %rdi
callq 0xd168
movq %rax, %r13
movq %rdx, %r15
callq 0x5cf8
movq %rax, 0x28(%r12)
movq %rdx, 0x30(%r12)
testl %r13d, %r13d
movq %rbx, -0x40(%rbp)
jle 0xd085
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movq %r13, -0x50(%rbp)
movq (%r15,%r14,8), %rbx
cmpl $0x2, (%rbx)
je 0xd07b
movq %r15, %r13
movq %r12, %r15
movq %r12, %rdi
callq 0x245c
movl %eax, %r12d
cmpl $0x0, (%rbx)
jne 0xd032
cmpl $-0x1, -0x2c(%rbp)
movl %r12d, -0x2c(%rbp)
jne 0xd15c
movups (%rbx), %xmm0
leaq -0x15c(%rbp), %rax
movups %xmm0, (%rax)
movl $0x1, %edi
movl $0x18, %esi
callq 0x10d0
movl %r12d, (%rax)
movups -0x160(%rbp), %xmm0
movups %xmm0, 0x4(%rax)
movl -0x150(%rbp), %ecx
movl %ecx, 0x14(%rax)
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x5d9b
movq %r15, %r12
movq %r13, %r15
movq -0x50(%rbp), %r13
incq %r14
cmpq %r14, %r13
jne 0xd005
jmp 0xd08c
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
leaq -0x460(%rbp), %rsi
leaq -0x160(%rbp), %r15
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
callq 0x1100
movq %r12, %rdi
movq %r15, %rsi
callq 0x4e8f
leaq 0x538d(%rip), %rsi      # 0x12446
movq %r15, %rdi
callq 0x95dd
cmpl $0x1, %eax
jne 0xd0cb
callq 0xaf84
callq 0xadc9
movq -0x40(%rbp), %rbx
movl (%rbx), %eax
testl %eax, %eax
jle 0xd103
xorl %r14d, %r14d
movq 0x30(%r12), %rcx
movq (%rcx,%r14,8), %rcx
cmpl $0x1, 0x8(%rcx)
jne 0xd0f8
movl (%rcx), %esi
movl 0xc(%rcx), %edi
callq 0xac37
movl (%rbx), %eax
incq %r14
movslq %eax, %rcx
cmpq %rcx, %r14
jl 0xd0dd
movl -0x2c(%rbp), %eax
cmpl $-0x1, %eax
movl -0x30(%rbp), %r14d
movl %r14d, %edi
cmovnel %eax, %edi
leaq 0x5341(%rip), %rsi      # 0x1245b
callq 0xaac7
movq %r12, %rdi
movq -0x48(%rbp), %rsi
callq 0x2468
movl %r14d, %edi
callq 0xac74
movl -0x38(%rbp), %eax
movl %eax, 0x8(%r12)
movl -0x34(%rbp), %eax
movl %eax, 0x24(%r12)
movaps -0x60(%rbp), %xmm0
movups %xmm0, (%rbx)
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x52c4(%rip), %rdi      # 0x12427
callq 0x9546