// File: rossy[P]crossc/SPIRV-Cross/spirv_cross.cpp
// Function: spirv_cross::Compiler::CombinedImageSamplerHandler::register_combined_image_sampler(spirv_cross::SPIRFunction&, unsigned int, unsigned int, bool)
void Compiler::CombinedImageSamplerHandler::register_combined_image_sampler(SPIRFunction &caller, uint32_t image_id,
                                                                            uint32_t sampler_id, bool depth)
{
	// We now have a texture ID and a sampler ID which will either be found as a global
	// or a parameter in our own function. If both are global, they will not need a parameter,
	// otherwise, add it to our list.
	SPIRFunction::CombinedImageSamplerParameter param = {
		0u, image_id, sampler_id, true, true, depth,
	};

	auto texture_itr = find_if(begin(caller.arguments), end(caller.arguments),
	                           [image_id](const SPIRFunction::Parameter &p) { return p.id == image_id; });
	auto sampler_itr = find_if(begin(caller.arguments), end(caller.arguments),
	                           [sampler_id](const SPIRFunction::Parameter &p) { return p.id == sampler_id; });

	if (texture_itr != end(caller.arguments))
	{
		param.global_image = false;
		param.image_id = uint32_t(texture_itr - begin(caller.arguments));
	}

	if (sampler_itr != end(caller.arguments))
	{
		param.global_sampler = false;
		param.sampler_id = uint32_t(sampler_itr - begin(caller.arguments));
	}

	if (param.global_image && param.global_sampler)
		return;

	auto itr = find_if(begin(caller.combined_parameters), end(caller.combined_parameters),
	                   [&param](const SPIRFunction::CombinedImageSamplerParameter &p) {
		                   return param.image_id == p.image_id && param.sampler_id == p.sampler_id &&
		                          param.global_image == p.global_image && param.global_sampler == p.global_sampler;
	                   });

	if (itr == end(caller.combined_parameters))
	{
		uint32_t id = compiler.ir.increase_bound_by(3);
		auto type_id = id + 0;
		auto ptr_type_id = id + 1;
		auto combined_id = id + 2;
		auto &base = compiler.expression_type(image_id);
		auto &type = compiler.set<SPIRType>(type_id);
		auto &ptr_type = compiler.set<SPIRType>(ptr_type_id);

		type = base;
		type.self = type_id;
		type.basetype = SPIRType::SampledImage;
		type.pointer = false;
		type.storage = StorageClassGeneric;
		type.image.depth = depth;

		ptr_type = type;
		ptr_type.pointer = true;
		ptr_type.storage = StorageClassUniformConstant;

		// Build new variable.
		compiler.set<SPIRVariable>(combined_id, ptr_type_id, StorageClassFunction, 0);

		// Inherit RelaxedPrecision (and potentially other useful flags if deemed relevant).
		auto &new_flags = compiler.ir.meta[combined_id].decoration.decoration_flags;
		auto &old_flags = compiler.ir.meta[sampler_id].decoration.decoration_flags;
		new_flags.reset();
		if (old_flags.get(DecorationRelaxedPrecision))
			new_flags.set(DecorationRelaxedPrecision);

		param.id = combined_id;

		compiler.set_name(combined_id,
		                  join("SPIRV_Cross_Combined", compiler.to_name(image_id), compiler.to_name(sampler_id)));

		caller.combined_parameters.push_back(param);
		caller.shadow_arguments.push_back({ ptr_type_id, combined_id, 0u, 0u, true });
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movb %r8b, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x180(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x24(%rbp)
movb $0x1, -0x20(%rbp)
movb $0x1, -0x1f(%rbp)
movb -0x19(%rbp), %al
andb $0x1, %al
movb %al, -0x1e(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x30b60
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x30b90
movq %rax, -0x48(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0x4c(%rbp), %edx
callq 0x26ce0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x30b60
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x30b90
movq %rax, -0x68(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
movl -0x6c(%rbp), %edx
callq 0x26d40
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x30b90
movq %rax, -0x78(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x11610
testb $0x1, %al
jne 0x2682b
jmp 0x26850
movb $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x30b60
movq %rax, -0x80(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x30bc0
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x30b90
movq %rax, -0x88(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x11610
testb $0x1, %al
jne 0x2687a
jmp 0x268a5
movb $0x0, -0x1f(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x30b60
movq %rax, -0x90(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x30bc0
movl %eax, -0x24(%rbp)
testb $0x1, -0x20(%rbp)
je 0x268b6
testb $0x1, -0x1f(%rbp)
je 0x268b6
jmp 0x26cc1
movq -0x10(%rbp), %rdi
addq $0x80, %rdi
callq 0x30c10
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x80, %rdi
callq 0x30c40
movq %rax, -0xa8(%rbp)
leaq -0x2c(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
callq 0x26da0
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rdi
addq $0x80, %rdi
callq 0x30c40
movq %rax, -0xb8(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x30c70
testb $0x1, %al
jne 0x26943
jmp 0x26cc1
movq -0x180(%rbp), %rax
movq 0x8(%rax), %rdi
addq $0x8, %rdi
movl $0x3, %esi
callq 0x55780
movl %eax, %ecx
movq -0x180(%rbp), %rax
movl %ecx, -0xbc(%rbp)
movl -0xbc(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, -0xc0(%rbp)
movl -0xbc(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0xc4(%rbp)
movl -0xbc(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, -0xc8(%rbp)
movq 0x8(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x1f760
movq %rax, %rcx
movq -0x180(%rbp), %rax
movq %rcx, -0xd0(%rbp)
movq 0x8(%rax), %rdi
movl -0xc0(%rbp), %esi
callq 0x30cb0
movq %rax, %rcx
movq -0x180(%rbp), %rax
movq %rcx, -0xd8(%rbp)
movq 0x8(%rax), %rdi
movl -0xc4(%rbp), %esi
callq 0x30cb0
movq %rax, -0xe0(%rbp)
movq -0xd0(%rbp), %rsi
movq -0xd8(%rbp), %rdi
callq 0x2f600
movl -0xc0(%rbp), %ecx
movq -0xd8(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0xd8(%rbp), %rax
movl $0xe, 0xc(%rax)
movq -0xd8(%rbp), %rax
movb $0x0, 0x60(%rax)
movq -0xd8(%rbp), %rax
movl $0x8, 0x64(%rax)
movb -0x19(%rbp), %cl
movq -0xd8(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x88(%rax)
movq -0xd8(%rbp), %rsi
movq -0xe0(%rbp), %rdi
callq 0x2f600
movq -0x180(%rbp), %rax
movq -0xe0(%rbp), %rcx
movb $0x1, 0x60(%rcx)
movq -0xe0(%rbp), %rcx
movl $0x0, 0x64(%rcx)
movq 0x8(%rax), %rdi
movl -0xc8(%rbp), %esi
movl $0x7, -0xe4(%rbp)
movl $0x0, -0xe8(%rbp)
leaq -0xc4(%rbp), %rdx
leaq -0xe4(%rbp), %rcx
leaq -0xe8(%rbp), %r8
callq 0x30d00
movq -0x180(%rbp), %rax
movq 0x8(%rax), %rdi
addq $0x8, %rdi
addq $0x30, %rdi
movl -0xc8(%rbp), %eax
movl %eax, %esi
callq 0xead0
movq %rax, %rcx
movq -0x180(%rbp), %rax
addq $0x60, %rcx
movq %rcx, -0xf0(%rbp)
movq 0x8(%rax), %rdi
addq $0x8, %rdi
addq $0x30, %rdi
movl -0x18(%rbp), %eax
movl %eax, %esi
callq 0xead0
addq $0x60, %rax
movq %rax, -0xf8(%rbp)
movq -0xf0(%rbp), %rdi
callq 0x30d60
movq -0xf8(%rbp), %rdi
xorl %esi, %esi
callq 0xef50
testb $0x1, %al
jne 0x26b29
jmp 0x26b37
movq -0xf0(%rbp), %rdi
xorl %esi, %esi
callq 0x300d0
movq -0x180(%rbp), %rax
movl -0xc8(%rbp), %ecx
movl %ecx, -0x2c(%rbp)
movq 0x8(%rax), %rsi
movq %rsi, -0x190(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0x188(%rbp)
movl -0x14(%rbp), %edx
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0x138(%rbp), %rdi
movl $0x1, %ecx
movl %ecx, -0x184(%rbp)
callq *%rax
movq -0x180(%rbp), %rax
movl -0x184(%rbp), %ecx
movq 0x8(%rax), %rsi
movl -0x18(%rbp), %edx
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0x158(%rbp), %rdi
callq *%rax
jmp 0x26ba2
leaq 0x118bb7(%rip), %rsi    # 0x13f760
leaq -0x118(%rbp), %rdi
leaq -0x138(%rbp), %rdx
leaq -0x158(%rbp), %rcx
callq 0x30d90
jmp 0x26bc5
movl -0x188(%rbp), %esi
movq -0x190(%rbp), %rdi
leaq -0x118(%rbp), %rdx
callq 0x22cd0
jmp 0x26be0
leaq -0x118(%rbp), %rdi
callq 0x7708
leaq -0x158(%rbp), %rdi
callq 0x7708
leaq -0x138(%rbp), %rdi
callq 0x7708
movq -0x10(%rbp), %rdi
addq $0x80, %rdi
leaq -0x2c(%rbp), %rsi
callq 0x30e50
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
movl -0xc4(%rbp), %eax
movl %eax, -0x178(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0x174(%rbp)
movl $0x0, -0x170(%rbp)
movl $0x0, -0x16c(%rbp)
movb $0x1, -0x168(%rbp)
leaq -0x178(%rbp), %rsi
callq 0x30ec0
jmp 0x26cc1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x160(%rbp)
movl %eax, -0x164(%rbp)
jmp 0x26cb3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x160(%rbp)
movl %eax, -0x164(%rbp)
jmp 0x26ca7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x160(%rbp)
movl %eax, -0x164(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x7708
leaq -0x158(%rbp), %rdi
callq 0x7708
leaq -0x138(%rbp), %rdi
callq 0x7708
jmp 0x26cca
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x160(%rbp), %rdi
callq 0x7640
nopw %cs:(%rax,%rax)