// File: /workspace/github_cpp_makefile_star2_O3/rossy[P]crossc/SPIRV-Cross/spirv_cross.cpp
// Function: spirv_cross::Compiler::block_is_loop_candidate(spirv_cross::SPIRBlock const&, spirv_cross::SPIRBlock::Method) const
bool Compiler::block_is_loop_candidate(const SPIRBlock &block, SPIRBlock::Method method) const
{
	// Tried and failed.
	if (block.disable_block_optimization || block.complex_continue)
		return false;

	if (method == SPIRBlock::MergeToSelectForLoop || method == SPIRBlock::MergeToSelectContinueForLoop)
	{
		// Try to detect common for loop pattern
		// which the code backend can use to create cleaner code.
		// for(;;) { if (cond) { some_body; } else { break; } }
		// is the pattern we're looking for.
		bool ret = block.terminator == SPIRBlock::Select && block.merge == SPIRBlock::MergeLoop &&
		           block.true_block != block.merge_block && block.true_block != block.self &&
		           block.false_block == block.merge_block;

		if (ret && method == SPIRBlock::MergeToSelectContinueForLoop)
			ret = block.true_block == block.continue_block;

		// If we have OpPhi which depends on branches which came from our own block,
		// we need to flush phi variables in else block instead of a trivial break,
		// so we cannot assume this is a for loop candidate.
		if (ret)
		{
			for (auto &phi : block.phi_variables)
				if (phi.parent == block.self)
					return false;

			auto *merge = maybe_get<SPIRBlock>(block.merge_block);
			if (merge)
				for (auto &phi : merge->phi_variables)
					if (phi.parent == block.self)
						return false;
		}
		return ret;
	}
	else if (method == SPIRBlock::MergeToDirectForLoop)
	{
		// Empty loop header that just sets up merge target
		// and branches to loop body.
		bool ret = block.terminator == SPIRBlock::Direct && block.merge == SPIRBlock::MergeLoop && block.ops.empty();

		if (!ret)
			return false;

		auto &child = get<SPIRBlock>(block.next_block);
		ret = child.terminator == SPIRBlock::Select && child.merge == SPIRBlock::MergeNone &&
		      child.false_block == block.merge_block && child.true_block != block.merge_block &&
		      child.true_block != block.self;

		// If we have OpPhi which depends on branches which came from our own block,
		// we need to flush phi variables in else block instead of a trivial break,
		// so we cannot assume this is a for loop candidate.
		if (ret)
		{
			for (auto &phi : block.phi_variables)
				if (phi.parent == block.self || phi.parent == child.self)
					return false;

			for (auto &phi : child.phi_variables)
				if (phi.parent == block.self)
					return false;

			auto *merge = maybe_get<SPIRBlock>(block.merge_block);
			if (merge)
				for (auto &phi : merge->phi_variables)
					if (phi.parent == block.self || phi.parent == child.false_block)
						return false;
		}

		return ret;
	}
	else
		return false;
}

// Assembly:
cmpb $0x0, 0xb0(%rsi)
je 0x14f00
xorl %eax, %eax
retq
pushq %r15
pushq %r14
pushq %rbx
movq %rsi, %rbx
cmpb $0x0, 0xb1(%rsi)
je 0x14f19
xorl %eax, %eax
popq %rbx
popq %r14
popq %r15
retq
movq %rdi, %r14
cmpl $0x2, %edx
je 0x14f2a
cmpl $0x1, %edx
je 0x14f71
testl %edx, %edx
jne 0x14f11
xorl %eax, %eax
cmpl $0x2, 0xc(%rbx)
jne 0x14f13
cmpl $0x1, 0x10(%rbx)
jne 0x14f13
movl 0x2c(%rbx), %ecx
movl 0x8(%rbx), %eax
movl 0x1c(%rbx), %esi
cmpl %esi, %ecx
sete %dil
cmpl %eax, %ecx
sete %r8b
orb %dil, %r8b
jne 0x14f11
xorl $0x2, %edx
movl 0x30(%rbx), %edi
movl %edi, %r8d
xorl %esi, %r8d
orl %edx, %r8d
jne 0x15021
cmpl 0x20(%rbx), %ecx
jne 0x14f11
jmp 0x15029
xorl %eax, %eax
cmpl $0x1, 0xc(%rbx)
jne 0x14f13
cmpl $0x1, 0x10(%rbx)
jne 0x14f13
movq 0x38(%rbx), %rax
cmpq 0x40(%rbx), %rax
jne 0x14f11
movl 0x18(%rbx), %esi
movq %r14, %rdi
callq 0x78a4
movq %rax, %r15
xorl %eax, %eax
cmpl $0x2, 0xc(%r15)
jne 0x14f13
cmpl $0x0, 0x10(%r15)
jne 0x14f13
movl 0x30(%r15), %esi
cmpl 0x1c(%rbx), %esi
jne 0x14f11
movl 0x2c(%r15), %eax
cmpl %esi, %eax
je 0x14f11
movl 0x8(%rbx), %ecx
cmpl %ecx, %eax
je 0x14f11
movq 0x50(%rbx), %rdx
movq 0x58(%rbx), %rdi
cmpq %rdi, %rdx
je 0x15005
movl 0x8(%r15), %r8d
xorl %eax, %eax
movl 0x4(%rdx), %r9d
cmpl %ecx, %r9d
je 0x14f13
cmpl %r8d, %r9d
je 0x14f13
addq $0xc, %rdx
cmpq %rdi, %rdx
jne 0x14fe6
movq 0x50(%r15), %rax
movq 0x58(%r15), %rdx
cmpq %rdx, %rax
je 0x15090
cmpl %ecx, 0x4(%rax)
je 0x14f11
addq $0xc, %rax
jmp 0x1500d
cmpl %esi, %edi
jne 0x14f11
movq 0x50(%rbx), %rcx
movq 0x58(%rbx), %rdx
cmpq %rdx, %rcx
je 0x15045
cmpl %eax, 0x4(%rcx)
je 0x14f11
addq $0xc, %rcx
jmp 0x15031
movq %r14, %rdi
callq 0x1ba0e
movq %rax, %rcx
movb $0x1, %al
testq %rcx, %rcx
je 0x14f13
movq 0x50(%rcx), %rdx
movq 0x58(%rcx), %rcx
cmpq %rcx, %rdx
je 0x14f13
movl 0x8(%rbx), %esi
addq $0xc, %rdx
cmpl %esi, -0x8(%rdx)
setne %al
je 0x14f13
leaq 0xc(%rdx), %rdi
cmpq %rcx, %rdx
movq %rdi, %rdx
jne 0x15073
jmp 0x14f13
movq %r14, %rdi
callq 0x1ba0e
movq %rax, %rcx
movb $0x1, %al
testq %rcx, %rcx
je 0x14f13
movq 0x50(%rcx), %rdx
movq 0x58(%rcx), %rcx
cmpq %rcx, %rdx
je 0x14f13
movl 0x8(%rbx), %esi
movl 0x30(%r15), %edi
addq $0xc, %rdx
movl -0x8(%rdx), %eax
cmpl %esi, %eax
setne %r8b
cmpl %edi, %eax
setne %al
andb %r8b, %al
cmpb $0x1, %al
jne 0x14f13
leaq 0xc(%rdx), %r8
cmpq %rcx, %rdx
movq %r8, %rdx
jne 0x150c2
jmp 0x14f13