#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*/ memcpy(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char);
void escape_json_attr(const char *buf, int src_len, char *out) {
  char *o = out;
  const char *b;
  int i;
  for (i = 0, b = buf; i < src_len; ++i, ++b) {
    unsigned char c = *b;
    switch (c) {
    case '"':
      memcpy(o, "\\\"", ((sizeof("\\\"") / sizeof("\\\""[0])) - 1));
      o += ((sizeof("\\\"") / sizeof("\\\""[0])) - 1);
      break;
    case '\\':
      memcpy(o, "\\\\", ((sizeof("\\\\") / sizeof("\\\\"[0])) - 1));
      o += ((sizeof("\\\\") / sizeof("\\\\"[0])) - 1);
      break;
    case '\t':
      memcpy(o, "\\t", ((sizeof("\\t") / sizeof("\\t"[0])) - 1));
      o += ((sizeof("\\t") / sizeof("\\t"[0])) - 1);
      break;
    case '\n':
      memcpy(o, "\\n", ((sizeof("\\n") / sizeof("\\n"[0])) - 1));
      o += ((sizeof("\\n") / sizeof("\\n"[0])) - 1);
      break;
    default:
      if ((c < 0x20) || (c == 0x7f)) {
        snprintf(o, 7, "\\u%04x", c);
        o += 6;
      } else {
        *o++ = c;
      }
      break;
    }
  }
  *o = '\0';
}


