#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*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, char *);
scalar_t__ strlen(char *);
char *strstr(char *, char *);
char *mysub(char *src, char *pat, char *with) {
  char *s1, *dest;
  dest = malloc(strlen(src) + strlen(with) - strlen(pat) + 1);
  s1 = strstr(src, pat);
  s1[0] = '\0';
  s1 += strlen(pat);
  sprintf(dest, "%s%s%s", src, with, s1);
  free(src);
  return (dest);
}


x86 assembly (clang, -O0):
<mysub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
sub    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xe1b(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mysub>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %r12,%rbx
sub    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %r13,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %rax,%rbx
lea    0xe4b(%rip),%rsi
mov    %r12,%rdi
mov    %r13,%rdx
mov    %r14,%rcx
mov    %rbx,%r8
call   1060 <sprintf@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%s%s%s\00"
define i8* @mysub(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** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%9 = load i8*, i8** %4
%10 = call i64 @strlen(i8* %9)
%11 = load i8*, i8** %6
%12 = call i64 @strlen(i8* %11)
%13 = add nsw i64 %10, %12
%14 = load i8*, i8** %5
%15 = call i64 @strlen(i8* %14)
%16 = sub nsw i64 %13, %15
%17 = add nsw i64 %16, 1
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %8
%19 = load i8*, i8** %4
%20 = load i8*, i8** %5
%21 = call i8* @strstr(i8* %19, i8* %20)
store i8* %21, i8** %7
%22 = load i8*, i8** %7
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 0, i8* %23
%24 = load i8*, i8** %5
%25 = call i64 @strlen(i8* %24)
%26 = load i8*, i8** %7
%27 = getelementptr inbounds i8, i8* %26, i64 %25
store i8* %27, i8** %7
%28 = load i8*, i8** %8
%29 = load i8*, i8** %4
%30 = load i8*, i8** %6
%31 = load i8*, i8** %7
%32 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %29, i8* %30, i8* %31)
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
%35 = load i8*, i8** %8
ret i8* %35
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s%s%s\00"
define noalias i8* @mysub(i8* %0, i8* nocapture %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = add i64 %4, 1
%8 = add i64 %7, %5
%9 = sub i64 %8, %6
%10 = tail call i8* @malloc(i64 %9)
%11 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
store i8 0, i8* %11
%12 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%13 = getelementptr inbounds i8, i8* %11, i64 %12
%14 = tail call i32 @sprintf(i8* dereferenceable(1) %10, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %2, i8* %13)
%15 = tail call i32 @free(i8* %0)
ret i8* %10
}