// File: /workspace/github_c_makefile_star2_O1/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 %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 0x28ff1
movq 0x18(%rcx), %r12
testq %r12, %r12
je 0x28fec
movl (%r12), %eax
andl $0x7f, %eax
jmp 0x28ff1
xorl %r12d, %r12d
xorl %eax, %eax
movq (%rdx), %rcx
cmpl $0x32, %eax
je 0x29052
cmpl $0xa, %eax
je 0x29027
cmpl $0x9, %eax
jne 0x290d6
xorl %r14d, %r14d
cmpl $0x3, 0x4(%rcx)
jne 0x29074
movq 0x18(%rcx), %r15
testq %r15, %r15
je 0x29074
movl (%r15), %eax
andl $0x7f, %eax
cmpl $0x9, %eax
sete %al
jmp 0x29079
cmpq %rcx, 0x38(%r12)
jne 0x290d6
movq 0x30(%r12), %rsi
movq %rbx, %rdi
callq 0x3c2b1
movq %rbx, %rdi
xorl %esi, %esi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x24adf
leaq 0x30(%rbx), %rdx
leaq 0x38(%rbx), %r8
movq 0x30(%r12), %r9
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2965b
movl $0x1, %r14d
jmp 0x290d6
xorl %r15d, %r15d
xorl %eax, %eax
testb %al, %al
je 0x290d6
movq 0x38(%r12), %r13
cmpq %r13, 0x38(%r15)
jne 0x29099
movq 0x20(%r15), %rax
movq 0x8(%rax), %rdi
callq 0x22603
testb %al, %al
je 0x290e3
movq 0x30(%r12), %r13
cmpq %r13, 0x30(%r15)
jne 0x290b5
movq 0x20(%r15), %rax
movq 0x8(%rax), %rdi
callq 0x22603
testb %al, %al
je 0x29113
movq 0x30(%r15), %rax
cmpq 0x38(%r12), %rax
jne 0x290d6
movq 0x30(%r12), %r13
movq 0x20(%r15), %rax
movq 0x8(%rax), %rdi
callq 0x22603
testb %al, %al
je 0x29151
movl %r14d, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
cmpl $0x3, 0x4(%r13)
jne 0x2910d
movq 0x18(%r13), %rcx
cmpq %r15, %rcx
je 0x29099
movq 0x8(%rcx), %rdi
movq 0x8(%r15), %rsi
cmpq %rsi, %rdi
je 0x291a1
callq 0x3e021
testb $0x1, %al
je 0x29099
movq 0x30(%r15), %rcx
jmp 0x29187
cmpl $0x3, 0x4(%r13)
jne 0x29141
movq 0x18(%r13), %rcx
cmpq %r15, %rcx
je 0x290b5
movq 0x8(%rcx), %rdi
movq 0x8(%r15), %rsi
cmpq %rsi, %rdi
je 0x2920d
callq 0x3e021
testb $0x1, %al
je 0x290b5
movq 0x30(%r12), %r8
movq 0x38(%r12), %rdx
movq 0x38(%r15), %rcx
jmp 0x29191
cmpl $0x3, 0x4(%r13)
jne 0x29183
movq 0x18(%r13), %rcx
cmpq %r15, %rcx
je 0x290d6
movq 0x8(%rcx), %rdi
movq 0x8(%r15), %rsi
cmpq %rsi, %rdi
je 0x29276
callq 0x3e021
testb $0x1, %al
je 0x290d6
movq 0x38(%r15), %rcx
movq 0x30(%r12), %rdx
movq 0x38(%r12), %r8
movq %rbx, %rdi
movq %r15, %rsi
callq 0x29312
jmp 0x2906c
movq 0x28(%rdi), %rdx
testq %rdx, %rdx
je 0x292d3
movq %rdx, %rsi
movl $0x1, %r10d
movzwl (%rsi), %eax
movsbl %al, %edi
testw %di, %di
jle 0x291fc
movzwl %di, %edi
movzwl %ax, %r8d
xorl %r9d, %r9d
movq 0x18(%rsi,%r9,8), %r11
cmpl $0x100, %r8d            # imm = 0x100
jb 0x291df
testq %r11, %r11
je 0x291f4
cmpq %rcx, %r11
sete %al
je 0x292d9
cmpq %r15, %r11
je 0x292d9
incq %r9
cmpq %r9, %rdi
jne 0x291cc
movq 0x10(%rsi), %rsi
cmpq %rdx, %rsi
jne 0x291b7
xorl %r10d, %r10d
jmp 0x292d9
movq 0x28(%rdi), %rdx
testq %rdx, %rdx
je 0x292e8
movq %rdx, %rsi
movl $0x1, %r10d
movzwl (%rsi), %eax
movsbl %al, %edi
testw %di, %di
jle 0x29268
movzwl %di, %edi
movzwl %ax, %r8d
xorl %r9d, %r9d
movq 0x18(%rsi,%r9,8), %r11
cmpl $0x100, %r8d            # imm = 0x100
jb 0x2924b
testq %r11, %r11
je 0x29260
cmpq %rcx, %r11
sete %al
je 0x292ee
cmpq %r15, %r11
je 0x292ee
incq %r9
cmpq %r9, %rdi
jne 0x29238
movq 0x10(%rsi), %rsi
cmpq %rdx, %rsi
jne 0x29223
xorl %r10d, %r10d
jmp 0x292ee
movq 0x28(%rdi), %rdx
testq %rdx, %rdx
je 0x292fd
movq %rdx, %rsi
movl $0x1, %r10d
movzwl (%rsi), %eax
movsbl %al, %edi
testw %di, %di
jle 0x292c5
movzwl %di, %edi
movzwl %ax, %r8d
xorl %r9d, %r9d
movq 0x18(%rsi,%r9,8), %r11
cmpl $0x100, %r8d            # imm = 0x100
jb 0x292b0
testq %r11, %r11
je 0x292bd
cmpq %rcx, %r11
sete %al
je 0x29303
cmpq %r15, %r11
je 0x29303
incq %r9
cmpq %r9, %rdi
jne 0x2929d
movq 0x10(%rsi), %rsi
cmpq %rdx, %rsi
jne 0x29288
xorl %r10d, %r10d
jmp 0x29303
movl $0x2, %r10d
testb $0x1, %r10b
je 0x29099
jmp 0x29109
movl $0x2, %r10d
testb $0x1, %r10b
je 0x290b5
jmp 0x29139
movl $0x2, %r10d
testb $0x1, %r10b
je 0x290d6
jmp 0x2917b