#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*/ strdel(char *, int, size_t);
int /*<<< orphan*/ strins(char *, int, char const *);
size_t strlen(char const *);
char *strstr(char *, char const *);
char *strrpl(char *s, const char *t, const char *v) {
  size_t len;
  char *p;
  int i;
  len = strlen(t);
  if (!len)
    return s;
  for (i = 3; i > 0 && (p = strstr(s, t)) != NULL; --i) {
    strdel(s, p - s, len);
    strins(s, p - s, v);
  }
  return s;
}


x86 assembly (clang, -O0):
<strrpl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1179 <strrpl+0x39>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1204 <strrpl+0xc4>
movl   $0x3,-0x34(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x34(%rbp)
mov    %al,-0x35(%rbp)
jle    11aa <strrpl+0x6a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    11ba <strrpl+0x7a>
jmp    11fc <strrpl+0xbc>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x28(%rbp),%rdx
call   1030 <strdel@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
call   1050 <strins@plt>
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    1180 <strrpl+0x40>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrpl>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
je     11fa <strrpl+0xba>
mov    %rax,%r15
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11fa <strrpl+0xba>
mov    %rax,%rbx
sub    %r13d,%ebx
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r15,%rdx
call   1030 <strdel@plt>
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r14,%rdx
call   1050 <strins@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11fa <strrpl+0xba>
mov    %rax,%rbx
sub    %r13d,%ebx
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r15,%rdx
call   1030 <strdel@plt>
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r14,%rdx
call   1050 <strins@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11fa <strrpl+0xba>
mov    %rax,%rbx
sub    %r13d,%ebx
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r15,%rdx
call   1030 <strdel@plt>
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r14,%rdx
call   1050 <strins@plt>
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strrpl(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = load i8*, i8** %6
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = icmp ne i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %3
%16 = load i8*, i8** %5
store i8* %16, i8** %4
br label %52
17: ; preds = %3
store i32 3, i32* %10
br label %18
18: ; preds = %47, %17
%19 = load i32, i32* %10
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i8*, i8** %5
%23 = load i8*, i8** %6
%24 = call i8* @strstr(i8* %22, i8* %23)
store i8* %24, i8** %9
%25 = icmp ne i8* %24, null
br label %26
26: ; preds = %21, %18
%27 = phi i1 [ false, %18 ], [ %25, %21 ]
br i1 %27, label %28, label %50
28: ; preds = %26
%29 = load i8*, i8** %5
%30 = load i8*, i8** %9
%31 = load i8*, i8** %5
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
%35 = trunc i64 %34 to i32
%36 = load i64, i64* %8
%37 = call i32 @strdel(i8* %29, i32 %35, i64 %36)
%38 = load i8*, i8** %5
%39 = load i8*, i8** %9
%40 = load i8*, i8** %5
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = trunc i64 %43 to i32
%45 = load i8*, i8** %7
%46 = call i32 @strins(i8* %38, i32 %44, i8* %45)
br label %47
47: ; preds = %28
%48 = load i32, i32* %10
%49 = add nsw i32 %48, -1
store i32 %49, i32* %10
br label %18
50: ; preds = %26
%51 = load i8*, i8** %5
store i8* %51, i8** %4
br label %52
52: ; preds = %50, %15
%53 = load i8*, i8** %4
ret i8* %53
}

LLVM IR (-O2):
define i8* @strrpl(i8* returned %0, i8* nocapture %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %32, label %6
6: ; preds = %3
%7 = ptrtoint i8* %0 to i64
%8 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %32, label %10
10: ; preds = %6
%11 = ptrtoint i8* %8 to i64
%12 = sub i64 %11, %7
%13 = trunc i64 %12 to i32
%14 = tail call i32 @strdel(i8* %0, i32 %13, i64 %4)
%15 = tail call i32 @strins(i8* %0, i32 %13, i8* %2)
%16 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%17 = icmp eq i8* %16, null
br i1 %17, label %32, label %18
18: ; preds = %10
%19 = ptrtoint i8* %16 to i64
%20 = sub i64 %19, %7
%21 = trunc i64 %20 to i32
%22 = tail call i32 @strdel(i8* %0, i32 %21, i64 %4)
%23 = tail call i32 @strins(i8* %0, i32 %21, i8* %2)
%24 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%25 = icmp eq i8* %24, null
br i1 %25, label %32, label %26
26: ; preds = %18
%27 = ptrtoint i8* %24 to i64
%28 = sub i64 %27, %7
%29 = trunc i64 %28 to i32
%30 = tail call i32 @strdel(i8* %0, i32 %29, i64 %4)
%31 = tail call i32 @strins(i8* %0, i32 %29, i8* %2)
br label %32
32: ; preds = %6, %10, %18, %26, %3
ret i8* %0
}