#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 *malloc(size_t);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *);
char *strdup(char *);
size_t strlen(char *);
char *strescape_or_null(char *str) {
  char *escaped;
  size_t len;
  if (!str || !str[0]) {
    return strdup("NULL");
  }
  len = strlen(str);
  escaped = malloc(len + 3);
  snprintf(escaped, len + 3, "'%s'", str);
  return escaped;
}


x86 assembly (clang, -O0):
<strescape_or_null>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1164 <strescape_or_null+0x24>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1179 <strescape_or_null+0x39>
lea    0xe95(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11bb <strescape_or_null+0x7b>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x3,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x3,%rsi
mov    -0x10(%rbp),%rcx
lea    0xe57(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strescape_or_null>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1187 <strescape_or_null+0x47>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     1187 <strescape_or_null+0x47>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x3(%rax),%r14
mov    %r14,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
lea    0xe95(%rip),%rdx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe72(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <strdup@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"NULL\00"
@.str.1 = [5 x i8] c"'%s'\00"
define i8* @strescape_or_null(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = icmp ne i8 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %8, %1
%14 = call i8* @strdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %2
br label %27
15: ; preds = %8
%16 = load i8*, i8** %3
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = add i64 %18, 3
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = load i64, i64* %5
%23 = add i64 %22, 3
%24 = load i8*, i8** %3
%25 = call i32 @snprintf(i8* %21, i64 %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %4
store i8* %26, i8** %2
br label %27
27: ; preds = %15, %13
%28 = load i8*, i8** %2
ret i8* %28
}

LLVM IR (-O2):
@.str = [5 x i8] c"NULL\00"
@.str.1 = [5 x i8] c"'%s'\00"
define i8* @strescape_or_null(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %6, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3, %1
%7 = tail call dereferenceable_or_null(5) i8* @strdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %13
8: ; preds = %3
%9 = tail call i64 @strlen(i8* %0)
%10 = add i64 %9, 3
%11 = tail call i8* @malloc(i64 %10)
%12 = tail call i32 @snprintf(i8* %11, i64 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %13
13: ; preds = %8, %6
%14 = phi i8* [ %11, %8 ], [ %7, %6 ]
ret i8* %14
}