#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 *);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
char *strcpy(char *, char *);
int strlen(char *);
char *strreplace(char *present, char *new) {
  char *retval;
  int len;
  if (new == NULL) {
    free(present);
    return NULL;
  }
  len = strlen(new) + 1;
  if (present == NULL)
    retval = (char *)malloc(len);
  else
    retval = (char *)realloc(present, len);
  return strcpy(retval, new);
}


x86 assembly (clang, -O0):
<strreplace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1181 <strreplace+0x31>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11cd <strreplace+0x7d>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ac <strreplace+0x5c>
mov    -0x24(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11bc <strreplace+0x6c>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strreplace>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rsi,%rsi
je     117b <strreplace+0x2b>
mov    %rsi,%r14
mov    %rsi,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
test   %rbx,%rbx
je     1187 <strreplace+0x37>
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <realloc@plt>
jmp    118e <strreplace+0x3e>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    119c <strreplace+0x4c>
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @strreplace(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = call i32 @free(i8* %11)
store i8* null, i8** %3
br label %32
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 1
store i32 %16, i32* %7
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = load i32, i32* %7
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %6
br label %28
23: ; preds = %13
%24 = load i8*, i8** %4
%25 = load i32, i32* %7
%26 = call i64 @realloc(i8* %24, i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %6
br label %28
28: ; preds = %23, %19
%29 = load i8*, i8** %6
%30 = load i8*, i8** %5
%31 = call i8* @strcpy(i8* %29, i8* %30)
store i8* %31, i8** %3
br label %32
32: ; preds = %28, %10
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
define i8* @strreplace(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @free(i8* %0)
br label %18
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %1)
%8 = add nsw i32 %7, 1
%9 = icmp eq i8* %0, null
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i64 @malloc(i32 %8)
br label %14
12: ; preds = %6
%13 = tail call i64 @realloc(i8* %0, i32 %8)
br label %14
14: ; preds = %12, %10
%15 = phi i64 [ %11, %10 ], [ %13, %12 ]
%16 = inttoptr i64 %15 to i8*
%17 = tail call i8* @strcpy(i8* dereferenceable(1) %16, i8* %1)
br label %18
18: ; preds = %14, %4
%19 = phi i8* [ null, %4 ], [ %16, %14 ]
ret i8* %19
}