// File: /workspace/github_cpp_makefile_star2_O3/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 0x27069
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 0x26d95
movl (%rbp), %esi
movq %rbx, %rdi
callq 0x7870
movq 0x38(%rax), %r12
movq 0x40(%rax), %r15
cmpq %r15, %r12
je 0x26d8f
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1b2ac
cmpw $0x39, (%r12)
jne 0x26d89
movq %rax, %r13
movl 0x8(%rax), %esi
movq %rbx, %rdi
callq 0xb98a
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 0x26cf4
addq $0xc, %r12
jmp 0x26d42
addq $0x4, %rbp
jmp 0x26d2a
movq (%rbx), %rax
movq %rbx, %rdi
movq (%rsp), %r15
movq %r15, %rsi
movq 0x8(%rsp), %rdx
callq *0x40(%rax)
leaq 0x93e26(%rip), %rsi     # 0xbabd7
movq %rbx, %rdi
callq 0x59e02
incl 0x638(%rbx)
movl 0x8(%r15), %eax
cmpl 0x108(%rbx), %eax
jne 0x26dd7
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x178(%rax)
movq %r15, 0x148(%rbx)
movl 0x60(%r15), %esi
movq %rbx, %rdi
callq 0x7870
movq %rax, %r14
movq 0x48(%r15), %r13
movq 0x50(%r15), %r15
cmpq %r15, %r13
je 0x27007
leaq 0x450(%rbx), %rax
movq %rax, (%rsp)
movq %r14, %rax
addq $0xb8, %rax
movq %rax, 0x30(%rsp)
movl (%r13), %esi
movq %rbx, %rdi
callq 0xb3ca
movq %rax, %r12
movl 0x10(%rax), %eax
cmpl $0x7, %eax
je 0x26f0e
cmpl $0x6, %eax
je 0x26ea3
cmpl $0x4, %eax
jne 0x26f29
movl 0x8(%r12), %eax
imulq $0x148, %rax, %rdx      # imm = 0x148
addq 0x38(%rbx), %rdx
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x2b3ee
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2c7ac
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x97c75(%rip), %rdx     # 0xbeaf1
callq 0xaa14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x26f1e
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x6260
jmp 0x26f1e
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 0x2b3ee
movq 0xb8(%r14), %rdi
movq 0xc0(%r14), %rsi
movq %rbp, %rdx
callq 0x78d8
movq 0xc0(%r14), %rsi
cmpq %rsi, %rax
jne 0x26fe0
cmpq 0xc8(%r14), %rax
je 0x26fd3
movl (%rbp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xc0(%r14)
jmp 0x26fe0
cmpb $0x1, 0x5b(%r12)
jne 0x26f29
cmpl $0x0, 0x3c(%r12)
je 0x26f29
movb $0x0, 0x59(%r12)
jmp 0x26fe6
movl (%r13), %esi
movq %rbx, %rdi
callq 0x131e0
testb %al, %al
je 0x26fcb
movl 0x8(%r12), %eax
imulq $0x148, %rax, %rdx      # imm = 0x148
addq 0x38(%rbx), %rdx
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x2b3ee
cmpl $0x0, 0x18(%r12)
je 0x26fe0
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 0x2c7ac
movl 0x8(%rsp), %eax
movl %eax, 0x10(%r12)
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x97b4f(%rip), %rdx     # 0xbeaf1
callq 0xaa14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
movq %r15, %r14
je 0x26fc6
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x6260
movq %rbp, %r15
jmp 0x26fe6
movb $0x1, 0x39(%r12)
jmp 0x26fe6
movq 0x30(%rsp), %rdi
movq %rbp, %rdx
callq 0x797e
movb $0x1, 0x59(%r12)
movb $0x0, 0x65(%r12)
cmpb $0x1, 0x64(%r12)
jne 0x26ffa
movb $0x0, 0x59(%r12)
addq $0x4, %r13
cmpq %r15, %r13
jne 0x26e17
movq 0x148(%rbx), %rax
movq 0xb0(%rax), %r15
movq 0xb8(%rax), %r12
cmpq %r12, %r15
je 0x27035
cmpq $0x0, 0x10(%r15)
je 0x27078
movq %r15, %rdi
callq *0x18(%r15)
addq $0x20, %r15
jmp 0x2701c
movl $0xffffffff, 0xb4(%r14) # imm = 0xFFFFFFFF
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4d4da
movq %rbx, %rdi
callq 0x42d5a
movb $0x0, 0x5d8(%rbx)
leaq 0x8d84e(%rip), %rsi     # 0xb48af
movq %rbx, %rdi
callq 0xa6c4
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x60f0
jmp 0x2707f
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x2709e
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x6260
movq %rbx, %rdi
callq 0x64a0