#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(int);
scalar_t__ strchr(char *, char);
int strlen(char *);
char *strescape(char *s, char *esc) {
  static char *b = NULL;
  char *p;
  if (b) {
    free(b);
    b = NULL;
  }
  if (!s)
    return NULL;
  b = malloc(strlen(s) * 2 + 1);
  p = b;
  for (; *s; s++) {
    if (strchr(esc, *s))
      *p++ = '\\';
    *p++ = *s;
  }
  *p = '\0';
  return b;
}


x86 assembly (clang, -O0):
<strescape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,0x2ef0(%rip)
je     1175 <strescape+0x35>
mov    0x2ee3(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2ed3(%rip)
cmpq   $0x0,-0x10(%rbp)
jne    118d <strescape+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    1228 <strescape+0xe8>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
shl    $0x1,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,0x2e9e(%rip)
mov    0x2e97(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1216 <strescape+0xd6>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movsbl (%rax),%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     11ee <strescape+0xae>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11b5 <strescape+0x75>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e24(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strescape>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2ef6(%rip),%rdi
test   %rdi,%rdi
je     1167 <strescape+0x27>
call   1030 <free@plt>
movq   $0x0,0x2ee1(%rip)
test   %r15,%r15
je     11d9 <strescape+0x99>
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rax,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,0x2ebf(%rip)
mov    (%r15),%al
test   %al,%al
je     11dd <strescape+0x9d>
add    $0x1,%r15
jmp    11b7 <strescape+0x77>
cs nopw 0x0(%rax,%rax,1)
movzbl -0x1(%r15),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
movzbl (%r15),%eax
add    $0x1,%r15
test   %al,%al
je     11d0 <strescape+0x90>
movsbl %al,%esi
mov    %r14,%rdi
call   1050 <strchr@plt>
test   %rax,%rax
je     11a0 <strescape+0x60>
movb   $0x5c,(%rbx)
add    $0x1,%rbx
jmp    11a0 <strescape+0x60>
mov    0x2e71(%rip),%rax
jmp    11e0 <strescape+0xa0>
xor    %eax,%eax
jmp    11e3 <strescape+0xa3>
mov    %rbx,%rax
movb   $0x0,(%rbx)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@strescape.b = internal global i8* null
define i8* @strescape(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** @strescape.b
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i8*, i8** @strescape.b
%11 = call i32 @free(i8* %10)
store i8* null, i8** @strescape.b
br label %12
12: ; preds = %9, %2
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12
store i8* null, i8** %3
br label %47
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
%19 = mul nsw i32 %18, 2
%20 = add nsw i32 %19, 1
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** @strescape.b
%22 = load i8*, i8** @strescape.b
store i8* %22, i8** %6
br label %23
23: ; preds = %41, %16
%24 = load i8*, i8** %4
%25 = load i8, i8* %24
%26 = icmp ne i8 %25, 0
br i1 %26, label %27, label %44
27: ; preds = %23
%28 = load i8*, i8** %5
%29 = load i8*, i8** %4
%30 = load i8, i8* %29
%31 = call i64 @strchr(i8* %28, i8 signext %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %6
store i8 92, i8* %34
br label %36
36: ; preds = %33, %27
%37 = load i8*, i8** %4
%38 = load i8, i8* %37
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %6
store i8 %38, i8* %39
br label %41
41: ; preds = %36
%42 = load i8*, i8** %4
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %4
br label %23
44: ; preds = %23
%45 = load i8*, i8** %6
store i8 0, i8* %45
%46 = load i8*, i8** @strescape.b
store i8* %46, i8** %3
br label %47
47: ; preds = %44, %15
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
@strescape.b = internal unnamed_addr global i8* null
define i8* @strescape(i8* %0, i8* %1) {
%3 = load i8*, i8** @strescape.b
%4 = icmp eq i8* %3, null
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @free(i8* %3)
store i8* null, i8** @strescape.b
br label %7
7: ; preds = %5, %2
%8 = icmp eq i8* %0, null
br i1 %8, label %36, label %9
9: ; preds = %7
%10 = tail call i32 @strlen(i8* %0)
%11 = shl nsw i32 %10, 1
%12 = or i32 %11, 1
%13 = tail call i8* @malloc(i32 %12)
store i8* %13, i8** @strescape.b
%14 = load i8, i8* %0
%15 = icmp eq i8 %14, 0
br i1 %15, label %33, label %16
16: ; preds = %9, %24
%17 = phi i8 [ %29, %24 ], [ %14, %9 ]
%18 = phi i8* [ %27, %24 ], [ %13, %9 ]
%19 = phi i8* [ %28, %24 ], [ %0, %9 ]
%20 = tail call i64 @strchr(i8* %1, i8 signext %17)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %16
%23 = getelementptr inbounds i8, i8* %18, i64 1
store i8 92, i8* %18
br label %24
24: ; preds = %22, %16
%25 = phi i8* [ %23, %22 ], [ %18, %16 ]
%26 = load i8, i8* %19
%27 = getelementptr inbounds i8, i8* %25, i64 1
store i8 %26, i8* %25
%28 = getelementptr inbounds i8, i8* %19, i64 1
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 0
br i1 %30, label %31, label %16
31: ; preds = %24
%32 = load i8*, i8** @strescape.b
br label %33
33: ; preds = %31, %9
%34 = phi i8* [ %13, %9 ], [ %32, %31 ]
%35 = phi i8* [ %13, %9 ], [ %27, %31 ]
store i8 0, i8* %35
br label %36
36: ; preds = %7, %33
%37 = phi i8* [ %34, %33 ], [ null, %7 ]
ret i8* %37
}