// File: /workspace/github_cpp_makefile_star2_O2/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
andq $0x0, 0x8(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rdi, %rbx
movb $0x0, (%rax)
leal -0xea(%r15), %eax
cmpl $-0x3, %eax
ja 0x59b9c
xorl %eax, %eax
cmpl $0xe6, %r15d
sete %al
movl 0x14(%r14,%rax,4), %edx
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x2b9f0
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x6320
movq %r12, %rdi
callq 0x6548
addl $0xffffff1b, %r15d      # imm = 0xFFFFFF1B
cmpl $0xd, %r15d
ja 0x59f4b
leaq 0x3c684(%rip), %rax     # 0x96238
movslq (%rax,%r15,4), %rcx
addq %rax, %rcx
leaq 0x3f8b1(%rip), %rax     # 0x99473
jmpq *%rcx
leaq 0x3f8b7(%rip), %rax     # 0x99482
jmp 0x59cc1
leaq 0x3f8ba(%rip), %rax     # 0x99491
jmp 0x59cc1
leaq 0x3f8cc(%rip), %rax     # 0x994af
jmp 0x59cc1
leaq 0x3b264(%rip), %rsi     # 0x94e53
jmp 0x59ca9
leaq 0x3f8c2(%rip), %rax     # 0x994bd
jmp 0x59cc1
leaq 0x3f899(%rip), %rax     # 0x994a0
jmp 0x59cc1
movq %rax, 0x8(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rdx
movq %rbx, %rsi
callq 0x2aa76
leaq 0x3b22e(%rip), %rsi     # 0x94e58
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x46531
jmp 0x59c7c
cmpl $0x7, %ebp
jbe 0x59f00
leaq 0x3f8b1(%rip), %rax     # 0x994fc
movq %rax, 0x8(%rsp)
movl 0x1c(%r14), %edx
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x2b9f0
leaq 0x3b423(%rip), %rdx     # 0x9508b
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x58(%rsp), %rcx
callq 0x63366
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x6320
movq %r15, %rdi
callq 0x6548
leaq 0x30(%rsp), %rdi
callq 0x6548
jmp 0x59cc6
leaq 0x3b1ab(%rip), %rsi     # 0x94e54
movq %rax, 0x8(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x64e0
jmp 0x59cc6
leaq 0x3f80b(%rip), %rax     # 0x994cc
movq %rax, 0x8(%rsp)
movl (%r14), %r15d
movl %r15d, 0x54(%rsp)
leaq 0x4(%r14), %rsi
movl 0x4(%r14), %ebp
leaq 0x228(%rbx), %rdi
callq 0x1af30
movq %rbx, %rdi
movl %r15d, %esi
callq 0x90a4
movq %rax, %r15
movq (%rbx), %rax
leaq 0x30(%rsp), %rdi
pushq $0x1
popq %rcx
movq %rbx, %rsi
movl %ebp, %edx
callq *0x38(%rax)
movq (%rbx), %rax
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rcx
movq %rbx, %rsi
movq %r15, %rdx
xorl %r8d, %r8d
callq *0xc8(%rax)
leaq 0x3fd5b(%rip), %rdx     # 0x99a81
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x953a
leaq 0x10(%rsp), %rdi
callq 0x6548
leaq 0x30(%rsp), %rdi
callq 0x6548
movl 0x8(%r14), %esi
movq %rbx, %rdi
callq 0x104b0
movq %rax, %r12
movl 0x8(%r14), %esi
movq %rbx, %rdi
callq 0x17014
movq %rax, %r13
testq %rax, %rax
je 0x59de4
cmpl $0xb, 0x64(%r12)
je 0x59de4
movq (%rbx), %rax
leaq 0x10(%rsp), %r12
pushq $0x1
popq %rcx
movq %r12, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq *0x38(%rax)
leaq 0x58(%r13), %rax
leaq 0x38(%r13), %r9
addq $0x18, %r13
leaq 0x3fce5(%rip), %r10     # 0x99a80
leaq 0x3b2e9(%rip), %r11     # 0x9508b
leaq 0x3ba80(%rip), %rdx     # 0x95829
leaq 0x43e79(%rip), %r8      # 0x9dc29
leaq 0x8(%rsp), %rcx
movq %rbx, %rdi
movq %r13, %rsi
pushq %r10
pushq %r12
pushq %r11
leaq 0x70(%rsp), %r10
pushq %r10
pushq %r11
pushq %rax
callq 0x63518
addq $0x30, %rsp
leaq 0x10(%rsp), %rdi
callq 0x6548
pushq $0x5
popq %r12
jmp 0x59e5d
movl 0x8(%r14), %edx
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x2b9f0
movq (%rbx), %rax
leaq 0x30(%rsp), %r13
pushq $0x1
popq %rcx
movq %r13, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq *0x38(%rax)
subq $0x8, %rsp
leaq 0x3fc6a(%rip), %rax     # 0x99a80
leaq 0x3b26e(%rip), %r8      # 0x9508b
leaq 0x43e05(%rip), %rdx     # 0x9dc29
leaq 0x10(%rsp), %rsi
leaq 0x18(%rsp), %rcx
leaq 0x60(%rsp), %r9
movq %rbx, %rdi
pushq %rax
pushq %r13
pushq %r8
callq 0x633e0
addq $0x20, %rsp
leaq 0x30(%rsp), %rdi
callq 0x6548
leaq 0x10(%rsp), %rdi
callq 0x6548
movl 0xc(%r12), %r12d
movq (%rbx), %rax
leaq 0x30(%rsp), %rdi
pushq $0x1
popq %rcx
movq %rbx, %rsi
movl %ebp, %edx
callq *0x38(%rax)
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %r8
movq %rbx, %rsi
movq %r15, %rdx
movl %r12d, %ecx
callq 0x32752
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x6548
movb $0x1, (%r15)
leaq 0x10(%rsp), %rdx
leaq 0x54(%rsp), %rcx
leaq 0x30(%rsp), %r8
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6368e
movq %rbx, %rdi
callq 0x10912
movl 0x4(%r14), %ebp
movl 0x8(%r14), %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x2f360
movzbl %al, %ecx
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x106dc
leaq 0x10(%rsp), %rdi
callq 0x6548
leaq 0x58(%rsp), %rdi
callq 0x6548
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x6140
movq %rax, %r14
leaq 0x3f5ce(%rip), %rsi     # 0x994e0
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x6100
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x6f6e
xorl %ebp, %ebp
leaq 0x8ce86(%rip), %rsi     # 0xe6dc0
movq 0x8e09f(%rip), %rdx     # 0xe7fe0
movq %r14, %rdi
callq 0x6490
jmp 0x59f94
pushq $0x10
popq %rdi
callq 0x6140
movq %rax, %r14
leaq 0x3f5ba(%rip), %rsi     # 0x99517
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x6100
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x6f6e
xorl %ebp, %ebp
leaq 0x8ce3b(%rip), %rsi     # 0xe6dc0
movq 0x8e054(%rip), %rdx     # 0xe7fe0
movq %r14, %rdi
callq 0x6490
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x6548
testb %bpl, %bpl
je 0x5a031
jmp 0x59fc8
movq %rax, %rbx
jmp 0x59fc8
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x6548
testb %bpl, %bpl
jne 0x59fc8
jmp 0x5a031
movq %rax, %rbx
movq %r14, %rdi
callq 0x6200
jmp 0x5a031
jmp 0x59ff7
jmp 0x5a01f
jmp 0x59ff7
jmp 0x5a01f
jmp 0x5a01f
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x6548
jmp 0x5a027
jmp 0x5a024
jmp 0x5a024
jmp 0x5a01f
jmp 0x5a01f
jmp 0x5a01f
jmp 0x5a024
movq %rax, %rbx
jmp 0x5a014
jmp 0x5a01f
jmp 0x5a01f
jmp 0x5a01f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x6548
jmp 0x5a014
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0x5a02c
jmp 0x5a01f
jmp 0x5a01f
movq %rax, %rbx
jmp 0x5a031
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x6548
leaq 0x58(%rsp), %rdi
callq 0x6548
movq %rbx, %rdi
callq 0x64b0
nop