#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*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
scalar_t__ realloc(char *, int);
size_t strlen(char const *);
char *escape(const char *str, size_t *reslenp, char **resbufp) {
  size_t reslen = *reslenp;
  char *resbuf = *resbufp;
  size_t len = strlen(str);
  char *wp;
  if (2 * len + 1 > reslen) {
    resbuf = (char *)realloc(resbuf, 2 * len + 1);
    if (resbuf == NULL)
      error(EXIT_FAILURE, errno, "while allocating buffer for printing");
    *reslenp = 2 * len + 1;
    *resbufp = resbuf;
  }
  wp = resbuf;
  while (*str != '\0')
    if (*str == '\t') {
      *wp++ = '\\';
      *wp++ = 't';
      ++str;
    } else if (*str == '\n') {
      *wp++ = '\\';
      *wp++ = 'n';
      ++str;
    } else if (*str == '"') {
      *wp++ = '\\';
      *wp++ = '"';
      ++str;
    } else if (*str == '\\') {
      *wp++ = '\\';
      *wp++ = '\\';
      ++str;
    } else
      *wp++ = *str++;
  *wp = '\0';
  return resbuf;
}


x86 assembly (clang, -O0):
<escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
cmp    -0x20(%rbp),%rax
jbe    11df <escape+0xaf>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c1 <escape+0x91>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    0xe44(%rip),%rdx
call   1050 <error@plt>
mov    -0x30(%rbp),%rcx
shl    $0x1,%rcx
add    $0x1,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1341 <escape+0x211>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x9,%eax
jne    123c <escape+0x10c>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x5c,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x74,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    133c <escape+0x20c>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1281 <escape+0x151>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x5c,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x6e,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1337 <escape+0x207>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
jne    12c6 <escape+0x196>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x5c,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x22,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1332 <escape+0x202>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
jne    130b <escape+0x1db>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x5c,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    132d <escape+0x1fd>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,(%rax)
jmp    1332 <escape+0x202>
jmp    1337 <escape+0x207>
jmp    133c <escape+0x20c>
jmp    11e7 <escape+0xb7>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
mov    (%rsi),%rbp
mov    (%rdx),%r14
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%r13
add    $0x1,%r13
cmp    %rbp,%r13
jbe    1194 <escape+0x64>
mov    %r14,%rdi
mov    %r13d,%esi
call   1040 <realloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    118d <escape+0x5d>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    0xf04(%rip),%rdx
call   1050 <error@plt>
mov    %r13,(%r12)
mov    %r14,(%r15)
lea    0xe65(%rip),%rax
mov    %r14,%rcx
jmp    11ad <escape+0x7d>
movw   $0x745c,(%rcx)
add    $0x2,%rcx
add    $0x1,%rbx
movzbl (%rbx),%edx
cmp    $0x22,%rdx
ja     11d0 <escape+0xa0>
movslq (%rax,%rdx,4),%rsi
add    %rax,%rsi
jmp    *%rsi
movw   $0x6e5c,(%rcx)
jmp    11a5 <escape+0x75>
cs nopw 0x0(%rax,%rax,1)
cmp    $0x5c,%dl
jne    11e0 <escape+0xb0>
movw   $0x5c5c,(%rcx)
jmp    11a5 <escape+0x75>
nopl   0x0(%rax)
mov    %dl,(%rcx)
add    $0x1,%rcx
add    $0x1,%rbx
jmp    11ad <escape+0x7d>
nopl   0x0(%rax)
movw   $0x225c,(%rcx)
jmp    11a5 <escape+0x75>
movb   $0x0,(%rcx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [37 x i8] c"while allocating buffer for printing\00"
define i8* @escape(i8* %0, i64* %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
store i8** %2, i8*** %6
%11 = load i64*, i64** %5
%12 = load i64, i64* %11
store i64 %12, i64* %7
%13 = load i8**, i8*** %6
%14 = load i8*, i8** %13
store i8* %14, i8** %8
%15 = load i8*, i8** %4
%16 = call i64 @strlen(i8* %15)
store i64 %16, i64* %9
%17 = load i64, i64* %9
%18 = mul i64 2, %17
%19 = add i64 %18, 1
%20 = load i64, i64* %7
%21 = icmp ugt i64 %19, %20
br i1 %21, label %22, label %43
22: ; preds = %3
%23 = load i8*, i8** %8
%24 = load i64, i64* %9
%25 = mul i64 2, %24
%26 = add i64 %25, 1
%27 = trunc i64 %26 to i32
%28 = call i64 @realloc(i8* %23, i32 %27)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %8
%30 = load i8*, i8** %8
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %36
32: ; preds = %22
%33 = load i32, i32* @EXIT_FAILURE
%34 = load i32, i32* @errno
%35 = call i32 @error(i32 %33, i32 %34, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %32, %22
%37 = load i64, i64* %9
%38 = mul i64 2, %37
%39 = add i64 %38, 1
%40 = load i64*, i64** %5
store i64 %39, i64* %40
%41 = load i8*, i8** %8
%42 = load i8**, i8*** %6
store i8* %41, i8** %42
br label %43
43: ; preds = %36, %3
%44 = load i8*, i8** %8
store i8* %44, i8** %10
br label %45
45: ; preds = %107, %43
%46 = load i8*, i8** %4
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %108
50: ; preds = %45
%51 = load i8*, i8** %4
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 9
br i1 %54, label %55, label %62
55: ; preds = %50
%56 = load i8*, i8** %10
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %10
store i8 92, i8* %56
%58 = load i8*, i8** %10
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %10
store i8 116, i8* %58
%60 = load i8*, i8** %4
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %4
br label %107
62: ; preds = %50
%63 = load i8*, i8** %4
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = icmp eq i32 %65, 10
br i1 %66, label %67, label %74
67: ; preds = %62
%68 = load i8*, i8** %10
%69 = getelementptr inbounds i8, i8* %68, i32 1
store i8* %69, i8** %10
store i8 92, i8* %68
%70 = load i8*, i8** %10
%71 = getelementptr inbounds i8, i8* %70, i32 1
store i8* %71, i8** %10
store i8 110, i8* %70
%72 = load i8*, i8** %4
%73 = getelementptr inbounds i8, i8* %72, i32 1
store i8* %73, i8** %4
br label %106
74: ; preds = %62
%75 = load i8*, i8** %4
%76 = load i8, i8* %75
%77 = sext i8 %76 to i32
%78 = icmp eq i32 %77, 34
br i1 %78, label %79, label %86
79: ; preds = %74
%80 = load i8*, i8** %10
%81 = getelementptr inbounds i8, i8* %80, i32 1
store i8* %81, i8** %10
store i8 92, i8* %80
%82 = load i8*, i8** %10
%83 = getelementptr inbounds i8, i8* %82, i32 1
store i8* %83, i8** %10
store i8 34, i8* %82
%84 = load i8*, i8** %4
%85 = getelementptr inbounds i8, i8* %84, i32 1
store i8* %85, i8** %4
br label %105
86: ; preds = %74
%87 = load i8*, i8** %4
%88 = load i8, i8* %87
%89 = sext i8 %88 to i32
%90 = icmp eq i32 %89, 92
br i1 %90, label %91, label %98
91: ; preds = %86
%92 = load i8*, i8** %10
%93 = getelementptr inbounds i8, i8* %92, i32 1
store i8* %93, i8** %10
store i8 92, i8* %92
%94 = load i8*, i8** %10
%95 = getelementptr inbounds i8, i8* %94, i32 1
store i8* %95, i8** %10
store i8 92, i8* %94
%96 = load i8*, i8** %4
%97 = getelementptr inbounds i8, i8* %96, i32 1
store i8* %97, i8** %4
br label %104
98: ; preds = %86
%99 = load i8*, i8** %4
%100 = getelementptr inbounds i8, i8* %99, i32 1
store i8* %100, i8** %4
%101 = load i8, i8* %99
%102 = load i8*, i8** %10
%103 = getelementptr inbounds i8, i8* %102, i32 1
store i8* %103, i8** %10
store i8 %101, i8* %102
br label %104
104: ; preds = %98, %91
br label %105
105: ; preds = %104, %79
br label %106
106: ; preds = %105, %67
br label %107
107: ; preds = %106, %55
br label %45
108: ; preds = %45
%109 = load i8*, i8** %10
store i8 0, i8* %109
%110 = load i8*, i8** %8
ret i8* %110
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [37 x i8] c"while allocating buffer for printing\00"
define i8* @escape(i8* nocapture %0, i64* nocapture %1, i8** nocapture %2) {
%4 = load i64, i64* %1
%5 = load i8*, i8** %2
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = shl i64 %6, 1
%8 = or i64 %7, 1
%9 = icmp ugt i64 %8, %4
br i1 %9, label %10, label %20
10: ; preds = %3
%11 = trunc i64 %8 to i32
%12 = tail call i64 @realloc(i8* %5, i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32, i32* @EXIT_FAILURE
%17 = load i32, i32* @errno
%18 = tail call i32 @error(i32 %16, i32 %17, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %15, %10
store i64 %8, i64* %1
store i8* %13, i8** %2
br label %20
20: ; preds = %19, %3
%21 = phi i8* [ %13, %19 ], [ %5, %3 ]
br label %22
22: ; preds = %40, %20
%23 = phi i8* [ %0, %20 ], [ %42, %40 ]
%24 = phi i8* [ %21, %20 ], [ %41, %40 ]
%25 = load i8, i8* %23
switch i8 %25, label %38 [
i8 0, label %43
i8 9, label %26
i8 10, label %29
i8 34, label %32
i8 92, label %35
]
26: ; preds = %22
%27 = getelementptr inbounds i8, i8* %24, i64 1
store i8 92, i8* %24
%28 = getelementptr inbounds i8, i8* %24, i64 2
store i8 116, i8* %27
br label %40
29: ; preds = %22
%30 = getelementptr inbounds i8, i8* %24, i64 1
store i8 92, i8* %24
%31 = getelementptr inbounds i8, i8* %24, i64 2
store i8 110, i8* %30
br label %40
32: ; preds = %22
%33 = getelementptr inbounds i8, i8* %24, i64 1
store i8 92, i8* %24
%34 = getelementptr inbounds i8, i8* %24, i64 2
store i8 34, i8* %33
br label %40
35: ; preds = %22
%36 = getelementptr inbounds i8, i8* %24, i64 1
store i8 92, i8* %24
%37 = getelementptr inbounds i8, i8* %24, i64 2
store i8 92, i8* %36
br label %40
38: ; preds = %22
%39 = getelementptr inbounds i8, i8* %24, i64 1
store i8 %25, i8* %24
br label %40
40: ; preds = %29, %35, %38, %32, %26
%41 = phi i8* [ %28, %26 ], [ %31, %29 ], [ %34, %32 ], [ %37, %35 ], [ %39, %38 ]
%42 = getelementptr inbounds i8, i8* %23, i64 1
br label %22
43: ; preds = %22
store i8 0, i8* %24
ret i8* %21
}