#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, const int mim) {
  char bufer[mim];
  char *ch;
  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)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11ac <StrRep+0x6c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1238 <StrRep+0xf8>
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    0xde4(%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    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r12
mov    %ecx,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
call   1060 <strstr@plt>
test   %rax,%rax
je     11bb <StrRep+0x7b>
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    0xe53(%rip),%rsi
mov    %r12,%rdi
mov    -0x30(%rbp),%rdx
call   1050 <sprintf@plt>
jmp    11be <StrRep+0x7e>
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 = [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** %6
%20 = load i8*, i8** %7
%21 = call i8* @strstr(i8* %19, i8* %20)
store i8* %21, i8** %12
%22 = icmp ne i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %4
%24 = load i8*, i8** %6
store i8* %24, i8** %5
store i32 1, i32* %13
br label %54
25: ; preds = %4
%26 = load i8*, i8** %6
%27 = load i8*, i8** %12
%28 = load i8*, i8** %6
%29 = ptrtoint i8* %27 to i64
%30 = ptrtoint i8* %28 to i64
%31 = sub i64 %29, %30
%32 = trunc i64 %31 to i32
%33 = call i32 @strncpy(i8* %18, i8* %26, i32 %32)
%34 = load i8*, i8** %12
%35 = load i8*, i8** %6
%36 = ptrtoint i8* %34 to i64
%37 = ptrtoint i8* %35 to i64
%38 = sub i64 %36, %37
%39 = getelementptr inbounds i8, i8* %18, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %12
%41 = load i8*, i8** %6
%42 = ptrtoint i8* %40 to i64
%43 = ptrtoint i8* %41 to i64
%44 = sub i64 %42, %43
%45 = getelementptr inbounds i8, i8* %18, i64 %44
%46 = load i8*, i8** %8
%47 = load i8*, i8** %12
%48 = load i8*, i8** %7
%49 = call i32 @strlen(i8* %48)
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
%52 = call i32 @sprintf(i8* %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %46, i8* %51)
store i8* %18, i8** %14
%53 = load i8*, i8** %14
store i8* %53, i8** %5
store i32 1, i32* %13
br label %54
54: ; preds = %25, %23
%55 = load i8*, i8** %10
%56 = load i8*, i8** %5
ret i8* %56
}

LLVM IR (-O2):
@.str = [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 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %20, label %9
9: ; preds = %4
%10 = ptrtoint i8* %7 to i64
%11 = ptrtoint i8* %0 to i64
%12 = sub i64 %10, %11
%13 = trunc i64 %12 to i32
%14 = call i32 @strncpy(i8* %6, i8* %0, i32 %13)
%15 = getelementptr inbounds i8, i8* %6, i64 %12
store i8 0, i8* %15
%16 = call i32 @strlen(i8* %1)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %7, i64 %17
%19 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %18)
br label %20
20: ; preds = %4, %9
%21 = phi i8* [ %6, %9 ], [ %0, %4 ]
ret i8* %21
}