// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, %rbx
movq (%rsi), %rcx
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rcx)
movl $0x0, %eax
jne 0x29b22
movq 0x18(%rcx), %r12
testq %r12, %r12
je 0x29b1d
movl (%r12), %eax
andl $0x7f, %eax
jmp 0x29b22
xorl %r12d, %r12d
xorl %eax, %eax
addl $-0xf, %eax
cmpl $0x22, %eax
ja 0x29d9c
leaq 0x1e85f(%rip), %rcx     # 0x48394
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%rdx), %rax
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rax)
jne 0x29b5a
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x29b5a
movl (%rax), %ecx
andl $0x7f, %ecx
jmp 0x29b5e
xorl %eax, %eax
xorl %ecx, %ecx
movl (%r12), %edx
andl $0x7f, %edx
leaq 0x190e4(%rip), %rsi     # 0x42c50
movsbl (%rsi,%rdx,8), %edx
cmpl %edx, %ecx
jne 0x29d9c
movq 0x30(%r12), %rcx
cmpq 0x30(%rax), %rcx
jne 0x29d9c
movq 0x38(%r12), %rcx
cmpq 0x38(%rax), %rcx
jne 0x29d9c
movl $0x1, %edi
jmp 0x29c54
movq (%rdx), %rax
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rax)
jne 0x29bbd
movq 0x18(%rax), %r15
testq %r15, %r15
je 0x29bbd
movl (%r15), %eax
andl $0x7f, %eax
jmp 0x29bc2
xorl %r15d, %r15d
xorl %eax, %eax
movl (%r12), %ecx
andl $0x7f, %ecx
cmpl %ecx, %eax
jne 0x29d9c
movq 0x38(%r15), %rax
cmpq 0x38(%r12), %rax
jne 0x29d9c
movq 0x30(%r12), %r13
movq 0x20(%r15), %rax
movq 0x8(%rax), %rdi
callq 0x22603
testb %al, %al
jne 0x29d9c
cmpl $0x3, 0x4(%r13)
jne 0x29cbe
movq 0x18(%r13), %rcx
cmpq %r15, %rcx
je 0x29d9c
movq 0x8(%rcx), %rdi
movq 0x8(%r15), %rsi
cmpq %rsi, %rdi
je 0x29d2f
callq 0x3e021
testb $0x1, %al
jne 0x29cbe
jmp 0x29d9c
movq 0x30(%r12), %rax
cmpq (%rdx), %rax
jne 0x29d9c
xorl %ecx, %ecx
movb 0x1(%rbx), %al
subb %al, %cl
movq $-0x1, %rdi
shrq %cl, %rdi
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 0x29c9a
movq 0x18(%rax), %r15
testq %r15, %r15
je 0x29c9a
movl (%r15), %eax
andl $0x7f, %eax
cmpl $0x16, %eax
sete %al
jmp 0x29c9f
xorl %r15d, %r15d
xorl %eax, %eax
testb %al, %al
je 0x29d9c
movq 0x38(%r12), %rdi
cmpq %rdi, 0x38(%r15)
jne 0x29ce2
movq %r15, %rsi
callq 0x28e73
testb %al, %al
je 0x29ce2
movq 0x30(%r15), %rcx
movq 0x30(%r12), %rdx
movq 0x38(%r12), %r8
movq %rbx, %rdi
movq %r15, %rsi
callq 0x29312
movl $0x1, %r14d
jmp 0x29d9c
movq 0x30(%r12), %rdi
cmpq %rdi, 0x30(%r15)
jne 0x29d09
movq %r15, %rsi
callq 0x28e73
testb %al, %al
je 0x29d09
movq 0x30(%r12), %r8
movq 0x38(%r12), %rdx
movq 0x38(%r15), %rcx
jmp 0x29ccc
movq 0x30(%r15), %rax
cmpq 0x38(%r12), %rax
jne 0x29d9c
movq 0x30(%r12), %rdi
movq %r15, %rsi
callq 0x28e73
testb %al, %al
je 0x29d9c
movq 0x38(%r15), %rcx
jmp 0x29cc2
movq 0x28(%rdi), %rdx
testq %rdx, %rdx
je 0x29d8c
movq %rdx, %rsi
movl $0x1, %r10d
movzwl (%rsi), %eax
movsbl %al, %edi
testw %di, %di
jle 0x29d7e
movzwl %di, %edi
movzwl %ax, %r8d
xorl %r9d, %r9d
movq 0x18(%rsi,%r9,8), %r11
cmpl $0x100, %r8d            # imm = 0x100
jb 0x29d69
testq %r11, %r11
je 0x29d76
cmpq %rcx, %r11
sete %al
je 0x29d92
cmpq %r15, %r11
je 0x29d92
incq %r9
cmpq %r9, %rdi
jne 0x29d56
movq 0x10(%rsi), %rsi
cmpq %rdx, %rsi
jne 0x29d41
xorl %r10d, %r10d
jmp 0x29d92
movl $0x2, %r10d
testb $0x1, %r10b
jne 0x29c28
movl %r14d, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq