// File: rossy[P]crossc/SPIRV-Cross/spirv_glsl.cpp
// Function: spirv_cross::CompilerGLSL::emit_for_loop_initializers[abi:cxx11](spirv_cross::SPIRBlock const&)
string CompilerGLSL::emit_for_loop_initializers(const SPIRBlock &block)
{
	if (block.loop_variables.empty())
		return "";

	bool same_types = for_loop_initializers_are_same_type(block);
	// We can only declare for loop initializers if all variables are of same type.
	// If we cannot do this, declare individual variables before the loop header.

	// We might have a loop variable candidate which was not assigned to for some reason.
	uint32_t missing_initializers = 0;
	for (auto &variable : block.loop_variables)
	{
		uint32_t expr = get<SPIRVariable>(variable).static_expression;

		// Sometimes loop variables are initialized with OpUndef, but we can just declare
		// a plain variable without initializer in this case.
		if (expr == 0 || ir.ids[expr].get_type() == TypeUndef)
			missing_initializers++;
	}

	if (block.loop_variables.size() == 1 && missing_initializers == 0)
	{
		return variable_decl(get<SPIRVariable>(block.loop_variables.front()));
	}
	else if (!same_types || missing_initializers == uint32_t(block.loop_variables.size()))
	{
		for (auto &loop_var : block.loop_variables)
			statement(variable_decl(get<SPIRVariable>(loop_var)), ";");
		return "";
	}
	else
	{
		// We have a mix of loop variables, either ones with a clear initializer, or ones without.
		// Separate the two streams.
		string expr;

		for (auto &loop_var : block.loop_variables)
		{
			uint32_t static_expr = get<SPIRVariable>(loop_var).static_expression;
			if (static_expr == 0 || ir.ids[static_expr].get_type() == TypeUndef)
			{
				statement(variable_decl(get<SPIRVariable>(loop_var)), ";");
			}
			else
			{
				if (expr.empty())
				{
					// For loop initializers are of the form <type id = value, id = value, id = value, etc ...
					auto &var = get<SPIRVariable>(loop_var);
					auto &type = get<SPIRType>(var.basetype);
					expr = join(to_qualifiers_glsl(var.self), type_to_glsl(type), " ");
				}
				else
					expr += ", ";

				auto &v = get<SPIRVariable>(loop_var);
				expr += join(to_name(loop_var), " = ", to_expression(v.static_expression));
			}
		}
		return expr;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movq %rdi, -0x1e0(%rbp)
movq %rdi, %rax
movq %rax, -0x1d8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq -0x18(%rbp), %rdi
addq $0xd0, %rdi
callq 0x8690
testb $0x1, %al
jne 0x9a5a9
jmp 0x9a5fd
leaq -0x19(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x7650
movq -0x1e0(%rbp), %rdi
movq -0x1e8(%rbp), %rdx
leaq 0xa42d0(%rip), %rsi     # 0x13e89e
callq 0x7520
jmp 0x9a5d5
leaq -0x19(%rbp), %rdi
callq 0x73d0
jmp 0x9aca6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x19(%rbp), %rdi
callq 0x73d0
jmp 0x9acb6
movq -0x1d0(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x9acc0
andb $0x1, %al
movb %al, -0x2d(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x18(%rbp), %rax
addq $0xd0, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0xbaf0
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
callq 0xbb20
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x2e710
testb $0x1, %al
jne 0x9a654
jmp 0x9a6bb
leaq -0x48(%rbp), %rdi
callq 0x2e750
movq -0x1d0(%rbp), %rdi
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movl (%rax), %esi
callq 0xfec0
movl 0x3c(%rax), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x9a6a5
movq -0x1d0(%rbp), %rdi
addq $0x8, %rdi
addq $0x18, %rdi
movl -0x5c(%rbp), %eax
movl %eax, %esi
callq 0xbdb0
movq %rax, %rdi
callq 0xfa00
cmpl $0xd, %eax
jne 0x9a6ae
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x9a6b0
leaq -0x48(%rbp), %rdi
callq 0x2e770
jmp 0x9a641
movq -0x18(%rbp), %rdi
addq $0xd0, %rdi
callq 0x8650
cmpq $0x1, %rax
jne 0x9a710
cmpl $0x0, -0x34(%rbp)
jne 0x9a710
movq -0x18(%rbp), %rdi
addq $0xd0, %rdi
callq 0x32110
movq -0x1d0(%rbp), %rdi
movl (%rax), %esi
callq 0xfec0
movq -0x1e0(%rbp), %rdi
movq -0x1d0(%rbp), %rsi
movq %rax, %rdx
callq 0x62d60
jmp 0x9aca6
testb $0x1, -0x2d(%rbp)
je 0x9a740
movl -0x34(%rbp), %eax
movl %eax, -0x1ec(%rbp)
movq -0x18(%rbp), %rdi
addq $0xd0, %rdi
callq 0x8650
movq %rax, %rcx
movl -0x1ec(%rbp), %eax
cmpl %ecx, %eax
jne 0x9a86a
movq -0x18(%rbp), %rax
addq $0xd0, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0xbaf0
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0xbb20
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x2e710
testb $0x1, %al
jne 0x9a77e
jmp 0x9a80d
leaq -0x70(%rbp), %rdi
callq 0x2e750
movq -0x1d0(%rbp), %rdi
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movl (%rax), %esi
callq 0xfec0
movq -0x1d0(%rbp), %rsi
movq %rax, %rdx
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x62d60
movq -0x1d0(%rbp), %rdi
movq -0x1f8(%rbp), %rsi
leaq 0xae8d7(%rip), %rdx     # 0x1490a6
callq 0xf270
jmp 0x9a7d6
leaq -0xa0(%rbp), %rdi
callq 0x7708
leaq -0x70(%rbp), %rdi
callq 0x2e770
jmp 0x9a768
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x7708
jmp 0x9acb6
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x7650
movq -0x1e0(%rbp), %rdi
movq -0x200(%rbp), %rdx
leaq 0xa4069(%rip), %rsi     # 0x13e89e
callq 0x7520
jmp 0x9a83c
leaq -0xa1(%rbp), %rdi
callq 0x73d0
jmp 0x9aca6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0xa1(%rbp), %rdi
callq 0x73d0
jmp 0x9acb6
movq -0x1e0(%rbp), %rdi
movb $0x0, -0xa2(%rbp)
callq 0x7530
movq -0x18(%rbp), %rax
addq $0xd0, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0xbaf0
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
callq 0xbb20
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x2e710
testb $0x1, %al
jne 0x9a8d0
jmp 0x9ac7a
leaq -0xb8(%rbp), %rdi
callq 0x2e750
movq -0x1d0(%rbp), %rdi
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movl (%rax), %esi
callq 0xfec0
movq %rax, -0x208(%rbp)
jmp 0x9a901
movq -0x208(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0xcc(%rbp)
cmpl $0x0, -0xcc(%rbp)
je 0x9a951
movq -0x1d0(%rbp), %rdi
addq $0x20, %rdi
movl -0xcc(%rbp), %eax
movl %eax, %esi
callq 0xbdb0
movq %rax, %rdi
callq 0xfa00
movl %eax, -0x20c(%rbp)
jmp 0x9a942
movl -0x20c(%rbp), %eax
cmpl $0xd, %eax
jne 0x9a9e6
movq -0x1d0(%rbp), %rdi
movq -0xc8(%rbp), %rax
movl (%rax), %esi
callq 0xfec0
movq %rax, -0x218(%rbp)
jmp 0x9a96f
movq -0x218(%rbp), %rdx
movq -0x1d0(%rbp), %rsi
leaq -0xf0(%rbp), %rdi
callq 0x62d60
jmp 0x9a98b
movq -0x1d0(%rbp), %rdi
leaq 0xae70d(%rip), %rdx     # 0x1490a6
leaq -0xf0(%rbp), %rsi
callq 0xf270
jmp 0x9a9a7
leaq -0xf0(%rbp), %rdi
callq 0x7708
jmp 0x9ac67
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x9ac98
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x7708
jmp 0x9ac98
movq -0x1e0(%rbp), %rdi
callq 0x75f0
testb $0x1, %al
jne 0x9a9fb
jmp 0x9ab32
movq -0x1d0(%rbp), %rdi
movq -0xc8(%rbp), %rax
movl (%rax), %esi
callq 0xfec0
movq %rax, -0x220(%rbp)
jmp 0x9aa19
movq -0x1d0(%rbp), %rdi
movq -0x220(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movl 0xc(%rax), %esi
callq 0xea90
movq %rax, -0x228(%rbp)
jmp 0x9aa46
movq -0x1d0(%rbp), %rsi
movq -0x228(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0xf8(%rbp), %rax
movl 0x8(%rax), %edx
movq (%rsi), %rax
movq 0x128(%rax), %rax
leaq -0x140(%rbp), %rdi
callq *%rax
jmp 0x9aa7a
movq -0x1d0(%rbp), %rsi
movq -0x100(%rbp), %rdx
movq (%rsi), %rax
movq 0x98(%rax), %rax
leaq -0x160(%rbp), %rdi
xorl %ecx, %ecx
callq *%rax
jmp 0x9aa9f
leaq 0xa3df7(%rip), %rcx     # 0x13e89d
leaq -0x120(%rbp), %rdi
leaq -0x140(%rbp), %rsi
leaq -0x160(%rbp), %rdx
callq 0xa8860
jmp 0x9aac2
movq -0x1e0(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x7430
leaq -0x120(%rbp), %rdi
callq 0x7708
leaq -0x160(%rbp), %rdi
callq 0x7708
leaq -0x140(%rbp), %rdi
callq 0x7708
jmp 0x9ab49
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x9ab21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x7708
leaq -0x140(%rbp), %rdi
callq 0x7708
jmp 0x9ac98
movq -0x1e0(%rbp), %rdi
leaq 0xa9a4f(%rip), %rsi     # 0x14458f
callq 0x7340
jmp 0x9ab47
jmp 0x9ab49
movq -0x1d0(%rbp), %rdi
movq -0xc8(%rbp), %rax
movl (%rax), %esi
callq 0xfec0
movq %rax, -0x230(%rbp)
jmp 0x9ab67
movq -0x1d0(%rbp), %rsi
movq -0x230(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0xc8(%rbp), %rax
movl (%rax), %edx
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0x1a8(%rbp), %rdi
movl $0x1, %ecx
callq *%rax
jmp 0x9ab9c
movq -0x1d0(%rbp), %rsi
movq -0x168(%rbp), %rax
movl 0x3c(%rax), %edx
leaq -0x1c8(%rbp), %rdi
callq 0x71b50
jmp 0x9abbb
leaq 0xae44d(%rip), %rdx     # 0x14900f
leaq -0x188(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
leaq -0x1c8(%rbp), %rcx
callq 0xa8920
jmp 0x9abde
movq -0x1e0(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x7470
jmp 0x9abf3
leaq -0x188(%rbp), %rdi
callq 0x7708
leaq -0x1c8(%rbp), %rdi
callq 0x7708
leaq -0x1a8(%rbp), %rdi
callq 0x7708
jmp 0x9ac67
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x9ac59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x9ac4d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x7708
leaq -0x1c8(%rbp), %rdi
callq 0x7708
leaq -0x1a8(%rbp), %rdi
callq 0x7708
jmp 0x9ac98
jmp 0x9ac69
leaq -0xb8(%rbp), %rdi
callq 0x2e770
jmp 0x9a8b4
movb $0x1, -0xa2(%rbp)
testb $0x1, -0xa2(%rbp)
jne 0x9ac96
movq -0x1e0(%rbp), %rdi
callq 0x7708
jmp 0x9aca6
movq -0x1e0(%rbp), %rdi
callq 0x7708
jmp 0x9acb6
movq -0x1d8(%rbp), %rax
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x7640
nop