// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
cmpb $0x0, 0xb0(%rsi)
jne 0x150e9
movq %rsi, %rbx
cmpb $0x0, 0xb1(%rsi)
je 0x150f6
xorl %ebp, %ebp
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r14
cmpl $0x2, %edx
je 0x1510b
cmpl $0x1, %edx
je 0x151ac
testl %edx, %edx
jne 0x150e9
xorl %ebp, %ebp
cmpl $0x2, 0xc(%rbx)
jne 0x15131
cmpl $0x1, 0x10(%rbx)
jne 0x15131
movl 0x2c(%rbx), %ecx
movl 0x1c(%rbx), %eax
xorl %ebp, %ebp
cmpl %eax, %ecx
je 0x15131
cmpl 0x8(%rbx), %ecx
je 0x15131
cmpl %eax, 0x30(%rbx)
sete %bpl
cmpl $0x2, %edx
setne %al
movl %ebp, %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x1514a
movl 0x2c(%rbx), %eax
cmpl 0x20(%rbx), %eax
sete %bpl
testb %bpl, %bpl
je 0x150eb
movq 0x50(%rbx), %rax
movq 0x58(%rbx), %rcx
cmpq %rcx, %rax
je 0x1516d
movl 0x8(%rbx), %edx
cmpl %edx, 0x4(%rax)
je 0x150e9
addq $0xc, %rax
cmpq %rcx, %rax
jne 0x1515f
movl 0x1c(%rbx), %esi
movq %r14, %rdi
callq 0x1c06e
testq %rax, %rax
je 0x150eb
movq 0x50(%rax), %rcx
movq 0x58(%rax), %rax
cmpq %rax, %rcx
je 0x150eb
movl 0x8(%rbx), %edx
cmpl %edx, 0x4(%rcx)
je 0x150e9
addq $0xc, %rcx
cmpq %rax, %rcx
jne 0x15195
jmp 0x150eb
xorl %ebp, %ebp
cmpl $0x1, 0xc(%rbx)
jne 0x150eb
cmpl $0x1, 0x10(%rbx)
jne 0x150eb
movq 0x38(%rbx), %rax
cmpq 0x40(%rbx), %rax
jne 0x150e9
movl 0x18(%rbx), %esi
movq %r14, %rdi
callq 0x7932
movq %rax, %r15
xorl %r12d, %r12d
cmpl $0x2, 0xc(%rax)
jne 0x1520b
cmpl $0x0, 0x10(%r15)
jne 0x1520b
movl 0x1c(%rbx), %eax
cmpl %eax, 0x30(%r15)
jne 0x151ff
movl 0x2c(%r15), %ecx
cmpl %eax, %ecx
jne 0x15204
xorl %r12d, %r12d
jmp 0x1520b
cmpl 0x8(%rbx), %ecx
setne %r12b
testb %r12b, %r12b
je 0x152aa
movq 0x50(%rbx), %rax
movq 0x58(%rbx), %rcx
cmpq %rcx, %rax
je 0x15246
movl 0x8(%rbx), %edx
movl 0x8(%r15), %esi
xorl %ebp, %ebp
movl 0x4(%rax), %edi
cmpl %edx, %edi
je 0x150eb
cmpl %esi, %edi
je 0x150eb
addq $0xc, %rax
cmpq %rcx, %rax
jne 0x1522a
movq 0x50(%r15), %rax
movq 0x58(%r15), %rcx
cmpq %rcx, %rax
je 0x15268
movl 0x8(%rbx), %edx
cmpl %edx, 0x4(%rax)
je 0x150e9
addq $0xc, %rax
cmpq %rcx, %rax
jne 0x15256
movl 0x1c(%rbx), %esi
movq %r14, %rdi
callq 0x1c06e
testq %rax, %rax
je 0x152aa
movq 0x50(%rax), %rcx
movq 0x58(%rax), %rax
cmpq %rax, %rcx
je 0x152aa
movl 0x8(%rbx), %edx
movl 0x30(%r15), %esi
xorl %ebp, %ebp
movl 0x4(%rcx), %edi
cmpl %edx, %edi
je 0x150eb
cmpl %esi, %edi
je 0x150eb
addq $0xc, %rcx
cmpq %rax, %rcx
jne 0x1528e
movl %r12d, %ebp
jmp 0x150eb