// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, %rbx
movq (%rsi), %rcx
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rcx)
movl $0x0, %eax
jne 0x297e6
movq 0x18(%rcx), %r12
testq %r12, %r12
je 0x297e1
movl (%r12), %eax
andl $0x7f, %eax
jmp 0x297e6
xorl %r12d, %r12d
xorl %eax, %eax
addl $-0xf, %eax
cmpl $0x22, %eax
ja 0x29a49
leaq 0x1eb0f(%rip), %rcx     # 0x48308
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%rdx), %rcx
movl 0x4(%rcx), %edx
cmpl $0x3, %edx
jne 0x2981d
movq 0x18(%rcx), %rax
testq %rax, %rax
je 0x2981d
movl (%rax), %edi
andl $0x7f, %edi
jmp 0x29821
xorl %eax, %eax
xorl %edi, %edi
movl (%r12), %esi
andl $0x7f, %esi
leaq 0x19421(%rip), %r8      # 0x42c50
movsbl (%r8,%rsi,8), %r8d
cmpl %r8d, %edi
jne 0x29853
movq 0x30(%r12), %rdi
cmpq 0x30(%rax), %rdi
jne 0x29853
movq 0x38(%r12), %rdi
cmpq 0x38(%rax), %rdi
je 0x29947
xorl %r14d, %r14d
cmpl $0x2a, %esi
jne 0x29a49
movq 0x38(%r12), %rsi
cmpl $0x6, 0x4(%rsi)
jne 0x29a49
cmpq $0x0, 0x18(%rsi)
jne 0x29a49
xorl %r14d, %r14d
movl $0x0, %esi
cmpl $0x3, %edx
jne 0x29a21
movq 0x18(%rcx), %rax
testq %rax, %rax
je 0x29a1d
movl (%rax), %ecx
andl $0x7f, %ecx
cmpl $0x29, %ecx
sete %sil
jmp 0x29a21
movq (%rdx), %rax
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rax)
jne 0x298c5
movq 0x18(%rax), %r15
testq %r15, %r15
je 0x298c5
movl (%r15), %eax
andl $0x7f, %eax
jmp 0x298ca
xorl %r15d, %r15d
xorl %eax, %eax
movl (%r12), %ecx
andl $0x7f, %ecx
cmpl %ecx, %eax
jne 0x29a49
movq 0x38(%r15), %rax
cmpq 0x38(%r12), %rax
jne 0x29a49
movq 0x30(%r12), %r13
movq 0x20(%r15), %rax
movq 0x8(%rax), %rdi
callq 0x22603
testb %al, %al
jne 0x29a49
cmpl $0x3, 0x4(%r13)
jne 0x299b3
movq 0x18(%r13), %rcx
cmpq %r15, %rcx
je 0x29a49
movq 0x8(%rcx), %rdi
movq 0x8(%r15), %rsi
cmpq %rsi, %rdi
je 0x29a7c
callq 0x3e021
testb $0x1, %al
jne 0x299b3
jmp 0x29a49
movq 0x30(%r12), %rax
cmpq (%rdx), %rax
jne 0x29a49
xorl %edi, %edi
callq 0x136d3
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3c2b1
movq %rbx, %rdi
xorl %esi, %esi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x24adf
movq (%rdx), %rax
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rax)
jne 0x2998f
movq 0x18(%rax), %r15
testq %r15, %r15
je 0x2998f
movl (%r15), %eax
andl $0x7f, %eax
cmpl $0x17, %eax
sete %al
jmp 0x29994
xorl %r15d, %r15d
xorl %eax, %eax
testb %al, %al
je 0x29a49
movq 0x38(%r12), %rdi
cmpq %rdi, 0x38(%r15)
jne 0x299d4
movq %r15, %rsi
callq 0x28e73
testb %al, %al
je 0x299d4
movq 0x30(%r15), %rcx
movq 0x30(%r12), %rdx
movq 0x38(%r12), %r8
movq %rbx, %rdi
movq %r15, %rsi
callq 0x29312
movl $0x1, %r14d
jmp 0x29a49
movq 0x30(%r12), %rdi
cmpq %rdi, 0x30(%r15)
jne 0x299fb
movq %r15, %rsi
callq 0x28e73
testb %al, %al
je 0x299fb
movq 0x30(%r12), %r8
movq 0x38(%r12), %rdx
movq 0x38(%r15), %rcx
jmp 0x299c1
movq 0x30(%r15), %rax
cmpq 0x38(%r12), %rax
jne 0x29a49
movq 0x30(%r12), %rdi
movq %r15, %rsi
callq 0x28e73
testb %al, %al
je 0x29a49
movq 0x38(%r15), %rcx
jmp 0x299b7
xorl %eax, %eax
xorl %esi, %esi
testb %sil, %sil
je 0x29a49
movq 0x38(%rax), %rdx
cmpl $0x6, 0x4(%rdx)
jne 0x29a49
movq 0x40(%rax), %rcx
movb 0x50(%rcx), %sil
movq 0x18(%rdx), %rdx
decb %sil
movzbl %sil, %esi
btq %rsi, %rdx
jae 0x29a56
movl %r14d, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
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 0x2965b
jmp 0x299cc
movq 0x28(%rdi), %rdx
testq %rdx, %rdx
je 0x29ad9
movq %rdx, %rsi
movl $0x1, %r10d
movzwl (%rsi), %eax
movsbl %al, %edi
testw %di, %di
jle 0x29acb
movzwl %di, %edi
movzwl %ax, %r8d
xorl %r9d, %r9d
movq 0x18(%rsi,%r9,8), %r11
cmpl $0x100, %r8d            # imm = 0x100
jb 0x29ab6
testq %r11, %r11
je 0x29ac3
cmpq %rcx, %r11
sete %al
je 0x29adf
cmpq %r15, %r11
je 0x29adf
incq %r9
cmpq %r9, %rdi
jne 0x29aa3
movq 0x10(%rsi), %rsi
cmpq %rdx, %rsi
jne 0x29a8e
xorl %r10d, %r10d
jmp 0x29adf
movl $0x2, %r10d
testb $0x1, %r10b
je 0x29a49
jmp 0x29930