// File: /workspace/github_c_makefile_star2_O3/lucvoo[P]sparse-dev/simplify.c
// Function: simplify_and_one_side
static int simplify_and_one_side(struct instruction *insn, pseudo_t *p1, pseudo_t *p2)
{
	struct instruction *def, *defr = NULL;
	pseudo_t src1 = *p1;

	switch (DEF_OPCODE(def, src1)) {
	case OP_NOT:
		if (def->src == *p2)
			return replace_with_value(insn, 0);
		break;
	case OP_BINCMP ... OP_BINCMP_END:
		if (DEF_OPCODE(defr, *p2) == opcode_negate(def->opcode)) {
			if (def->src1 == defr->src1 && def->src2 == defr->src2)
				return replace_with_value(insn, 0);
		}
		if (def->opcode == OP_SET_GE && is_zero(def->src2)) {
			switch (DEF_OPCODE(defr, *p2)) {
			case OP_SET_LE:
				if (!is_positive(defr->src2, defr->itype->bit_size))
					break;
				// (x >= 0) && (x <= C) --> (x u<= C)
				insn->itype = defr->itype;
				replace_binop(insn, OP_SET_BE, &insn->src1, defr->src1, &insn->src2, defr->src2);
				return REPEAT_CSE;
			}
		}
		break;
	case OP_OR:
		if (DEF_OPCODE(defr, *p2) == OP_OR) {
			if (defr->src2 == def->src2 && can_move_to(def->src2, defr)) {
				// ((x | z) & (y | z)) into ((x & y) | z)
				swap_insn(insn, defr, def->src1, defr->src1, def->src2);
				return REPEAT_CSE;
			}
			if (defr->src1 == def->src1 && can_move_to(def->src1, defr)) {
				// ((z | x) & (z | y)) into ((x & y) | z)
				swap_insn(insn, defr, def->src2, defr->src2, def->src1);
				return REPEAT_CSE;
			}
			if (defr->src1 == def->src2 && can_move_to(def->src1, defr)) {
				// ((x | z) & (z | y)) into ((x & y) | z)
				swap_insn(insn, defr, def->src1, defr->src2, def->src2);
				return REPEAT_CSE;
			}
		}
		break;
	case OP_SHL: case OP_LSR: case OP_ASR:
		if (DEF_OPCODE(defr, *p2) == def->opcode && defr->src2 == def->src2) {
			if (can_move_to(def->src1, defr)) {
				// SHIFT(x, s) & SHIFT(y, s) --> SHIFT((x & y), s)
				swap_insn(insn, defr, def->src1, defr->src1, def->src2);
				return REPEAT_CSE;
			}
		}
		break;
	}
	return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rsi)
jne 0x29684
movq 0x18(%rsi), %r12
testq %r12, %r12
je 0x29684
movl (%r12), %eax
andl $0x7f, %eax
leal -0xf(%rax), %ecx
cmpl $0x22, %ecx
ja 0x29684
movq %rdi, %rbx
leaq 0x1ddfe(%rip), %rsi     # 0x47288
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
movq (%rdx), %rcx
movl 0x4(%rcx), %edx
cmpl $0x3, %edx
jne 0x294ae
movq 0x18(%rcx), %rsi
testq %rsi, %rsi
je 0x294ae
movl (%rsi), %edi
andl $0x7f, %edi
jmp 0x294b2
xorl %esi, %esi
xorl %edi, %edi
leaq 0x18767(%rip), %r8      # 0x41c20
movsbl (%r8,%rax,8), %r8d
cmpl %r8d, %edi
jne 0x294dd
movq 0x30(%r12), %rdi
cmpq 0x30(%rsi), %rdi
jne 0x294dd
movq 0x38(%r12), %rdi
cmpq 0x38(%rsi), %rdi
je 0x2960c
xorl %r14d, %r14d
cmpl $0x2a, %eax
jne 0x29684
movq 0x38(%r12), %rax
cmpl $0x6, 0x4(%rax)
jne 0x29684
cmpl $0x3, %edx
jne 0x29684
cmpq $0x0, 0x18(%rax)
jne 0x29684
movq 0x18(%rcx), %rax
testq %rax, %rax
je 0x29684
movl (%rax), %ecx
andl $0x7f, %ecx
cmpl $0x29, %ecx
jne 0x29684
movq 0x38(%rax), %rdx
cmpl $0x6, 0x4(%rdx)
jne 0x29684
movq 0x40(%rax), %rcx
movb 0x50(%rcx), %sil
movq 0x18(%rdx), %rdx
decb %sil
movzbl %sil, %esi
btq %rsi, %rdx
jb 0x29684
leaq 0x30(%rbx), %rdx
movq %rcx, 0x40(%rbx)
leaq 0x38(%rbx), %r8
movq 0x30(%rax), %rcx
movq 0x38(%rax), %r9
movq %rbx, %rdi
movl $0x2d, %esi
callq 0x292f3
jmp 0x2967e
movq (%rdx), %rcx
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rcx)
jne 0x29595
movq 0x18(%rcx), %r15
testq %r15, %r15
je 0x29595
movl (%r15), %ecx
andl $0x7f, %ecx
jmp 0x2959a
xorl %r15d, %r15d
xorl %ecx, %ecx
cmpl %eax, %ecx
jne 0x29684
movq 0x38(%r15), %rax
cmpq 0x38(%r12), %rax
jne 0x29684
movq 0x30(%r12), %r13
movq 0x20(%r15), %rax
movq 0x8(%rax), %rdi
callq 0x2257f
testb %al, %al
jne 0x29684
cmpl $0x3, 0x4(%r13)
jne 0x29665
movq 0x18(%r13), %rax
cmpq %r15, %rax
je 0x29684
movq 0x8(%rax), %rdi
movq 0x8(%r15), %rsi
cmpq %rsi, %rdi
je 0x296bb
callq 0x3d05e
testb %al, %al
jne 0x29665
jmp 0x29684
movq 0x30(%r12), %rax
cmpq (%rdx), %rax
jne 0x29684
xorl %edi, %edi
callq 0x13745
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3b384
movq %rbx, %rdi
xorl %esi, %esi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x24a3f
movq (%rdx), %rax
cmpl $0x3, 0x4(%rax)
jne 0x29684
movq 0x18(%rax), %r15
testq %r15, %r15
je 0x29684
movl (%r15), %eax
andl $0x7f, %eax
cmpl $0x17, %eax
jne 0x29684
movq 0x38(%r12), %rdi
cmpq %rdi, 0x38(%r15)
jne 0x29691
movq %r15, %rsi
callq 0x28b81
testb %al, %al
je 0x29691
movq 0x30(%r15), %rcx
movq 0x30(%r12), %rdx
movq 0x38(%r12), %r8
movq %rbx, %rdi
movq %r15, %rsi
callq 0x28faa
movl $0x1, %r14d
movl %r14d, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x30(%r15), %rax
movq 0x30(%r12), %rdi
cmpq %rdi, %rax
jne 0x29719
movq %r15, %rsi
callq 0x28b81
testb %al, %al
je 0x29715
movq 0x30(%r12), %r8
movq 0x38(%r12), %rdx
movq 0x38(%r15), %rcx
jmp 0x29673
movq 0x28(%rdi), %rcx
testq %rcx, %rcx
je 0x29684
movq %rcx, %rdx
movzwl (%rdx), %edi
movsbl %dil, %esi
testw %si, %si
jle 0x29704
movzwl %si, %esi
movzwl %di, %edi
xorl %r8d, %r8d
movq 0x18(%rdx,%r8,8), %r9
cmpl $0x100, %edi            # imm = 0x100
jb 0x296ee
testq %r9, %r9
je 0x296fc
cmpq %rax, %r9
je 0x29665
cmpq %r15, %r9
je 0x29742
incq %r8
cmpq %r8, %rsi
jne 0x296dc
movq 0x10(%rdx), %rdx
xorl %r14d, %r14d
cmpq %rcx, %rdx
jne 0x296c7
jmp 0x29684
movq 0x30(%r15), %rax
cmpq 0x38(%r12), %rax
jne 0x29684
movq 0x30(%r12), %rdi
movq %r15, %rsi
callq 0x28b81
testb %al, %al
je 0x29684
movq 0x38(%r15), %rcx
jmp 0x29669
xorl %r14d, %r14d
jmp 0x29684