// File: /workspace/github_c_makefile_star2_O3/lucvoo[P]sparse-dev/simplify.c
// Function: simplify_ior_one_side
static int simplify_ior_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_AND:
		if (DEF_OPCODE(defr, *p2) == OP_AND) {
			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_NOT:
		if (def->src == *p2)
			return replace_with_value(insn, bits_mask(insn->size));
		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, 1);
		}
		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 0x299cb
movq 0x18(%rsi), %r12
testq %r12, %r12
je 0x299cb
movl (%r12), %eax
andl $0x7f, %eax
leal -0xf(%rax), %ecx
cmpl $0x22, %ecx
ja 0x299cb
movq %rdi, %rbx
leaq 0x1db8a(%rip), %rsi     # 0x47314
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
movq (%rdx), %rcx
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rcx)
jne 0x297af
movq 0x18(%rcx), %rcx
testq %rcx, %rcx
je 0x297af
movl (%rcx), %edx
andl $0x7f, %edx
jmp 0x297b3
xorl %ecx, %ecx
xorl %edx, %edx
leaq 0x18466(%rip), %rsi     # 0x41c20
movsbl (%rsi,%rax,8), %eax
cmpl %eax, %edx
jne 0x299cb
movq 0x30(%r12), %rax
cmpq 0x30(%rcx), %rax
jne 0x299cb
movq 0x38(%r12), %rax
cmpq 0x38(%rcx), %rax
jne 0x299cb
movl $0x1, %edi
jmp 0x2989b
movq (%rdx), %rcx
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rcx)
jne 0x2980b
movq 0x18(%rcx), %r15
testq %r15, %r15
je 0x2980b
movl (%r15), %ecx
andl $0x7f, %ecx
jmp 0x29810
xorl %r15d, %r15d
xorl %ecx, %ecx
cmpl %eax, %ecx
jne 0x299cb
movq 0x38(%r15), %rax
cmpq 0x38(%r12), %rax
jne 0x299cb
movq 0x30(%r12), %r13
movq 0x20(%r15), %rax
movq 0x8(%rax), %rdi
callq 0x2257f
testb %al, %al
jne 0x299cb
cmpl $0x3, 0x4(%r13)
jne 0x298fe
movq 0x18(%r13), %rax
cmpq %r15, %rax
je 0x299cb
movq 0x8(%rax), %rdi
movq 0x8(%r15), %rsi
cmpq %rsi, %rdi
je 0x2994c
callq 0x3d05e
testb %al, %al
jne 0x298fe
jmp 0x299cb
movq 0x30(%r12), %rax
cmpq (%rdx), %rax
jne 0x299cb
xorl %ecx, %ecx
movb 0x1(%rbx), %al
subb %al, %cl
movq $-0x1, %rdi
shrq %cl, %rdi
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 0x299cb
movq 0x18(%rax), %r15
testq %r15, %r15
je 0x299cb
movl (%r15), %eax
andl $0x7f, %eax
cmpl $0x16, %eax
jne 0x299cb
movq 0x38(%r12), %rdi
cmpq %rdi, 0x38(%r15)
jne 0x29922
movq %r15, %rsi
callq 0x28b81
testb %al, %al
je 0x29922
movq 0x30(%r15), %rcx
movq 0x30(%r12), %rdx
movq 0x38(%r12), %r8
movq %rbx, %rdi
movq %r15, %rsi
callq 0x28faa
movl $0x1, %r14d
jmp 0x299cb
movq 0x30(%r15), %rax
movq 0x30(%r12), %rdi
cmpq %rdi, %rax
jne 0x299a7
movq %r15, %rsi
callq 0x28b81
testb %al, %al
je 0x299a3
movq 0x30(%r12), %r8
movq 0x38(%r12), %rdx
movq 0x38(%r15), %rcx
jmp 0x2990c
movq 0x28(%rdi), %rcx
testq %rcx, %rcx
je 0x299cb
movq %rcx, %rdx
movzwl (%rdx), %edi
movsbl %dil, %esi
testw %si, %si
jle 0x29995
movzwl %si, %esi
movzwl %di, %edi
xorl %r8d, %r8d
movq 0x18(%rdx,%r8,8), %r9
cmpl $0x100, %edi            # imm = 0x100
jb 0x2997f
testq %r9, %r9
je 0x2998d
cmpq %rax, %r9
je 0x298fe
cmpq %r15, %r9
je 0x299c8
incq %r8
cmpq %r8, %rsi
jne 0x2996d
movq 0x10(%rdx), %rdx
xorl %r14d, %r14d
cmpq %rcx, %rdx
jne 0x29958
jmp 0x299cb
movq 0x30(%r15), %rax
cmpq 0x38(%r12), %rax
jne 0x299cb
movq 0x30(%r12), %rdi
movq %r15, %rsi
callq 0x28b81
testb %al, %al
je 0x299cb
movq 0x38(%r15), %rcx
jmp 0x29902
xorl %r14d, %r14d
movl %r14d, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq