// File: /workspace/github_cpp_makefile_star2_O2/rossy[P]crossc/SPIRV-Cross/spirv_cross.cpp
// Function: spirv_cross::Compiler::ActiveBuiltinHandler::handle(spv::Op, unsigned int const*, unsigned int)
bool Compiler::ActiveBuiltinHandler::handle(spv::Op opcode, const uint32_t *args, uint32_t length)
{
	const auto add_if_builtin = [&](uint32_t id) {
		// Only handles variables here.
		// Builtins which are part of a block are handled in AccessChain.
		auto *var = compiler.maybe_get<SPIRVariable>(id);
		auto &decorations = compiler.ir.meta[id].decoration;
		if (var && decorations.builtin)
		{
			auto &type = compiler.get<SPIRType>(var->basetype);
			auto &flags =
			    type.storage == StorageClassInput ? compiler.active_input_builtins : compiler.active_output_builtins;
			flags.set(decorations.builtin_type);
			handle_builtin(type, decorations.builtin_type, decorations.decoration_flags);
		}
	};

	switch (opcode)
	{
	case OpStore:
		if (length < 1)
			return false;

		add_if_builtin(args[0]);
		break;

	case OpCopyMemory:
		if (length < 2)
			return false;

		add_if_builtin(args[0]);
		add_if_builtin(args[1]);
		break;

	case OpCopyObject:
	case OpLoad:
		if (length < 3)
			return false;

		add_if_builtin(args[2]);
		break;

	case OpFunctionCall:
	{
		if (length < 3)
			return false;

		uint32_t count = length - 3;
		args += 3;
		for (uint32_t i = 0; i < count; i++)
			add_if_builtin(args[i]);
		break;
	}

	case OpAccessChain:
	case OpInBoundsAccessChain:
	{
		if (length < 4)
			return false;

		// Only consider global variables, cannot consider variables in functions yet, or other
		// access chains as they have not been created yet.
		auto *var = compiler.maybe_get<SPIRVariable>(args[2]);
		if (!var)
			break;

		// Required if we access chain into builtins like gl_GlobalInvocationID.
		add_if_builtin(args[2]);

		// Start traversing type hierarchy at the proper non-pointer types.
		auto *type = &compiler.get_non_pointer_type(var->basetype);

		auto &flags =
		    type->storage == StorageClassInput ? compiler.active_input_builtins : compiler.active_output_builtins;

		uint32_t count = length - 3;
		args += 3;
		for (uint32_t i = 0; i < count; i++)
		{
			// Arrays
			if (!type->array.empty())
			{
				type = &compiler.get<SPIRType>(type->parent_type);
			}
			// Structs
			else if (type->basetype == SPIRType::Struct)
			{
				uint32_t index = compiler.get<SPIRConstant>(args[i]).scalar();

				if (index < uint32_t(compiler.ir.meta[type->self].members.size()))
				{
					auto &decorations = compiler.ir.meta[type->self].members[index];
					if (decorations.builtin)
					{
						flags.set(decorations.builtin_type);
						handle_builtin(compiler.get<SPIRType>(type->member_types[index]), decorations.builtin_type,
						               decorations.decoration_flags);
					}
				}

				type = &compiler.get<SPIRType>(type->member_types[index]);
			}
			else
			{
				// No point in traversing further. We won't find any extra builtins.
				break;
			}
		}
		break;
	}

	default:
		break;
	}

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %r15d
movq %rdx, %r14
movq %rdi, (%rsp)
movb $0x1, %bpl
leal -0x39(%rsi), %eax
cmpl $0x9, %eax
ja 0x15f78
leaq 0x79b68(%rip), %rcx     # 0x8f97c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x4, %r15d
jb 0x15fb8
movq %rdi, %r12
movq 0x8(%rdi), %rdi
movl 0x8(%r14), %esi
callq 0xa9e4
testq %rax, %rax
je 0x15fba
movq %rax, %r13
movq %r14, 0x10(%rsp)
movl 0x8(%r14), %esi
movq %rsp, %rdi
callq 0x15ff0
movq 0x8(%r12), %rdi
movl 0xc(%r13), %esi
callq 0x11e8c
movq %rax, %rbp
xorl %eax, %eax
cmpl $0x1, 0x64(%rbp)
setne %al
movq 0x8(%r12), %rcx
shll $0x6, %eax
addq %rcx, %rax
addq $0x2d0, %rax            # imm = 0x2D0
movq %rax, 0x8(%rsp)
addl $-0x3, %r15d
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x15fcb
movq 0x20(%rbp), %rax
cmpq 0x28(%rbp), %rax
je 0x15eb1
movq 0x8(%r12), %rdi
addq $0x9c, %rbp
movq %rbp, %r14
jmp 0x15f65
cmpl $0xc, 0xc(%rbp)
movq 0x10(%rsp), %rax
jne 0x15fcb
movq 0x8(%r12), %rdi
movl 0xc(%rax,%rbx,4), %esi
callq 0x17bce
movl 0x10(%rax), %r14d
movq 0x8(%r12), %rdi
movl 0x8(%rbp), %eax
movq 0x38(%rdi), %rcx
imulq $0x148, %rax, %rax      # imm = 0x148
movq 0xd0(%rcx,%rax), %r13
movq 0xd8(%rcx,%rax), %rax
subq %r13, %rax
cqto
movl $0xd0, %ecx
idivq %rcx
cmpl %eax, %r14d
jae 0x15f5d
imulq $0xd0, %r14, %rax
cmpb $0x1, 0xcc(%r13,%rax)
jne 0x15f5d
addq %rax, %r13
movl 0xa0(%r13), %esi
movq 0x8(%rsp), %rdi
callq 0x17654
movq 0x8(%r12), %rdi
movq 0x68(%rbp), %rax
movl (%rax,%r14,4), %esi
callq 0x90a4
movl 0xa0(%r13), %edx
addq $0x60, %r13
movq %r12, %rdi
movq %rax, %rsi
movq %r13, %rcx
callq 0x15bf6
movq 0x8(%r12), %rdi
shlq $0x2, %r14
addq 0x68(%rbp), %r14
movl (%r14), %esi
callq 0x90a4
movq %rax, %rbp
incq %rbx
jmp 0x15e8a
cmpl $0x53, %esi
jne 0x15fba
cmpl $0x3, %r15d
jb 0x15fb8
addq $0x8, %r14
jmp 0x15fa5
testl %r15d, %r15d
jne 0x15fa5
jmp 0x15fb8
cmpl $0x2, %r15d
jb 0x15fb8
movl (%r14), %esi
movq %rsp, %rdi
callq 0x15ff0
addq $0x4, %r14
movl (%r14), %esi
movq %rsp, %rdi
callq 0x15ff0
jmp 0x15fba
cmpl $0x3, %r15d
jae 0x15fd0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %bpl
jmp 0x15fba
addl $-0x3, %r15d
xorl %ebx, %ebx
movq %rsp, %r12
cmpq %rbx, %r15
je 0x15fba
movl 0xc(%r14,%rbx,4), %esi
movq %r12, %rdi
callq 0x15ff0
incq %rbx
jmp 0x15fd9