// File: /workspace/github_cpp_makefile_star2_O2/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 %rdx, %r14
movq %rsi, %r13
movq %rdx, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
movq %r8, 0x20(%rsp)
movq %r8, 0x38(%rsp)
movq %r9, 0x40(%rsp)
movzbl 0xf(%rsi), %esi
movl %esi, %ebp
xorl $0xf, %ebp
testb %sil, %sil
cmovsl 0x8(%r13), %ebp
movq %rdi, 0x28(%rsp)
movq %rcx, %rdx
shrq $0x38, %rdx
xorl $0xf, %edx
testq %rcx, %rcx
cmovsl %ecx, %edx
movq %r9, %rax
shrq $0x38, %rax
xorl $0xf, %eax
testq %r9, %r9
movq %r9, 0x30(%rsp)
movl %r9d, %edi
movq %rax, 0x18(%rsp)
cmovnsl %eax, %edi
cmpl %edi, %edx
jne 0x14f33
movl %edi, %r15d
movq 0x28(%rsp), %rsi
movb $0xf, 0xf(%rsi)
movb $0x0, (%rsi)
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rdx
callq 0x4af2
movzbl 0xf(%r13), %eax
movl %eax, %ebx
xorl $0xf, %ebx
testb %al, %al
movq (%r13), %r14
cmovnsq %r13, %r14
cmovsl 0x8(%r13), %ebx
addq %r14, %rbx
movq 0x30(%rsp), %rdx
testq %rdx, %rdx
leaq 0x38(%rsp), %rax
movq 0x20(%rsp), %rcx
cmovnsq %rax, %rcx
movq %rcx, 0x20(%rsp)
movq (%r12), %r13
movq 0x18(%rsp), %rax
cmovsl %edx, %eax
movq %rax, 0x18(%rsp)
movq %rbx, %r12
subq %r14, %r12
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 %r14, %rdi
movq %r12, %rsi
callq 0x113c4
testq %rax, %rax
je 0x14f11
movq %rax, %rbp
movq %rax, %r12
subq %r14, %r12
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x3280
addq %r13, %r12
movzbl 0x17(%rsp), %eax
movl %eax, %r14d
xorl $0xf, %r14d
testb %al, %al
cmovsl 0x10(%rsp), %r14d
addq %rbp, %r14
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
callq 0x3280
addq 0x18(%rsp), %r12
movq %r12, %r13
jmp 0x14e91
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x3280
movq 0x28(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rax
testb %sil, %sil
jns 0x14f3f
movq (%r13), %rax
leaq (%rax,%rbp), %rbx
testq %rcx, %rcx
leaq 0x8(%rsp), %rcx
cmovnsq %rcx, %r14
movl %edx, %r12d
movl %edi, %r15d
movq %rbx, %rsi
subq %rax, %rsi
movq %rax, %rdi
movq %r14, %rdx
movq %r12, %rcx
callq 0x113c4
testq %rax, %rax
je 0x14e2d
addq %r12, %rax
addq %r15, %rbp
subq %r12, %rbp
jmp 0x14f55
nop