#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*/ check_capacity(char **, size_t *, size_t, char **,
                                  int /*<<< orphan*/);
char *lua_quote(char *str, size_t ilen, size_t *len) {
  static char *buf = NULL;
  static size_t buf_cap = 0;
  const char Q = '\'';
  const char E = '\\';
  check_capacity(&buf, &buf_cap, ilen, 0, 0);
  if (len)
    *len = 0;
  if (buf) {
    size_t i = 0;
    char *p = buf;
    p[i++] = Q;
    while (ilen > 0) {
      check_capacity(&buf, &buf_cap, i + 8, &p, 0);
      if (!buf)
        return buf;
      if (*str == Q) {
        p[i++] = E;
        p[i++] = Q;
      } else if (*str == E) {
        p[i++] = E;
        p[i++] = E;
      } else {
        p[i++] = *str;
      }
      str++;
      ilen--;
    }
    p[i++] = Q;
    p[i] = 0;
    if (len)
      *len = i;
  }
  return buf;
}


x86 assembly (clang, -O0):
<lua_quote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movb   $0x27,-0x21(%rbp)
movb   $0x5c,-0x22(%rbp)
mov    -0x18(%rbp),%rdx
lea    0x2ef9(%rip),%rdi
lea    0x2efa(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1030 <check_capacity@plt>
cmpq   $0x0,-0x20(%rbp)
je     1160 <lua_quote+0x50>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,0x2ec8(%rip)
je     12e5 <lua_quote+0x1d5>
movq   $0x0,-0x30(%rbp)
mov    0x2eb3(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x27,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
jbe    12a7 <lua_quote+0x197>
mov    -0x30(%rbp),%rdx
add    $0x8,%rdx
lea    0x2e7e(%rip),%rdi
lea    0x2e7f(%rip),%rsi
lea    -0x38(%rbp),%rcx
xor    %r8d,%r8d
call   1030 <check_capacity@plt>
cmpq   $0x0,0x2e63(%rip)
jne    11e3 <lua_quote+0xd3>
mov    0x2e56(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12f0 <lua_quote+0x1e0>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x27,%eax
jne    1226 <lua_quote+0x116>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x5c,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x27,(%rax,%rcx,1)
jmp    128a <lua_quote+0x17a>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
jne    1269 <lua_quote+0x159>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x5c,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x5c,(%rax,%rcx,1)
jmp    1285 <lua_quote+0x175>
mov    -0x10(%rbp),%rax
mov    (%rax),%dl
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x30(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    128a <lua_quote+0x17a>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1198 <lua_quote+0x88>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x27,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x20(%rbp)
je     12e0 <lua_quote+0x1d0>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    12e5 <lua_quote+0x1d5>
mov    0x2d44(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lua_quote>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x2f02(%rip),%rdi
lea    0x2f03(%rip),%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1030 <check_capacity@plt>
test   %rbp,%rbp
je     114f <lua_quote+0x3f>
movq   $0x0,0x0(%rbp)
mov    0x2eda(%rip),%rax
test   %rax,%rax
je     121e <lua_quote+0x10e>
mov    %rax,0x8(%rsp)
movb   $0x27,(%rax)
test   %rbx,%rbx
mov    %rbp,0x10(%rsp)
mov    $0x1,%r14d
je     1222 <lua_quote+0x112>
xor    %r12d,%r12d
lea    0x2eb3(%rip),%r13
lea    0x8(%rsp),%rbp
jmp    11a6 <lua_quote+0x96>
nopl   0x0(%rax)
mov    0x8(%rsp),%rcx
mov    %al,(%rcx,%r14,1)
add    $0x1,%r14
add    $0x1,%r12
cmp    %r12,%rbx
je     1222 <lua_quote+0x112>
lea    0x8(%r14),%rdx
lea    0x2e7f(%rip),%rdi
mov    %r13,%rsi
mov    %rbp,%rcx
xor    %r8d,%r8d
call   1030 <check_capacity@plt>
cmpq   $0x0,0x2e69(%rip)
je     121e <lua_quote+0x10e>
movzbl (%r15,%r12,1),%eax
cmp    $0x5c,%al
je     1200 <lua_quote+0xf0>
cmp    $0x27,%al
jne    1190 <lua_quote+0x80>
mov    0x8(%rsp),%rax
movb   $0x5c,(%rax,%r14,1)
mov    0x8(%rsp),%rax
movb   $0x27,0x1(%rax,%r14,1)
add    $0x1,%r14
jmp    1199 <lua_quote+0x89>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
movb   $0x5c,(%rax,%r14,1)
mov    0x8(%rsp),%rax
movb   $0x5c,0x1(%rax,%r14,1)
add    $0x1,%r14
jmp    1199 <lua_quote+0x89>
xor    %eax,%eax
jmp    124f <lua_quote+0x13f>
mov    0x8(%rsp),%rax
movb   $0x27,(%rax,%r14,1)
mov    0x8(%rsp),%rax
movb   $0x0,0x1(%rax,%r14,1)
mov    0x10(%rsp),%rax
test   %rax,%rax
je     1248 <lua_quote+0x138>
add    $0x1,%r14
mov    %r14,(%rax)
mov    0x2de1(%rip),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@lua_quote.buf = internal global i8* null
@lua_quote.buf_cap = internal global i64 0
define i8* @lua_quote(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 i64
%11 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
store i8 39, i8* %8
store i8 92, i8* %9
%12 = load i64, i64* %6
%13 = call i32 @check_capacity(i8** @lua_quote.buf, i64* @lua_quote.buf_cap, i64 %12, i8** null, i32 0)
%14 = load i64*, i64** %7
%15 = icmp ne i64* %14, null
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i64*, i64** %7
store i64 0, i64* %17
br label %18
18: ; preds = %16, %3
%19 = load i8*, i8** @lua_quote.buf
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %93
21: ; preds = %18
store i64 0, i64* %10
%22 = load i8*, i8** @lua_quote.buf
store i8* %22, i8** %11
%23 = load i8*, i8** %11
%24 = load i64, i64* %10
%25 = add i64 %24, 1
store i64 %25, i64* %10
%26 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 39, i8* %26
br label %27
27: ; preds = %74, %21
%28 = load i64, i64* %6
%29 = icmp ugt i64 %28, 0
br i1 %29, label %30, label %79
30: ; preds = %27
%31 = load i64, i64* %10
%32 = add i64 %31, 8
%33 = call i32 @check_capacity(i8** @lua_quote.buf, i64* @lua_quote.buf_cap, i64 %32, i8** %11, i32 0)
%34 = load i8*, i8** @lua_quote.buf
%35 = icmp ne i8* %34, null
br i1 %35, label %38, label %36
36: ; preds = %30
%37 = load i8*, i8** @lua_quote.buf
store i8* %37, i8** %4
br label %95
38: ; preds = %30
%39 = load i8*, i8** %5
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 39
br i1 %42, label %43, label %52
43: ; preds = %38
%44 = load i8*, i8** %11
%45 = load i64, i64* %10
%46 = add i64 %45, 1
store i64 %46, i64* %10
%47 = getelementptr inbounds i8, i8* %44, i64 %45
store i8 92, i8* %47
%48 = load i8*, i8** %11
%49 = load i64, i64* %10
%50 = add i64 %49, 1
store i64 %50, i64* %10
%51 = getelementptr inbounds i8, i8* %48, i64 %49
store i8 39, i8* %51
br label %74
52: ; preds = %38
%53 = load i8*, i8** %5
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 92
br i1 %56, label %57, label %66
57: ; preds = %52
%58 = load i8*, i8** %11
%59 = load i64, i64* %10
%60 = add i64 %59, 1
store i64 %60, i64* %10
%61 = getelementptr inbounds i8, i8* %58, i64 %59
store i8 92, i8* %61
%62 = load i8*, i8** %11
%63 = load i64, i64* %10
%64 = add i64 %63, 1
store i64 %64, i64* %10
%65 = getelementptr inbounds i8, i8* %62, i64 %63
store i8 92, i8* %65
br label %73
66: ; preds = %52
%67 = load i8*, i8** %5
%68 = load i8, i8* %67
%69 = load i8*, i8** %11
%70 = load i64, i64* %10
%71 = add i64 %70, 1
store i64 %71, i64* %10
%72 = getelementptr inbounds i8, i8* %69, i64 %70
store i8 %68, i8* %72
br label %73
73: ; preds = %66, %57
br label %74
74: ; preds = %73, %43
%75 = load i8*, i8** %5
%76 = getelementptr inbounds i8, i8* %75, i32 1
store i8* %76, i8** %5
%77 = load i64, i64* %6
%78 = add i64 %77, -1
store i64 %78, i64* %6
br label %27
79: ; preds = %27
%80 = load i8*, i8** %11
%81 = load i64, i64* %10
%82 = add i64 %81, 1
store i64 %82, i64* %10
%83 = getelementptr inbounds i8, i8* %80, i64 %81
store i8 39, i8* %83
%84 = load i8*, i8** %11
%85 = load i64, i64* %10
%86 = getelementptr inbounds i8, i8* %84, i64 %85
store i8 0, i8* %86
%87 = load i64*, i64** %7
%88 = icmp ne i64* %87, null
br i1 %88, label %89, label %92
89: ; preds = %79
%90 = load i64, i64* %10
%91 = load i64*, i64** %7
store i64 %90, i64* %91
br label %92
92: ; preds = %89, %79
br label %93
93: ; preds = %92, %18
%94 = load i8*, i8** @lua_quote.buf
store i8* %94, i8** %4
br label %95
95: ; preds = %93, %36
%96 = load i8*, i8** %4
ret i8* %96
}

LLVM IR (-O2):
@lua_quote.buf = internal global i8* null
@lua_quote.buf_cap = internal global i64 0
define i8* @lua_quote(i8* nocapture %0, i64 %1, i64* writeonly %2) {
%4 = alloca i8*
%5 = tail call i32 @check_capacity(i8** @lua_quote.buf, i64* @lua_quote.buf_cap, i64 %1, i8** null, i32 0)
%6 = icmp eq i64* %2, null
br i1 %6, label %8, label %7
7: ; preds = %3
store i64 0, i64* %2
br label %8
8: ; preds = %7, %3
%9 = load i8*, i8** @lua_quote.buf
%10 = icmp eq i8* %9, null
br i1 %10, label %56, label %11
11: ; preds = %8
%12 = bitcast i8** %4 to i8*
store i8* %9, i8** %4
store i8 39, i8* %9
%13 = icmp eq i64 %1, 0
br i1 %13, label %45, label %14
14: ; preds = %11, %39
%15 = phi i64 [ %41, %39 ], [ 1, %11 ]
%16 = phi i8* [ %42, %39 ], [ %0, %11 ]
%17 = phi i64 [ %43, %39 ], [ %1, %11 ]
%18 = add i64 %15, 8
%19 = call i32 @check_capacity(i8** @lua_quote.buf, i64* @lua_quote.buf_cap, i64 %18, i8** %4, i32 0)
%20 = load i8*, i8** @lua_quote.buf
%21 = icmp eq i8* %20, null
br i1 %21, label %55, label %22
22: ; preds = %14
%23 = load i8, i8* %16
switch i8 %23, label %36 [
i8 39, label %24
i8 92, label %30
]
24: ; preds = %22
%25 = load i8*, i8** %4
%26 = add i64 %15, 1
%27 = getelementptr inbounds i8, i8* %25, i64 %15
store i8 92, i8* %27
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i64 %26
store i8 39, i8* %29
br label %39
30: ; preds = %22
%31 = load i8*, i8** %4
%32 = add i64 %15, 1
%33 = getelementptr inbounds i8, i8* %31, i64 %15
store i8 92, i8* %33
%34 = load i8*, i8** %4
%35 = getelementptr inbounds i8, i8* %34, i64 %32
store i8 92, i8* %35
br label %39
36: ; preds = %22
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 %15
store i8 %23, i8* %38
br label %39
39: ; preds = %30, %36, %24
%40 = phi i64 [ %26, %24 ], [ %32, %30 ], [ %15, %36 ]
%41 = add i64 %40, 1
%42 = getelementptr inbounds i8, i8* %16, i64 1
%43 = add i64 %17, -1
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %14
45: ; preds = %39, %11
%46 = phi i64 [ 1, %11 ], [ %41, %39 ]
%47 = load i8*, i8** %4
%48 = add i64 %46, 1
%49 = getelementptr inbounds i8, i8* %47, i64 %46
store i8 39, i8* %49
%50 = load i8*, i8** %4
%51 = getelementptr inbounds i8, i8* %50, i64 %48
store i8 0, i8* %51
br i1 %6, label %53, label %52
52: ; preds = %45
store i64 %48, i64* %2
br label %53
53: ; preds = %45, %52
%54 = load i8*, i8** @lua_quote.buf
br label %56
55: ; preds = %14
br label %56
56: ; preds = %8, %53, %55
%57 = phi i8* [ null, %55 ], [ %54, %53 ], [ null, %8 ]
ret i8* %57
}