#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);
char *EscapeLuaString(const char *text, size_t length, size_t *encoded_length) {
  const char *s = text;
  const char *end = s + length;
  char *t;
  char *buffer = malloc(length * 2 + 1);
  if (buffer == NULL)
    return NULL;
  t = buffer;
  while (s < end) {
    switch (*s) {
    case '\0':
      *t++ = '\\';
      *t++ = '0';
      break;
    case '\a':
      *t++ = '\\';
      *t++ = 'a';
      break;
    case '\b':
      *t++ = '\\';
      *t++ = 'b';
      break;
    case '\f':
      *t++ = '\\';
      *t++ = 'f';
      break;
    case '\n':
      *t++ = '\\';
      *t++ = '\n';
      break;
    case '\r':
      *t++ = '\\';
      *t++ = 'r';
      break;
    case '\t':
      *t++ = '\\';
      *t++ = 't';
      break;
    case '\v':
      *t++ = '\\';
      *t++ = 'v';
      break;
    case '\\':
      *t++ = '\\';
      *t++ = '\\';
      break;
    case '\'':
      *t++ = '\\';
      *t++ = '\'';
      break;
    case '\"':
      *t++ = '\\';
      *t++ = '\"';
      break;
    default:
      *t++ = *s;
      break;
    }
    s++;
  }
  *encoded_length = t - buffer;
  return buffer;
}


