#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 */
typedef int wchar_t;

/* Variables and functions */
int *malloc(int);
size_t wcslen(int *);
wchar_t *escape_wcstr(wchar_t *str) {
  size_t len = wcslen(str);
  wchar_t *buf = malloc(sizeof(wchar_t) * (len * 2 + 1));
  wchar_t *p = buf;
  wchar_t c;
  while ((c = *(str++)) != L'\0') {
    switch (c) {
    case L'\n':
      *(p++) = L'\\';
      *(p++) = L'n';
      break;
    case L'\r':
      *(p++) = L'\\';
      *(p++) = L'r';
      break;
    case L'\a':
      *(p++) = L'\\';
      *(p++) = L'a';
      break;
    case L'\v':
      *(p++) = L'\\';
      *(p++) = L'v';
      break;
    case L'\t':
      *(p++) = L'\\';
      *(p++) = L't';
      break;
    case L'\f':
      *(p++) = L'\\';
      *(p++) = L'f';
      break;
    case L'\b':
      *(p++) = L'\\';
      *(p++) = L'b';
      break;
    default:
      *(p++) = c;
      break;
    }
  }
  *p = L'\0';
  return buf;
}


x86 assembly (clang, -O0):
<escape_wcstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <wcslen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     1304 <escape_wcstr+0x1e4>
mov    -0x24(%rbp),%eax
add    $0xfffffff9,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x6,%eax
ja     12eb <escape_wcstr+0x1cb>
mov    -0x30(%rbp),%rax
lea    0xe67(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x6e,(%rax)
jmp    12ff <escape_wcstr+0x1df>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x72,(%rax)
jmp    12ff <escape_wcstr+0x1df>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x61,(%rax)
jmp    12ff <escape_wcstr+0x1df>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x76,(%rax)
jmp    12ff <escape_wcstr+0x1df>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x74,(%rax)
jmp    12ff <escape_wcstr+0x1df>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x66,(%rax)
jmp    12ff <escape_wcstr+0x1df>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x20(%rbp)
movl   $0x62,(%rax)
jmp    12ff <escape_wcstr+0x1df>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x20(%rbp)
mov    %ecx,(%rax)
jmp    115c <escape_wcstr+0x3c>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_wcstr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <wcslen@plt>
lea    0x4(,%rax,8),%edi
call   1040 <malloc@plt>
lea    0xebd(%rip),%rcx
movabs $0x610000005c,%r8
movabs $0x620000005c,%r9
movabs $0x740000005c,%r10
movabs $0x6e0000005c,%r11
movabs $0x760000005c,%r14
movabs $0x660000005c,%r15
movabs $0x720000005c,%r12
mov    %rax,%rdx
jmp    11a0 <escape_wcstr+0x80>
mov    %r9,(%rdx)
add    $0x8,%rdx
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rbx),%edi
add    $0x4,%rbx
cmp    $0xd,%rdi
ja     11be <escape_wcstr+0x9e>
movslq (%rcx,%rdi,4),%rsi
add    %rcx,%rsi
jmp    *%rsi
mov    %r8,(%rdx)
add    $0x8,%rdx
jmp    11a0 <escape_wcstr+0x80>
mov    %edi,(%rdx)
add    $0x4,%rdx
jmp    11a0 <escape_wcstr+0x80>
mov    %r10,(%rdx)
add    $0x8,%rdx
jmp    11a0 <escape_wcstr+0x80>
mov    %r11,(%rdx)
add    $0x8,%rdx
jmp    11a0 <escape_wcstr+0x80>
mov    %r14,(%rdx)
add    $0x8,%rdx
jmp    11a0 <escape_wcstr+0x80>
mov    %r15,(%rdx)
add    $0x8,%rdx
jmp    11a0 <escape_wcstr+0x80>
mov    %r12,(%rdx)
add    $0x8,%rdx
jmp    11a0 <escape_wcstr+0x80>
movl   $0x0,(%rdx)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @escape_wcstr(i32* %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
store i32* %0, i32** %2
%7 = load i32*, i32** %2
%8 = call i64 @wcslen(i32* %7)
store i64 %8, i64* %3
%9 = load i64, i64* %3
%10 = mul i64 %9, 2
%11 = add i64 %10, 1
%12 = mul i64 4, %11
%13 = trunc i64 %12 to i32
%14 = call i32* @malloc(i32 %13)
store i32* %14, i32** %4
%15 = load i32*, i32** %4
store i32* %15, i32** %5
br label %16
16: ; preds = %62, %1
%17 = load i32*, i32** %2
%18 = getelementptr inbounds i32, i32* %17, i32 1
store i32* %18, i32** %2
%19 = load i32, i32* %17
store i32 %19, i32* %6
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %63
21: ; preds = %16
%22 = load i32, i32* %6
switch i32 %22, label %58 [
i32 10, label %23
i32 13, label %28
i32 7, label %33
i32 11, label %38
i32 9, label %43
i32 12, label %48
i32 8, label %53
]
23: ; preds = %21
%24 = load i32*, i32** %5
%25 = getelementptr inbounds i32, i32* %24, i32 1
store i32* %25, i32** %5
store i32 92, i32* %24
%26 = load i32*, i32** %5
%27 = getelementptr inbounds i32, i32* %26, i32 1
store i32* %27, i32** %5
store i32 110, i32* %26
br label %62
28: ; preds = %21
%29 = load i32*, i32** %5
%30 = getelementptr inbounds i32, i32* %29, i32 1
store i32* %30, i32** %5
store i32 92, i32* %29
%31 = load i32*, i32** %5
%32 = getelementptr inbounds i32, i32* %31, i32 1
store i32* %32, i32** %5
store i32 114, i32* %31
br label %62
33: ; preds = %21
%34 = load i32*, i32** %5
%35 = getelementptr inbounds i32, i32* %34, i32 1
store i32* %35, i32** %5
store i32 92, i32* %34
%36 = load i32*, i32** %5
%37 = getelementptr inbounds i32, i32* %36, i32 1
store i32* %37, i32** %5
store i32 97, i32* %36
br label %62
38: ; preds = %21
%39 = load i32*, i32** %5
%40 = getelementptr inbounds i32, i32* %39, i32 1
store i32* %40, i32** %5
store i32 92, i32* %39
%41 = load i32*, i32** %5
%42 = getelementptr inbounds i32, i32* %41, i32 1
store i32* %42, i32** %5
store i32 118, i32* %41
br label %62
43: ; preds = %21
%44 = load i32*, i32** %5
%45 = getelementptr inbounds i32, i32* %44, i32 1
store i32* %45, i32** %5
store i32 92, i32* %44
%46 = load i32*, i32** %5
%47 = getelementptr inbounds i32, i32* %46, i32 1
store i32* %47, i32** %5
store i32 116, i32* %46
br label %62
48: ; preds = %21
%49 = load i32*, i32** %5
%50 = getelementptr inbounds i32, i32* %49, i32 1
store i32* %50, i32** %5
store i32 92, i32* %49
%51 = load i32*, i32** %5
%52 = getelementptr inbounds i32, i32* %51, i32 1
store i32* %52, i32** %5
store i32 102, i32* %51
br label %62
53: ; preds = %21
%54 = load i32*, i32** %5
%55 = getelementptr inbounds i32, i32* %54, i32 1
store i32* %55, i32** %5
store i32 92, i32* %54
%56 = load i32*, i32** %5
%57 = getelementptr inbounds i32, i32* %56, i32 1
store i32* %57, i32** %5
store i32 98, i32* %56
br label %62
58: ; preds = %21
%59 = load i32, i32* %6
%60 = load i32*, i32** %5
%61 = getelementptr inbounds i32, i32* %60, i32 1
store i32* %61, i32** %5
store i32 %59, i32* %60
br label %62
62: ; preds = %58, %53, %48, %43, %38, %33, %28, %23
br label %16
63: ; preds = %16
%64 = load i32*, i32** %5
store i32 0, i32* %64
%65 = load i32*, i32** %4
ret i32* %65
}

