#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 */
char *Strsafe_find(char *);
int /*<<< orphan*/ Strsafe_insert(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *Strsafe(char *y) {
  char *z;
  if (y == 0)
    return 0;
  z = Strsafe_find(y);
  if (z == 0 && (z = malloc(strlen(y) + 1)) != 0) {
    strcpy(z, y);
    Strsafe_insert(z);
  }
  if ((z) == 0) {
    extern void memory_error();
    memory_error();
  };
  return z;
}


x86 assembly (clang, -O0):
<Strsafe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1184 <Strsafe+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11ef <Strsafe+0x8f>
mov    -0x10(%rbp),%rdi
call   1030 <Strsafe_find@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d5 <Strsafe+0x75>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11d5 <Strsafe+0x75>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1070 <Strsafe_insert@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11e7 <Strsafe+0x87>
mov    $0x0,%al
call   1080 <memory_error@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Strsafe>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11a7 <Strsafe+0x47>
mov    %rdi,%r14
call   1030 <Strsafe_find@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b4 <Strsafe+0x54>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11ab <Strsafe+0x4b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1070 <Strsafe_insert@plt>
jmp    11b4 <Strsafe+0x54>
xor    %ebx,%ebx
jmp    11b4 <Strsafe+0x54>
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <memory_error@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @Strsafe(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %15, label %3
3: ; preds = %1
%4 = tail call i8* @Strsafe_find(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %15
6: ; preds = %3
%7 = tail call i64 @strlen(i8* %0)
%8 = add nsw i64 %7, 1
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = tail call i32 @strcpy(i8* %9, i8* %0)
%13 = tail call i32 @Strsafe_insert(i8* %9)
br label %15
14: ; preds = %6
tail call void (...) @memory_error()
br label %15
15: ; preds = %3, %11, %14, %1
%16 = phi i8* [ null, %1 ], [ null, %14 ], [ %4, %3 ], [ %9, %11 ]
ret i8* %16
}