x86 assembly (clang, -O0):
<EscapeLuaString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x1,%rdi
add    $0x1,%rdi
call   1030 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1165 <EscapeLuaString+0x55>
movq   $0x0,-0x8(%rbp)
jmp    13aa <EscapeLuaString+0x29a>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1390 <EscapeLuaString+0x280>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,%ecx
mov    %rcx,-0x48(%rbp)
sub    $0x5c,%eax
ja     1368 <EscapeLuaString+0x258>
mov    -0x48(%rbp),%rax
lea    0xe64(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
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   $0x30,(%rax)
jmp    137f <EscapeLuaString+0x26f>
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   $0x61,(%rax)
jmp    137f <EscapeLuaString+0x26f>
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   $0x62,(%rax)
jmp    137f <EscapeLuaString+0x26f>
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   $0x66,(%rax)
jmp    137f <EscapeLuaString+0x26f>
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   $0xa,(%rax)
jmp    137f <EscapeLuaString+0x26f>
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   $0x72,(%rax)
jmp    137f <EscapeLuaString+0x26f>
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)
jmp    137f <EscapeLuaString+0x26f>
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   $0x76,(%rax)
jmp    137f <EscapeLuaString+0x26f>
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)
jmp    137f <EscapeLuaString+0x26f>
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   $0x27,(%rax)
jmp    137f <EscapeLuaString+0x26f>
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)
jmp    137f <EscapeLuaString+0x26f>
mov    -0x28(%rbp),%rax
mov    (%rax),%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    116d <EscapeLuaString+0x5d>
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%rax
sub    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EscapeLuaString>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    (%rsi,%rsi,1),%rdi
add    $0x1,%rdi
call   1030 <malloc@plt>
test   %rax,%rax
je     1206 <EscapeLuaString+0xf6>
mov    %rax,%rcx
test   %r15,%r15
jle    1200 <EscapeLuaString+0xf0>
add    %rbx,%r15
lea    0xeb6(%rip),%rdx
mov    %rax,%rcx
jmp    116d <EscapeLuaString+0x5d>
mov    %sil,(%rcx)
add    $0x1,%rcx
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %r15,%rbx
jae    1200 <EscapeLuaString+0xf0>
movsbl (%rbx),%esi
cmp    $0x5c,%esi
ja     114f <EscapeLuaString+0x3f>
movslq (%rdx,%rsi,4),%rdi
add    %rdx,%rdi
jmp    *%rdi
movw   $0x305c,(%rcx)
add    $0x2,%rcx
jmp    1160 <EscapeLuaString+0x50>
movw   $0x615c,(%rcx)
add    $0x2,%rcx
jmp    1160 <EscapeLuaString+0x50>
movw   $0x625c,(%rcx)
add    $0x2,%rcx
jmp    1160 <EscapeLuaString+0x50>
movw   $0x745c,(%rcx)
add    $0x2,%rcx
jmp    1160 <EscapeLuaString+0x50>
movw   $0xa5c,(%rcx)
add    $0x2,%rcx
jmp    1160 <EscapeLuaString+0x50>
movw   $0x765c,(%rcx)
add    $0x2,%rcx
jmp    1160 <EscapeLuaString+0x50>
movw   $0x665c,(%rcx)
add    $0x2,%rcx
jmp    1160 <EscapeLuaString+0x50>
movw   $0x725c,(%rcx)
add    $0x2,%rcx
jmp    1160 <EscapeLuaString+0x50>
movw   $0x225c,(%rcx)
add    $0x2,%rcx
jmp    1160 <EscapeLuaString+0x50>
movw   $0x275c,(%rcx)
add    $0x2,%rcx
jmp    1160 <EscapeLuaString+0x50>
movw   $0x5c5c,(%rcx)
add    $0x2,%rcx
jmp    1160 <EscapeLuaString+0x50>
sub    %rax,%rcx
mov    %rcx,(%r14)
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @EscapeLuaString(i8* %0, i64 %1, i64* nocapture writeonly %2) {
%4 = getelementptr inbounds i8, i8* %0, i64 %1
%5 = shl i64 %1, 1
%6 = or i64 %5, 1
%7 = tail call i8* @malloc(i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %60, label %9
9: ; preds = %3
%10 = icmp sgt i64 %1, 0
br i1 %10, label %11, label %55
11: ; preds = %9, %51
%12 = phi i8* [ %52, %51 ], [ %7, %9 ]
%13 = phi i8* [ %53, %51 ], [ %0, %9 ]
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
switch i32 %15, label %49 [
i32 0, label %16
i32 7, label %19
i32 8, label %22
i32 12, label %25
i32 10, label %28
i32 13, label %31
i32 9, label %34
i32 11, label %37
i32 92, label %40
i32 39, label %43
i32 34, label %46
]
16: ; preds = %11
%17 = getelementptr inbounds i8, i8* %12, i64 1
store i8 92, i8* %12
%18 = getelementptr inbounds i8, i8* %12, i64 2
store i8 48, i8* %17
br label %51
19: ; preds = %11
%20 = getelementptr inbounds i8, i8* %12, i64 1
store i8 92, i8* %12
%21 = getelementptr inbounds i8, i8* %12, i64 2
store i8 97, i8* %20
br label %51
22: ; preds = %11
%23 = getelementptr inbounds i8, i8* %12, i64 1
store i8 92, i8* %12
%24 = getelementptr inbounds i8, i8* %12, i64 2
store i8 98, i8* %23
br label %51
25: ; preds = %11
%26 = getelementptr inbounds i8, i8* %12, i64 1
store i8 92, i8* %12
%27 = getelementptr inbounds i8, i8* %12, i64 2
store i8 102, i8* %26
br label %51
28: ; preds = %11
%29 = getelementptr inbounds i8, i8* %12, i64 1
store i8 92, i8* %12
%30 = getelementptr inbounds i8, i8* %12, i64 2
store i8 10, i8* %29
br label %51
31: ; preds = %11
%32 = getelementptr inbounds i8, i8* %12, i64 1
store i8 92, i8* %12
%33 = getelementptr inbounds i8, i8* %12, i64 2
store i8 114, i8* %32
br label %51
34: ; preds = %11
%35 = getelementptr inbounds i8, i8* %12, i64 1
store i8 92, i8* %12
%36 = getelementptr inbounds i8, i8* %12, i64 2
store i8 116, i8* %35
br label %51
37: ; preds = %11
%38 = getelementptr inbounds i8, i8* %12, i64 1
store i8 92, i8* %12
%39 = getelementptr inbounds i8, i8* %12, i64 2
store i8 118, i8* %38
br label %51
40: ; preds = %11
%41 = getelementptr inbounds i8, i8* %12, i64 1
store i8 92, i8* %12
%42 = getelementptr inbounds i8, i8* %12, i64 2
store i8 92, i8* %41
br label %51
43: ; preds = %11
%44 = getelementptr inbounds i8, i8* %12, i64 1
store i8 92, i8* %12
%45 = getelementptr inbounds i8, i8* %12, i64 2
store i8 39, i8* %44
br label %51
46: ; preds = %11
%47 = getelementptr inbounds i8, i8* %12, i64 1
store i8 92, i8* %12
%48 = getelementptr inbounds i8, i8* %12, i64 2
store i8 34, i8* %47
br label %51
49: ; preds = %11
%50 = getelementptr inbounds i8, i8* %12, i64 1
store i8 %14, i8* %12
br label %51
51: ; preds = %49, %46, %43, %40, %37, %34, %31, %28, %25, %22, %19, %16
%52 = phi i8* [ %50, %49 ], [ %48, %46 ], [ %45, %43 ], [ %42, %40 ], [ %39, %37 ], [ %36, %34 ], [ %33, %31 ], [ %30, %28 ], [ %27, %25 ], [ %24, %22 ], [ %21, %19 ], [ %18, %16 ]
%53 = getelementptr inbounds i8, i8* %13, i64 1
%54 = icmp ult i8* %53, %4
br i1 %54, label %11, label %55
55: ; preds = %51, %9
%56 = phi i8* [ %7, %9 ], [ %52, %51 ]
%57 = ptrtoint i8* %56 to i64
%58 = ptrtoint i8* %7 to i64
%59 = sub i64 %57, %58
store i64 %59, i64* %2
br label %60
60: ; preds = %3, %55
ret i8* %7
}