// File: /workspace/github_c_makefile_star2_O2/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), %rbx
movl 0x8(%r12), %eax
movl %eax, -0x38(%rbp)
movl 0x24(%r12), %eax
movl %eax, -0x34(%rbp)
leaq 0x28(%r12), %rax
movq %rax, -0x40(%rbp)
movups 0x28(%r12), %xmm0
movaps %xmm0, -0x60(%rbp)
movq %r12, %rdi
callq 0x12a8
movl %eax, -0x30(%rbp)
movl %eax, 0x8(%r12)
movl $0x1, 0x24(%r12)
movq %rbx, -0x48(%rbp)
movq %rbx, %rdi
callq 0xbe75
movq %rax, %r13
movq %rdx, %r15
callq 0x49e0
movq %rax, 0x28(%r12)
movq %rdx, 0x30(%r12)
xorl %r14d, %r14d
testl %r13d, %r13d
cmovlel %r14d, %r13d
pushq $-0x1
popq %rax
movl %eax, -0x2c(%rbp)
movq %r13, -0x50(%rbp)
cmpq %r14, %r13
je 0xbd9f
movq (%r15,%r14,8), %rbx
cmpl $0x2, (%rbx)
je 0xbd9a
movq %r15, %r13
movq %r12, %r15
movq %r12, %rdi
callq 0x12a8
movl %eax, %r12d
cmpl $0x0, (%rbx)
jne 0xbd55
cmpl $-0x1, -0x2c(%rbp)
movl %r12d, -0x2c(%rbp)
jne 0xbe69
movups (%rbx), %xmm0
leaq -0x15c(%rbp), %rax
movups %xmm0, (%rax)
pushq $0x1
popq %rdi
pushq $0x18
popq %rsi
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 0x4a57
movq %r15, %r12
movq %r13, %r15
movq -0x50(%rbp), %r13
incq %r14
jmp 0xbd23
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 0x3b8f
leaq 0x569a(%rip), %rsi      # 0x11466
movq %r15, %rdi
callq 0x81de
cmpl $0x1, %eax
jne 0xbdde
callq 0x9e65
callq 0x9caa
xorl %r14d, %r14d
movq -0x40(%rbp), %rbx
movslq (%rbx), %rax
cmpq %rax, %r14
jge 0xbe10
movq 0x30(%r12), %rax
movq (%rax,%r14,8), %rax
cmpl $0x1, 0x8(%rax)
jne 0xbe0b
movl (%rax), %esi
movl 0xc(%rax), %edi
callq 0x9b18
incq %r14
jmp 0xbdea
movl -0x2c(%rbp), %eax
cmpl $-0x1, %eax
movl -0x30(%rbp), %r14d
movl %r14d, %edi
cmovnel %eax, %edi
leaq 0x5654(%rip), %rsi      # 0x1147b
callq 0x99c9
movq %r12, %rdi
movq -0x48(%rbp), %rsi
callq 0x12b4
movl %r14d, %edi
callq 0x9b55
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 0x55d7(%rip), %rdi      # 0x11447
callq 0x814d