// File: /workspace/github_cpp_makefile_star2_O2/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 0x21ebe
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 0x21cc1
movl (%rbp), %esi
movq %rbx, %rdi
callq 0x6bf0
movq 0x38(%rax), %r12
movq 0x40(%rax), %r15
cmpq %r15, %r12
je 0x21cbb
movq %rbx, %rdi
movq %r12, %rsi
callq 0x16f1e
cmpw $0x39, (%r12)
jne 0x21cb5
movq %rax, %r13
movl 0x8(%rax), %esi
movq %rbx, %rdi
callq 0xa048
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 0x21c20
addq $0xc, %r12
jmp 0x21c6e
addq $0x4, %rbp
jmp 0x21c56
movq (%rbx), %rax
movq %rbx, %rdi
movq (%rsp), %r14
movq %r14, %rsi
movq 0x8(%rsp), %rdx
callq *0x40(%rax)
movq %rbx, %rdi
callq 0x22d28
movl 0x8(%r14), %eax
cmpl 0x108(%rbx), %eax
jne 0x21cf6
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x178(%rax)
movq %r14, 0x148(%rbx)
movl 0x60(%r14), %esi
movq %rbx, %rdi
callq 0x6bf0
movq 0x48(%r14), %r13
movq 0x50(%r14), %r14
movq %rax, (%rsp)
addq $0xb8, %rax
movq %rax, 0x10(%rsp)
leaq 0x18(%rsp), %r15
movq %r14, 0x8(%rsp)
movq (%rsp), %r12
cmpq %r14, %r13
je 0x21e65
movl (%r13), %esi
movq %rbx, %rdi
callq 0x9c0e
movq %rax, %rbp
movl 0x10(%rax), %eax
cmpl $0x7, %eax
je 0x21de7
cmpl $0x6, %eax
je 0x21d95
cmpl $0x4, %eax
jne 0x21df9
movl 0x8(%rbp), %esi
movq %rbx, %rdi
callq 0x2f168
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x2619e
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x77cfb(%rip), %rdx     # 0x99a81
callq 0x953a
movq %r15, %rdi
callq 0x6548
jmp 0x21df3
movq %r15, %r14
movq %rbp, %r15
addq $0x8, %r15
movl 0x8(%rbp), %esi
movq %rbx, %rdi
callq 0x2f168
movq 0xb8(%r12), %rdi
movq 0xc0(%r12), %rsi
movq %r15, %rdx
callq 0x6c66
cmpq 0xc0(%r12), %rax
jne 0x21dd9
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x6c06
movb $0x1, 0x59(%rbp)
movq %r14, %r15
movq 0x8(%rsp), %r14
jmp 0x21e4e
cmpb $0x1, 0x5b(%rbp)
jne 0x21df9
cmpl $0x0, 0x3c(%rbp)
je 0x21df9
movb $0x0, 0x59(%rbp)
jmp 0x21e4e
movl (%r13), %esi
movq %rbx, %rdi
callq 0x10b42
testb %al, %al
je 0x21e44
movl 0x8(%rbp), %esi
movq %rbx, %rdi
callq 0x2f168
cmpl $0x0, 0x18(%rbp)
je 0x21e4a
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x364e6
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x77c4c(%rip), %rdx     # 0x99a81
callq 0x953a
movq %r15, %rdi
callq 0x6548
jmp 0x21e4e
movb $0x1, 0x39(%rbp)
jmp 0x21e4e
movb $0x1, 0x59(%rbp)
movb $0x0, 0x65(%rbp)
cmpb $0x1, 0x64(%rbp)
jne 0x21e5c
movb $0x0, 0x59(%rbp)
addq $0x4, %r13
jmp 0x21d2e
movq 0x148(%rbx), %rax
movq 0xb0(%rax), %r15
movq 0xb8(%rax), %r12
cmpq %r12, %r15
je 0x21e8d
movq %r15, %rdi
callq 0x46c38
addq $0x20, %r15
jmp 0x21e7a
movq (%rsp), %rsi
orl $-0x1, 0xb4(%rsi)
movq %rbx, %rdi
callq 0x3eae8
movq %rbx, %rdi
callq 0x3650a
movb $0x0, 0x5d8(%rbx)
leaq 0x6d9d1(%rip), %rsi     # 0x8f887
movq %rbx, %rdi
callq 0x928e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x21ecf
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x6548
movq %rbx, %rdi
callq 0x64b0