// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movl %ecx, %ebp
movq %rdx, %r15
movq %rdi, %r14
movb $0x1, %bl
leal -0x39(%rsi), %eax
cmpl $0x9, %eax
ja 0x1a37d
leaq 0x9b6d3(%rip), %rcx     # 0xb59a4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x4, %ebp
jb 0x1a439
movq 0x8(%r14), %rdi
movl 0x8(%r15), %esi
callq 0xc7f4
testq %rax, %rax
je 0x1a43b
movq %rax, %r12
movl 0x8(%r15), %r13d
movq 0x8(%r14), %rdi
movl %r13d, %esi
callq 0xc7f4
testq %rax, %rax
je 0x1a544
movq 0x8(%r14), %rdi
imulq $0x148, %r13, %r13      # imm = 0x148
addq 0x38(%rdi), %r13
cmpb $0x1, 0xcc(%r13)
jne 0x1a544
movl 0xc(%rax), %esi
callq 0xa426
movq %rax, %rsi
xorl %eax, %eax
cmpl $0x1, 0x64(%rsi)
setne %al
movq 0x8(%r14), %rcx
shll $0x6, %eax
leaq (%rax,%rcx), %rdi
addq $0x2d0, %rdi            # imm = 0x2D0
movl 0xa0(%r13), %ecx
movl %ecx, 0x4(%rsp)
cmpq $0x3f, %rcx
ja 0x1a50e
movl $0x1, %eax
shlq %cl, %rax
orq %rax, (%rdi)
jmp 0x1a52e
cmpl $0x53, %esi
jne 0x1a43b
cmpl $0x3, %ebp
jb 0x1a439
movl 0x8(%r15), %r15d
jmp 0x1a6ec
testl %ebp, %ebp
je 0x1a439
movl (%r15), %r15d
jmp 0x1a6ec
cmpl $0x2, %ebp
jb 0x1a439
movl (%r15), %r12d
movq 0x8(%r14), %rdi
movl %r12d, %esi
callq 0xc7f4
testq %rax, %rax
je 0x1a6e8
movq 0x8(%r14), %rdi
imulq $0x148, %r12, %r12      # imm = 0x148
addq 0x38(%rdi), %r12
cmpb $0x1, 0xcc(%r12)
jne 0x1a6e8
movl 0xc(%rax), %esi
callq 0xa426
movq %rax, %r13
xorl %eax, %eax
cmpl $0x1, 0x64(%r13)
setne %al
movq 0x8(%r14), %rcx
shll $0x6, %eax
leaq (%rax,%rcx), %rdi
addq $0x2d0, %rdi            # imm = 0x2D0
movl 0xa0(%r12), %ecx
movl %ecx, 0x4(%rsp)
cmpq $0x3f, %rcx
ja 0x1a6b8
movl $0x1, %eax
shlq %cl, %rax
orq %rax, (%rdi)
jmp 0x1a6ce
cmpl $0x3, %ebp
jae 0x1a44c
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl $-0x3, %ebp
je 0x1a43b
movl %ebp, %ebx
xorl %r13d, %r13d
movl 0xc(%r15,%r13,4), %r12d
movq 0x8(%r14), %rdi
movl %r12d, %esi
callq 0xc7f4
testq %rax, %rax
je 0x1a4fd
movq 0x8(%r14), %rdi
imulq $0x148, %r12, %rbp      # imm = 0x148
addq 0x38(%rdi), %rbp
cmpb $0x1, 0xcc(%rbp)
jne 0x1a4fd
movl 0xc(%rax), %esi
callq 0xa426
movq %rax, %r12
xorl %eax, %eax
cmpl $0x1, 0x64(%r12)
setne %al
movq 0x8(%r14), %rcx
shll $0x6, %eax
leaq (%rax,%rcx), %rdi
addq $0x2d0, %rdi            # imm = 0x2D0
movl 0xa0(%rbp), %ecx
movl %ecx, 0x4(%rsp)
cmpq $0x3f, %rcx
ja 0x1a4cd
movl $0x1, %eax
shlq %cl, %rax
orq %rax, (%rdi)
jmp 0x1a4e5
addq $0x8, %rdi
movq %rdi, 0x8(%rsp)
leaq 0x4(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x1fc02
movl 0xa0(%rbp), %edx
addq $0x60, %rbp
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rcx
callq 0x1a048
incq %r13
cmpq %r13, %rbx
jne 0x1a456
jmp 0x1a6b1
addq $0x8, %rdi
leaq 0x8(%rsp), %rdx
movq %rdi, (%rdx)
movq %rsi, 0x10(%rsp)
leaq 0x4(%rsp), %rsi
callq 0x1fc02
movq 0x10(%rsp), %rsi
movl 0xa0(%r13), %edx
addq $0x60, %r13
movq %r14, %rdi
movq %r13, %rcx
callq 0x1a048
movq 0x8(%r14), %r13
movl 0xc(%r12), %esi
movq %r13, %rdi
callq 0x1b9de
movq %r13, %rdi
movq %rax, %rsi
callq 0x14c4e
movq %rax, %r12
xorl %eax, %eax
cmpl $0x1, 0x64(%r12)
setne %cl
addl $-0x3, %ebp
je 0x1a43b
movq %r15, 0x10(%rsp)
movq 0x8(%r14), %rdx
movb %cl, %al
shll $0x6, %eax
addq %rdx, %rax
addq $0x2d0, %rax            # imm = 0x2D0
movq %rax, 0x20(%rsp)
addq $0x8, %rax
movq %rax, 0x18(%rsp)
movl %ebp, %r13d
xorl %ebx, %ebx
movq 0x20(%r12), %rax
cmpq 0x28(%r12), %rax
je 0x1a5bc
addq $0x9c, %r12
movq %r12, %r15
jmp 0x1a696
cmpl $0xc, 0xc(%r12)
jne 0x1a6b1
movq 0x8(%r14), %rdi
movq 0x10(%rsp), %rax
movl 0xc(%rax,%rbx,4), %esi
callq 0x1c66e
movl 0x10(%rax), %r15d
movq 0x8(%r14), %rax
movl 0x8(%r12), %ecx
movq 0x38(%rax), %rax
imulq $0x148, %rcx, %rcx      # imm = 0x148
movq 0xd0(%rax,%rcx), %rbp
movq 0xd8(%rax,%rcx), %rax
subq %rbp, %rax
shrq $0x4, %rax
imull $0xc4ec4ec5, %eax, %eax # imm = 0xC4EC4EC5
cmpl %eax, %r15d
jae 0x1a68d
imulq $0xd0, %r15, %rax
cmpb $0x1, 0xcc(%rbp,%rax)
jne 0x1a68d
addq %rax, %rbp
movl 0xa0(%rbp), %ecx
movl %ecx, 0x4(%rsp)
cmpq $0x3f, %rcx
ja 0x1a64a
movl $0x1, %eax
shlq %cl, %rax
movq 0x20(%rsp), %rcx
orq %rax, (%rcx)
jmp 0x1a663
movq 0x18(%rsp), %rdi
movq %rdi, 0x8(%rsp)
leaq 0x4(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x1fc02
movq 0x8(%r14), %rdi
movq 0x68(%r12), %rax
movl (%rax,%r15,4), %esi
callq 0xa426
movl 0xa0(%rbp), %edx
addq $0x60, %rbp
movq %r14, %rdi
movq %rax, %rsi
movq %rbp, %rcx
callq 0x1a048
shlq $0x2, %r15
addq 0x68(%r12), %r15
movq 0x8(%r14), %rdi
movl (%r15), %esi
callq 0xa426
movq %rax, %r12
incq %rbx
cmpq %rbx, %r13
jne 0x1a5a1
movb $0x1, %bl
jmp 0x1a43b
addq $0x8, %rdi
leaq 0x8(%rsp), %rdx
movq %rdi, (%rdx)
leaq 0x4(%rsp), %rsi
callq 0x1fc02
movl 0xa0(%r12), %edx
addq $0x60, %r12
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rcx
callq 0x1a048
movl 0x4(%r15), %r15d
movq 0x8(%r14), %rdi
movl %r15d, %esi
callq 0xc7f4
testq %rax, %rax
je 0x1a43b
movq 0x8(%r14), %rdi
imulq $0x148, %r15, %r15      # imm = 0x148
addq 0x38(%rdi), %r15
cmpb $0x1, 0xcc(%r15)
jne 0x1a43b
movl 0xc(%rax), %esi
callq 0xa426
movq %rax, %r12
xorl %eax, %eax
cmpl $0x1, 0x64(%r12)
setne %al
movq 0x8(%r14), %rcx
shll $0x6, %eax
leaq (%rax,%rcx), %rdi
addq $0x2d0, %rdi            # imm = 0x2D0
movl 0xa0(%r15), %ecx
movl %ecx, 0x4(%rsp)
cmpq $0x3f, %rcx
ja 0x1a764
movl $0x1, %eax
shlq %cl, %rax
orq %rax, (%rdi)
jmp 0x1a77a
addq $0x8, %rdi
leaq 0x8(%rsp), %rdx
movq %rdi, (%rdx)
leaq 0x4(%rsp), %rsi
callq 0x1fc02
movl 0xa0(%r15), %edx
addq $0x60, %r15
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x1a048
jmp 0x1a43b