// File: rossy[P]crossc/SPIRV-Cross/spirv_cross.cpp
// Function: spirv_cross::Compiler::CombinedImageSamplerHandler::end_function_scope(unsigned int const*, unsigned int)
bool Compiler::CombinedImageSamplerHandler::end_function_scope(const uint32_t *args, uint32_t length)
{
	if (length < 3)
		return false;

	auto &callee = compiler.get<SPIRFunction>(args[2]);
	args += 3;

	// There are two types of cases we have to handle,
	// a callee might call sampler2D(texture2D, sampler) directly where
	// one or more parameters originate from parameters.
	// Alternatively, we need to provide combined image samplers to our callees,
	// and in this case we need to add those as well.

	pop_remap_parameters();

	// Our callee has now been processed at least once.
	// No point in doing it again.
	callee.do_combined_parameters = false;

	auto &params = functions.top()->combined_parameters;
	functions.pop();
	if (functions.empty())
		return true;

	auto &caller = *functions.top();
	if (caller.do_combined_parameters)
	{
		for (auto &param : params)
		{
			uint32_t image_id = param.global_image ? param.image_id : args[param.image_id];
			uint32_t sampler_id = param.global_sampler ? param.sampler_id : args[param.sampler_id];

			auto *i = compiler.maybe_get_backing_variable(image_id);
			auto *s = compiler.maybe_get_backing_variable(sampler_id);
			if (i)
				image_id = i->self;
			if (s)
				sampler_id = s->self;

			register_combined_image_sampler(caller, image_id, sampler_id, param.depth);
		}
	}

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpl $0x3, -0x1c(%rbp)
jae 0x2656d
movb $0x0, -0x1(%rbp)
jmp 0x2672d
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x106d0
movq -0x78(%rbp), %rdi
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x18(%rbp)
callq 0x26490
movq -0x78(%rbp), %rdi
movq -0x28(%rbp), %rax
movb $0x0, 0xca(%rax)
addq $0x60, %rdi
callq 0x30a20
movq -0x78(%rbp), %rdi
movq (%rax), %rax
addq $0x80, %rax
movq %rax, -0x30(%rbp)
addq $0x60, %rdi
callq 0x30a40
movq -0x78(%rbp), %rdi
addq $0x60, %rdi
callq 0x30a60
testb $0x1, %al
jne 0x265df
jmp 0x265e8
movb $0x1, -0x1(%rbp)
jmp 0x2672d
movq -0x78(%rbp), %rdi
addq $0x60, %rdi
callq 0x30a20
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
testb $0x1, 0xca(%rax)
je 0x26729
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x30a80
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
callq 0x30ab0
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x30ae0
testb $0x1, %al
jne 0x26645
jmp 0x26727
leaq -0x48(%rbp), %rdi
callq 0x30b20
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
testb $0x1, 0xc(%rax)
je 0x26668
movq -0x58(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x7c(%rbp)
jmp 0x26679
movq -0x18(%rbp), %rax
movq -0x58(%rbp), %rcx
movl 0x4(%rcx), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x58(%rbp), %rax
testb $0x1, 0xd(%rax)
je 0x26695
movq -0x58(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x80(%rbp)
jmp 0x266a6
movq -0x18(%rbp), %rax
movq -0x58(%rbp), %rcx
movl 0x8(%rcx), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x80(%rbp)
movq -0x78(%rbp), %rax
movl -0x80(%rbp), %ecx
movl %ecx, -0x60(%rbp)
movq 0x8(%rax), %rdi
movl -0x5c(%rbp), %esi
callq 0x1fa90
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, -0x68(%rbp)
movq 0x8(%rax), %rdi
movl -0x60(%rbp), %esi
callq 0x1fa90
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x266e8
movq -0x68(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x5c(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x266f9
movq -0x70(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x78(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x5c(%rbp), %edx
movl -0x60(%rbp), %ecx
movq -0x58(%rbp), %rax
movb 0xe(%rax), %al
andb $0x1, %al
movzbl %al, %r8d
callq 0x26740
leaq -0x48(%rbp), %rdi
callq 0x30b40
jmp 0x2662f
jmp 0x26729
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)