#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*/ sprintf(char *, char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char const *);
int repstr(char *str, const char *pat, const char *rep) {
  int len = strlen(pat);
  char buff[1024], *p, *q, *r;
  for (p = str, r = buff; *p; p = q + len) {
    if (!(q = strstr(p, pat)))
      break;
    strncpy(r, p, q - p);
    r += q - p;
    r += sprintf(r, "%s", rep);
  }
  if (p <= str)
    return 0;
  strcpy(r, p);
  strcpy(str, buff);
  return 1;
}


x86 assembly (clang, -O0):
<repstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x438(%rbp),%rax
cmpb   $0x0,(%rax)
je     124e <repstr+0xfe>
mov    -0x438(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x440(%rbp)
cmp    $0x0,%rax
jne    11c2 <repstr+0x72>
jmp    124e <repstr+0xfe>
mov    -0x448(%rbp),%rdi
mov    -0x438(%rbp),%rsi
mov    -0x440(%rbp),%rax
mov    -0x438(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x440(%rbp),%rax
mov    -0x438(%rbp),%rcx
sub    %rcx,%rax
add    -0x448(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x448(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xde7(%rip),%rsi
call   1060 <sprintf@plt>
mov    %eax,%ecx
mov    -0x448(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x448(%rbp)
mov    -0x440(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x438(%rbp)
jmp    118c <repstr+0x3c>
mov    -0x438(%rbp),%rax
cmp    -0x10(%rbp),%rax
ja     126b <repstr+0x11b>
movl   $0x0,-0x4(%rbp)
jmp    1295 <repstr+0x145>
mov    -0x448(%rbp),%rdi
mov    -0x438(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<repstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,%r13
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1060 <strlen@plt>
xor    %ecx,%ecx
cmpb   $0x0,(%rbx)
je     1208 <repstr+0xb8>
lea    0x10(%rsp),%rbp
movslq %eax,%r12
mov    %rbx,(%rsp)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11dd <repstr+0x8d>
mov    %rax,%r15
mov    %rax,%r14
sub    %rbx,%r14
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
add    %rbp,%r14
mov    %r14,%rdi
mov    0x8(%rsp),%rsi
call   1050 <stpcpy@plt>
sub    %r14d,%eax
movslq %eax,%rbp
add    %r14,%rbp
mov    %r15,%rbx
add    %r12,%rbx
cmpb   $0x0,(%r15,%r12,1)
jne    1190 <repstr+0x40>
mov    (%rsp),%r14
cmp    %r14,%rbx
mov    $0x0,%ecx
jbe    1208 <repstr+0xb8>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    $0x1,%ecx
mov    %ecx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
define i32 @repstr(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i8*, i8** %6
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %8
%15 = load i8*, i8** %5
store i8* %15, i8** %10
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
store i8* %16, i8** %12
br label %17
17: ; preds = %50, %3
%18 = load i8*, i8** %10
%19 = load i8, i8* %18
%20 = icmp ne i8 %19, 0
br i1 %20, label %21, label %55
21: ; preds = %17
%22 = load i8*, i8** %10
%23 = load i8*, i8** %6
%24 = call i8* @strstr(i8* %22, i8* %23)
store i8* %24, i8** %11
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %21
br label %55
27: ; preds = %21
%28 = load i8*, i8** %12
%29 = load i8*, i8** %10
%30 = load i8*, i8** %11
%31 = load i8*, i8** %10
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
%35 = trunc i64 %34 to i32
%36 = call i32 @strncpy(i8* %28, i8* %29, i32 %35)
%37 = load i8*, i8** %11
%38 = load i8*, i8** %10
%39 = ptrtoint i8* %37 to i64
%40 = ptrtoint i8* %38 to i64
%41 = sub i64 %39, %40
%42 = load i8*, i8** %12
%43 = getelementptr inbounds i8, i8* %42, i64 %41
store i8* %43, i8** %12
%44 = load i8*, i8** %12
%45 = load i8*, i8** %7
%46 = call i32 @sprintf(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %45)
%47 = load i8*, i8** %12
%48 = sext i32 %46 to i64
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8* %49, i8** %12
br label %50
50: ; preds = %27
%51 = load i8*, i8** %11
%52 = load i32, i32* %8
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8* %54, i8** %10
br label %17
55: ; preds = %26, %17
%56 = load i8*, i8** %10
%57 = load i8*, i8** %5
%58 = icmp ule i8* %56, %57
br i1 %58, label %59, label %60
59: ; preds = %55
store i32 0, i32* %4
br label %67
60: ; preds = %55
%61 = load i8*, i8** %12
%62 = load i8*, i8** %10
%63 = call i32 @strcpy(i8* %61, i8* %62)
%64 = load i8*, i8** %5
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%66 = call i32 @strcpy(i8* %64, i8* %65)
store i32 1, i32* %4
br label %67
67: ; preds = %60, %59
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
define i32 @repstr(i8* %0, i8* %1, i8* nocapture %2) {
%4 = alloca [1024 x i8]
%5 = tail call i32 @strlen(i8* %1)
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %40, label %9
9: ; preds = %3
%10 = sext i32 %5 to i64
br label %11
11: ; preds = %9, %16
%12 = phi i8* [ %6, %9 ], [ %29, %16 ]
%13 = phi i8* [ %0, %9 ], [ %30, %16 ]
%14 = call i8* @strstr(i8* %13, i8* dereferenceable(1) %1)
%15 = icmp eq i8* %14, null
br i1 %15, label %33, label %16
16: ; preds = %11
%17 = ptrtoint i8* %14 to i64
%18 = ptrtoint i8* %13 to i64
%19 = sub i64 %17, %18
%20 = trunc i64 %19 to i32
%21 = call i32 @strncpy(i8* %12, i8* %13, i32 %20)
%22 = getelementptr inbounds i8, i8* %12, i64 %19
%23 = call i8* @stpcpy(i8* %22, i8* %2)
%24 = ptrtoint i8* %23 to i64
%25 = ptrtoint i8* %22 to i64
%26 = sub i64 %24, %25
%27 = shl i64 %26, 32
%28 = ashr exact i64 %27, 32
%29 = getelementptr inbounds i8, i8* %22, i64 %28
%30 = getelementptr inbounds i8, i8* %14, i64 %10
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 0
br i1 %32, label %33, label %11
33: ; preds = %16, %11
%34 = phi i8* [ %30, %16 ], [ %13, %11 ]
%35 = phi i8* [ %29, %16 ], [ %12, %11 ]
%36 = icmp ugt i8* %34, %0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 @strcpy(i8* %35, i8* %34)
%39 = call i32 @strcpy(i8* %0, i8* %6)
br label %40
40: ; preds = %3, %33, %37
%41 = phi i32 [ 1, %37 ], [ 0, %33 ], [ 0, %3 ]
ret i32 %41
}