#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 */
struct template_buffer {
  int dummy;
};
typedef int /*<<< orphan*/ esq;

/* Variables and functions */
int /*<<< orphan*/ buf_append(struct template_buffer *, char *, int);
int /*<<< orphan*/ buf_putchar(struct template_buffer *, char);
int snprintf(char *, int, char *, char);
void luastr_escape(struct template_buffer *out, const char *s, unsigned int l,
                   int escape_xml) {
  int esl;
  char esq[8];
  char *ptr;
  for (ptr = (char *)s; ptr < (s + l); ptr++) {
    switch (*ptr) {
    case '\\':
      buf_append(out, "\\\\", 2);
      break;
    case '"':
      if (escape_xml)
        buf_append(out, "&#34;", 5);
      else
        buf_append(out, "\\\"", 2);
      break;
    case '\n':
      buf_append(out, "\\n", 2);
      break;
    case '\'':
    case '&':
    case '<':
    case '>':
      if (escape_xml) {
        esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr);
        buf_append(out, esq, esl);
        break;
      }
    default:
      buf_putchar(out, *ptr);
    }
  }
}


x86 assembly (clang, -O0):
<luastr_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%edx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    12b0 <luastr_escape+0x180>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x34(%rbp)
sub    $0xa,%eax
je     122d <luastr_escape+0xfd>
jmp    117d <luastr_escape+0x4d>
mov    -0x34(%rbp),%eax
sub    $0x22,%eax
je     11ef <luastr_escape+0xbf>
jmp    118e <luastr_escape+0x5e>
mov    -0x34(%rbp),%eax
add    $0xffffffda,%eax
sub    $0x2,%eax
jb     1247 <luastr_escape+0x117>
jmp    11a2 <luastr_escape+0x72>
mov    -0x34(%rbp),%eax
sub    $0x3c,%eax
je     1247 <luastr_escape+0x117>
jmp    11b3 <luastr_escape+0x83>
mov    -0x34(%rbp),%eax
sub    $0x3e,%eax
je     1247 <luastr_escape+0x117>
jmp    11c4 <luastr_escape+0x94>
mov    -0x34(%rbp),%eax
sub    $0x5c,%eax
jne    128a <luastr_escape+0x15a>
jmp    11d5 <luastr_escape+0xa5>
mov    -0x8(%rbp),%rdi
lea    0xe20(%rip),%rsi
mov    $0x2,%edx
call   1040 <buf_append@plt>
jmp    129a <luastr_escape+0x16a>
cmpl   $0x0,-0x18(%rbp)
je     1213 <luastr_escape+0xe3>
mov    -0x8(%rbp),%rdi
lea    0xdff(%rip),%rsi
mov    $0x5,%edx
call   1040 <buf_append@plt>
jmp    1228 <luastr_escape+0xf8>
mov    -0x8(%rbp),%rdi
lea    0xdeb(%rip),%rsi
mov    $0x2,%edx
call   1040 <buf_append@plt>
jmp    129a <luastr_escape+0x16a>
mov    -0x8(%rbp),%rdi
lea    0xdd4(%rip),%rsi
mov    $0x2,%edx
call   1040 <buf_append@plt>
jmp    129a <luastr_escape+0x16a>
cmpl   $0x0,-0x18(%rbp)
je     1285 <luastr_escape+0x155>
lea    -0x24(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    $0x8,%esi
lea    0xdaa(%rip),%rdx
movsbl (%rax),%ecx
call   1030 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x24(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <buf_append@plt>
jmp    129a <luastr_escape+0x16a>
jmp    128a <luastr_escape+0x15a>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rax
movsbl (%rax),%esi
call   1050 <buf_putchar@plt>
jmp    129f <luastr_escape+0x16f>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    114e <luastr_escape+0x1e>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<luastr_escape>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     121f <luastr_escape+0xef>
mov    %ecx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %edx,%r15d
add    %rsi,%r15
mov    $0x14000030,%r12d
mov    %rsp,%r13
jmp    1177 <luastr_escape+0x47>
nopl   (%rax)
mov    %rbp,%rdi
mov    %ecx,%esi
call   1050 <buf_putchar@plt>
add    $0x1,%rbx
cmp    %r15,%rbx
jae    121f <luastr_escape+0xef>
movsbl (%rbx),%ecx
lea    -0x22(%rcx),%eax
cmp    $0x3a,%eax
ja     11d0 <luastr_escape+0xa0>
bt     %rax,%r12
jae    11b0 <luastr_escape+0x80>
test   %r14d,%r14d
je     1160 <luastr_escape+0x30>
mov    %r13,%rdi
mov    $0x8,%esi
lea    0xe73(%rip),%rdx
call   1030 <snprintf@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <buf_append@plt>
jmp    116a <luastr_escape+0x3a>
test   %rax,%rax
je     11e1 <luastr_escape+0xb1>
cmp    $0x3a,%rax
jne    11d0 <luastr_escape+0xa0>
mov    %rbp,%rdi
lea    0xe3b(%rip),%rsi
jmp    1210 <luastr_escape+0xe0>
nopw   0x0(%rax,%rax,1)
cmp    $0xa,%ecx
jne    1160 <luastr_escape+0x30>
mov    %rbp,%rdi
lea    0xe2d(%rip),%rsi
jmp    1210 <luastr_escape+0xe0>
mov    %rbp,%rdi
test   %r14d,%r14d
je     11ff <luastr_escape+0xcf>
lea    0xe13(%rip),%rsi
mov    $0x5,%edx
call   1040 <buf_append@plt>
jmp    116a <luastr_escape+0x3a>
lea    0xe03(%rip),%rsi
cs nopw 0x0(%rax,%rax,1)
mov    $0x2,%edx
call   1040 <buf_append@plt>
jmp    116a <luastr_escape+0x3a>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.template_buffer = type { i32 }
@.str = [3 x i8] c"\\\\\00"
@.str.1 = [6 x i8] c"&;\00"
@.str.2 = [3 x i8] c"\\\22\00"
@.str.3 = [3 x i8] c"\\n\00"
@.str.4 = [6 x i8] c"&#%i;\00"
define void @luastr_escape(%struct.template_buffer* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca %struct.template_buffer*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [8 x i8]
%11 = alloca i8*
store %struct.template_buffer* %0, %struct.template_buffer** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i8*, i8** %6
store i8* %12, i8** %11
br label %13
13: ; preds = %59, %4
%14 = load i8*, i8** %11
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = icmp ult i8* %14, %18
br i1 %19, label %20, label %62
20: ; preds = %13
%21 = load i8*, i8** %11
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
switch i32 %23, label %53 [
i32 92, label %24
i32 34, label %27
i32 10, label %37
i32 39, label %40
i32 38, label %40
i32 60, label %40
i32 62, label %40
]
24: ; preds = %20
%25 = load %struct.template_buffer*, %struct.template_buffer** %5
%26 = call i32 @buf_append(%struct.template_buffer* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
br label %58
27: ; preds = %20
%28 = load i32, i32* %8
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load %struct.template_buffer*, %struct.template_buffer** %5
%32 = call i32 @buf_append(%struct.template_buffer* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
br label %36
33: ; preds = %27
%34 = load %struct.template_buffer*, %struct.template_buffer** %5
%35 = call i32 @buf_append(%struct.template_buffer* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %36
36: ; preds = %33, %30
br label %58
37: ; preds = %20
%38 = load %struct.template_buffer*, %struct.template_buffer** %5
%39 = call i32 @buf_append(%struct.template_buffer* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
br label %58
40: ; preds = %20, %20, %20, %20
%41 = load i32, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %52
43: ; preds = %40
%44 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%45 = load i8*, i8** %11
%46 = load i8, i8* %45
%47 = call i32 @snprintf(i8* %44, i32 8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8 signext %46)
store i32 %47, i32* %9
%48 = load %struct.template_buffer*, %struct.template_buffer** %5
%49 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%50 = load i32, i32* %9
%51 = call i32 @buf_append(%struct.template_buffer* %48, i8* %49, i32 %50)
br label %58
52: ; preds = %40
br label %53
53: ; preds = %20, %52
%54 = load %struct.template_buffer*, %struct.template_buffer** %5
%55 = load i8*, i8** %11
%56 = load i8, i8* %55
%57 = call i32 @buf_putchar(%struct.template_buffer* %54, i8 signext %56)
br label %58
58: ; preds = %53, %43, %37, %36, %24
br label %59
59: ; preds = %58
%60 = load i8*, i8** %11
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %11
br label %13
62: ; preds = %13
ret void
}

LLVM IR (-O2):
%struct.template_buffer = type { i32 }
@.str = [3 x i8] c"\\\\\00"
@.str.1 = [6 x i8] c"&;\00"
@.str.2 = [3 x i8] c"\\\22\00"
@.str.3 = [3 x i8] c"\\n\00"
@.str.4 = [6 x i8] c"&#%i;\00"
define void @luastr_escape(%struct.template_buffer* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [8 x i8]
%6 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%7 = zext i32 %2 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = icmp eq i32 %2, 0
br i1 %9, label %34, label %10
10: ; preds = %4
%11 = icmp eq i32 %3, 0
br label %12
12: ; preds = %10, %31
%13 = phi i8* [ %1, %10 ], [ %32, %31 ]
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
switch i32 %15, label %29 [
i32 92, label %16
i32 34, label %18
i32 10, label %23
i32 39, label %25
i32 38, label %25
i32 60, label %25
i32 62, label %25
]
16: ; preds = %12
%17 = call i32 @buf_append(%struct.template_buffer* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
br label %31
18: ; preds = %12
br i1 %11, label %21, label %19
19: ; preds = %18
%20 = call i32 @buf_append(%struct.template_buffer* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
br label %31
21: ; preds = %18
%22 = call i32 @buf_append(%struct.template_buffer* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %31
23: ; preds = %12
%24 = call i32 @buf_append(%struct.template_buffer* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
br label %31
25: ; preds = %12, %12, %12, %12
br i1 %11, label %29, label %26
26: ; preds = %25
%27 = call i32 @snprintf(i8* %6, i32 8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8 signext %14)
%28 = call i32 @buf_append(%struct.template_buffer* %0, i8* %6, i32 %27)
br label %31
29: ; preds = %25, %12
%30 = call i32 @buf_putchar(%struct.template_buffer* %0, i8 signext %14)
br label %31
31: ; preds = %16, %23, %26, %29, %21, %19
%32 = getelementptr inbounds i8, i8* %13, i64 1
%33 = icmp ult i8* %32, %8
br i1 %33, label %12, label %34
34: ; preds = %31, %4
ret void
}