// File: /workspace/github_cpp_makefile_star2_O1/rossy[P]crossc/SPIRV-Cross/spirv_glsl.cpp
// Function: spirv_cross::CompilerGLSL::emit_function(spirv_cross::SPIRFunction&, spirv_cross::Bitset const&)
void CompilerGLSL::emit_function(SPIRFunction &func, const Bitset &return_flags)
{
	// Avoid potential cycles.
	if (func.active)
		return;
	func.active = true;

	// If we depend on a function, emit that function before we emit our own function.
	for (auto block : func.blocks)
	{
		auto &b = get<SPIRBlock>(block);
		for (auto &i : b.ops)
		{
			auto ops = stream(i);
			auto op = static_cast<Op>(i.op);

			if (op == OpFunctionCall)
			{
				// Recursively emit functions which are called.
				uint32_t id = ops[2];
				emit_function(get<SPIRFunction>(id), ir.meta[ops[1]].decoration.decoration_flags);
			}
		}
	}

	emit_function_prototype(func, return_flags);
	begin_scope();

	if (func.self == ir.default_entry_point)
		emit_entry_point_declarations();

	current_function = &func;
	auto &entry_block = get<SPIRBlock>(func.entry_block);

	for (auto &v : func.local_variables)
	{
		auto &var = get<SPIRVariable>(v);
		if (var.storage == StorageClassWorkgroup)
		{
			// Special variable type which cannot have initializer,
			// need to be declared as standalone variables.
			// Comes from MSL which can push global variables as local variables in main function.
			add_local_variable_name(var.self);
			statement(variable_decl(var), ";");
			var.deferred_declaration = false;
		}
		else if (var.storage == StorageClassPrivate)
		{
			// These variables will not have had their CFG usage analyzed, so move it to the entry block.
			// Comes from MSL which can push global variables as local variables in main function.
			// We could just declare them right now, but we would miss out on an important initialization case which is
			// LUT declaration in MSL.
			// If we don't declare the variable when it is assigned we're forced to go through a helper function
			// which copies elements one by one.
			add_local_variable_name(var.self);
			auto &dominated = entry_block.dominated_variables;
			if (find(begin(dominated), end(dominated), var.self) == end(dominated))
				entry_block.dominated_variables.push_back(var.self);
			var.deferred_declaration = true;
		}
		else if (var.storage == StorageClassFunction && var.remapped_variable && var.static_expression)
		{
			// No need to declare this variable, it has a static expression.
			var.deferred_declaration = false;
		}
		else if (expression_is_lvalue(v))
		{
			add_local_variable_name(var.self);

			if (var.initializer)
				statement(variable_decl_function_local(var), ";");
			else
			{
				// Don't declare variable until first use to declutter the GLSL output quite a lot.
				// If we don't touch the variable before first branch,
				// declare it then since we need variable declaration to be in top scope.
				var.deferred_declaration = true;
			}
		}
		else
		{
			// HACK: SPIR-V in older glslang output likes to use samplers and images as local variables, but GLSL does not allow this.
			// For these types (non-lvalue), we enforce forwarding through a shadowed variable.
			// This means that when we OpStore to these variables, we just write in the expression ID directly.
			// This breaks any kind of branching, since the variable must be statically assigned.
			// Branching on samplers and images would be pretty much impossible to fake in GLSL.
			var.statically_assigned = true;
		}

		var.loop_variable_enable = false;

		// Loop variables are never declared outside their for-loop, so block any implicit declaration.
		if (var.loop_variable)
			var.deferred_declaration = false;
	}

	for (auto &line : current_function->fixup_hooks_in)
		line();

	entry_block.loop_dominator = SPIRBlock::NoDominator;
	emit_block_chain(entry_block);

	end_scope();
	processing_entry_point = false;
	statement("");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
cmpb $0x0, 0xc8(%rsi)
jne 0x276e3
movq %rdi, %rbx
movq %rdx, 0x8(%rsp)
movb $0x1, 0xc8(%rsi)
movq 0x68(%rsi), %rbp
movq %rsi, (%rsp)
movq 0x70(%rsi), %r14
cmpq %r14, %rbp
je 0x2740f
movl (%rbp), %esi
movq %rbx, %rdi
callq 0x78fe
movq 0x38(%rax), %r12
movq 0x40(%rax), %r15
cmpq %r15, %r12
je 0x27409
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1b906
cmpw $0x39, (%r12)
jne 0x27403
movq %rax, %r13
movl 0x8(%rax), %esi
movq %rbx, %rdi
callq 0xb9ca
movl 0x4(%r13), %ecx
movq 0x38(%rbx), %rdx
imulq $0x148, %rcx, %rcx      # imm = 0x148
addq %rcx, %rdx
addq $0x60, %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2736e
addq $0xc, %r12
jmp 0x273bc
addq $0x4, %rbp
jmp 0x273a4
movq (%rbx), %rax
movq %rbx, %rdi
movq (%rsp), %r15
movq %r15, %rsi
movq 0x8(%rsp), %rdx
callq *0x40(%rax)
leaq 0x947bc(%rip), %rsi     # 0xbbbe7
movq %rbx, %rdi
callq 0x5a5fc
incl 0x638(%rbx)
movl 0x8(%r15), %eax
cmpl 0x108(%rbx), %eax
jne 0x27451
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x178(%rax)
movq %r15, 0x148(%rbx)
movl 0x60(%r15), %esi
movq %rbx, %rdi
callq 0x78fe
movq %rax, %r14
movq 0x48(%r15), %r13
movq 0x50(%r15), %r15
cmpq %r15, %r13
je 0x27681
leaq 0x450(%rbx), %rax
movq %rax, (%rsp)
movq %r14, %rax
addq $0xb8, %rax
movq %rax, 0x30(%rsp)
movl (%r13), %esi
movq %rbx, %rdi
callq 0xb40a
movq %rax, %r12
movl 0x10(%rax), %eax
cmpl $0x7, %eax
je 0x27588
cmpl $0x6, %eax
je 0x2751d
cmpl $0x4, %eax
jne 0x275a3
movl 0x8(%r12), %eax
imulq $0x148, %rax, %rdx      # imm = 0x148
addq 0x38(%rbx), %rdx
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x2b9f6
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2ce16
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x985f3(%rip), %rdx     # 0xbfae9
callq 0xaa50
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x27598
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x6260
jmp 0x27598
movq %r12, %rbp
addq $0x8, %rbp
movl 0x8(%r12), %eax
imulq $0x148, %rax, %rdx      # imm = 0x148
addq 0x38(%rbx), %rdx
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x2b9f6
movq 0xb8(%r14), %rdi
movq 0xc0(%r14), %rsi
movq %rbp, %rdx
callq 0x7966
movq 0xc0(%r14), %rsi
cmpq %rsi, %rax
jne 0x2765a
cmpq 0xc8(%r14), %rsi
je 0x2764d
movl (%rbp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xc0(%r14)
jmp 0x2765a
cmpb $0x1, 0x5b(%r12)
jne 0x275a3
cmpl $0x0, 0x3c(%r12)
je 0x275a3
movb $0x0, 0x59(%r12)
jmp 0x27660
movl (%r13), %esi
movq %rbx, %rdi
callq 0x1322a
testb %al, %al
je 0x27645
movl 0x8(%r12), %eax
imulq $0x148, %rax, %rdx      # imm = 0x148
addq 0x38(%rbx), %rdx
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x2b9f6
cmpl $0x0, 0x18(%r12)
je 0x2765a
movq %r15, %rbp
movq %r14, %r15
movl 0x10(%r12), %eax
movl %eax, 0x8(%rsp)
movl $0x7, 0x10(%r12)
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2ce16
movl 0x8(%rsp), %eax
movl %eax, 0x10(%r12)
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x984cd(%rip), %rdx     # 0xbfae9
callq 0xaa50
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
movq %r15, %r14
je 0x27640
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x6260
movq %rbp, %r15
jmp 0x27660
movb $0x1, 0x39(%r12)
jmp 0x27660
movq 0x30(%rsp), %rdi
movq %rbp, %rdx
callq 0x7a00
movb $0x1, 0x59(%r12)
movb $0x0, 0x65(%r12)
cmpb $0x1, 0x64(%r12)
jne 0x27674
movb $0x0, 0x59(%r12)
addq $0x4, %r13
cmpq %r15, %r13
jne 0x27491
movq 0x148(%rbx), %rax
movq 0xb0(%rax), %r15
movq 0xb8(%rax), %r12
cmpq %r12, %r15
je 0x276af
cmpq $0x0, 0x10(%r15)
je 0x276f2
movq %r15, %rdi
callq *0x18(%r15)
addq $0x20, %r15
jmp 0x27696
movl $0xffffffff, 0xb4(%r14) # imm = 0xFFFFFFFF
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4dc78
movq %rbx, %rdi
callq 0x4358a
movb $0x0, 0x5d8(%rbx)
leaq 0x8e1d4(%rip), %rsi     # 0xb58af
movq %rbx, %rdi
callq 0xa700
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x60f0
jmp 0x276f9
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x27718
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x6260
movq %rbx, %rdi
callq 0x64a0