// File: /workspace/github_cpp_makefile_star2_O3/Alcaro[P]bridges/arlib/string.cpp
// Function: cstring::replace(cstring, cstring) const
string cstring::replace(cstring in, cstring out) const
{
	size_t outlen = length();
	
	if (in.length() != out.length())
	{
		const uint8_t* haystack = ptr();
		const uint8_t* haystackend = ptr()+length();
		while (true)
		{
			haystack = (uint8_t*)memmem(haystack, haystackend-haystack, in.ptr(), in.length());
			if (!haystack) break;
			
			haystack += in.length();
			outlen += out.length(); // outlen-inlen is type uint32 - bad idea
			outlen -= in.length();
		}
	}
	
	string ret;
	uint8_t* retptr = ret.construct(outlen).ptr();
	
	const uint8_t* prev = ptr();
	const uint8_t* myend = ptr()+length();
	while (true)
	{
		const uint8_t* match = (uint8_t*)memmem(prev, myend-prev, in.ptr(), in.length());
		if (!match) break;
		
		memcpy(retptr, prev, match-prev);
		retptr += match-prev;
		prev = match + in.length();
		
		memcpy(retptr, out.ptr(), out.length());
		retptr += out.length();
	}
	memcpy(retptr, prev, myend-prev);
	
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r15
movq %rcx, %rbx
movq %rdi, %r14
movq %rdx, (%rsp)
movq %rdx, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
movq %r8, 0x48(%rsp)
movq %r9, 0x50(%rsp)
movzbl 0xf(%rsi), %eax
movl %eax, %ebp
xorl $0xf, %ebp
testb %al, %al
cmovsl 0x8(%rsi), %ebp
shrq $0x38, %rcx
xorb $0xf, %cl
testq %rbx, %rbx
movzbl %cl, %r13d
movl %ebx, %ecx
cmovnsl %r13d, %ecx
shrq $0x38, %r9
xorl $0xf, %r9d
testq %r15, %r15
movl %r15d, %edx
cmovnsl %r9d, %edx
movl %edx, %edi
cmpl %edx, %ecx
movq %r14, 0x18(%rsp)
movq %r8, 0x28(%rsp)
movq %r9, 0x20(%rsp)
movq %rdi, 0x38(%rsp)
movq %rsi, 0x40(%rsp)
je 0x19d79
movq %r15, 0x30(%rsp)
movq %rsi, %rdi
testb %al, %al
jns 0x19d22
movq (%rsi), %rdi
leaq (%rdi,%rbp), %r15
movl %ebx, %r12d
movl %ecx, %r14d
movq %r15, %rsi
subq %rdi, %rsi
testq %rbx, %rbx
js 0x19d4e
leaq 0x8(%rsp), %rdx
movq %r13, %rcx
callq 0x3e3d
testq %rax, %rax
je 0x19d6f
movq %r13, %rdi
jmp 0x19d62
movq (%rsp), %rdx
movq %r12, %rcx
callq 0x3e3d
testq %rax, %rax
je 0x19d6f
movq %r12, %rdi
addq %rax, %rdi
addq 0x38(%rsp), %rbp
subq %r14, %rbp
jmp 0x19d2c
movq 0x18(%rsp), %r14
movq 0x30(%rsp), %r15
movb $0xf, 0xf(%r14)
movb $0x0, (%r14)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x193c4
cmpb $0x0, 0xf(%r14)
movq %r14, %r12
jns 0x19d9a
movq (%r14), %r12
movq 0x40(%rsp), %rcx
movzbl 0xf(%rcx), %eax
movl %eax, %esi
xorl $0xf, %esi
testb %al, %al
movq (%rcx), %rbx
cmovnsq %rcx, %rbx
cmovsl 0x8(%rcx), %esi
leaq 0x8(%rsp), %rdi
movzbl 0xf(%rdi), %eax
movl %eax, %ecx
xorl $0xf, %ecx
testb %al, %al
movq (%rdi), %rdx
cmovnsq %rdi, %rdx
cmovsl 0x8(%rdi), %ecx
movq %rbx, %rdi
movq %rsi, (%rsp)
callq 0x3e3d
testq %rax, %rax
je 0x19e93
movq %rax, %r14
addq %rbx, (%rsp)
testq %r15, %r15
leaq 0x48(%rsp), %rax
movq 0x28(%rsp), %rcx
cmovnsq %rax, %rcx
movq %rcx, 0x28(%rsp)
movq 0x20(%rsp), %rax
cmovsl %r15d, %eax
movq %rax, 0x20(%rsp)
movq 0x38(%rsp), %r15
movq %r14, %r13
subq %rbx, %r13
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x3280
addq %r12, %r13
movzbl 0x17(%rsp), %eax
movl %eax, %ebx
xorl $0xf, %ebx
testb %al, %al
cmovsl 0x10(%rsp), %ebx
addq %r14, %rbx
movq %r13, %rdi
movq 0x28(%rsp), %rsi
movq %r15, %rdx
callq 0x3280
addq 0x20(%rsp), %r13
movq (%rsp), %rbp
subq %rbx, %rbp
movzbl 0x17(%rsp), %eax
movl %eax, %ecx
xorl $0xf, %ecx
testb %al, %al
movq 0x8(%rsp), %rdx
leaq 0x8(%rsp), %rax
cmovnsq %rax, %rdx
cmovsl 0x10(%rsp), %ecx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3e3d
movq %rax, %r14
movq %r13, %r12
testq %rax, %rax
jne 0x19e15
jmp 0x19e9a
movq (%rsp), %rbp
movq %r12, %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x3280
movq 0x18(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq