#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 *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *StrRep(char *st, char *orig, char *repl, int mim) {
  char bufer[mim];
  char *ch;
  if (!orig)
    return "\0";
  if (!(ch = strstr(st, orig)))
    return st;
  strncpy(bufer, st, ch - st);
  bufer[ch - st] = 0;
  sprintf(bufer + (ch - st), "%s%s", repl, ch + strlen(orig));
  char *out = bufer;
  return out;
}


x86 assembly (clang, -O0):
<StrRep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119f <StrRep+0x5f>
lea    0xe71(%rip),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    125a <StrRep+0x11a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11ce <StrRep+0x8e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    125a <StrRep+0x11a>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x58(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x68(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x70(%rbp),%rcx
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xdc4(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrRep>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rsi,%rsi
je     11c0 <StrRep+0x80>
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r12
call   1060 <strstr@plt>
test   %rax,%rax
je     11c9 <StrRep+0x89>
mov    %rax,%r14
mov    %r13,-0x30(%rbp)
mov    %rax,%r13
sub    %r12,%r13
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1030 <strncpy@plt>
lea    (%rbx,%r13,1),%r12
movb   $0x0,(%rbx,%r13,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
add    %r14,%rcx
lea    0xe50(%rip),%rsi
mov    %r12,%rdi
mov    -0x30(%rbp),%rdx
call   1050 <sprintf@plt>
jmp    11cc <StrRep+0x8c>
lea    0xe39(%rip),%rbx
jmp    11cc <StrRep+0x8c>
mov    %r12,%rbx
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] zeroinitializer
@.str.1 = [5 x i8] c"%s%s\00"
define i8* @StrRep(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = zext i32 %3 to i64
%6 = alloca i8, i64 %5
%7 = icmp eq i8* %1, null
br i1 %7, label %22, label %8
8: ; preds = %4
%9 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* %1)
%10 = icmp eq i8* %9, null
br i1 %10, label %22, label %11
11: ; preds = %8
%12 = ptrtoint i8* %9 to i64
%13 = ptrtoint i8* %0 to i64
%14 = sub i64 %12, %13
%15 = trunc i64 %14 to i32
%16 = call i32 @strncpy(i8* %6, i8* %0, i32 %15)
%17 = getelementptr inbounds i8, i8* %6, i64 %14
store i8 0, i8* %17
%18 = call i32 @strlen(i8* %1)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %9, i64 %19
%21 = call i32 @sprintf(i8* %17, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %20)
br label %22
22: ; preds = %8, %4, %11
%23 = phi i8* [ %6, %11 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %4 ], [ %0, %8 ]
ret i8* %23
}