// File: /workspace/github_c_makefile_star2_O3/lucvoo[P]sparse-dev/simplify.c
// Function: simplify_cgoto
static int simplify_cgoto(struct instruction *insn)
{
	struct basic_block *target, *bb = insn->bb;
	struct basic_block *bbt, *bbf;
	struct instruction *def;
	struct multijmp *jmp;

	switch (DEF_OPCODE(def, insn->src)) {
	case OP_SEL:	// CGOTO(SEL(x, L1, L2)) --> CBR x, L1, L2
		if ((bbt = is_label(def->src2)) && (bbf = is_label(def->src3))) {
			insn->opcode = OP_CBR;
			insn->bb_true = bbt;
			insn->bb_false = bbf;
			return replace_pseudo(insn, &insn->src1, def->cond);
		}
		break;
	case OP_LABEL:
		target = def->bb_true;
		if (!target->ep)
			return 0;
		FOR_EACH_PTR(insn->multijmp_list, jmp) {
			if (jmp->target == target)
				continue;
			remove_bb_from_list(&jmp->target->parents, bb, 1);
			remove_bb_from_list(&bb->children, jmp->target, 1);
			DELETE_CURRENT_PTR(jmp);
		} END_FOR_EACH_PTR(jmp);
		kill_use(&insn->src);
		insn->opcode = OP_BR;
		insn->bb_true = target;
		return REPEAT_CSE|REPEAT_CFG_CLEANUP;
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq 0x30(%rdi), %r12
xorl %ebp, %ebp
cmpl $0x3, 0x4(%r12)
jne 0x2801c
movq 0x18(%r12), %rax
testq %rax, %rax
je 0x2801c
movq %rdi, %r14
leaq 0x30(%rdi), %rbx
movl (%rax), %ecx
andl $0x7f, %ecx
cmpl $0x47, %ecx
je 0x27ee4
cmpl $0x41, %ecx
jne 0x2801c
movq 0x38(%rax), %rcx
cmpl $0x3, 0x4(%rcx)
jne 0x2801c
movq 0x18(%rcx), %rcx
testq %rcx, %rcx
je 0x2801c
movl (%rcx), %edx
andl $0x7f, %edx
cmpl $0x47, %edx
jne 0x2801c
movq 0x38(%rcx), %rcx
testq %rcx, %rcx
je 0x2801c
movq 0x40(%rax), %rdx
cmpl $0x3, 0x4(%rdx)
jne 0x2801c
movq 0x18(%rdx), %rdx
testq %rdx, %rdx
je 0x2801c
movl (%rdx), %esi
andl $0x7f, %esi
cmpl $0x47, %esi
jne 0x2801c
movq 0x38(%rdx), %rdx
testq %rdx, %rdx
je 0x2801c
movl (%r14), %esi
andl $-0x80, %esi
orl $0x4, %esi
movl %esi, (%r14)
movq %rcx, 0x38(%r14)
movq %rdx, 0x40(%r14)
movq 0x30(%rax), %r15
movq %r15, 0x30(%r14)
testq %r15, %r15
je 0x27e8c
movl 0x4(%r15), %eax
cmpl $0x6, %eax
ja 0x27e6f
movl $0x43, %ecx
btl %eax, %ecx
jb 0x27e8c
xorl %edi, %edi
callq 0x12264
movq %rbx, 0x8(%rax)
movq %r14, (%rax)
addq $0x8, %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x227b4
movl 0x4(%r12), %eax
movl $0x1, %ebp
cmpl $0x6, %eax
ja 0x27ea9
movl $0x43, %ecx
btl %eax, %ecx
jb 0x2801c
leaq 0x8(%r12), %rdi
movq %rbx, %rsi
callq 0x2806f
cmpq $0x0, 0x8(%r12)
jne 0x2801c
movl 0x4(%r12), %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0x2801c
movq 0x18(%r12), %rdi
xorl %esi, %esi
callq 0x24a3f
jmp 0x2801c
movq 0x38(%rax), %r13
cmpq $0x0, 0x10(%r13)
je 0x2801c
movq %rbx, 0x18(%rsp)
movq %r14, 0x8(%rsp)
movq 0x38(%r14), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x27ffa
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
addq $0x20, %rax
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rbp
movzwl (%rbp), %eax
testb %al, %al
jle 0x27feb
leaq 0x18(%rbp), %r14
leaq 0x10(%rbp), %rcx
movq %rcx, 0x30(%rsp)
xorl %r15d, %r15d
movslq %r15d, %rcx
movq (%r14,%rcx,8), %rbx
movzwl %ax, %edx
cmpl $0x100, %edx            # imm = 0x100
jb 0x27f5e
testq %rbx, %rbx
je 0x27fdc
movq (%rbx), %rdi
cmpq %r13, %rdi
je 0x27fdc
leaq (%r14,%rcx,8), %r12
addq $0x18, %rdi
movq 0x28(%rsp), %rsi
movl $0x1, %edx
callq 0x228ce
movq (%rbx), %rsi
movq 0x20(%rsp), %rdi
movl $0x1, %edx
callq 0x228ce
movsbq (%rbp), %rax
movq 0x30(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
cmpq %rax, %r12
jae 0x27fba
leaq 0x8(%r12), %rcx
movq 0x8(%r12), %rdx
movq %rdx, (%r12)
movq %rcx, %r12
cmpq %rax, %rcx
jb 0x27fa2
jmp 0x27fbd
movq %r12, %rcx
movl $0xf0f0f0f0, %eax       # imm = 0xF0F0F0F0
movq %rax, (%rcx)
movl (%rbp), %eax
leal -0x1(%rax), %ecx
movzbl %cl, %ecx
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
orl %ecx, %eax
movw %ax, (%rbp)
decl %r15d
incl %r15d
movsbl %al, %ecx
cmpl %ecx, %r15d
jl 0x27f47
movq 0x10(%rbp), %rbp
cmpq 0x10(%rsp), %rbp
jne 0x27f2b
movq 0x18(%rsp), %rdi
callq 0x249b8
movq 0x8(%rsp), %rcx
movl (%rcx), %eax
andl $-0x80, %eax
orl $0x3, %eax
movl %eax, (%rcx)
movq %r13, 0x38(%rcx)
movl $0x5, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq