// File: /workspace/github_cpp_makefile_star2_O1/rossy[P]crossc/SPIRV-Cross/spirv_hlsl.cpp
// Function: spirv_cross::CompilerHLSL::emit_atomic(unsigned int const*, unsigned int, spv::Op)
void CompilerHLSL::emit_atomic(const uint32_t *ops, uint32_t length, spv::Op op)
{
	const char *atomic_op = nullptr;

	string value_expr;
	if (op != OpAtomicIDecrement && op != OpAtomicIIncrement)
		value_expr = to_expression(ops[op == OpAtomicCompareExchange ? 6 : 5]);

	switch (op)
	{
	case OpAtomicIIncrement:
		atomic_op = "InterlockedAdd";
		value_expr = "1";
		break;

	case OpAtomicIDecrement:
		atomic_op = "InterlockedAdd";
		value_expr = "-1";
		break;

	case OpAtomicISub:
		atomic_op = "InterlockedAdd";
		value_expr = join("-", enclose_expression(value_expr));
		break;

	case OpAtomicSMin:
	case OpAtomicUMin:
		atomic_op = "InterlockedMin";
		break;

	case OpAtomicSMax:
	case OpAtomicUMax:
		atomic_op = "InterlockedMax";
		break;

	case OpAtomicAnd:
		atomic_op = "InterlockedAnd";
		break;

	case OpAtomicOr:
		atomic_op = "InterlockedOr";
		break;

	case OpAtomicXor:
		atomic_op = "InterlockedXor";
		break;

	case OpAtomicIAdd:
		atomic_op = "InterlockedAdd";
		break;

	case OpAtomicExchange:
		atomic_op = "InterlockedExchange";
		break;

	case OpAtomicCompareExchange:
		if (length < 8)
			SPIRV_CROSS_THROW("Not enough data for opcode.");
		atomic_op = "InterlockedCompareExchange";
		value_expr = join(to_expression(ops[7]), ", ", value_expr);
		break;

	default:
		SPIRV_CROSS_THROW("Unknown atomic opcode.");
	}

	uint32_t result_type = ops[0];
	uint32_t id = ops[1];
	forced_temporaries.insert(ops[1]);

	auto &type = get<SPIRType>(result_type);
	statement(variable_decl(type, to_name(id)), ";");

	auto &data_type = expression_type(ops[2]);
	auto *chain = maybe_get<SPIRAccessChain>(ops[2]);
	SPIRType::BaseType expr_type;
	if (data_type.storage == StorageClassImage || !chain)
	{
		statement(atomic_op, "(", to_expression(ops[2]), ", ", value_expr, ", ", to_name(id), ");");
		expr_type = data_type.basetype;
	}
	else
	{
		// RWByteAddress buffer is always uint in its underlying type.
		expr_type = SPIRType::UInt;
		statement(chain->base, ".", atomic_op, "(", chain->dynamic_index, chain->static_index, ", ", value_expr, ", ",
		          to_name(id), ");");
	}

	auto expr = bitcast_expression(type, expr_type, to_name(id));
	set<SPIRExpression>(id, expr, result_type, true);
	flush_all_atomic_capable_variables();
	register_read(ops[1], ops[2], should_forward(ops[2]));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %ecx, %r15d
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
xorl %eax, %eax
movq %rax, (%rsp)
leaq 0x60(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
leal -0xea(%r15), %eax
cmpl $-0x3, %eax
ja 0x6f76e
xorl %eax, %eax
cmpl $0xe6, %r15d
sete %al
movl 0x14(%r14,%rax,4), %edx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x345e6
leaq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0x6310
movq (%r12), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x6f76e
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x6260
addl $0xffffff1b, %r15d      # imm = 0xFFFFFF1B
cmpl $0xd, %r15d
ja 0x6fc23
leaq 0x4cb32(%rip), %rax     # 0xbc2b8
movslq (%rax,%r15,4), %rcx
addq %rax, %rcx
jmpq *%rcx
leaq 0x4fd54(%rip), %rax     # 0xbf4ea
jmp 0x6f84c
leaq 0x4fd57(%rip), %rax     # 0xbf4f9
jmp 0x6f84c
leaq 0x4fd69(%rip), %rax     # 0xbf517
jmp 0x6f84c
leaq 0x4fd21(%rip), %rax     # 0xbf4db
jmp 0x6f84c
leaq 0x4fd5f(%rip), %rax     # 0xbf525
jmp 0x6f84c
leaq 0x4fd36(%rip), %rax     # 0xbf508
jmp 0x6f84c
leaq 0x4fd00(%rip), %rax     # 0xbf4db
movq %rax, (%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x50(%rsp), %rdx
movq %rbx, %rsi
callq 0x33218
leaq 0x4b690(%rip), %rsi     # 0xbae88
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x585b8
jmp 0x6fb6d
movl $0x1, %r8d
leaq 0x4b66b(%rip), %rcx     # 0xbae84
jmp 0x6f828
movl $0x2, %r8d
leaq 0x4b65b(%rip), %rcx     # 0xbae83
leaq 0x4fcac(%rip), %rax     # 0xbf4db
movq %rax, (%rsp)
leaq 0x50(%rsp), %rdi
movq 0x8(%rdi), %rdx
xorl %esi, %esi
callq 0x63f0
jmp 0x6f850
leaq 0x4fce8(%rip), %rax     # 0xbf534
movq %rax, (%rsp)
movl (%r14), %eax
movl %eax, 0x4c(%rsp)
leaq 0x4(%r14), %rsi
movl 0x4(%r14), %ebp
leaq 0x228(%rbx), %rdi
leaq 0x8(%rsp), %rdx
movq %rdi, (%rdx)
callq 0x1fc02
movl 0x4c(%rsp), %esi
movq %rbx, %rdi
callq 0xa426
movq %rax, %r12
movq (%rbx), %rax
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
movl %ebp, %edx
movl $0x1, %ecx
callq *0x38(%rax)
movq (%rbx), %rax
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rcx
movq %rbx, %rsi
movq %r12, %rdx
xorl %r8d, %r8d
callq *0xc8(%rax)
leaq 0x5022f(%rip), %rdx     # 0xbfae9
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0xaa50
leaq 0x18(%rsp), %r15
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0x6f8e2
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x6260
movq %r12, 0x70(%rsp)
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6f902
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x6260
movl 0x8(%r14), %esi
movq %rbx, %rdi
callq 0x12aba
movq %rax, %r12
movl 0x8(%r14), %esi
movq %rbx, %rdi
callq 0x1ba4c
movq %rax, %r13
testq %rax, %rax
je 0x6f9be
cmpl $0xb, 0x64(%r12)
je 0x6f9be
movq (%rbx), %rax
leaq 0x8(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movl $0x1, %ecx
callq *0x38(%rax)
leaq 0x58(%r13), %rax
leaq 0x38(%r13), %r9
addq $0x18, %r13
leaq 0x50188(%rip), %r10     # 0xbfae8
leaq 0x4b754(%rip), %r11     # 0xbb0bb
leaq 0x50(%rsp), %r15
leaq 0x4bee6(%rip), %rdx     # 0xbb859
leaq 0x54327(%rip), %r8      # 0xc3ca1
movq %rsp, %rcx
movq %rbx, %rdi
movq %r13, %rsi
pushq %r10
pushq %r12
pushq %r11
pushq %r15
pushq %r11
pushq %rax
callq 0x7c240
addq $0x30, %rsp
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %r15
cmpq %r15, %rdi
je 0x6f9b3
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x6260
movl $0x5, %r12d
jmp 0x6fa58
movl 0x8(%r14), %edx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x345e6
movq (%rbx), %rax
leaq 0x28(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movl $0x1, %ecx
callq *0x38(%rax)
subq $0x8, %rsp
leaq 0x500f6(%rip), %rax     # 0xbfae8
leaq 0x4b6c2(%rip), %r8      # 0xbb0bb
leaq 0x542a1(%rip), %rdx     # 0xc3ca1
leaq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rcx
leaq 0x58(%rsp), %r9
movq %rbx, %rdi
pushq %rax
pushq %r13
pushq %r8
callq 0x7c0a2
addq $0x20, %rsp
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x6fa3c
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x6260
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x6fa53
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x6260
movl 0xc(%r12), %r12d
movq (%rbx), %rax
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
movl %ebp, %edx
movl $0x1, %ecx
callq *0x38(%rax)
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %r8
movq %rbx, %rsi
movq 0x70(%rsp), %rdx
movl %r12d, %ecx
callq 0x3e3d8
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x6faa3
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x6260
leaq 0x28(%rsp), %r8
movb $0x1, (%r8)
leaq 0x8(%rsp), %rdx
leaq 0x4c(%rsp), %rcx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x7c3dc
movq %rbx, %rdi
callq 0x12fb4
movl 0x4(%r14), %ebp
movl 0x8(%r14), %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x39e96
movzbl %al, %ecx
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x12d38
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x6fb02
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x6260
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x6fb1e
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x6260
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x7, %ebp
jbe 0x6fbbd
leaq 0x4fa27(%rip), %rax     # 0xbf564
movq %rax, (%rsp)
movl 0x1c(%r14), %edx
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x345e6
leaq 0x4b562(%rip), %rdx     # 0xbb0bb
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
leaq 0x50(%rsp), %rcx
callq 0x7bff6
leaq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0x6310
movq (%r15), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x6fb99
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x6260
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6f850
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x6260
jmp 0x6f850
movl $0x10, %edi
callq 0x6140
movq %rax, %r14
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x4f96e(%rip), %rsi     # 0xbf548
leaq 0x4f982(%rip), %rdx     # 0xbf563
leaq 0x8(%rsp), %rdi
callq 0x7c64
movb $0x1, %bpl
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x6320
leaq 0x8d196(%rip), %rax     # 0xfcd98
addq $0x10, %rax
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x8d16e(%rip), %rsi     # 0xfcd80
movq 0x8e3bf(%rip), %rdx     # 0xfdfd8
movq %r14, %rdi
callq 0x6470
jmp 0x6fc87
movl $0x10, %edi
callq 0x6140
movq %rax, %r14
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x4f93f(%rip), %rsi     # 0xbf57f
leaq 0x4f94e(%rip), %rdx     # 0xbf595
leaq 0x8(%rsp), %rdi
callq 0x7c64
movb $0x1, %bpl
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x6320
leaq 0x8d130(%rip), %rax     # 0xfcd98
addq $0x10, %rax
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x8d108(%rip), %rsi     # 0xfcd80
movq 0x8e359(%rip), %rdx     # 0xfdfd8
movq %r14, %rdi
callq 0x6470
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x6fca9
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x6260
jmp 0x6fca9
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0x6fdb8
jmp 0x6fcdf
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x6fcd6
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x6260
jmp 0x6fcd6
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0x6fdb8
movq %r14, %rdi
callq 0x6200
jmp 0x6fdb8
jmp 0x6fd7d
jmp 0x6fd99
jmp 0x6fd7d
jmp 0x6fd99
jmp 0x6fd99
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x6fda1
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x6260
jmp 0x6fda1
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
jne 0x6fdab
jmp 0x6fdb8
jmp 0x6fd9e
jmp 0x6fd99
jmp 0x6fd99
jmp 0x6fd99
jmp 0x6fd9e
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
jmp 0x6fd89
jmp 0x6fd99
jmp 0x6fd99
jmp 0x6fd99
movq %rax, %rbx
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6fd80
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x6260
jmp 0x6fd80
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6fdb8
movq 0x38(%rsp), %rsi
jmp 0x6fdb0
jmp 0x6fd99
jmp 0x6fd99
movq %rax, %rbx
jmp 0x6fdb8
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x6fdb8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x6260
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x6fdd4
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x6260
movq %rbx, %rdi
callq 0x64a0