// File: /workspace/github_c_makefile_star2_O0/lucvoo[P]sparse-dev/simplify.c
// Function: simplify_select
static int simplify_select(struct instruction *insn)
{
	pseudo_t cond, src1, src2;
	struct instruction *def;

	cond = insn->src1;
	src1 = insn->src2;
	src2 = insn->src3;

	if (constant(cond))
		return replace_with_pseudo(insn, cond->value ? src1 : src2);
	if (src1 == src2)
		return replace_with_pseudo(insn, src1);

	if (constant(src1) && constant(src2)) {
		long long val1 = src1->value;
		long long val2 = src2->value;

		/* The pair 0/1 is special - replace with SETNE/SETEQ */
		if ((val1 | val2) == 1) {
			int opcode = OP_SET_EQ;
			if (val1) {
				src1 = src2;
				opcode = OP_SET_NE;
			}
			insn->opcode = opcode;
			insn->itype = insn->type;
			/* insn->src1 is already cond */
			insn->src2 = src1; /* Zero */
			return REPEAT_CSE;
		}
	}
	if (cond == src2 && is_zero(src1))			// SEL(x, 0, x) --> 0
		return replace_with_pseudo(insn, src1);
	if (cond == src1 && is_zero(src2))			// SEL(x, x, 0) --> x
		return replace_with_pseudo(insn, cond);

	switch (DEF_OPCODE(def, cond)) {
	case OP_SET_EQ:
		if (src1 == def->src1 && src2 == def->src2)
			return replace_with_pseudo(insn, src2); // SEL(x==y,x,y) --> y
		if (src2 == def->src1 && src1 == def->src2)
			return replace_with_pseudo(insn, src2); // SEL(y==x,x,y) --> y
		break;
	case OP_SET_NE:
		if (src1 == def->src1 && src2 == def->src2)
			return replace_with_pseudo(insn, src1); // SEL(x!=y,x,y) --> x
		if (src2 == def->src1 && src1 == def->src2)
			return replace_with_pseudo(insn, src1); // SEL(y!=x,x,y) --> x
		break;
	case OP_SET_LE: case OP_SET_LT:
	case OP_SET_BE: case OP_SET_B:
		if (!one_use(cond))
			break;
		// SEL(x {<,<=} y, a, b) --> SEL(x {>=,>} y, b, a)
		def->opcode = opcode_negate(def->opcode);
		return switch_pseudo(insn, &insn->src2, insn, &insn->src3);
	case OP_SET_GT:
		if (one_use(cond) && is_zero(def->src2)) {
			if (is_negate_of(src2, src1))
				// SEL(x > 0, a, -a) --> SEL(x >= 0, a, -a)
				return replace_opcode(def, OP_SET_GE);
		}
		break;
	case OP_SEL:
		if (constant(def->src2) && constant(def->src3)) {
			// Is the def of the conditional another select?
			// And if that one results in a "zero or not", use the
			// original conditional instead.
			//	SEL(SEL(x, C, 0), y, z) --> SEL(x, y, z)
			//	SEL(SEL(x, C, 0), C, 0) --> SEL(x, C, 0) == cond
			//	SEL(SEL(x, 0, C), y, z) --> SEL(x, z, y)
			//	SEL(SEL(x, C1, C2), y, z) --> y
			if (!def->src3->value) {
				if ((src1 == def->src2) && (src2 == def->src3))
					return replace_with_pseudo(insn, cond);
				return replace_pseudo(insn, &insn->cond, def->cond);
			}
			if (!def->src2->value) {
				switch_pseudo(insn, &insn->src2, insn, &insn->src3);
				return replace_pseudo(insn, &insn->cond, def->cond);
			}
			// both values must be non-zero
			return replace_with_pseudo(insn, src1);
		}
	case OP_AND:
		if (is_pow2(def->src2) && is_pow2(src1) && is_zero(src2) && insn->size == def->size && one_use(cond)) {
			unsigned s1 = log2_exact(def->src2->value);
			unsigned s2 = log2_exact(insn->src2->value);
			unsigned shift;

			if (s1 == s2)
				return replace_with_pseudo(insn, cond);

			// SEL(x & A, B, 0) --> SHIFT(x & A, S)
			insn->opcode = (s1 < s2) ? OP_SHL : OP_LSR;
			shift = (s1 < s2) ? (s2 - s1) : (s1 - s2);
			insn->src2 = value_pseudo(shift);
			return REPEAT_CSE;
		}
		break;
	}

	switch (DEF_OPCODE(def, src1)) {
	case OP_ADD: case OP_OR: case OP_XOR:
		if ((def->src1 == src2) && can_move_to(cond, def)) {
			// SEL(x, OP(y,z), y) --> OP(SEL(x, z, 0), y)
			swap_select(insn, def, cond, def->src2, value_pseudo(0), src2);
			return REPEAT_CSE;
		}
		if ((def->src2 == src2) && can_move_to(cond, def)) {
			// SEL(x, OP(z,y), y) --> OP(SEL(x, z, 0), y)
			swap_select(insn, def, cond, def->src1, value_pseudo(0), src2);
			return REPEAT_CSE;
		}
		break;
	}

	switch (DEF_OPCODE(def, src2)) {
	case OP_ADD: case OP_OR: case OP_XOR:
		if ((def->src1 == src1) && can_move_to(cond, def)) {
			// SEL(x, y, OP(y,z)) --> OP(SEL(x, 0, z), y)
			swap_select(insn, def, cond, value_pseudo(0), def->src2, src1);
			return REPEAT_CSE;
		}
		if ((def->src2 == src1) && can_move_to(cond, def)) {
			// SEL(x, y, OP(z,y)) --> OP(SEL(x, 0, z), y)
			swap_select(insn, def, cond, value_pseudo(0), def->src1, src1);
			return REPEAT_CSE;
		}
		break;
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x382a0
cmpl $0x0, %eax
je 0x36f2b
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x36f0e
movq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x36f16
movq -0x28(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x357e0
movl %eax, -0x4(%rbp)
jmp 0x3770a
movq -0x20(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x36f4a
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x357e0
movl %eax, -0x4(%rbp)
jmp 0x3770a
movq -0x20(%rbp), %rdi
callq 0x382a0
cmpl $0x0, %eax
je 0x36fee
movq -0x28(%rbp), %rdi
callq 0x382a0
cmpl $0x0, %eax
je 0x36fee
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
orq -0x40(%rbp), %rax
cmpq $0x1, %rax
jne 0x36fec
movl $0x27, -0x44(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x36fb1
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x30, -0x44(%rbp)
movl -0x44(%rbp), %edx
movq -0x10(%rbp), %rax
movl (%rax), %ecx
andl $0x7f, %edx
andl $-0x80, %ecx
orl %edx, %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x3770a
jmp 0x36fee
movq -0x18(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x3701c
movq -0x20(%rbp), %rdi
callq 0x3c010
testb $0x1, %al
jne 0x37007
jmp 0x3701c
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x357e0
movl %eax, -0x4(%rbp)
jmp 0x3770a
movq -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
jne 0x3704a
movq -0x28(%rbp), %rdi
callq 0x3c010
testb $0x1, %al
jne 0x37035
jmp 0x3704a
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x357e0
movl %eax, -0x4(%rbp)
jmp 0x3770a
movq -0x18(%rbp), %rax
cmpl $0x3, 0x4(%rax)
jne 0x37074
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x37074
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0x7f, %eax
movl %eax, -0x64(%rbp)
jmp 0x3707b
xorl %eax, %eax
movl %eax, -0x64(%rbp)
jmp 0x3707b
movl -0x64(%rbp), %eax
addl $-0x16, %eax
movl %eax, %ecx
movq %rcx, -0x70(%rbp)
subl $0x2b, %eax
ja 0x37431
movq -0x70(%rbp), %rax
leaq 0x31a39(%rip), %rcx     # 0x68ad4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jne 0x370d5
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jne 0x370d5
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x357e0
movl %eax, -0x4(%rbp)
jmp 0x3770a
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jne 0x37106
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jne 0x37106
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x357e0
movl %eax, -0x4(%rbp)
jmp 0x3770a
jmp 0x37431
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jne 0x3713c
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jne 0x3713c
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x357e0
movl %eax, -0x4(%rbp)
jmp 0x3770a
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jne 0x3716d
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jne 0x3716d
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x357e0
movl %eax, -0x4(%rbp)
jmp 0x3770a
jmp 0x37431
movq -0x18(%rbp), %rdi
callq 0x3ad20
testb $0x1, %al
jne 0x37184
jmp 0x37431
movq -0x30(%rbp), %rax
movl (%rax), %edi
andl $0x7f, %edi
callq 0x3bff0
movl %eax, %edx
movq -0x30(%rbp), %rax
movl (%rax), %ecx
andl $0x7f, %edx
andl $-0x80, %ecx
orl %edx, %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x30, %rsi
addq $0x8, %rsi
movq -0x10(%rbp), %rdx
movq -0x10(%rbp), %rcx
addq $0x30, %rcx
addq $0x10, %rcx
callq 0x38130
movl %eax, -0x4(%rbp)
jmp 0x3770a
movq -0x18(%rbp), %rdi
callq 0x3ad20
testb $0x1, %al
jne 0x371e0
jmp 0x3721e
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x3c010
testb $0x1, %al
jne 0x371f3
jmp 0x3721e
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x3d080
testb $0x1, %al
jne 0x37206
jmp 0x3721c
movq -0x30(%rbp), %rdi
movl $0x2a, %esi
callq 0x3b2f0
movl %eax, -0x4(%rbp)
jmp 0x3770a
jmp 0x3721e
jmp 0x37431
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x382a0
cmpl $0x0, %eax
je 0x3731a
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x382a0
cmpl $0x0, %eax
je 0x3731a
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rax
cmpq $0x0, 0x18(%rax)
jne 0x372b0
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jne 0x3728f
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jne 0x3728f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x357e0
movl %eax, -0x4(%rbp)
jmp 0x3770a
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x30, %rsi
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rdx
callq 0x38d50
movl %eax, -0x4(%rbp)
jmp 0x3770a
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
cmpq $0x0, 0x18(%rax)
jne 0x37305
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x30, %rsi
addq $0x8, %rsi
movq -0x10(%rbp), %rdx
movq -0x10(%rbp), %rcx
addq $0x30, %rcx
addq $0x10, %rcx
callq 0x38130
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x30, %rsi
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rdx
callq 0x38d50
movl %eax, -0x4(%rbp)
jmp 0x3770a
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x357e0
movl %eax, -0x4(%rbp)
jmp 0x3770a
jmp 0x3731c
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x3d0f0
testb $0x1, %al
jne 0x37332
jmp 0x3742f
movq -0x20(%rbp), %rdi
callq 0x3d0f0
testb $0x1, %al
jne 0x37344
jmp 0x3742f
movq -0x28(%rbp), %rdi
callq 0x3c010
testb $0x1, %al
jne 0x37356
jmp 0x3742f
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x8, %eax
movq -0x30(%rbp), %rcx
movl (%rcx), %ecx
shrl $0x8, %ecx
cmpl %ecx, %eax
jne 0x3742f
movq -0x18(%rbp), %rdi
callq 0x3ad20
testb $0x1, %al
jne 0x37382
jmp 0x3742f
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x3d130
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x3d130
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jne 0x373c7
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x357e0
movl %eax, -0x4(%rbp)
jmp 0x3770a
movl -0x48(%rbp), %ecx
movl -0x4c(%rbp), %esi
movl $0xf, %edx
movl $0x11, %eax
cmpl %esi, %ecx
cmovbl %eax, %edx
movq -0x10(%rbp), %rax
movl (%rax), %ecx
andl $0x7f, %edx
andl $-0x80, %ecx
orl %edx, %ecx
movl %ecx, (%rax)
movl -0x48(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jae 0x373ff
movl -0x4c(%rbp), %eax
subl -0x48(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x37408
movl -0x48(%rbp), %eax
subl -0x4c(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
movl %eax, %edi
callq 0x1a390
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x3770a
jmp 0x37431
movq -0x20(%rbp), %rax
cmpl $0x3, 0x4(%rax)
jne 0x3745b
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x3745b
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0x7f, %eax
movl %eax, -0x78(%rbp)
jmp 0x37462
xorl %eax, %eax
movl %eax, -0x78(%rbp)
jmp 0x37462
movl -0x78(%rbp), %eax
movl %eax, -0x7c(%rbp)
subl $0x8, %eax
je 0x37480
jmp 0x3746f
movl -0x7c(%rbp), %eax
addl $-0x17, %eax
subl $0x1, %eax
ja 0x375a6
jmp 0x37480
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rax
cmpq -0x28(%rbp), %rax
jne 0x37512
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x3b4b0
testb $0x1, %al
jne 0x374a5
jmp 0x37512
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x88(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x1a390
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq -0x88(%rbp), %rcx
movq %rax, %r8
movq -0x28(%rbp), %r9
callq 0x3d160
movl $0x1, -0x4(%rbp)
jmp 0x3770a
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
cmpq -0x28(%rbp), %rax
jne 0x375a4
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x3b4b0
testb $0x1, %al
jne 0x37537
jmp 0x375a4
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0xa8(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x1a390
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movq %rax, %r8
movq -0x28(%rbp), %r9
callq 0x3d160
movl $0x1, -0x4(%rbp)
jmp 0x3770a
jmp 0x375a6
movq -0x28(%rbp), %rax
cmpl $0x3, 0x4(%rax)
jne 0x375d3
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x375d3
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0x7f, %eax
movl %eax, -0xc4(%rbp)
jmp 0x375dd
xorl %eax, %eax
movl %eax, -0xc4(%rbp)
jmp 0x375dd
movl -0xc4(%rbp), %eax
movl %eax, -0xc8(%rbp)
subl $0x8, %eax
je 0x37604
jmp 0x375f0
movl -0xc8(%rbp), %eax
addl $-0x17, %eax
subl $0x1, %eax
ja 0x37703
jmp 0x37604
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rax
cmpq -0x20(%rbp), %rax
jne 0x37684
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x3b4b0
testb $0x1, %al
jne 0x37625
jmp 0x37684
movq -0x10(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xd0(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x1a390
movq -0xe0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq -0xd0(%rbp), %rdx
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq 0x38(%rax), %r8
movq -0x20(%rbp), %r9
callq 0x3d160
movl $0x1, -0x4(%rbp)
jmp 0x3770a
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
cmpq -0x20(%rbp), %rax
jne 0x37701
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x3b4b0
testb $0x1, %al
jne 0x376a5
jmp 0x37701
movq -0x10(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xe8(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x1a390
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
movq -0xe8(%rbp), %rdx
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq 0x30(%rax), %r8
movq -0x20(%rbp), %r9
callq 0x3d160
movl $0x1, -0x4(%rbp)
jmp 0x3770a
jmp 0x37703
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)