LLVM IR (-O2):
define i32* @escape_wcstr(i32* nocapture %0) {
%2 = tail call i64 @wcslen(i32* %0)
%3 = trunc i64 %2 to i32
%4 = shl i32 %3, 3
%5 = or i32 %4, 4
%6 = tail call i32* @malloc(i32 %5)
br label %7
7: ; preds = %35, %1
%8 = phi i32* [ %6, %1 ], [ %36, %35 ]
%9 = phi i32* [ %0, %1 ], [ %10, %35 ]
%10 = getelementptr inbounds i32, i32* %9, i64 1
%11 = load i32, i32* %9
switch i32 %11, label %33 [
i32 0, label %37
i32 10, label %12
i32 13, label %15
i32 7, label %18
i32 11, label %21
i32 9, label %24
i32 12, label %27
i32 8, label %30
]
12: ; preds = %7
%13 = getelementptr inbounds i32, i32* %8, i64 1
store i32 92, i32* %8
%14 = getelementptr inbounds i32, i32* %8, i64 2
store i32 110, i32* %13
br label %35
15: ; preds = %7
%16 = getelementptr inbounds i32, i32* %8, i64 1
store i32 92, i32* %8
%17 = getelementptr inbounds i32, i32* %8, i64 2
store i32 114, i32* %16
br label %35
18: ; preds = %7
%19 = getelementptr inbounds i32, i32* %8, i64 1
store i32 92, i32* %8
%20 = getelementptr inbounds i32, i32* %8, i64 2
store i32 97, i32* %19
br label %35
21: ; preds = %7
%22 = getelementptr inbounds i32, i32* %8, i64 1
store i32 92, i32* %8
%23 = getelementptr inbounds i32, i32* %8, i64 2
store i32 118, i32* %22
br label %35
24: ; preds = %7
%25 = getelementptr inbounds i32, i32* %8, i64 1
store i32 92, i32* %8
%26 = getelementptr inbounds i32, i32* %8, i64 2
store i32 116, i32* %25
br label %35
27: ; preds = %7
%28 = getelementptr inbounds i32, i32* %8, i64 1
store i32 92, i32* %8
%29 = getelementptr inbounds i32, i32* %8, i64 2
store i32 102, i32* %28
br label %35
30: ; preds = %7
%31 = getelementptr inbounds i32, i32* %8, i64 1
store i32 92, i32* %8
%32 = getelementptr inbounds i32, i32* %8, i64 2
store i32 98, i32* %31
br label %35
33: ; preds = %7
%34 = getelementptr inbounds i32, i32* %8, i64 1
store i32 %11, i32* %8
br label %35
35: ; preds = %33, %30, %27, %24, %21, %18, %15, %12
%36 = phi i32* [ %34, %33 ], [ %32, %30 ], [ %29, %27 ], [ %26, %24 ], [ %23, %21 ], [ %20, %18 ], [ %17, %15 ], [ %14, %12 ]
br label %7
37: ; preds = %7
store i32 0, i32* %8
ret i32* %6
}