#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ Q_strlcat(char *, size_t, char const *, int);
int strlen(char const *);
char *strstr(char const *, char const *);
void Q_strnrepl(char *dest, size_t size, const char *src, const char *find,
                const char *replacement) {
  char *new;
  *dest = 0;
  int findlen = strlen(find);
  while ((new = strstr(src, find)) != NULL) {
    Q_strlcat(dest, size, src, new - src);
    Q_strlcat(dest, size, replacement, 0);
    src = &new[findlen];
  }
  Q_strlcat(dest, size, src, 0);
}


x86 assembly (clang, -O0):
<Q_strnrepl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11bf <Q_strnrepl+0x8f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%ecx
call   1040 <Q_strlcat@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <Q_strlcat@plt>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    115f <Q_strnrepl+0x2f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <Q_strlcat@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Q_strnrepl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r12
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r13
movb   $0x0,(%rdi)
mov    %rcx,%rdi
call   1030 <strlen@plt>
mov    %eax,(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     11bc <Q_strnrepl+0x8c>
mov    %rax,%rbp
movslq (%rsp),%rax
mov    %rax,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%ecx
sub    %ebx,%ecx
mov    %r13,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <Q_strlcat@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1040 <Q_strlcat@plt>
mov    %rbp,%rbx
add    (%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strstr@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1180 <Q_strnrepl+0x50>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <Q_strlcat@plt>

LLVM IR (-O0):
define void @Q_strnrepl(i8* %0, i64 %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* %4, i8** %10
%13 = load i8*, i8** %6
store i8 0, i8* %13
%14 = load i8*, i8** %9
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %12
br label %16
16: ; preds = %21, %5
%17 = load i8*, i8** %8
%18 = load i8*, i8** %9
%19 = call i8* @strstr(i8* %17, i8* %18)
store i8* %19, i8** %11
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %40
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = load i64, i64* %7
%24 = load i8*, i8** %8
%25 = load i8*, i8** %11
%26 = load i8*, i8** %8
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
%30 = trunc i64 %29 to i32
%31 = call i32 @Q_strlcat(i8* %22, i64 %23, i8* %24, i32 %30)
%32 = load i8*, i8** %6
%33 = load i64, i64* %7
%34 = load i8*, i8** %10
%35 = call i32 @Q_strlcat(i8* %32, i64 %33, i8* %34, i32 0)
%36 = load i8*, i8** %11
%37 = load i32, i32* %12
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8* %39, i8** %8
br label %16
40: ; preds = %16
%41 = load i8*, i8** %6
%42 = load i64, i64* %7
%43 = load i8*, i8** %8
%44 = call i32 @Q_strlcat(i8* %41, i64 %42, i8* %43, i32 0)
ret void
}

LLVM IR (-O2):
define void @Q_strnrepl(i8* %0, i64 %1, i8* %2, i8* %3, i8* %4) {
store i8 0, i8* %0
%6 = tail call i32 @strlen(i8* %3)
%7 = tail call i8* @strstr(i8* dereferenceable(1) %2, i8* dereferenceable(1) %3)
%8 = icmp eq i8* %7, null
br i1 %8, label %23, label %9
9: ; preds = %5
%10 = sext i32 %6 to i64
br label %11
11: ; preds = %9, %11
%12 = phi i8* [ %7, %9 ], [ %21, %11 ]
%13 = phi i8* [ %2, %9 ], [ %20, %11 ]
%14 = ptrtoint i8* %12 to i64
%15 = ptrtoint i8* %13 to i64
%16 = sub i64 %14, %15
%17 = trunc i64 %16 to i32
%18 = tail call i32 @Q_strlcat(i8* %0, i64 %1, i8* %13, i32 %17)
%19 = tail call i32 @Q_strlcat(i8* %0, i64 %1, i8* %4, i32 0)
%20 = getelementptr inbounds i8, i8* %12, i64 %10
%21 = tail call i8* @strstr(i8* dereferenceable(1) %20, i8* dereferenceable(1) %3)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %11
23: ; preds = %11, %5
%24 = phi i8* [ %2, %5 ], [ %20, %11 ]
%25 = tail call i32 @Q_strlcat(i8* %0, i64 %1, i8* %24, i32 0)
ret void
}