// File: 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
movq %rsp, %rbp
subq $0x340, %rsp            # imm = 0x340
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2b8(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x10710
leaq -0x90(%rbp), %rdi
callq 0x10710
movq -0x2b8(%rbp), %rax
movq $0x0, -0x98(%rbp)
movb $0x0, -0x99(%rbp)
movb $0x0, -0x9a(%rbp)
movl $0x0, -0xa0(%rbp)
movl $0x0, -0xa4(%rbp)
addq $0x8, %rax
addq $0x18, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0xf940
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
callq 0xf970
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0xf9a0
testb $0x1, %al
jne 0x70e0a
jmp 0x7136e
leaq -0xb8(%rbp), %rdi
callq 0xf9e0
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdi
callq 0xfa00
movl %eax, -0x2bc(%rbp)
jmp 0x70e31
movl -0x2bc(%rbp), %eax
cmpl $0x2, %eax
je 0x70e58
jmp 0x7134c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x7191c
movq -0xc8(%rbp), %rdi
callq 0xfd10
movq %rax, -0x2c8(%rbp)
jmp 0x70e6d
movq -0x2b8(%rbp), %rdi
movq -0x2c8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movl 0xc(%rax), %esi
callq 0xea90
movq %rax, -0x2d0(%rbp)
jmp 0x70e9a
movq -0x2b8(%rbp), %rdi
movq -0x2d0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x2, %edx
callq 0x1f560
movb %al, -0x2d1(%rbp)
jmp 0x70ecb
movb -0x2d1(%rbp), %al
andb $0x1, %al
movb %al, -0xe9(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x10710
movq -0xe0(%rbp), %rax
movl 0x10(%rax), %eax
cmpl -0xc(%rbp), %eax
jne 0x710da
testb $0x1, -0xe9(%rbp)
je 0x710da
movq -0x2b8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x205a0
movb %al, -0x2d2(%rbp)
jmp 0x70f20
movb -0x2d2(%rbp), %al
testb $0x1, %al
jne 0x70f2f
jmp 0x710da
movq -0x2b8(%rbp), %rdi
movl $0x0, -0x134(%rbp)
addq $0x8, %rdi
addq $0x30, %rdi
movq -0xe8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %esi
callq 0xead0
addq $0xd0, %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rdi
callq 0x38cd0
movq %rax, -0x148(%rbp)
movq -0x140(%rbp), %rdi
callq 0x38d00
movq %rax, -0x150(%rbp)
leaq -0x148(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x38e30
testb $0x1, %al
jne 0x70fa8
jmp 0x710d5
leaq -0x148(%rbp), %rdi
callq 0x38e70
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
testb $0x1, 0xcc(%rax)
je 0x710b5
movq -0x158(%rbp), %rax
movl 0xa0(%rax), %esi
leaq -0x130(%rbp), %rdi
callq 0x300d0
jmp 0x70fea
movq -0x158(%rbp), %rax
cmpl $0x4, 0xa0(%rax)
jne 0x7105a
movq -0xe8(%rbp), %rdi
addq $0x68, %rdi
movl -0x134(%rbp), %eax
movl %eax, %esi
callq 0x8630
movq -0x2b8(%rbp), %rdi
movl (%rax), %esi
callq 0xea90
movq %rax, -0x2e0(%rbp)
jmp 0x71029
movq -0x2e0(%rbp), %rdi
addq $0x20, %rdi
callq 0x32570
movl (%rax), %eax
movl %eax, -0xa0(%rbp)
jmp 0x710b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x7135d
movq -0x158(%rbp), %rax
cmpl $0x3, 0xa0(%rax)
jne 0x710b1
movq -0xe8(%rbp), %rdi
addq $0x68, %rdi
movl -0x134(%rbp), %eax
movl %eax, %esi
callq 0x8630
movq -0x2b8(%rbp), %rdi
movl (%rax), %esi
callq 0xea90
movq %rax, -0x2e8(%rbp)
jmp 0x71099
movq -0x2e8(%rbp), %rdi
addq $0x20, %rdi
callq 0x32570
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
jmp 0x710b3
jmp 0x710b5
movl -0x134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x134(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x38e90
jmp 0x70f8c
jmp 0x711d0
movq -0xe0(%rbp), %rax
movl 0x10(%rax), %eax
cmpl -0xc(%rbp), %eax
jne 0x711ce
testb $0x1, -0xe9(%rbp)
jne 0x711ce
movq -0x2b8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x205a0
movb %al, -0x2e9(%rbp)
jmp 0x71115
movb -0x2e9(%rbp), %al
testb $0x1, %al
jne 0x71124
jmp 0x711ce
movq -0x2b8(%rbp), %rdi
addq $0x8, %rdi
addq $0x30, %rdi
movq -0xe0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %esi
callq 0xead0
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rax
testb $0x1, 0xcc(%rax)
je 0x711cc
movq -0x160(%rbp), %rax
movl 0xa0(%rax), %esi
leaq -0x90(%rbp), %rdi
callq 0x300d0
jmp 0x71176
movq -0x160(%rbp), %rax
cmpl $0x4, 0xa0(%rax)
jne 0x711a0
movq -0xe8(%rbp), %rdi
addq $0x20, %rdi
callq 0x32570
movl (%rax), %eax
movl %eax, -0xa0(%rbp)
jmp 0x711ca
movq -0x160(%rbp), %rax
cmpl $0x3, 0xa0(%rax)
jne 0x711c8
movq -0xe8(%rbp), %rdi
addq $0x20, %rdi
callq 0x32570
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
jmp 0x711ca
jmp 0x711cc
jmp 0x711ce
jmp 0x711d0
leaq -0x130(%rbp), %rdi
callq 0xa0a60
movb %al, -0x2ea(%rbp)
jmp 0x711e4
movb -0x2ea(%rbp), %al
testb $0x1, %al
jne 0x711f0
jmp 0x711ff
movl $0x3, -0x164(%rbp)
jmp 0x71330
testb $0x1, -0x99(%rbp)
je 0x712e5
movb $0x1, -0x18a(%rbp)
movl $0x10, %edi
callq 0x71a0
movq %rax, -0x300(%rbp)
leaq -0x189(%rbp), %rdi
movq %rdi, -0x2f8(%rbp)
callq 0x7650
movq -0x2f8(%rbp), %rdx
leaq 0xd1165(%rip), %rsi     # 0x1423aa
leaq -0x188(%rbp), %rdi
callq 0x7520
jmp 0x71253
movq -0x300(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x9c50
jmp 0x71268
movq -0x300(%rbp), %rdi
movb $0x0, -0x18a(%rbp)
leaq 0x13cb5b(%rip), %rsi    # 0x1addd8
leaq -0x675f4(%rip), %rdx    # 0x9c90
callq 0x7610
jmp 0x7193d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x712c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x7708
leaq -0x189(%rbp), %rdi
callq 0x73d0
testb $0x1, -0x18a(%rbp)
jne 0x712d7
jmp 0x712e3
movq -0x300(%rbp), %rdi
callq 0x72c0
jmp 0x7135d
leaq -0x50(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x38560
jmp 0x712f7
movb $0x1, -0x99(%rbp)
movq -0xe8(%rbp), %rdi
addq $0x20, %rdi
callq 0x8690
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x9a(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x98(%rbp)
movl $0x0, -0x164(%rbp)
leaq -0x130(%rbp), %rdi
callq 0xf250
movl -0x164(%rbp), %eax
testl %eax, %eax
je 0x7134a
jmp 0x71348
jmp 0x7134c
jmp 0x7134c
leaq -0xb8(%rbp), %rdi
callq 0xfbd0
jmp 0x70dee
leaq -0x130(%rbp), %rdi
callq 0xf250
jmp 0x7191c
leaq -0x90(%rbp), %rdi
callq 0x2fa90
movq %rax, -0x308(%rbp)
jmp 0x71383
movq -0x308(%rbp), %rax
andl $0x1b, %eax
movl %eax, %esi
leaq -0x1d0(%rbp), %rdi
callq 0xa0aa0
jmp 0x7139d
leaq -0x90(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0xa0ad0
leaq -0x1d0(%rbp), %rdi
callq 0xf250
testb $0x1, -0x99(%rbp)
jne 0x713d9
leaq -0x50(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x38560
jmp 0x713d7
jmp 0x713d9
leaq -0x50(%rbp), %rdi
callq 0xa0a60
movb %al, -0x309(%rbp)
jmp 0x713ea
movb -0x309(%rbp), %al
testb $0x1, %al
jne 0x713f6
jmp 0x71405
movl $0x1, -0x164(%rbp)
jmp 0x718fe
cmpl $0x3, -0xc(%rbp)
jne 0x71422
movq -0x2b8(%rbp), %rdi
leaq 0xd0fbd(%rip), %rsi     # 0x1423d6
callq 0x11d40
jmp 0x71420
jmp 0x71439
movq -0x2b8(%rbp), %rdi
leaq 0xd0fb7(%rip), %rsi     # 0x1423e7
callq 0x11460
jmp 0x71437
jmp 0x71439
movq -0x2b8(%rbp), %rdi
callq 0x5c200
jmp 0x71447
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0xef50
movb %al, -0x30a(%rbp)
jmp 0x7145a
movb -0x30a(%rbp), %al
testb $0x1, %al
jne 0x71466
jmp 0x7147d
movq -0x2b8(%rbp), %rdi
leaq 0xd0f83(%rip), %rsi     # 0x1423f7
callq 0x10360
jmp 0x7147b
jmp 0x7147d
leaq -0x50(%rbp), %rdi
movl $0x1, %esi
callq 0xef50
movb %al, -0x30b(%rbp)
jmp 0x71493
movb -0x30b(%rbp), %al
testb $0x1, %al
jne 0x7149f
jmp 0x714b6
movq -0x2b8(%rbp), %rdi
leaq 0xd0f5c(%rip), %rsi     # 0x142409
callq 0x11210
jmp 0x714b4
jmp 0x714b6
leaq -0x50(%rbp), %rdi
movl $0x3, %esi
callq 0xef50
movb %al, -0x30c(%rbp)
jmp 0x714cc
movb -0x30c(%rbp), %al
testb $0x1, %al
jne 0x714d8
jmp 0x714fd
movq -0x2b8(%rbp), %rdi
leaq 0xd0f5e(%rip), %rsi     # 0x142444
leaq 0xd5b5c(%rip), %rcx     # 0x147049
leaq -0xa4(%rbp), %rdx
callq 0xa0b10
jmp 0x714fb
jmp 0x714fd
leaq -0x50(%rbp), %rdi
movl $0x4, %esi
callq 0xef50
movb %al, -0x30d(%rbp)
jmp 0x71513
movb -0x30d(%rbp), %al
testb $0x1, %al
jne 0x7151f
jmp 0x71544
movq -0x2b8(%rbp), %rdi
leaq 0xd0f2f(%rip), %rsi     # 0x14245c
leaq 0xd5b15(%rip), %rcx     # 0x147049
leaq -0xa0(%rbp), %rdx
callq 0xa0b10
jmp 0x71542
jmp 0x71544
movb $0x1, %al
cmpl $0x2, -0x10(%rbp)
movb %al, -0x30e(%rbp)
je 0x7155f
cmpl $0x1, -0x10(%rbp)
sete %al
movb %al, -0x30e(%rbp)
movb -0x30e(%rbp), %al
andb $0x1, %al
movb %al, -0x1d1(%rbp)
testb $0x1, -0x9a(%rbp)
je 0x718cf
cmpl $0x3, -0xc(%rbp)
jne 0x7163e
movq -0x98(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x31c(%rbp)
leaq -0x1f9(%rbp), %rdi
movq %rdi, -0x318(%rbp)
callq 0x7650
movq -0x318(%rbp), %rdx
leaq 0xd0e68(%rip), %rsi     # 0x14241d
leaq -0x1f8(%rbp), %rdi
callq 0x7520
jmp 0x715c3
movl -0x31c(%rbp), %esi
movq -0x2b8(%rbp), %rdi
leaq -0x1f8(%rbp), %rdx
callq 0x22cd0
jmp 0x715de
leaq -0x1f8(%rbp), %rdi
callq 0x7708
leaq -0x1f9(%rbp), %rdi
callq 0x73d0
jmp 0x71701
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x7162d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0x7708
leaq -0x1f9(%rbp), %rdi
callq 0x73d0
jmp 0x7191c
cmpl $0x1, -0xc(%rbp)
jne 0x716ff
movq -0x98(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x32c(%rbp)
leaq -0x221(%rbp), %rdi
movq %rdi, -0x328(%rbp)
callq 0x7650
movq -0x328(%rbp), %rdx
leaq 0xd0dab(%rip), %rsi     # 0x142424
leaq -0x220(%rbp), %rdi
callq 0x7520
jmp 0x71687
movl -0x32c(%rbp), %esi
movq -0x2b8(%rbp), %rdi
leaq -0x220(%rbp), %rdx
callq 0x22cd0
jmp 0x716a2
leaq -0x220(%rbp), %rdi
callq 0x7708
leaq -0x221(%rbp), %rdi
callq 0x73d0
jmp 0x716ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x716ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x7708
leaq -0x221(%rbp), %rdi
callq 0x73d0
jmp 0x7191c
jmp 0x71701
cmpl $0x1, -0x10(%rbp)
jne 0x717f8
cmpl $0x3, -0xc(%rbp)
jne 0x717f8
movq -0x2b8(%rbp), %rsi
movq -0x98(%rbp), %rax
movl 0x8(%rax), %edx
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0x268(%rbp), %rdi
movl $0x1, %ecx
callq *%rax
jmp 0x7173d
movq -0x2b8(%rbp), %rdi
callq 0x25150
movq %rax, -0x338(%rbp)
jmp 0x71752
movq -0x338(%rbp), %rcx
addq $0xb4, %rcx
leaq 0xdbc41(%rip), %rdx     # 0x14d3a8
leaq 0xd6937(%rip), %r8      # 0x1480a5
leaq -0x248(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0xa0c40
jmp 0x71783
movq -0x2b8(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x61240
jmp 0x71798
leaq -0x248(%rbp), %rdi
callq 0x7708
leaq -0x268(%rbp), %rdi
callq 0x7708
jmp 0x718cd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x717e7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x7708
leaq -0x268(%rbp), %rdi
callq 0x7708
jmp 0x7191c
movq -0x2b8(%rbp), %rsi
movq -0x98(%rbp), %rax
movl 0x8(%rax), %edx
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0x2a8(%rbp), %rdi
movl $0x1, %ecx
callq *%rax
jmp 0x71820
movb -0x1d1(%rbp), %dl
leaq 0xd0bfd(%rip), %rcx     # 0x14242a
leaq 0xd0c0c(%rip), %rax     # 0x142440
testb $0x1, %dl
cmovneq %rcx, %rax
movq %rax, -0x2b0(%rbp)
leaq -0x288(%rbp), %rdi
leaq -0x2a8(%rbp), %rsi
leaq -0x2b0(%rbp), %rdx
callq 0xa0d00
jmp 0x7185e
movq -0x2b8(%rbp), %rdi
leaq -0x288(%rbp), %rsi
callq 0x61240
jmp 0x71873
leaq -0x288(%rbp), %rdi
callq 0x7708
leaq -0x2a8(%rbp), %rdi
callq 0x7708
jmp 0x718cd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x718bf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x7708
leaq -0x2a8(%rbp), %rdi
callq 0x7708
jmp 0x7191c
jmp 0x718df
movq -0x2b8(%rbp), %rdi
callq 0x5c350
jmp 0x718dd
jmp 0x718df
movq -0x2b8(%rbp), %rdi
leaq 0xccfb1(%rip), %rsi     # 0x13e89e
callq 0xedf0
jmp 0x718f4
movl $0x0, -0x164(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xf250
leaq -0x50(%rbp), %rdi
callq 0xf250
addq $0x340, %rsp            # imm = 0x340
popq %rbp
retq
leaq -0x90(%rbp), %rdi
callq 0xf250
leaq -0x50(%rbp), %rdi
callq 0xf250
movq -0xd0(%rbp), %rdi
callq 0x7640
nopl (%rax)