#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*/ abort();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t,
                           int /*<<< orphan*/);
char *malloc(size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *test_json_escape(const char *str) {
  static const char *hex_digits = "0123456789abcdef";
  const char *iptr;
  char *escaped_str, *optr;
  size_t len;
  iptr = str;
  len = 0;
  while (*iptr != '\0') {
    if (*iptr == '"' || *iptr == '\\') {
      len += 2;
    } else if ((*iptr >= 0x00 && *iptr <= 0x1f) || *iptr == 0x7f) {
      len += 6;
    } else {
      len += 1;
    }
    iptr++;
  }
  escaped_str = malloc(len + 1);
  if (!escaped_str) {
    fprintf(stderr, "cannot allocate %zu bytes: %s\n", len, strerror(errno));
    abort();
  }
  iptr = str;
  optr = escaped_str;
  len = 0;
  while (*iptr != '\0') {
    unsigned char c;
    c = (unsigned char)*iptr;
    if (c == '"' || c == '\\') {
      *optr++ = '\\';
      *optr++ = *iptr++;
    } else if ((c >= 0x00 && c <= 0x1f) || c == 0x7f) {
      *optr++ = '\\';
      *optr++ = 'u';
      *optr++ = '0';
      *optr++ = '0';
      *optr++ = hex_digits[c >> 4];
      *optr++ = hex_digits[c & 0x0f];
      iptr++;
    } else {
      *optr++ = *iptr++;
    }
  }
  *optr = '\0';
  return escaped_str;
}


x86 assembly (clang, -O0):
<test_json_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1200 <test_json_escape+0xc0>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
je     118c <test_json_escape+0x4c>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
jne    119d <test_json_escape+0x5d>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
jmp    11ef <test_json_escape+0xaf>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jl     11bd <test_json_escape+0x7d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x1f,%eax
jle    11cd <test_json_escape+0x8d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7f,%eax
jne    11de <test_json_escape+0x9e>
mov    -0x28(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x28(%rbp)
jmp    11ea <test_json_escape+0xaa>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11ef <test_json_escape+0xaf>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    115c <test_json_escape+0x1c>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    125a <test_json_escape+0x11a>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
lea    0xdc3(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     13d4 <test_json_escape+0x294>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x29(%rbp)
movzbl -0x29(%rbp),%eax
cmp    $0x22,%eax
je     12a5 <test_json_escape+0x165>
movzbl -0x29(%rbp),%eax
cmp    $0x5c,%eax
jne    12de <test_json_escape+0x19e>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    13cf <test_json_escape+0x28f>
movzbl -0x29(%rbp),%eax
cmp    $0x0,%eax
jl     12f8 <test_json_escape+0x1b8>
movzbl -0x29(%rbp),%eax
cmp    $0x1f,%eax
jle    1305 <test_json_escape+0x1c5>
movzbl -0x29(%rbp),%eax
cmp    $0x7f,%eax
jne    13a8 <test_json_escape+0x268>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x75,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x30,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x30,(%rax)
mov    0x2cec(%rip),%rax
movzbl -0x29(%rbp),%ecx
sar    $0x4,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    0x2cc7(%rip),%rax
movzbl -0x29(%rbp),%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    13ca <test_json_escape+0x28a>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    13cf <test_json_escape+0x28f>
jmp    1272 <test_json_escape+0x132>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_json_escape>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebx,%ebx
mov    $0x6,%esi
mov    %rdi,%rcx
jmp    1167 <test_json_escape+0x27>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %rdx,%rbx
add    $0x1,%rcx
movzbl (%rcx),%eax
mov    $0x2,%edx
cmp    $0x22,%al
je     1160 <test_json_escape+0x20>
cmp    $0x5c,%al
je     1160 <test_json_escape+0x20>
test   %al,%al
je     118f <test_json_escape+0x4f>
xor    %edx,%edx
cmp    $0x7f,%al
sete   %dl
cmp    $0x20,%al
lea    0x1(%rdx,%rdx,4),%rdx
cmovb  %rsi,%rdx
jmp    1160 <test_json_escape+0x20>
lea    0x1(%rbx),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1223 <test_json_escape+0xe3>
mov    (%r14),%bl
mov    %rax,%rcx
test   %bl,%bl
je     121b <test_json_escape+0xdb>
add    $0x1,%r14
lea    0xe4a(%rip),%rdx
mov    %rax,%rcx
jmp    11d6 <test_json_escape+0x96>
nopl   0x0(%rax,%rax,1)
movb   $0x5c,(%rcx)
mov    %bl,0x1(%rcx)
add    $0x2,%rcx
movzbl (%r14),%ebx
add    $0x1,%r14
test   %bl,%bl
je     121b <test_json_escape+0xdb>
cmp    $0x5c,%bl
je     11c0 <test_json_escape+0x80>
cmp    $0x22,%bl
je     11c0 <test_json_escape+0x80>
cmp    $0x20,%bl
jb     11f2 <test_json_escape+0xb2>
cmp    $0x7f,%bl
je     11f2 <test_json_escape+0xb2>
mov    %bl,(%rcx)
add    $0x1,%rcx
jmp    11ca <test_json_escape+0x8a>
movzbl %bl,%esi
movl   $0x3030755c,(%rcx)
mov    %esi,%esi
mov    %rsi,%rdi
shr    $0x4,%rdi
movzbl (%rdi,%rdx,1),%ebx
mov    %bl,0x4(%rcx)
and    $0xf,%esi
movzbl (%rsi,%rdx,1),%ebx
mov    %bl,0x5(%rcx)
add    $0x6,%rcx
jmp    11ca <test_json_escape+0x8a>
movb   $0x0,(%rcx)
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2db6(%rip),%rax
mov    (%rax),%ebp
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xdd0(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@test_json_escape.hex_digits = internal global i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i32 0, i32 0)
@.str = [17 x i8] c"0123456789abcdef\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"cannot allocate %zu bytes: %s\0A\00"
@errno = global i32 0
define i8* @test_json_escape(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8
store i8* %0, i8** %2
%8 = load i8*, i8** %2
store i8* %8, i8** %3
store i64 0, i64* %6
br label %9
9: ; preds = %49, %1
%10 = load i8*, i8** %3
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %52
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 34
br i1 %18, label %24, label %19
19: ; preds = %14
%20 = load i8*, i8** %3
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 92
br i1 %23, label %24, label %27
24: ; preds = %19, %14
%25 = load i64, i64* %6
%26 = add i64 %25, 2
store i64 %26, i64* %6
br label %49
27: ; preds = %19
%28 = load i8*, i8** %3
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp sge i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = load i8*, i8** %3
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp sle i32 %35, 31
br i1 %36, label %42, label %37
37: ; preds = %32, %27
%38 = load i8*, i8** %3
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 127
br i1 %41, label %42, label %45
42: ; preds = %37, %32
%43 = load i64, i64* %6
%44 = add i64 %43, 6
store i64 %44, i64* %6
br label %48
45: ; preds = %37
%46 = load i64, i64* %6
%47 = add i64 %46, 1
store i64 %47, i64* %6
br label %48
48: ; preds = %45, %42
br label %49
49: ; preds = %48, %24
%50 = load i8*, i8** %3
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %3
br label %9
52: ; preds = %9
%53 = load i64, i64* %6
%54 = add i64 %53, 1
%55 = call i8* @malloc(i64 %54)
store i8* %55, i8** %4
%56 = load i8*, i8** %4
%57 = icmp ne i8* %56, null
br i1 %57, label %65, label %58
58: ; preds = %52
%59 = load i32, i32* @stderr
%60 = load i64, i64* %6
%61 = load i32, i32* @errno
%62 = call i32 @strerror(i32 %61)
%63 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %60, i32 %62)
%64 = call i32 (...) @abort()
unreachable
65: ; preds = %52
%66 = load i8*, i8** %2
store i8* %66, i8** %3
%67 = load i8*, i8** %4
store i8* %67, i8** %5
store i64 0, i64* %6
br label %68
68: ; preds = %139, %65
%69 = load i8*, i8** %3
%70 = load i8, i8* %69
%71 = sext i8 %70 to i32
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %140
73: ; preds = %68
%74 = load i8*, i8** %3
%75 = load i8, i8* %74
store i8 %75, i8* %7
%76 = load i8, i8* %7
%77 = zext i8 %76 to i32
%78 = icmp eq i32 %77, 34
br i1 %78, label %83, label %79
79: ; preds = %73
%80 = load i8, i8* %7
%81 = zext i8 %80 to i32
%82 = icmp eq i32 %81, 92
br i1 %82, label %83, label %91
83: ; preds = %79, %73
%84 = load i8*, i8** %5
%85 = getelementptr inbounds i8, i8* %84, i32 1
store i8* %85, i8** %5
store i8 92, i8* %84
%86 = load i8*, i8** %3
%87 = getelementptr inbounds i8, i8* %86, i32 1
store i8* %87, i8** %3
%88 = load i8, i8* %86
%89 = load i8*, i8** %5
%90 = getelementptr inbounds i8, i8* %89, i32 1
store i8* %90, i8** %5
store i8 %88, i8* %89
br label %139
91: ; preds = %79
%92 = load i8, i8* %7
%93 = zext i8 %92 to i32
%94 = icmp sge i32 %93, 0
br i1 %94, label %95, label %99
95: ; preds = %91
%96 = load i8, i8* %7
%97 = zext i8 %96 to i32
%98 = icmp sle i32 %97, 31
br i1 %98, label %103, label %99
99: ; preds = %95, %91
%100 = load i8, i8* %7
%101 = zext i8 %100 to i32
%102 = icmp eq i32 %101, 127
br i1 %102, label %103, label %132
103: ; preds = %99, %95
%104 = load i8*, i8** %5
%105 = getelementptr inbounds i8, i8* %104, i32 1
store i8* %105, i8** %5
store i8 92, i8* %104
%106 = load i8*, i8** %5
%107 = getelementptr inbounds i8, i8* %106, i32 1
store i8* %107, i8** %5
store i8 117, i8* %106
%108 = load i8*, i8** %5
%109 = getelementptr inbounds i8, i8* %108, i32 1
store i8* %109, i8** %5
store i8 48, i8* %108
%110 = load i8*, i8** %5
%111 = getelementptr inbounds i8, i8* %110, i32 1
store i8* %111, i8** %5
store i8 48, i8* %110
%112 = load i8*, i8** @test_json_escape.hex_digits
%113 = load i8, i8* %7
%114 = zext i8 %113 to i32
%115 = ashr i32 %114, 4
%116 = sext i32 %115 to i64
%117 = getelementptr inbounds i8, i8* %112, i64 %116
%118 = load i8, i8* %117
%119 = load i8*, i8** %5
%120 = getelementptr inbounds i8, i8* %119, i32 1
store i8* %120, i8** %5
store i8 %118, i8* %119
%121 = load i8*, i8** @test_json_escape.hex_digits
%122 = load i8, i8* %7
%123 = zext i8 %122 to i32
%124 = and i32 %123, 15
%125 = sext i32 %124 to i64
%126 = getelementptr inbounds i8, i8* %121, i64 %125
%127 = load i8, i8* %126
%128 = load i8*, i8** %5
%129 = getelementptr inbounds i8, i8* %128, i32 1
store i8* %129, i8** %5
store i8 %127, i8* %128
%130 = load i8*, i8** %3
%131 = getelementptr inbounds i8, i8* %130, i32 1
store i8* %131, i8** %3
br label %138
132: ; preds = %99
%133 = load i8*, i8** %3
%134 = getelementptr inbounds i8, i8* %133, i32 1
store i8* %134, i8** %3
%135 = load i8, i8* %133
%136 = load i8*, i8** %5
%137 = getelementptr inbounds i8, i8* %136, i32 1
store i8* %137, i8** %5
store i8 %135, i8* %136
br label %138
138: ; preds = %132, %103
br label %139
139: ; preds = %138, %83
br label %68
140: ; preds = %68
%141 = load i8*, i8** %5
store i8 0, i8* %141
%142 = load i8*, i8** %4
ret i8* %142
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"cannot allocate %zu bytes: %s\0A\00"
@errno = global i32 0
define noalias i8* @test_json_escape(i8* nocapture %0) {
br label %2
2: ; preds = %11, %1
%3 = phi i64 [ 0, %1 ], [ %13, %11 ]
%4 = phi i8* [ %0, %1 ], [ %14, %11 ]
%5 = load i8, i8* %4
switch i8 %5, label %6 [
i8 0, label %15
i8 34, label %11
i8 92, label %11
]
6: ; preds = %2
%7 = icmp ult i8 %5, 32
%8 = icmp eq i8 %5, 127
%9 = or i1 %7, %8
%10 = select i1 %9, i64 6, i64 1
br label %11
11: ; preds = %6, %2, %2
%12 = phi i64 [ 2, %2 ], [ 2, %2 ], [ %10, %6 ]
%13 = add i64 %3, %12
%14 = getelementptr inbounds i8, i8* %4, i64 1
br label %2
15: ; preds = %2
%16 = add i64 %3, 1
%17 = tail call i8* @malloc(i64 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load i8, i8* %0
%21 = icmp eq i8 %20, 0
br i1 %21, label %61, label %28
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = tail call i32 @strerror(i32 %24)
%26 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %3, i32 %25)
%27 = tail call i32 (...) @abort()
unreachable
28: ; preds = %19, %56
%29 = phi i8 [ %59, %56 ], [ %20, %19 ]
%30 = phi i8* [ %58, %56 ], [ %0, %19 ]
%31 = phi i8* [ %57, %56 ], [ %17, %19 ]
%32 = zext i8 %29 to i32
switch i8 %29, label %36 [
i8 92, label %33
i8 34, label %33
]
33: ; preds = %28, %28
%34 = getelementptr inbounds i8, i8* %31, i64 1
store i8 92, i8* %31
%35 = getelementptr inbounds i8, i8* %31, i64 2
store i8 %29, i8* %34
br label %56
36: ; preds = %28
%37 = icmp ult i8 %29, 32
%38 = icmp eq i8 %29, 127
%39 = or i1 %37, %38
%40 = getelementptr inbounds i8, i8* %31, i64 1
br i1 %39, label %41, label %55
41: ; preds = %36
store i8 92, i8* %31
%42 = getelementptr inbounds i8, i8* %31, i64 2
store i8 117, i8* %40
%43 = getelementptr inbounds i8, i8* %31, i64 3
store i8 48, i8* %42
%44 = getelementptr inbounds i8, i8* %31, i64 4
store i8 48, i8* %43
%45 = lshr i32 %32, 4
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %46
%48 = load i8, i8* %47
%49 = getelementptr inbounds i8, i8* %31, i64 5
store i8 %48, i8* %44
%50 = and i32 %32, 15
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %51
%53 = load i8, i8* %52
%54 = getelementptr inbounds i8, i8* %31, i64 6
store i8 %53, i8* %49
br label %56
55: ; preds = %36
store i8 %29, i8* %31
br label %56
56: ; preds = %41, %55, %33
%57 = phi i8* [ %35, %33 ], [ %54, %41 ], [ %40, %55 ]
%58 = getelementptr inbounds i8, i8* %30, i64 1
%59 = load i8, i8* %58
%60 = icmp eq i8 %59, 0
br i1 %60, label %61, label %28
61: ; preds = %56, %19
%62 = phi i8* [ %17, %19 ], [ %57, %56 ]
store i8 0, i8* %62
ret i8* %17
}