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

	switch (DEF_OPCODE(def, src1)) {
	case OP_MUL:
		if (DEF_OPCODE(defr, *p2) == OP_MUL) {
			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_NEG:				// (-x + y) --> (y - x)
		return replace_binop(insn, OP_SUB, &insn->src1, src2, &insn->src2, def->src);

	case OP_SUB:
		if (def->src2 == src2)		// (x - y) + y --> x
			return replace_with_pseudo(insn, def->src1);
		break;
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
xorl %ebp, %ebp
cmpl $0x3, 0x4(%rsi)
jne 0x28def
movq 0x18(%rsi), %r15
testq %r15, %r15
je 0x28def
movq %rdx, %rcx
movq %rdi, %rbx
movl (%r15), %eax
andl $0x7f, %eax
cmpl $0x32, %eax
je 0x28dd1
cmpl $0xa, %eax
je 0x28dad
cmpl $0x9, %eax
jne 0x28def
cmpl $0x3, 0x4(%rcx)
jne 0x28def
movq 0x18(%rcx), %r14
testq %r14, %r14
je 0x28def
movl (%r14), %eax
andl $0x7f, %eax
cmpl $0x9, %eax
jne 0x28def
movq 0x38(%r15), %r12
cmpq %r12, 0x38(%r14)
jne 0x28d36
movq 0x20(%r14), %rax
movq 0x8(%rax), %rdi
callq 0x2257f
testb %al, %al
je 0x28dfa
movq 0x30(%r15), %r12
cmpq %r12, 0x30(%r14)
jne 0x28d55
movq 0x20(%r14), %rax
movq 0x8(%rax), %rdi
callq 0x2257f
testb %al, %al
je 0x28e38
movq 0x30(%r14), %rax
cmpq 0x38(%r15), %rax
jne 0x28def
movq 0x30(%r15), %r12
movq 0x20(%r14), %rax
movq 0x8(%rax), %rdi
callq 0x2257f
testb %al, %al
jne 0x28def
cmpl $0x3, 0x4(%r12)
jne 0x28da4
movq 0x18(%r12), %rax
cmpq %r14, %rax
je 0x28def
movq 0x8(%rax), %rdi
movq 0x8(%r14), %rsi
cmpq %rsi, %rdi
je 0x28f46
callq 0x3d05e
testb %al, %al
je 0x28def
movq 0x38(%r14), %rcx
jmp 0x28e2e
cmpq %rcx, 0x38(%r15)
jne 0x28def
movq 0x30(%r15), %rsi
movq %rbx, %rdi
callq 0x3b384
movq %rbx, %rdi
xorl %esi, %esi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x24a3f
leaq 0x30(%rbx), %rdx
leaq 0x38(%rbx), %r8
movq 0x30(%r15), %r9
movq %rbx, %rdi
movl $0xa, %esi
callq 0x292f3
movl $0x1, %ebp
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x3, 0x4(%r12)
jne 0x28e2a
movq 0x18(%r12), %rax
cmpq %r14, %rax
je 0x28d36
movq 0x8(%rax), %rdi
movq 0x8(%r14), %rsi
cmpq %rsi, %rdi
je 0x28e88
callq 0x3d05e
testb %al, %al
je 0x28d36
movq 0x30(%r14), %rcx
movq 0x30(%r15), %rdx
movq 0x38(%r15), %r8
jmp 0x28e78
cmpl $0x3, 0x4(%r12)
jne 0x28e6c
movq 0x18(%r12), %rax
cmpq %r14, %rax
je 0x28d55
movq 0x8(%rax), %rdi
movq 0x8(%r14), %rsi
cmpq %rsi, %rdi
je 0x28ee7
callq 0x3d05e
testb %al, %al
je 0x28d55
movq 0x30(%r15), %r8
movq 0x38(%r15), %rdx
movq 0x38(%r14), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28faa
jmp 0x28dea
movq 0x28(%rdi), %rcx
testq %rcx, %rcx
je 0x28d36
movq %rcx, %rdx
movzwl (%rdx), %edi
movsbl %dil, %esi
testw %si, %si
jle 0x28ed9
movzwl %si, %esi
movzwl %di, %edi
xorl %r8d, %r8d
movq 0x18(%rdx,%r8,8), %r9
cmpl $0x100, %edi            # imm = 0x100
jb 0x28ebf
testq %r9, %r9
je 0x28ed1
cmpq %rax, %r9
je 0x28e2a
cmpq %r14, %r9
je 0x28d36
incq %r8
cmpq %r8, %rsi
jne 0x28ead
movq 0x10(%rdx), %rdx
cmpq %rcx, %rdx
jne 0x28e98
jmp 0x28d36
movq 0x28(%rdi), %rcx
testq %rcx, %rcx
je 0x28d55
movq %rcx, %rdx
movzwl (%rdx), %edi
movsbl %dil, %esi
testw %si, %si
jle 0x28f38
movzwl %si, %esi
movzwl %di, %edi
xorl %r8d, %r8d
movq 0x18(%rdx,%r8,8), %r9
cmpl $0x100, %edi            # imm = 0x100
jb 0x28f1e
testq %r9, %r9
je 0x28f30
cmpq %rax, %r9
je 0x28e6c
cmpq %r14, %r9
je 0x28d55
incq %r8
cmpq %r8, %rsi
jne 0x28f0c
movq 0x10(%rdx), %rdx
cmpq %rcx, %rdx
jne 0x28ef7
jmp 0x28d55
movq 0x28(%rdi), %rcx
testq %rcx, %rcx
je 0x28def
movq %rcx, %rdx
xorl %ebp, %ebp
movzwl (%rdx), %edi
movsbl %dil, %esi
testw %si, %si
jle 0x28f95
movzwl %si, %esi
movzwl %di, %edi
xorl %r8d, %r8d
movq 0x18(%rdx,%r8,8), %r9
cmpl $0x100, %edi            # imm = 0x100
jb 0x28f7f
testq %r9, %r9
je 0x28f8d
cmpq %rax, %r9
je 0x28da4
cmpq %r14, %r9
je 0x28fa3
incq %r8
cmpq %r8, %rsi
jne 0x28f6d
movq 0x10(%rdx), %rdx
cmpq %rcx, %rdx
jne 0x28f58
jmp 0x28def
xorl %ebp, %ebp
jmp 0x28def