// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
pushq %rax
movq (%rsi), %rax
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rax)
jne 0x251dc
movq 0x18(%rax), %r12
testq %r12, %r12
je 0x251dc
movl (%r12), %eax
andl $0x7f, %eax
leal -0xf(%rax), %ecx
cmpl $0x22, %ecx
ja 0x251dc
movq %rdi, %rbx
leaq 0x1b47e(%rip), %rsi     # 0x40458
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
movq (%rdx), %rcx
movl 0x4(%rcx), %edx
cmpl $0x3, %edx
jne 0x24ffe
movq 0x18(%rcx), %rsi
testq %rsi, %rsi
je 0x24ffe
movl (%rsi), %edi
andl $0x7f, %edi
jmp 0x25002
xorl %esi, %esi
xorl %edi, %edi
leaq 0x15c77(%rip), %r8      # 0x3ac80
movsbl (%r8,%rax,8), %r8d
cmpl %r8d, %edi
jne 0x2502d
movq 0x30(%r12), %rdi
cmpq 0x30(%rsi), %rdi
jne 0x2502d
movq 0x38(%r12), %rdi
cmpq 0x38(%rsi), %rdi
je 0x25141
xorl %r14d, %r14d
cmpl $0x2a, %eax
jne 0x251dc
movq 0x38(%r12), %rax
cmpl $0x6, 0x4(%rax)
jne 0x251dc
cmpl $0x3, %edx
jne 0x251dc
cmpq $0x0, 0x18(%rax)
jne 0x251dc
movq 0x18(%rcx), %rax
testq %rax, %rax
je 0x251dc
movl (%rax), %ecx
andl $0x7f, %ecx
cmpl $0x29, %ecx
jne 0x251dc
movq 0x38(%rax), %rdx
cmpl $0x6, 0x4(%rdx)
jne 0x251dc
movq 0x40(%rax), %rcx
movb 0x50(%rcx), %sil
movq 0x18(%rdx), %rdx
decb %sil
movzbl %sil, %esi
btq %rsi, %rdx
jb 0x251dc
leaq 0x30(%rbx), %rdx
movq %rcx, 0x40(%rbx)
leaq 0x38(%rbx), %r8
movq 0x30(%rax), %rcx
movq 0x38(%rax), %r9
pushq $0x2d
popq %rsi
movq %rbx, %rdi
callq 0x24f35
jmp 0x2512a
movq (%rdx), %rcx
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rcx)
jne 0x250e0
movq 0x18(%rcx), %r15
testq %r15, %r15
je 0x250e0
movl (%r15), %ecx
andl $0x7f, %ecx
jmp 0x250e5
xorl %r15d, %r15d
xorl %ecx, %ecx
cmpl %eax, %ecx
jne 0x251dc
movq 0x38(%r15), %rax
cmpq 0x38(%r12), %rax
jne 0x251dc
movq 0x30(%r12), %rdi
movq %r15, %rsi
callq 0x24c0b
testb %al, %al
je 0x251dc
movq 0x30(%r15), %rcx
movq 0x30(%r12), %rdx
movq 0x38(%r12), %r8
movq %rbx, %rdi
movq %r15, %rsi
callq 0x24e3b
pushq $0x1
popq %r14
jmp 0x251dc
movq 0x30(%r12), %rax
cmpq (%rdx), %rax
jne 0x251dc
movq %rbx, %rdi
xorl %esi, %esi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x2495f
movq (%rdx), %rax
cmpl $0x3, 0x4(%rax)
jne 0x251dc
movq 0x18(%rax), %r15
testq %r15, %r15
je 0x251dc
movl (%r15), %eax
andl $0x7f, %eax
cmpl $0x17, %eax
jne 0x251dc
movq 0x38(%r12), %rdi
cmpq %rdi, 0x38(%r15)
jne 0x2518a
movq %r15, %rsi
callq 0x24c0b
testb %al, %al
jne 0x25111
movq 0x30(%r15), %rax
movq 0x30(%r12), %rdi
cmpq %rdi, %rax
jne 0x251bb
movq %r15, %rsi
callq 0x24c0b
testb %al, %al
je 0x251b7
movq 0x30(%r12), %r8
movq 0x38(%r12), %rdx
movq 0x38(%r15), %rcx
jmp 0x2511f
movq 0x30(%r15), %rax
cmpq 0x38(%r12), %rax
jne 0x251dc
movq 0x30(%r12), %rdi
movq %r15, %rsi
callq 0x24c0b
testb %al, %al
je 0x251dc
movq 0x38(%r15), %rcx
jmp 0x25115
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq