// File: /workspace/github_c_makefile_star2_O0/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
subq $0x540, %rsp            # imm = 0x540
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x3a0(%rbp), %rdi
movl $0x390, %edx            # imm = 0x390
callq 0x10e0
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x3a4(%rbp)
movq -0x8(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x3a8(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rcx
movq %rcx, -0x3b8(%rbp)
movq 0x30(%rax), %rax
movq %rax, -0x3b0(%rbp)
movq -0x8(%rbp), %rdi
callq 0x12b0
movl %eax, -0x3bc(%rbp)
movl -0x3bc(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x24(%rax)
movq -0x88(%rbp), %rdi
callq 0x115d0
movq %rax, -0x3d0(%rbp)
movq %rdx, -0x3c8(%rbp)
movl $0xffffffff, -0x3d4(%rbp) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rax
movq %rax, -0x540(%rbp)
callq 0x61d0
movq %rax, %rcx
movq -0x540(%rbp), %rax
movq %rcx, -0x3e8(%rbp)
movq %rdx, -0x3e0(%rbp)
movq -0x3e8(%rbp), %rcx
movq %rcx, 0x28(%rax)
movq -0x3e0(%rbp), %rcx
movq %rcx, 0x30(%rax)
movl $0x0, -0x3ec(%rbp)
movl -0x3ec(%rbp), %eax
cmpl -0x3d0(%rbp), %eax
jge 0x11478
movq -0x3c8(%rbp), %rax
movslq -0x3ec(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x3f8(%rbp)
movq -0x3f8(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x113ab
jmp 0x11464
movq -0x8(%rbp), %rdi
callq 0x12b0
movl %eax, -0x3fc(%rbp)
movq -0x3f8(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x113eb
cmpl $-0x1, -0x3d4(%rbp)
jne 0x113dd
movl -0x3fc(%rbp), %eax
movl %eax, -0x3d4(%rbp)
jmp 0x113e9
leaq 0x61ec(%rip), %rdi      # 0x175d0
callq 0xb150
jmp 0x113eb
movl -0x3fc(%rbp), %eax
movl %eax, -0x418(%rbp)
movq -0x3f8(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x410(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x408(%rbp)
movl $0x1, %edi
movl $0x18, %esi
callq 0x10b0
movq %rax, -0x420(%rbp)
movq -0x420(%rbp), %rax
movq -0x418(%rbp), %rcx
movq %rcx, (%rax)
movq -0x410(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x408(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
addq $0x28, %rdi
movq -0x420(%rbp), %rsi
callq 0x62c0
movl -0x3ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3ec(%rbp)
jmp 0x1136f
leaq -0x3a0(%rbp), %rsi
addq $0x18, %rsi
leaq -0x520(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x10e0
movq -0x8(%rbp), %rdi
leaq -0x520(%rbp), %rsi
callq 0x4f90
leaq -0x520(%rbp), %rdi
leaq 0x613d(%rip), %rsi      # 0x175ef
callq 0xb280
cmpl $0x1, %eax
jne 0x114c1
callq 0xda90
cmpl $-0x1, -0x3d4(%rbp)
jne 0x114d6
movl -0x3bc(%rbp), %eax
movl %eax, -0x3d4(%rbp)
callq 0xd880
movl $0x0, -0x524(%rbp)
movl -0x524(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x1155e
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x524(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x530(%rbp)
movq -0x530(%rbp), %rax
cmpl $0x1, 0x8(%rax)
je 0x1151d
jmp 0x1154d
movq -0x530(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x534(%rbp)
movq -0x530(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x538(%rbp)
movl -0x534(%rbp), %edi
movl -0x538(%rbp), %esi
callq 0xd6a0
movl -0x524(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x524(%rbp)
jmp 0x114e5
movl -0x3d4(%rbp), %edi
leaq 0x6099(%rip), %rsi      # 0x17604
callq 0xd4f0
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x12d0
movl -0x3bc(%rbp), %edi
callq 0xd6f0
movl -0x3a4(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x3a8(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x8(%rbp), %rax
movq -0x3b8(%rbp), %rcx
movq %rcx, 0x28(%rax)
movq -0x3b0(%rbp), %rcx
movq %rcx, 0x30(%rax)
addq $0x540, %rsp            # imm = 0x540
popq %rbp
retq
nopl (%rax,%rax)