// File: 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
movq %rsp, %rbp
subq $0x3e0, %rsp            # imm = 0x3E0
movq %rdi, -0x2e8(%rbp)
movq %rdi, %rax
movq %rax, -0x2e0(%rbp)
movq %rdi, -0x250(%rbp)
movq %rdx, -0x260(%rbp)
movq %rcx, -0x258(%rbp)
movq %r8, -0x270(%rbp)
movq %r9, -0x268(%rbp)
movq %rsi, -0x278(%rbp)
movq -0x278(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0x2d0(%rbp)
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x395e9
movq -0x2d0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x11c(%rbp)
jmp 0x395f9
movq -0x2d0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x11c(%rbp)
movl -0x11c(%rbp), %eax
movq %rax, -0x280(%rbp)
leaq -0x260(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0x2f0(%rbp)
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39662
movq -0x2f0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x134(%rbp)
jmp 0x39672
movq -0x2f0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x134(%rbp)
movl -0x134(%rbp), %eax
movl %eax, -0x2fc(%rbp)
leaq -0x270(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
movq %rax, -0x2f8(%rbp)
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x396da
movq -0x2f8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x14c(%rbp)
jmp 0x396ea
movq -0x2f8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x14c(%rbp)
movl -0x2fc(%rbp), %eax
cmpl -0x14c(%rbp), %eax
je 0x39af7
movq -0x2d8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x308(%rbp)
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39734
movq -0x308(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x39742
movq -0x308(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x2d8(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rcx, -0x288(%rbp)
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x310(%rbp)
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39794
movq -0x310(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x397a5
movq -0x310(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x2d8(%rbp), %rax
movq -0x90(%rbp), %rcx
movq %rcx, -0x320(%rbp)
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rax
movq %rax, -0x318(%rbp)
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x3980f
movq -0x318(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x164(%rbp)
jmp 0x3981f
movq -0x318(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x164(%rbp)
movq -0x320(%rbp), %rax
movl -0x164(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x290(%rbp)
movq -0x288(%rbp), %rax
movq %rax, -0x338(%rbp)
movq -0x290(%rbp), %rax
movq -0x288(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x330(%rbp)
leaq -0x260(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x328(%rbp)
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x398a3
movq -0x328(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x398b4
movq -0x328(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x348(%rbp)
leaq -0x260(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rax
movq %rax, -0x340(%rbp)
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x3991e
movq -0x340(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x17c(%rbp)
jmp 0x3992e
movq -0x340(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x17c(%rbp)
movq -0x348(%rbp), %rdx
movq -0x330(%rbp), %rsi
movq -0x338(%rbp), %rdi
movl -0x17c(%rbp), %eax
movl %eax, %ecx
callq 0x2d1d0
movq %rax, -0x288(%rbp)
cmpq $0x0, -0x288(%rbp)
jne 0x39966
jmp 0x39af5
leaq -0x260(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x1a0(%rbp), %rax
movq %rax, -0x350(%rbp)
movq %rax, -0x190(%rbp)
movq -0x190(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x399c2
movq -0x350(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x194(%rbp)
jmp 0x399d2
movq -0x350(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x194(%rbp)
movl -0x194(%rbp), %ecx
movq -0x288(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x288(%rbp)
leaq -0x270(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rax
movq %rax, -0x358(%rbp)
movq %rax, -0x1a8(%rbp)
movq -0x1a8(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39a47
movq -0x358(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x1ac(%rbp)
jmp 0x39a57
movq -0x358(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x1ac(%rbp)
movl -0x1ac(%rbp), %eax
addq -0x280(%rbp), %rax
movq %rax, -0x280(%rbp)
leaq -0x260(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq -0x1d0(%rbp), %rax
movq %rax, -0x360(%rbp)
movq %rax, -0x1c0(%rbp)
movq -0x1c0(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39ac7
movq -0x360(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x1c4(%rbp)
jmp 0x39ad7
movq -0x360(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x1c4(%rbp)
movl -0x1c4(%rbp), %eax
movl %eax, %ecx
movq -0x280(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x280(%rbp)
jmp 0x39836
jmp 0x39af7
movq -0x2e8(%rbp), %rdi
movb $0x0, -0x291(%rbp)
callq 0x7800
movq -0x2e8(%rbp), %rsi
movq -0x280(%rbp), %rdx
leaq -0x2b0(%rbp), %rdi
callq 0x7850
leaq -0x2b0(%rbp), %rdi
callq 0x7490
movq %rax, %rcx
movq -0x2d8(%rbp), %rax
movq %rcx, -0x2a0(%rbp)
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x368(%rbp)
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39b81
movq -0x368(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x39b92
movq -0x368(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0x2d8(%rbp), %rax
movq -0xc0(%rbp), %rcx
movq %rcx, -0x2b8(%rbp)
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x370(%rbp)
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39be7
movq -0x370(%rbp), %rax
movq %rax, -0xd8(%rbp)
jmp 0x39bf8
movq -0x370(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
movq -0x2d8(%rbp), %rax
movq -0xd8(%rbp), %rcx
movq %rcx, -0x380(%rbp)
movq %rax, -0x1e8(%rbp)
movq -0x1e8(%rbp), %rax
movq %rax, -0x378(%rbp)
movq %rax, -0x1d8(%rbp)
movq -0x1d8(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39c62
movq -0x378(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x1dc(%rbp)
jmp 0x39c72
movq -0x378(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x1dc(%rbp)
movq -0x380(%rbp), %rax
movl -0x1dc(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x2c0(%rbp)
jmp 0x39c8b
movq -0x2b8(%rbp), %rax
movq %rax, -0x398(%rbp)
movq -0x2c0(%rbp), %rax
movq -0x2b8(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x390(%rbp)
leaq -0x260(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x388(%rbp)
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39cf8
movq -0x388(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x39d09
movq -0x388(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x3a8(%rbp)
leaq -0x260(%rbp), %rax
movq %rax, -0x200(%rbp)
movq -0x200(%rbp), %rax
movq %rax, -0x3a0(%rbp)
movq %rax, -0x1f0(%rbp)
movq -0x1f0(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39d73
movq -0x3a0(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x1f4(%rbp)
jmp 0x39d83
movq -0x3a0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x1f4(%rbp)
movq -0x3a8(%rbp), %rdx
movq -0x390(%rbp), %rsi
movq -0x398(%rbp), %rdi
movl -0x1f4(%rbp), %eax
movl %eax, %ecx
callq 0x2d1d0
movq %rax, -0x2c8(%rbp)
cmpq $0x0, -0x2c8(%rbp)
jne 0x39dbb
jmp 0x3a014
movq -0x2a0(%rbp), %rdi
movq -0x2b8(%rbp), %rsi
movq -0x2c8(%rbp), %rdx
movq -0x2b8(%rbp), %rax
subq %rax, %rdx
callq 0x4280
movq -0x2c8(%rbp), %rax
movq -0x2b8(%rbp), %rcx
subq %rcx, %rax
addq -0x2a0(%rbp), %rax
movq %rax, -0x2a0(%rbp)
movq -0x2c8(%rbp), %rax
movq %rax, -0x3b8(%rbp)
leaq -0x260(%rbp), %rax
movq %rax, -0x218(%rbp)
movq -0x218(%rbp), %rax
movq %rax, -0x3b0(%rbp)
movq %rax, -0x208(%rbp)
movq -0x208(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39e68
movq -0x3b0(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x20c(%rbp)
jmp 0x39e78
movq -0x3b0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x20c(%rbp)
movq -0x3b8(%rbp), %rax
movl -0x20c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x2b8(%rbp)
movq -0x2a0(%rbp), %rax
movq %rax, -0x3c8(%rbp)
leaq -0x270(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0x3c0(%rbp)
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39ee4
movq -0x3c0(%rbp), %rax
movq %rax, -0x108(%rbp)
jmp 0x39ef5
movq -0x3c0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0x3d8(%rbp)
leaq -0x270(%rbp), %rax
movq %rax, -0x230(%rbp)
movq -0x230(%rbp), %rax
movq %rax, -0x3d0(%rbp)
movq %rax, -0x220(%rbp)
movq -0x220(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39f5f
movq -0x3d0(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x224(%rbp)
jmp 0x39f6f
movq -0x3d0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x224(%rbp)
movq -0x3d8(%rbp), %rsi
movq -0x3c8(%rbp), %rdi
movl -0x224(%rbp), %eax
movl %eax, %edx
callq 0x4280
leaq -0x270(%rbp), %rax
movq %rax, -0x248(%rbp)
movq -0x248(%rbp), %rax
movq %rax, -0x3e0(%rbp)
movq %rax, -0x238(%rbp)
movq -0x238(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x39fe6
movq -0x3e0(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x23c(%rbp)
jmp 0x39ff6
movq -0x3e0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x23c(%rbp)
movl -0x23c(%rbp), %ecx
movq -0x2a0(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x2a0(%rbp)
jmp 0x39c89
movq -0x2a0(%rbp), %rdi
movq -0x2b8(%rbp), %rsi
movq -0x2c0(%rbp), %rdx
movq -0x2b8(%rbp), %rax
subq %rax, %rdx
callq 0x4280
movb $0x1, -0x291(%rbp)
testb $0x1, -0x291(%rbp)
jne 0x3a054
movq -0x2e8(%rbp), %rdi
callq 0x5870
movq -0x2e0(%rbp), %rax
addq $0x3e0, %rsp            # imm = 0x3E0
popq %rbp
retq
nopw %cs:(%rax,%rax)