// File: /workspace/github_cpp_makefile_star2_O2/rossy[P]crossc/SPIRV-Cross/spirv_glsl.cpp
// Function: spirv_cross::CompilerGLSL::emit_declared_builtin_block(spv::StorageClass, spv::ExecutionModel)
void CompilerGLSL::emit_declared_builtin_block(StorageClass storage, ExecutionModel model)
{
	Bitset emitted_builtins;
	Bitset global_builtins;
	const SPIRVariable *block_var = nullptr;
	bool emitted_block = false;
	bool builtin_array = false;

	// Need to use declared size in the type.
	// These variables might have been declared, but not statically used, so we haven't deduced their size yet.
	uint32_t cull_distance_size = 0;
	uint32_t clip_distance_size = 0;

	for (auto &id : ir.ids)
	{
		if (id.get_type() != TypeVariable)
			continue;

		auto &var = id.get<SPIRVariable>();
		auto &type = get<SPIRType>(var.basetype);
		bool block = has_decoration(type.self, DecorationBlock);
		Bitset builtins;

		if (var.storage == storage && block && is_builtin_variable(var))
		{
			uint32_t index = 0;
			for (auto &m : ir.meta[type.self].members)
			{
				if (m.builtin)
				{
					builtins.set(m.builtin_type);
					if (m.builtin_type == BuiltInCullDistance)
						cull_distance_size = get<SPIRType>(type.member_types[index]).array.front();
					else if (m.builtin_type == BuiltInClipDistance)
						clip_distance_size = get<SPIRType>(type.member_types[index]).array.front();
				}
				index++;
			}
		}
		else if (var.storage == storage && !block && is_builtin_variable(var))
		{
			// While we're at it, collect all declared global builtins (HLSL mostly ...).
			auto &m = ir.meta[var.self].decoration;
			if (m.builtin)
			{
				global_builtins.set(m.builtin_type);
				if (m.builtin_type == BuiltInCullDistance)
					cull_distance_size = type.array.front();
				else if (m.builtin_type == BuiltInClipDistance)
					clip_distance_size = type.array.front();
			}
		}

		if (builtins.empty())
			continue;

		if (emitted_block)
			SPIRV_CROSS_THROW("Cannot use more than one builtin I/O block.");

		emitted_builtins = builtins;
		emitted_block = true;
		builtin_array = !type.array.empty();
		block_var = &var;
	}

	global_builtins =
	    Bitset(global_builtins.get_lower() & ((1ull << BuiltInPosition) | (1ull << BuiltInPointSize) |
	                                          (1ull << BuiltInClipDistance) | (1ull << BuiltInCullDistance)));

	// Try to collect all other declared builtins.
	if (!emitted_block)
		emitted_builtins = global_builtins;

	// Can't declare an empty interface block.
	if (emitted_builtins.empty())
		return;

	if (storage == StorageClassOutput)
		statement("out gl_PerVertex");
	else
		statement("in gl_PerVertex");

	begin_scope();
	if (emitted_builtins.get(BuiltInPosition))
		statement("vec4 gl_Position;");
	if (emitted_builtins.get(BuiltInPointSize))
		statement("float gl_PointSize;");
	if (emitted_builtins.get(BuiltInClipDistance))
		statement("float gl_ClipDistance[", clip_distance_size, "];");
	if (emitted_builtins.get(BuiltInCullDistance))
		statement("float gl_CullDistance[", cull_distance_size, "];");

	bool tessellation = model == ExecutionModelTessellationEvaluation || model == ExecutionModelTessellationControl;
	if (builtin_array)
	{
		// Make sure the array has a supported name in the code.
		if (storage == StorageClassOutput)
			set_name(block_var->self, "gl_out");
		else if (storage == StorageClassInput)
			set_name(block_var->self, "gl_in");

		if (model == ExecutionModelTessellationControl && storage == StorageClassOutput)
			end_scope_decl(join(to_name(block_var->self), "[", get_entry_point().output_vertices, "]"));
		else
			end_scope_decl(join(to_name(block_var->self), tessellation ? "[gl_MaxPatchVertices]" : "[]"));
	}
	else
		end_scope_decl();
	statement("");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl %edx, 0x20(%rsp)
movl %esi, %ebp
movq %rdi, %r15
leaq 0xb0(%rsp), %rax
andq $0x0, -0x8(%rax)
leaq 0xe0(%rsp), %rax
movq %rax, -0x30(%rax)
pushq $0x1
popq %rcx
movq %rcx, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, %edx       # imm = 0x3F800000
movl %edx, -0x10(%rax)
leaq 0xf0(%rsp), %rsi
andq $0x0, -0x8(%rsi)
movups %xmm0, -0x8(%rax)
leaq 0x120(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movups %xmm0, -0x20(%rax)
andl $0x0, 0xc(%rsp)
movl %edx, -0x10(%rax)
andl $0x0, 0x8(%rsp)
movups %xmm0, -0x8(%rax)
movq 0x20(%rdi), %rbx
movq 0x28(%rdi), %rax
movq $0x0, 0x90(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movl %ebp, 0x24(%rsp)
movq %rax, 0x98(%rsp)
cmpq %rax, %rbx
je 0x2b514
cmpl $0x2, 0x8(%rbx)
jne 0x2b50b
movq %rbx, %rdi
callq 0x9b30
movq %rax, %r14
movl 0xc(%rax), %esi
movq %r15, %rdi
callq 0x90a4
movq %rax, %r12
movl 0x8(%rax), %esi
movq %r15, %rdi
pushq $0x2
popq %rdx
callq 0x103d2
movl %eax, %r13d
andq $0x0, 0x30(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, 0x38(%rsp)
movq $0x1, 0x40(%rsp)
leaq 0x38(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movl $0x3f800000, 0x58(%rsp) # imm = 0x3F800000
movups %xmm0, 0x28(%rax)
movl 0x10(%r14), %eax
cmpl %ebp, %eax
setne %cl
movl %r13d, %edx
xorb $0x1, %dl
orb %cl, %dl
jne 0x2b425
movq %r15, %rdi
movq %r14, %rsi
callq 0x10c60
testb %al, %al
je 0x2b41d
movl 0x8(%r12), %eax
movq 0x38(%r15), %rcx
imulq $0x148, %rax, %rax      # imm = 0x148
movq 0xd0(%rcx,%rax), %r13
movq 0xd8(%rcx,%rax), %rbp
xorl %r15d, %r15d
cmpq %rbp, %r13
je 0x2b49f
cmpb $0x1, 0xcc(%r13)
jne 0x2b411
movl 0xa0(%r13), %esi
leaq 0x30(%rsp), %rdi
callq 0x17654
movl 0xa0(%r13), %ecx
movl %r15d, %eax
cmpl $0x3, %ecx
je 0x2b3f5
cmpl $0x4, %ecx
jne 0x2b411
movq 0x68(%r12), %rcx
movl (%rcx,%rax,4), %esi
movq 0x10(%rsp), %rdi
callq 0x90a4
movq 0x20(%rax), %rax
movl (%rax), %eax
movl %eax, 0xc(%rsp)
jmp 0x2b411
movq 0x68(%r12), %rcx
movl (%rcx,%rax,4), %esi
movq 0x10(%rsp), %rdi
callq 0x90a4
movq 0x20(%rax), %rax
movl (%rax), %eax
movl %eax, 0x8(%rsp)
incl %r15d
addq $0xd0, %r13
jmp 0x2b39f
movl 0x10(%r14), %eax
movl 0x24(%rsp), %ebp
cmpl %ebp, %eax
setne %al
orb %al, %r13b
jne 0x2b49f
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x10c60
testb %al, %al
je 0x2b49f
movl 0x8(%r14), %eax
movq 0x10(%rsp), %rcx
movq 0x38(%rcx), %r15
imulq $0x148, %rax, %rax      # imm = 0x148
cmpb $0x1, 0xcc(%r15,%rax)
jne 0x2b49f
addq %rax, %r15
movl 0xa0(%r15), %esi
leaq 0xe8(%rsp), %rdi
callq 0x17654
movl 0xa0(%r15), %eax
cmpl $0x3, %eax
je 0x2b494
cmpl $0x4, %eax
jne 0x2b49f
movq 0x20(%r12), %rax
movl (%rax), %eax
movl %eax, 0xc(%rsp)
jmp 0x2b49f
movq 0x20(%r12), %rax
movl (%rax), %eax
movl %eax, 0x8(%rsp)
movq 0x50(%rsp), %rax
orq 0x30(%rsp), %rax
jne 0x2b4b2
movq 0x10(%rsp), %r15
jmp 0x2b4f5
testb $0x1, 0x28(%rsp)
jne 0x2b7e7
leaq 0xa8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x19146
movq 0x10(%rsp), %r15
movq 0x20(%r12), %rax
cmpq 0x28(%r12), %rax
setne %al
movq %rax, 0x90(%rsp)
movb $0x1, %al
movq %rax, 0x28(%rsp)
movq %r14, 0x18(%rsp)
leaq 0x38(%rsp), %rdi
callq 0xb51c
movl 0x24(%rsp), %ebp
movq 0x98(%rsp), %rax
addq $0x10, %rbx
jmp 0x2b2e4
leaq 0xe8(%rsp), %rdi
movq (%rdi), %rax
andl $0x1b, %eax
leaq 0x38(%rsp), %rbx
movq %rax, -0x8(%rbx)
leaq 0x68(%rsp), %rax
movq %rax, (%rbx)
movq $0x1, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
movl $0x3f800000, 0x20(%rbx) # imm = 0x3F800000
movups %xmm0, 0x28(%rbx)
leaq 0x30(%rsp), %rsi
callq 0x42804
movq %rbx, %rdi
callq 0xb51c
testb $0x1, 0x28(%rsp)
jne 0x2b57b
leaq 0xa8(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x19146
movq 0xc8(%rsp), %rax
orq 0xa8(%rsp), %rax
je 0x2b7bb
cmpl $0x3, %ebp
jne 0x2b5a7
leaq 0x67935(%rip), %rsi     # 0x92ed2
movq %r15, %rdi
callq 0xadca
jmp 0x2b5b6
leaq 0x67935(%rip), %rsi     # 0x92ee3
movq %r15, %rdi
callq 0xa928
movq %r15, %rdi
callq 0x22d28
movq 0xa8(%rsp), %rax
testb $0x1, %al
je 0x2b5e1
leaq 0x67922(%rip), %rsi     # 0x92ef3
movq %r15, %rdi
callq 0x9e6a
movq 0xa8(%rsp), %rax
testb $0x2, %al
je 0x2b5fc
leaq 0x67919(%rip), %rsi     # 0x92f05
movq %r15, %rdi
callq 0xa792
movq 0xa8(%rsp), %rax
testb $0x8, %al
je 0x2b623
leaq 0x67939(%rip), %rsi     # 0x92f40
leaq 0x6c464(%rip), %rcx     # 0x97a72
leaq 0x8(%rsp), %rdx
movq %r15, %rdi
callq 0x42820
movq 0xa8(%rsp), %rax
testb $0x10, %al
je 0x2b642
leaq 0x6792a(%rip), %rsi     # 0x92f58
leaq 0x6c43d(%rip), %rcx     # 0x97a72
leaq 0xc(%rsp), %rdx
movq %r15, %rdi
callq 0x42820
testb $0x1, 0x90(%rsp)
je 0x2b6f4
cmpl $0x3, %ebp
jne 0x2b701
movq 0x18(%rsp), %rax
movl 0x8(%rax), %ebx
leaq 0x678b1(%rip), %rsi     # 0x92f19
leaq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x6100
leaq 0x30(%rsp), %rdx
movq %r15, %rdi
movl %ebx, %esi
callq 0x11dda
leaq 0x30(%rsp), %rdi
callq 0x6548
cmpl $0x1, 0x20(%rsp)
jne 0x2b73d
movq 0x18(%rsp), %rax
movl 0x8(%rax), %edx
movq (%r15), %rax
leaq 0x70(%rsp), %rdi
pushq $0x1
popq %rcx
movq %r15, %rsi
callq *0x38(%rax)
movq %r15, %rdi
callq 0x12fa4
addq $0xb4, %rax
leaq 0x725db(%rip), %rdx     # 0x9dca4
leaq 0x6d3fe(%rip), %r8      # 0x98ace
leaq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %rax, %rcx
callq 0x428fa
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
callq 0x253d4
jmp 0x2b798
movq %r15, %rdi
callq 0x22dba
jmp 0x2b7ac
cmpl $0x1, %ebp
jne 0x2b73d
movq 0x18(%rsp), %rax
movl 0x8(%rax), %ebx
leaq 0x6780b(%rip), %rsi     # 0x92f20
leaq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x6100
leaq 0x30(%rsp), %rdx
movq %r15, %rdi
movl %ebx, %esi
callq 0x11dda
leaq 0x30(%rsp), %rdi
callq 0x6548
movq 0x18(%rsp), %rax
movl 0x8(%rax), %edx
movq (%r15), %rax
leaq 0x70(%rsp), %rdi
pushq $0x1
popq %rcx
movq %r15, %rsi
callq *0x38(%rax)
movl 0x20(%rsp), %eax
decl %eax
cmpl $0x2, %eax
leaq 0x677c0(%rip), %rax     # 0x92f26
leaq 0x677cf(%rip), %rcx     # 0x92f3c
cmovbq %rax, %rcx
leaq 0xa0(%rsp), %rdx
movq %rcx, (%rdx)
leaq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x42976
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
callq 0x253d4
leaq 0x30(%rsp), %rdi
callq 0x6548
leaq 0x70(%rsp), %rdi
callq 0x6548
leaq 0x640d4(%rip), %rsi     # 0x8f887
movq %r15, %rdi
callq 0x928e
leaq 0xf0(%rsp), %rdi
callq 0xb51c
leaq 0xb0(%rsp), %rdi
callq 0xb51c
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x6140
movq %rax, %r14
leaq 0x676ad(%rip), %rsi     # 0x92ea6
leaq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
callq 0x6100
movb $0x1, %bpl
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x6f6e
xorl %ebp, %ebp
leaq 0xbb59c(%rip), %rsi     # 0xe6dc0
movq 0xbc7b5(%rip), %rdx     # 0xe7fe0
movq %r14, %rdi
callq 0x6490
jmp 0x2b849
jmp 0x2b894
jmp 0x2b83d
jmp 0x2b894
jmp 0x2b858
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0x2b860
jmp 0x2b894
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x6548
jmp 0x2b85b
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x6548
jmp 0x2b8a8
jmp 0x2b894
jmp 0x2b894
jmp 0x2b89b
jmp 0x2b894
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x6548
testb %bpl, %bpl
jne 0x2b886
jmp 0x2b89e
movq %rax, %rbx
movq %r14, %rdi
callq 0x6200
jmp 0x2b89e
jmp 0x2b894
jmp 0x2b894
movq %rax, %rbx
jmp 0x2b8a8
jmp 0x2b89b
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0xb51c
leaq 0xf0(%rsp), %rdi
callq 0xb51c
leaq 0xb0(%rsp), %rdi
callq 0xb51c
movq %rbx, %rdi
callq 0x64b0