// File: /workspace/github_cpp_makefile_star2_O3/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, %r12d
movq %rdx, %r15
movq %rdi, %r14
movb $0x1, %bl
leal -0x39(%rsi), %eax
cmpl $0x9, %eax
ja 0x19d12
leaq 0x9ad62(%rip), %rcx     # 0xb49c4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x4, %r12d
jb 0x19dd5
movq 0x8(%r14), %rdi
movl 0x8(%r15), %esi
callq 0xc7b4
testq %rax, %rax
je 0x1a119
movq %rax, %rbx
movl 0x8(%r15), %ebp
movq 0x8(%r14), %rdi
movl %ebp, %esi
callq 0xc7b4
movq 0x8(%r14), %r13
testq %rax, %rax
je 0x19ed8
imulq $0x148, %rbp, %rbp      # imm = 0x148
addq 0x38(%r13), %rbp
cmpb $0x1, 0xcc(%rbp)
jne 0x19ed8
movl 0xc(%rax), %esi
movq %r13, %rdi
callq 0xa3ea
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(%rbp), %edx
movl %edx, 0x4(%rsp)
cmpq $0x3f, %rdx
ja 0x19ea6
movl $0x1, %eax
movl %edx, %ecx
shlq %cl, %rax
orq %rax, (%rdi)
jmp 0x19ec2
cmpl $0x53, %esi
jne 0x1a119
cmpl $0x3, %r12d
jb 0x19dd5
movl 0x8(%r15), %r15d
jmp 0x19d37
testl %r12d, %r12d
je 0x19dd5
movl (%r15), %r15d
movq 0x8(%r14), %rdi
jmp 0x1a078
cmpl $0x2, %r12d
jb 0x19dd5
movl (%r15), %r12d
movq 0x8(%r14), %rdi
movl %r12d, %esi
callq 0xc7b4
movq 0x8(%r14), %rdi
testq %rax, %rax
je 0x1a074
imulq $0x148, %r12, %r12      # imm = 0x148
addq 0x38(%rdi), %r12
cmpb $0x1, 0xcc(%r12)
jne 0x1a074
movl 0xc(%rax), %esi
callq 0xa3ea
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), %edx
movl %edx, 0x4(%rsp)
cmpq $0x3f, %rdx
ja 0x1a040
movl $0x1, %eax
movl %edx, %ecx
shlq %cl, %rax
orq %rax, (%rdi)
jmp 0x1a05e
cmpl $0x3, %r12d
jae 0x19ddc
xorl %ebx, %ebx
jmp 0x1a119
addl $-0x3, %r12d
je 0x1a119
movl %r12d, %ebx
xorl %r13d, %r13d
movl 0xc(%r15,%r13,4), %r12d
movq 0x8(%r14), %rdi
movl %r12d, %esi
callq 0xc7b4
testq %rax, %rax
je 0x19e95
movq 0x8(%r14), %rdi
imulq $0x148, %r12, %rbp      # imm = 0x148
addq 0x38(%rdi), %rbp
cmpb $0x1, 0xcc(%rbp)
jne 0x19e95
movl 0xc(%rax), %esi
callq 0xa3ea
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), %edx
movl %edx, 0x4(%rsp)
cmpq $0x3f, %rdx
ja 0x19e65
movl $0x1, %eax
movl %edx, %ecx
shlq %cl, %rax
orq %rax, (%rdi)
jmp 0x19e83
addq $0x8, %rdi
movq %rdi, 0x8(%rsp)
leaq 0x4(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x1f510
movl 0xa0(%rbp), %edx
addq $0x60, %rbp
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rcx
callq 0x199dc
incq %r13
cmpq %r13, %rbx
jne 0x19dec
jmp 0x1a039
addq $0x8, %rdi
leaq 0x8(%rsp), %rdx
movq %rdi, (%rdx)
leaq 0x4(%rsp), %rsi
callq 0x1f510
movl 0xa0(%rbp), %edx
addq $0x60, %rbp
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rcx
callq 0x199dc
movq 0x8(%r14), %r13
movq %r15, 0x20(%rsp)
movl 0xc(%rbx), %esi
movq %r13, %rdi
callq 0x1b382
movq %r13, %rdi
movq %rax, %rsi
callq 0x14ac0
movq %rax, %rbp
xorl %eax, %eax
cmpl $0x1, 0x64(%rbp)
setne %al
movq 0x8(%r14), %rcx
shll $0x6, %eax
leaq (%rax,%rcx), %rdx
addq $0x2d0, %rdx            # imm = 0x2D0
movq %rdx, 0x18(%rsp)
addl $-0x3, %r12d
addq %rcx, %rax
addq $0x2d8, %rax            # imm = 0x2D8
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
movq 0x20(%rbp), %rax
cmpq 0x28(%rbp), %rax
je 0x19f47
movq 0x8(%r14), %rdi
addq $0x9c, %rbp
movq %rbp, %r15
jmp 0x1a022
cmpl $0xc, 0xc(%rbp)
jne 0x1a039
movq 0x8(%r14), %rdi
movq 0x20(%rsp), %rax
movl 0xc(%rax,%rbx,4), %esi
callq 0x1c036
movl 0x10(%rax), %r15d
movq 0x8(%r14), %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
shrq $0x4, %rax
imull $0xc4ec4ec5, %eax, %eax # imm = 0xC4EC4EC5
cmpl %eax, %r15d
jae 0x1a01a
imulq $0xd0, %r15, %rax
cmpb $0x1, 0xcc(%r13,%rax)
jne 0x1a01a
addq %rax, %r13
movl 0xa0(%r13), %ecx
movl %ecx, 0x4(%rsp)
cmpq $0x3f, %rcx
ja 0x19fd3
movl $0x1, %eax
shlq %cl, %rax
movq 0x18(%rsp), %rcx
orq %rax, (%rcx)
jmp 0x19ff0
movq 0x10(%rsp), %rdi
movq %rdi, 0x8(%rsp)
leaq 0x4(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x1f510
movq 0x8(%r14), %rdi
movq 0x68(%rbp), %rax
movl (%rax,%r15,4), %esi
callq 0xa3ea
movl 0xa0(%r13), %edx
addq $0x60, %r13
movq %r14, %rdi
movq %rax, %rsi
movq %r13, %rcx
callq 0x199dc
movq 0x8(%r14), %rdi
shlq $0x2, %r15
addq 0x68(%rbp), %r15
movl (%r15), %esi
callq 0xa3ea
movq %rax, %rbp
incq %rbx
cmpq %rbx, %r12
jne 0x19f2a
movb $0x1, %bl
jmp 0x1a119
addq $0x8, %rdi
leaq 0x8(%rsp), %rdx
movq %rdi, (%rdx)
leaq 0x4(%rsp), %rsi
callq 0x1f510
movl 0xa0(%r12), %edx
addq $0x60, %r12
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rcx
callq 0x199dc
movq 0x8(%r14), %rdi
movl 0x4(%r15), %r15d
movl %r15d, %esi
callq 0xc7b4
testq %rax, %rax
je 0x1a119
movq 0x8(%r14), %rdi
imulq $0x148, %r15, %r15      # imm = 0x148
addq 0x38(%rdi), %r15
cmpb $0x1, 0xcc(%r15)
jne 0x1a119
movl 0xc(%rax), %esi
callq 0xa3ea
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), %edx
movl %edx, 0x4(%rsp)
cmpq $0x3f, %rdx
ja 0x1a0ea
movl $0x1, %eax
movl %edx, %ecx
shlq %cl, %rax
orq %rax, (%rdi)
jmp 0x1a107
addq $0x8, %rdi
leaq 0x8(%rsp), %rdx
movq %rdi, (%rdx)
leaq 0x4(%rsp), %rsi
callq 0x1f510
movl 0xa0(%r15), %edx
addq $0x60, %r15
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x199dc
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq