#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 *);
int /*<<< orphan*/ out_of_memory();
char *strdup(char const *);
int replace_string(char **dst, const char *src) {
  if (!dst) {
    return (-1);
  }
  if (*dst) {
    free(*dst);
  }
  if (!(*dst = strdup(src))) {
    out_of_memory();
  }
  return (0);
}


x86 assembly (clang, -O0):
<replace_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1157 <replace_string+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    1199 <replace_string+0x69>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1171 <replace_string+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <strdup@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1192 <replace_string+0x62>
mov    $0x0,%al
call   1040 <out_of_memory@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_string>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1169 <replace_string+0x39>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rdi,%rdi
je     114c <replace_string+0x1c>
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <strdup@plt>
mov    %rax,(%rbx)
xor    %ebx,%ebx
test   %rax,%rax
jne    116e <replace_string+0x3e>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <out_of_memory@plt>
jmp    116e <replace_string+0x3e>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @replace_string(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
%6 = load i8**, i8*** %4
%7 = icmp ne i8** %6, null
br i1 %7, label %9, label %8
8: ; preds = %2
store i32 -1, i32* %3
br label %25
9: ; preds = %2
%10 = load i8**, i8*** %4
%11 = load i8*, i8** %10
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i8**, i8*** %4
%15 = load i8*, i8** %14
%16 = call i32 @free(i8* %15)
br label %17
17: ; preds = %13, %9
%18 = load i8*, i8** %5
%19 = call i8* @strdup(i8* %18)
%20 = load i8**, i8*** %4
store i8* %19, i8** %20
%21 = icmp ne i8* %19, null
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = call i32 (...) @out_of_memory()
br label %24
24: ; preds = %22, %17
store i32 0, i32* %3
br label %25
25: ; preds = %24, %8
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
define i32 @replace_string(i8** %0, i8* nocapture %1) {
%3 = icmp eq i8** %0, null
br i1 %3, label %14, label %4
4: ; preds = %2
%5 = load i8*, i8** %0
%6 = icmp eq i8* %5, null
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = tail call i32 @free(i8* %5)
br label %9
9: ; preds = %7, %4
%10 = tail call i8* @strdup(i8* %1)
store i8* %10, i8** %0
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 (...) @out_of_memory()
br label %14
14: ; preds = %9, %12, %2
%15 = phi i32 [ -1, %2 ], [ 0, %12 ], [ 0, %9 ]
ret i32 %15
}