// File: /workspace/github_cpp_makefile_star2_O1/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 %rsi, %rbx
movq %rdx, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
movq %r8, 0x18(%rsp)
movq %r9, 0x20(%rsp)
movzbl 0xf(%rsi), %eax
movl %eax, %esi
xorl $0xf, %esi
testb %al, %al
movl 0x8(%rbx), %r15d
cmovnsl %esi, %r15d
movzbl 0x17(%rsp), %r13d
movl %r13d, %r8d
xorl $0xf, %r8d
testb %r13b, %r13b
cmovnsl %r8d, %ecx
movq %rdi, %r12
movzbl 0x27(%rsp), %edi
movl %edi, %ebp
xorl $0xf, %ebp
testb %dil, %dil
cmovnsl %ebp, %r9d
cmpl %r9d, %ecx
movq %r12, 0x38(%rsp)
je 0x19c29
movq %r8, %r14
movl %edi, 0x2c(%rsp)
testb %al, %al
movq (%rbx), %rdi
cmovnsq %rbx, %rdi
movq %rbx, 0x30(%rsp)
cmovsl 0x8(%rbx), %esi
testb %r13b, %r13b
leaq 0x8(%rsp), %rax
cmovnsq %rax, %rdx
movl 0x8(%rax), %ebx
cmovnsl %r14d, %ebx
movq %rdi, 0x40(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdx, 0x50(%rsp)
movq %rbx, %rcx
callq 0x3b51
testq %rax, %rax
je 0x19c24
movq 0x48(%rsp), %rcx
addq %rcx, 0x40(%rsp)
cmpb $0x0, 0x2c(%rsp)
cmovsl 0x20(%rsp), %ebp
testb %r13b, %r13b
cmovsl 0x10(%rsp), %r14d
addq %rbp, %r15
movq %r14, %r13
subq %r14, %rbp
movq 0x50(%rsp), %r14
movq %r15, %r12
addq %r13, %rax
movq 0x40(%rsp), %rsi
subq %rax, %rsi
movq %rax, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x3b51
addq %rbp, %r15
testq %rax, %rax
jne 0x19bf5
subq %r13, %r12
movq %r12, %r15
movq 0x38(%rsp), %r12
movq 0x30(%rsp), %rbx
movb $0xf, 0xf(%r12)
movb $0x0, (%r12)
movq %r12, %rdi
movq %r15, %rsi
callq 0x191a4
cmpb $0x0, 0xf(%r12)
movq %r12, %r13
jns 0x19c4e
movq (%r12), %r13
movzbl 0xf(%rbx), %eax
movl %eax, %r15d
xorl $0xf, %r15d
testb %al, %al
movq (%rbx), %r12
cmovnsq %rbx, %r12
cmovsl 0x8(%rbx), %r15d
addq %r12, %r15
leaq 0x8(%rsp), %rbp
movq %r15, %rsi
subq %r12, %rsi
movzbl 0x17(%rsp), %eax
movl %eax, %ecx
xorl $0xf, %ecx
testb %al, %al
movq 0x8(%rsp), %rdx
cmovnsq %rbp, %rdx
cmovsl 0x10(%rsp), %ecx
movq %r12, %rdi
callq 0x3b51
movq %rax, %r14
testq %rax, %rax
je 0x19d0b
movq %r14, %rbx
subq %r12, %rbx
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3280
addq %r13, %rbx
movzbl 0x17(%rsp), %eax
movl %eax, %r12d
xorl $0xf, %r12d
testb %al, %al
cmovsl 0x10(%rsp), %r12d
addq %r14, %r12
movzbl 0x27(%rsp), %eax
movl %eax, %edx
xorl $0xf, %edx
testb %al, %al
movq 0x18(%rsp), %rsi
leaq 0x18(%rsp), %rax
cmovnsq %rax, %rsi
cmovsl 0x20(%rsp), %edx
movq %rbx, %rdi
callq 0x3280
movzbl 0x27(%rsp), %eax
movl %eax, %r13d
xorl $0xf, %r13d
testb %al, %al
cmovsl 0x20(%rsp), %r13d
addq %rbx, %r13
testq %r14, %r14
jne 0x19c6f
subq %r12, %r15
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3280
movq 0x38(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop