// File: /workspace/github_c_makefile_star2_O1/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, %rbx
leaq -0x478(%rbp), %rdi
movl $0x318, %edx            # imm = 0x318
callq 0x1100
movq 0x318(%r14), %r14
movl 0x8(%rbx), %eax
movl %eax, -0x38(%rbp)
movl 0x24(%rbx), %eax
movl %eax, -0x34(%rbp)
leaq 0x28(%rbx), %r12
movups 0x28(%rbx), %xmm0
movaps %xmm0, -0x60(%rbp)
movq %rbx, %rdi
callq 0x245c
movl %eax, -0x30(%rbp)
movl %eax, 0x8(%rbx)
movl $0x1, 0x24(%rbx)
movq %r14, -0x48(%rbp)
movq %r14, %rdi
callq 0xd196
movq %rax, %r13
movq %rdx, -0x50(%rbp)
callq 0x5af4
movq %rax, 0x28(%rbx)
movq %rbx, -0x40(%rbp)
movq %rdx, 0x30(%rbx)
testl %r13d, %r13d
jle 0xd0b6
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movq -0x50(%rbp), %rax
movq (%rax,%r14,8), %r15
cmpl $0x2, (%r15)
je 0xd0ac
movq %r12, %rbx
movq -0x40(%rbp), %rdi
callq 0x245c
movl %eax, %r12d
cmpl $0x0, (%r15)
jne 0xd06a
cmpl $-0x1, -0x2c(%rbp)
movl %r12d, -0x2c(%rbp)
jne 0xd18a
movups (%r15), %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 %rbx, %r12
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5ba7
incq %r14
cmpq %r14, %r13
jne 0xd039
jmp 0xd0bd
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 -0x40(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4cdb
leaq 0x54a3(%rip), %rsi      # 0x12591
movq %r15, %rdi
callq 0x9529
cmpl $0x1, %eax
jne 0xd100
callq 0xaf18
callq 0xad5d
cmpl $0x0, (%r12)
jle 0xd133
xorl %r14d, %r14d
movq 0x30(%rbx), %rax
movq (%rax,%r14,8), %rax
cmpl $0x1, 0x8(%rax)
jne 0xd127
movl (%rax), %esi
movl 0xc(%rax), %edi
callq 0xabcb
incq %r14
movslq (%r12), %rax
cmpq %rax, %r14
jl 0xd10f
movl -0x2c(%rbp), %eax
cmpl $-0x1, %eax
movl -0x30(%rbp), %r14d
movl %r14d, %edi
cmovnel %eax, %edi
leaq 0x545c(%rip), %rsi      # 0x125a6
callq 0xaa5b
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
callq 0x2468
movl %r14d, %edi
callq 0xac08
movl -0x38(%rbp), %eax
movl %eax, 0x8(%rbx)
movl -0x34(%rbp), %eax
movl %eax, 0x24(%rbx)
movaps -0x60(%rbp), %xmm0
movups %xmm0, (%r12)
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x53e1(%rip), %rdi      # 0x12572
callq 0x9492