#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*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ strcat(char *, char const *);
int strlen(char const *);
char *strstr(char const *, char const *);
scalar_t__ xmalloc(int);
char *strsub(const char *s, const char *search, const char *replace) {
  char *p;
  int len = strlen(s);
  int slen = strlen(search);
  int rlen = strlen(replace);
  char *d;
  p = strstr(s, search);
  if (!slen || !p) {
    return NULL;
  }
  d = (char *)xmalloc(len + rlen);
  len = p - s;
  memcpy(d, s, len);
  d[len] = 0;
  strcat(d, replace);
  strcat(d, p + slen);
  return d;
}


x86 assembly (clang, -O0):
<strsub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     11ae <strsub+0x5e>
cmpq   $0x0,-0x28(%rbp)
jne    11bb <strsub+0x6b>
movq   $0x0,-0x8(%rbp)
jmp    121d <strsub+0xcd>
mov    -0x2c(%rbp),%edi
add    -0x34(%rbp),%edi
call   1040 <xmalloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x40(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
movslq -0x30(%rbp),%rax
add    %rax,%rsi
call   1060 <strcat@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strsub>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,0x4(%rsp)
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %r14,%rdi
mov    %rbp,%rsi
call   1070 <strstr@plt>
xor    %ebp,%ebp
test   %r15d,%r15d
je     11e4 <strsub+0x94>
mov    %rax,%r12
test   %rax,%rax
je     11e4 <strsub+0x94>
add    0x4(%rsp),%r13d
mov    %r13d,%edi
call   1040 <xmalloc@plt>
mov    %rax,%rbp
mov    %rbx,%r13
mov    %r12,%rbx
sub    %r14,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
movslq %ebx,%rax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,%rdi
mov    %r13,%rsi
call   1060 <strcat@plt>
movslq %r15d,%rsi
add    %r12,%rsi
mov    %rbp,%rdi
call   1060 <strcat@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @strsub(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %9
%15 = load i8*, i8** %6
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %10
%17 = load i8*, i8** %7
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %11
%19 = load i8*, i8** %5
%20 = load i8*, i8** %6
%21 = call i8* @strstr(i8* %19, i8* %20)
store i8* %21, i8** %8
%22 = load i32, i32* %10
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %3
%25 = load i8*, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %28, label %27
27: ; preds = %24, %3
store i8* null, i8** %4
br label %58
28: ; preds = %24
%29 = load i32, i32* %9
%30 = load i32, i32* %11
%31 = add nsw i32 %29, %30
%32 = call i64 @xmalloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %12
%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 = trunc i64 %38 to i32
store i32 %39, i32* %9
%40 = load i8*, i8** %12
%41 = load i8*, i8** %5
%42 = load i32, i32* %9
%43 = call i32 @memcpy(i8* %40, i8* %41, i32 %42)
%44 = load i8*, i8** %12
%45 = load i32, i32* %9
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %12
%49 = load i8*, i8** %7
%50 = call i32 @strcat(i8* %48, i8* %49)
%51 = load i8*, i8** %12
%52 = load i8*, i8** %8
%53 = load i32, i32* %10
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = call i32 @strcat(i8* %51, i8* %55)
%57 = load i8*, i8** %12
store i8* %57, i8** %4
br label %58
58: ; preds = %28, %27
%59 = load i8*, i8** %4
ret i8* %59
}

LLVM IR (-O2):
define i8* @strsub(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i32 @strlen(i8* %2)
%7 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%8 = icmp ne i32 %5, 0
%9 = icmp ne i8* %7, null
%10 = select i1 %8, i1 %9, i1 false
br i1 %10, label %11, label %27
11: ; preds = %3
%12 = add nsw i32 %6, %4
%13 = tail call i64 @xmalloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = ptrtoint i8* %7 to i64
%16 = ptrtoint i8* %0 to i64
%17 = sub i64 %15, %16
%18 = trunc i64 %17 to i32
%19 = tail call i32 @memcpy(i8* %14, i8* %0, i32 %18)
%20 = shl i64 %17, 32
%21 = ashr exact i64 %20, 32
%22 = getelementptr inbounds i8, i8* %14, i64 %21
store i8 0, i8* %22
%23 = tail call i32 @strcat(i8* %14, i8* %2)
%24 = sext i32 %5 to i64
%25 = getelementptr inbounds i8, i8* %7, i64 %24
%26 = tail call i32 @strcat(i8* %14, i8* %25)
br label %27
27: ; preds = %3, %11
%28 = phi i8* [ %14, %11 ], [ null, %3 ]
ret i8* %28
}