x86 assembly (clang, -O0):
<escape_json_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12bb <escape_json_attr+0x19b>
mov    -0x28(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x2d(%rbp)
movzbl -0x2d(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x9,%eax
je     11f3 <escape_json_attr+0xd3>
jmp    1174 <escape_json_attr+0x54>
mov    -0x34(%rbp),%eax
sub    $0xa,%eax
je     1219 <escape_json_attr+0xf9>
jmp    1185 <escape_json_attr+0x65>
mov    -0x34(%rbp),%eax
sub    $0x22,%eax
je     11a7 <escape_json_attr+0x87>
jmp    1196 <escape_json_attr+0x76>
mov    -0x34(%rbp),%eax
sub    $0x5c,%eax
je     11cd <escape_json_attr+0xad>
jmp    123f <escape_json_attr+0x11f>
mov    -0x20(%rbp),%rdi
lea    0xe4e(%rip),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    129c <escape_json_attr+0x17c>
mov    -0x20(%rbp),%rdi
lea    0xe2b(%rip),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    129c <escape_json_attr+0x17c>
mov    -0x20(%rbp),%rdi
lea    0xe08(%rip),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    129c <escape_json_attr+0x17c>
mov    -0x20(%rbp),%rdi
lea    0xde5(%rip),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    129c <escape_json_attr+0x17c>
movzbl -0x2d(%rbp),%eax
cmp    $0x20,%eax
jl     1259 <escape_json_attr+0x139>
movzbl -0x2d(%rbp),%eax
cmp    $0x7f,%eax
jne    1283 <escape_json_attr+0x163>
mov    -0x20(%rbp),%rdi
mov    $0x7,%esi
lea    0xda3(%rip),%rdx
movzbl -0x2d(%rbp),%ecx
call   1030 <snprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x20(%rbp)
jmp    1297 <escape_json_attr+0x177>
mov    -0x2d(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    129c <escape_json_attr+0x17c>
jmp    12a1 <escape_json_attr+0x181>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    114a <escape_json_attr+0x2a>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_json_attr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
test   %esi,%esi
jle    11dd <escape_json_attr+0xbd>
mov    %rdi,%r14
mov    %esi,%r15d
xor    %r12d,%r12d
lea    0xebd(%rip),%rbp
lea    0xeb3(%rip),%r13
jmp    1171 <escape_json_attr+0x51>
nop
mov    %rbx,%rdi
lea    0xeac(%rip),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
add    $0x2,%rbx
add    $0x1,%r12
cmp    %r12d,%r15d
je     11dd <escape_json_attr+0xbd>
movzbl (%r14,%r12,1),%eax
cmp    $0x21,%al
jg     1190 <escape_json_attr+0x70>
cmp    $0x9,%al
je     1150 <escape_json_attr+0x30>
cmp    $0xa,%al
jne    11b0 <escape_json_attr+0x90>
mov    %rbx,%rdi
lea    0xe7d(%rip),%rsi
jmp    115a <escape_json_attr+0x3a>
xchg   %ax,%ax
cmp    $0x5c,%al
je     11a0 <escape_json_attr+0x80>
cmp    $0x22,%al
jne    11b0 <escape_json_attr+0x90>
mov    %rbx,%rdi
mov    %r13,%rsi
jmp    115a <escape_json_attr+0x3a>
mov    %rbx,%rdi
mov    %rbp,%rsi
jmp    115a <escape_json_attr+0x3a>
nopl   0x0(%rax,%rax,1)
cmp    $0x20,%al
jb     11c0 <escape_json_attr+0xa0>
cmp    $0x7f,%al
je     11c0 <escape_json_attr+0xa0>
mov    %al,(%rbx)
add    $0x1,%rbx
jmp    1168 <escape_json_attr+0x48>
movzbl %al,%ecx
mov    %rbx,%rdi
mov    $0x7,%esi
lea    0xe3a(%rip),%rdx
call   1030 <snprintf@plt>
add    $0x6,%rbx
jmp    1168 <escape_json_attr+0x48>
movb   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\\\22\00"
@.str.1 = [3 x i8] c"\\\\\00"
@.str.2 = [3 x i8] c"\\t\00"
@.str.3 = [3 x i8] c"\\n\00"
@.str.4 = [7 x i8] c"\\u%04x\00"
define void @escape_json_attr(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%11 = load i8*, i8** %6
store i8* %11, i8** %7
store i32 0, i32* %9
%12 = load i8*, i8** %4
store i8* %12, i8** %8
br label %13
13: ; preds = %62, %3
%14 = load i32, i32* %9
%15 = load i32, i32* %5
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %67
17: ; preds = %13
%18 = load i8*, i8** %8
%19 = load i8, i8* %18
store i8 %19, i8* %10
%20 = load i8, i8* %10
%21 = zext i8 %20 to i32
switch i32 %21, label %42 [
i32 34, label %22
i32 92, label %27
i32 9, label %32
i32 10, label %37
]
22: ; preds = %17
%23 = load i8*, i8** %7
%24 = call i32 @memcpy(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%25 = load i8*, i8** %7
%26 = getelementptr inbounds i8, i8* %25, i64 2
store i8* %26, i8** %7
br label %61
27: ; preds = %17
%28 = load i8*, i8** %7
%29 = call i32 @memcpy(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 2
store i8* %31, i8** %7
br label %61
32: ; preds = %17
%33 = load i8*, i8** %7
%34 = call i32 @memcpy(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%35 = load i8*, i8** %7
%36 = getelementptr inbounds i8, i8* %35, i64 2
store i8* %36, i8** %7
br label %61
37: ; preds = %17
%38 = load i8*, i8** %7
%39 = call i32 @memcpy(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
%40 = load i8*, i8** %7
%41 = getelementptr inbounds i8, i8* %40, i64 2
store i8* %41, i8** %7
br label %61
42: ; preds = %17
%43 = load i8, i8* %10
%44 = zext i8 %43 to i32
%45 = icmp slt i32 %44, 32
br i1 %45, label %50, label %46
46: ; preds = %42
%47 = load i8, i8* %10
%48 = zext i8 %47 to i32
%49 = icmp eq i32 %48, 127
br i1 %49, label %50, label %56
50: ; preds = %46, %42
%51 = load i8*, i8** %7
%52 = load i8, i8* %10
%53 = call i32 @snprintf(i8* %51, i32 7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8 zeroext %52)
%54 = load i8*, i8** %7
%55 = getelementptr inbounds i8, i8* %54, i64 6
store i8* %55, i8** %7
br label %60
56: ; preds = %46
%57 = load i8, i8* %10
%58 = load i8*, i8** %7
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %7
store i8 %57, i8* %58
br label %60
60: ; preds = %56, %50
br label %61
61: ; preds = %60, %37, %32, %27, %22
br label %62
62: ; preds = %61
%63 = load i32, i32* %9
%64 = add nsw i32 %63, 1
store i32 %64, i32* %9
%65 = load i8*, i8** %8
%66 = getelementptr inbounds i8, i8* %65, i32 1
store i8* %66, i8** %8
br label %13
67: ; preds = %13
%68 = load i8*, i8** %7
store i8 0, i8* %68
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"\\\22\00"
@.str.1 = [3 x i8] c"\\\\\00"
@.str.2 = [3 x i8] c"\\t\00"
@.str.3 = [3 x i8] c"\\n\00"
@.str.4 = [7 x i8] c"\\u%04x\00"
define void @escape_json_attr(i8* nocapture %0, i32 %1, i8* %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %36
5: ; preds = %3, %31
%6 = phi i8* [ %32, %31 ], [ %2, %3 ]
%7 = phi i32 [ %33, %31 ], [ 0, %3 ]
%8 = phi i8* [ %34, %31 ], [ %0, %3 ]
%9 = load i8, i8* %8
switch i8 %9, label %22 [
i8 34, label %10
i8 92, label %13
i8 9, label %16
i8 10, label %19
]
10: ; preds = %5
%11 = tail call i32 @memcpy(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%12 = getelementptr inbounds i8, i8* %6, i64 2
br label %31
13: ; preds = %5
%14 = tail call i32 @memcpy(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%15 = getelementptr inbounds i8, i8* %6, i64 2
br label %31
16: ; preds = %5
%17 = tail call i32 @memcpy(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%18 = getelementptr inbounds i8, i8* %6, i64 2
br label %31
19: ; preds = %5
%20 = tail call i32 @memcpy(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
%21 = getelementptr inbounds i8, i8* %6, i64 2
br label %31
22: ; preds = %5
%23 = icmp ult i8 %9, 32
%24 = icmp eq i8 %9, 127
%25 = or i1 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = tail call i32 @snprintf(i8* %6, i32 7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8 zeroext %9)
%28 = getelementptr inbounds i8, i8* %6, i64 6
br label %31
29: ; preds = %22
%30 = getelementptr inbounds i8, i8* %6, i64 1
store i8 %9, i8* %6
br label %31
31: ; preds = %26, %29, %19, %16, %13, %10
%32 = phi i8* [ %28, %26 ], [ %30, %29 ], [ %21, %19 ], [ %18, %16 ], [ %15, %13 ], [ %12, %10 ]
%33 = add nuw nsw i32 %7, 1
%34 = getelementptr inbounds i8, i8* %8, i64 1
%35 = icmp eq i32 %33, %1
br i1 %35, label %36, label %5
36: ; preds = %31, %3
%37 = phi i8* [ %2, %3 ], [ %32, %31 ]
store i8 0, i8* %37
ret void
}