// File: /workspace/github_cpp_makefile_star2_O2/arnoldrobbins[P]mcilroy-regex/sed3.cpp
// Function: substitute(regex_t*, Text*, unsigned char*, int)
int
substitute(regex_t *re, Text* data, uchar *rhs, int n)
{
	Text t;
	uchar *where = data->s;
	if(regexec(re, (char*)data->s, NMATCH, matches, 0))
		return 0;
	vacate(&gendata);
	if(n == 0)
		do {
			docopy(where, so);
			if(!dosub(where, rhs))
				return 0;
			where += eo;
			if(eo == so) {
				if(where < data->w)
					docopy(where++, 1);
				else
					goto done;
			}
		} while(regexec(re, (char*)where, NMATCH, matches, REG_NOTBOL) == 0);
	else {
		while(--n > 0) {
			where += eo;
			if(eo == so) {
				if(where < data->w)
					where++;
				else
					return 0;
			}
			if(regexec(re, (char*)where, NMATCH, matches, REG_NOTBOL))
				return 0;
		}
		docopy(data->s, where-data->s+so);
		if(!dosub(where, rhs))
			return 0;
		where += eo;
	}			
	eo = so = data->w - where;
	docopy(where, so);
done:
	exch(gendata, *data, t);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %r13d
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r12
movq 0x10(%rsi), %r14
leaq 0xb25d(%rip), %rcx      # 0x12820
pushq $0xa
popq %rdx
xorl %ebp, %ebp
movq %r14, %rsi
xorl %r8d, %r8d
callq 0x8cde
testl %eax, %eax
jne 0x7745
leaq 0xb21e(%rip), %rdi      # 0x12800
callq 0x6abd
testl %r13d, %r13d
je 0x763d
incl %r13d
pushq $0x10
popq %rbp
decl %r13d
cmpl $0x2, %r13d
jl 0x76cc
movq 0xb222(%rip), %rax      # 0x12828
addq %rax, %r14
cmpq 0xb210(%rip), %rax      # 0x12820
jne 0x761a
cmpq (%rbx), %r14
jae 0x7636
incq %r14
movq %r12, %rdi
movq %r14, %rsi
pushq $0xa
popq %rdx
leaq 0xb1f6(%rip), %rcx      # 0x12820
movl %ebp, %r8d
callq 0x8cde
testl %eax, %eax
je 0x75f2
xorl %ebp, %ebp
jmp 0x7745
leaq 0xb1dc(%rip), %rbp      # 0x12820
pushq $0xa
popq %r13
movq %r12, 0x8(%rsp)
movl 0xb1cd(%rip), %esi      # 0x12820
movq %r14, %rdi
callq 0x7756
movq %r14, %rdi
movq %r15, %rsi
callq 0x77c4
movq 0xb1bb(%rip), %rax      # 0x12828
addq %rax, %r14
cmpq 0xb1a9(%rip), %rax      # 0x12820
jne 0x76b1
cmpq (%rbx), %r14
jae 0x770e
movq %rbx, %r12
movq %r15, %rbx
movq %r13, %r15
movq %rbp, %r13
leaq 0x1(%r14), %rbp
movq %r14, %rdi
pushq $0x1
popq %rsi
callq 0x7756
movq %rbp, %r14
movq %r13, %rbp
movq %r15, %r13
movq %rbx, %r15
movq %r12, %rbx
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbp, %rcx
pushq $0x10
popq %r8
callq 0x8cde
testl %eax, %eax
je 0x764d
jmp 0x76f2
movq 0x10(%rbx), %rdi
movl %r14d, %esi
subl %edi, %esi
addl 0xb145(%rip), %esi      # 0x12820
callq 0x7756
movq %r14, %rdi
movq %r15, %rsi
callq 0x77c4
addq 0xb136(%rip), %r14      # 0x12828
movq (%rbx), %rsi
subq %r14, %rsi
movq %rsi, 0xb121(%rip)      # 0x12820
movq %rsi, 0xb122(%rip)      # 0x12828
movq %r14, %rdi
callq 0x7756
movq 0xb0fb(%rip), %rax      # 0x12810
movq %rax, 0x20(%rsp)
movups 0xb0df(%rip), %xmm0     # 0x12800
movaps %xmm0, 0x10(%rsp)
movq 0x10(%rbx), %rcx
movq %rcx, 0xb0df(%rip)      # 0x12810
movups (%rbx), %xmm1
movups %xmm1, 0xb0c5(%rip)     # 0x12800
movups %xmm0, (%rbx)
movq %rax, 0x10(%rbx)
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq