#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 *replace_one(char *st, char *orig, char *repl) {
  static char buffer[4096];
  char *ch;
  if (!st || !orig || !repl) {
    return NULL;
  }
  if (!(ch = strstr(st, orig))) {
    return st;
  }
  strncpy(buffer, st, ch - st);
  buffer[ch - st] = 0;
  sprintf(buffer + (ch - st), "%s%s", repl, ch + strlen(orig));
  return buffer;
}


x86 assembly (clang, -O0):
<replace_one>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1175 <replace_one+0x35>
cmpq   $0x0,-0x18(%rbp)
je     1175 <replace_one+0x35>
cmpq   $0x0,-0x20(%rbp)
jne    1182 <replace_one+0x42>
movq   $0x0,-0x8(%rbp)
jmp    1237 <replace_one+0xf7>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11aa <replace_one+0x6a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1237 <replace_one+0xf7>
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    0x2e8e(%rip),%rdi
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
lea    0x2e77(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
lea    0x2e61(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rcx
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xdd9(%rip),%rsi
call   1050 <sprintf@plt>
lea    0x2e1d(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_one>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r12d,%r12d
test   %rdi,%rdi
je     11bf <replace_one+0x7f>
mov    %rsi,%r15
test   %rsi,%rsi
je     11bf <replace_one+0x7f>
mov    %rdx,%r14
test   %rdx,%rdx
je     11bf <replace_one+0x7f>
mov    %rdi,%rbx
mov    %r15,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11bc <replace_one+0x7c>
mov    %rax,%r13
mov    %rax,%rbp
sub    %rbx,%rbp
lea    0x2ecd(%rip),%r12
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
lea    (%r12,%rbp,1),%rbx
movb   $0x0,0x0(%rbp,%r12,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
add    %r13,%rcx
lea    0xe51(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1050 <sprintf@plt>
jmp    11bf <replace_one+0x7f>
mov    %rbx,%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@replace_one.buffer = internal global [4096 x i8] zeroinitializer
@.str = [5 x i8] c"%s%s\00"
define i8* @replace_one(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %14, %11, %3
store i8* null, i8** %4
br label %53
18: ; preds = %14
%19 = load i8*, i8** %5
%20 = load i8*, i8** %6
%21 = call i8* @strstr(i8* %19, i8* %20)
store i8* %21, i8** %8
%22 = icmp ne i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %18
%24 = load i8*, i8** %5
store i8* %24, i8** %4
br label %53
25: ; preds = %18
%26 = load i8*, i8** %5
%27 = load i8*, i8** %8
%28 = load i8*, i8** %5
%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* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_one.buffer, i64 0, i64 0), i8* %26, i32 %32)
%34 = load i8*, i8** %8
%35 = load i8*, i8** %5
%36 = ptrtoint i8* %34 to i64
%37 = ptrtoint i8* %35 to i64
%38 = sub i64 %36, %37
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* @replace_one.buffer, i64 0, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %8
%41 = load i8*, i8** %5
%42 = ptrtoint i8* %40 to i64
%43 = ptrtoint i8* %41 to i64
%44 = sub i64 %42, %43
%45 = getelementptr inbounds i8, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_one.buffer, i64 0, i64 0), i64 %44
%46 = load i8*, i8** %7
%47 = load i8*, i8** %8
%48 = load i8*, i8** %6
%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* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_one.buffer, i64 0, i64 0), i8** %4
br label %53
53: ; preds = %25, %23, %17
%54 = load i8*, i8** %4
ret i8* %54
}

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