// File: /workspace/github_cpp_makefile_star2_O3/rossy[P]crossc/SPIRV-Cross/spirv_glsl.cpp
// Function: spirv_cross::CompilerGLSL::optimize_read_modify_write(spirv_cross::SPIRType const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
bool CompilerGLSL::optimize_read_modify_write(const SPIRType &type, const string &lhs, const string &rhs)
{
	// Do this with strings because we have a very clear pattern we can check for and it avoids
	// adding lots of special cases to the code emission.
	if (rhs.size() < lhs.size() + 3)
		return false;

	// Do not optimize matrices. They are a bit awkward to reason about in general
	// (in which order does operation happen?), and it does not work on MSL anyways.
	if (type.vecsize > 1 && type.columns > 1)
		return false;

	auto index = rhs.find(lhs);
	if (index != 0)
		return false;

	// TODO: Shift operators, but it's not important for now.
	auto op = rhs.find_first_of("+-/*%|&^", lhs.size() + 1);
	if (op != lhs.size() + 1)
		return false;

	// Check that the op is followed by space. This excludes && and ||.
	if (rhs[op + 1] != ' ')
		return false;

	char bop = rhs[op];
	auto expr = rhs.substr(lhs.size() + 3);
	// Try to find increments and decrements. Makes it look neater as += 1, -= 1 is fairly rare to see in real code.
	// Find some common patterns which are equivalent.
	if ((bop == '+' || bop == '-') && (expr == "1" || expr == "uint(1)" || expr == "1u" || expr == "int(1u)"))
		statement(lhs, bop, bop, ";");
	else
		statement(lhs, " ", bop, "= ", expr, ";");
	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
movq %rcx, %r15
movq 0x8(%rdx), %rcx
leaq 0x3(%rcx), %rax
cmpq %rax, 0x8(%r15)
jb 0x440b3
movq %rdx, %rbx
movq %rdi, %r14
cmpl $0x2, 0x14(%rsi)
jb 0x440c6
cmpl $0x1, 0x18(%rsi)
jbe 0x440c6
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rsi
xorl %r12d, %r12d
movq %r15, %rdi
xorl %edx, %edx
callq 0x6490
testq %rax, %rax
jne 0x440b6
movq 0x8(%rbx), %rdx
incq %rdx
leaq 0x75b6e(%rip), %rsi     # 0xb9c57
movl $0x8, %ecx
movq %r15, %rdi
callq 0x63e0
movq 0x8(%rbx), %rdx
leaq 0x1(%rdx), %rcx
cmpq %rcx, %rax
jne 0x440b3
movq (%r15), %rcx
cmpb $0x20, 0x1(%rcx,%rax)
jne 0x440b3
movzbl (%rcx,%rax), %ebp
movb %bpl, 0xf(%rsp)
addq $0x3, %rdx
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq $-0x1, %rcx
callq 0x6290
cmpl $0x2d, %ebp
je 0x44138
cmpl $0x2b, %ebp
jne 0x4418c
leaq 0x75d35(%rip), %rsi     # 0xb9e74
leaq 0x10(%rsp), %rdi
callq 0x60a0
testl %eax, %eax
je 0x441bc
leaq 0x75b0c(%rip), %rsi     # 0xb9c60
leaq 0x10(%rsp), %rdi
callq 0x60a0
testl %eax, %eax
je 0x441bc
leaq 0x75aff(%rip), %rsi     # 0xb9c68
leaq 0x10(%rsp), %rdi
callq 0x60a0
testl %eax, %eax
je 0x441bc
leaq 0x75aed(%rip), %rsi     # 0xb9c6b
leaq 0x10(%rsp), %rdi
callq 0x60a0
testl %eax, %eax
je 0x441bc
leaq 0x7a95e(%rip), %rax     # 0xbeaf1
movq %rax, (%rsp)
leaq 0x70710(%rip), %rdx     # 0xb48ae
leaq 0x7a8b6(%rip), %r8      # 0xbea5b
leaq 0xf(%rsp), %rcx
leaq 0x10(%rsp), %r9
movq %r14, %rdi
movq %rbx, %rsi
callq 0x55d02
jmp 0x441d6
leaq 0x7a92e(%rip), %r8      # 0xbeaf1
leaq 0xf(%rsp), %rcx
movq %r14, %rdi
movq %rbx, %rsi
movq %rcx, %rdx
callq 0x55b6a
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x441f1
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x6260
movb $0x1, %r12b
jmp 0x440b6
movq %rax, %rbx
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x44217
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x6260
movq %rbx, %rdi
callq 0x64a0
nop