#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 sr_strbuf {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ sr_strbuf_append_char(struct sr_strbuf *, char const);
int /*<<< orphan*/ sr_strbuf_append_str(struct sr_strbuf *, char *);
char *sr_strbuf_free_nobuf(struct sr_strbuf *);
struct sr_strbuf *sr_strbuf_new();
char *sr_json_escape(const char *text) {
  struct sr_strbuf *strbuf = sr_strbuf_new();
  const char *c = text;
  while (*c != '\0') {
    switch (*c) {
    case '"':
      sr_strbuf_append_str(strbuf, "\\\"");
      break;
    case '\n':
      sr_strbuf_append_str(strbuf, "\\n");
      break;
    case '\\':
      sr_strbuf_append_str(strbuf, "\\\\");
      break;
    case '\r':
      sr_strbuf_append_str(strbuf, "\\r");
      break;
    case '\f':
      sr_strbuf_append_str(strbuf, "\\f");
      break;
    case '\b':
      sr_strbuf_append_str(strbuf, "\\b");
      break;
    case '\t':
      sr_strbuf_append_str(strbuf, "\\t");
      break;
    default:
      sr_strbuf_append_char(strbuf, *c);
    }
    ++c;
  }
  return sr_strbuf_free_nobuf(strbuf);
}


x86 assembly (clang, -O0):
<sr_json_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1060 <sr_strbuf_new@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12a1 <sr_json_escape+0x161>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x8,%eax
je     1256 <sr_json_escape+0x116>
jmp    1187 <sr_json_escape+0x47>
mov    -0x1c(%rbp),%eax
sub    $0x9,%eax
je     126b <sr_json_escape+0x12b>
jmp    1198 <sr_json_escape+0x58>
mov    -0x1c(%rbp),%eax
sub    $0xa,%eax
je     1202 <sr_json_escape+0xc2>
jmp    11a9 <sr_json_escape+0x69>
mov    -0x1c(%rbp),%eax
sub    $0xc,%eax
je     1241 <sr_json_escape+0x101>
jmp    11ba <sr_json_escape+0x7a>
mov    -0x1c(%rbp),%eax
sub    $0xd,%eax
je     122c <sr_json_escape+0xec>
jmp    11cb <sr_json_escape+0x8b>
mov    -0x1c(%rbp),%eax
sub    $0x22,%eax
je     11ed <sr_json_escape+0xad>
jmp    11dc <sr_json_escape+0x9c>
mov    -0x1c(%rbp),%eax
sub    $0x5c,%eax
je     1217 <sr_json_escape+0xd7>
jmp    1280 <sr_json_escape+0x140>
mov    -0x10(%rbp),%rdi
lea    0xe08(%rip),%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1290 <sr_json_escape+0x150>
mov    -0x10(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1290 <sr_json_escape+0x150>
mov    -0x10(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1290 <sr_json_escape+0x150>
mov    -0x10(%rbp),%rdi
lea    0xdd2(%rip),%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1290 <sr_json_escape+0x150>
mov    -0x10(%rbp),%rdi
lea    0xdc0(%rip),%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1290 <sr_json_escape+0x150>
mov    -0x10(%rbp),%rdi
lea    0xdae(%rip),%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1290 <sr_json_escape+0x150>
mov    -0x10(%rbp),%rdi
lea    0xd9c(%rip),%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1290 <sr_json_escape+0x150>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1050 <sr_strbuf_append_char@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    115f <sr_json_escape+0x1f>
mov    -0x10(%rbp),%rdi
call   1040 <sr_strbuf_free_nobuf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sr_json_escape>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <sr_strbuf_new@plt>
mov    %rax,%r14
mov    (%rbx),%al
test   %al,%al
je     122a <sr_json_escape+0xea>
add    $0x1,%rbx
lea    0xe93(%rip),%rbp
lea    0xf04(%rip),%r15
lea    0xefa(%rip),%r12
lea    0xeea(%rip),%r13
jmp    119f <sr_json_escape+0x5f>
mov    %r14,%rdi
call   1050 <sr_strbuf_append_char@plt>
nopl   0x0(%rax)
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     122a <sr_json_escape+0xea>
movsbl %al,%esi
lea    -0x8(%rsi),%eax
cmp    $0x1a,%eax
ja     11c5 <sr_json_escape+0x85>
movslq 0x0(%rbp,%rax,4),%rax
add    %rbp,%rax
jmp    *%rax
mov    %r14,%rdi
lea    0xebd(%rip),%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1190 <sr_json_escape+0x50>
cmp    $0x5c,%esi
jne    1184 <sr_json_escape+0x44>
mov    %r14,%rdi
lea    0xe9e(%rip),%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1190 <sr_json_escape+0x50>
mov    %r14,%rdi
lea    0xe99(%rip),%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1190 <sr_json_escape+0x50>
mov    %r14,%rdi
lea    0xe79(%rip),%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1190 <sr_json_escape+0x50>
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1190 <sr_json_escape+0x50>
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1190 <sr_json_escape+0x50>
mov    %r14,%rdi
mov    %r13,%rsi
call   1030 <sr_strbuf_append_str@plt>
jmp    1190 <sr_json_escape+0x50>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <sr_strbuf_free_nobuf@plt>

LLVM IR (-O0):
%struct.sr_strbuf = type { i32 }
@.str = [3 x i8] c"\\\22\00"
@.str.1 = [3 x i8] c"\\n\00"
@.str.2 = [3 x i8] c"\\\\\00"
@.str.3 = [3 x i8] c"\\r\00"
@.str.4 = [3 x i8] c"\\f\00"
@.str.5 = [3 x i8] c"\\b\00"
@.str.6 = [3 x i8] c"\\t\00"
define i8* @sr_json_escape(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sr_strbuf*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = call %struct.sr_strbuf* (...) @sr_strbuf_new()
store %struct.sr_strbuf* %5, %struct.sr_strbuf** %3
%6 = load i8*, i8** %2
store i8* %6, i8** %4
br label %7
7: ; preds = %42, %1
%8 = load i8*, i8** %4
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %45
12: ; preds = %7
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
switch i32 %15, label %37 [
i32 34, label %16
i32 10, label %19
i32 92, label %22
i32 13, label %25
i32 12, label %28
i32 8, label %31
i32 9, label %34
]
16: ; preds = %12
%17 = load %struct.sr_strbuf*, %struct.sr_strbuf** %3
%18 = call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %42
19: ; preds = %12
%20 = load %struct.sr_strbuf*, %struct.sr_strbuf** %3
%21 = call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %42
22: ; preds = %12
%23 = load %struct.sr_strbuf*, %struct.sr_strbuf** %3
%24 = call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %42
25: ; preds = %12
%26 = load %struct.sr_strbuf*, %struct.sr_strbuf** %3
%27 = call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %42
28: ; preds = %12
%29 = load %struct.sr_strbuf*, %struct.sr_strbuf** %3
%30 = call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %42
31: ; preds = %12
%32 = load %struct.sr_strbuf*, %struct.sr_strbuf** %3
%33 = call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %42
34: ; preds = %12
%35 = load %struct.sr_strbuf*, %struct.sr_strbuf** %3
%36 = call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %42
37: ; preds = %12
%38 = load %struct.sr_strbuf*, %struct.sr_strbuf** %3
%39 = load i8*, i8** %4
%40 = load i8, i8* %39
%41 = call i32 @sr_strbuf_append_char(%struct.sr_strbuf* %38, i8 signext %40)
br label %42
42: ; preds = %37, %34, %31, %28, %25, %22, %19, %16
%43 = load i8*, i8** %4
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %4
br label %7
45: ; preds = %7
%46 = load %struct.sr_strbuf*, %struct.sr_strbuf** %3
%47 = call i8* @sr_strbuf_free_nobuf(%struct.sr_strbuf* %46)
ret i8* %47
}

LLVM IR (-O2):
%struct.sr_strbuf = type { i32 }
@.str = [3 x i8] c"\\\22\00"
@.str.1 = [3 x i8] c"\\n\00"
@.str.2 = [3 x i8] c"\\\\\00"
@.str.3 = [3 x i8] c"\\r\00"
@.str.4 = [3 x i8] c"\\f\00"
@.str.5 = [3 x i8] c"\\b\00"
@.str.6 = [3 x i8] c"\\t\00"
define i8* @sr_json_escape(i8* nocapture %0) {
%2 = tail call %struct.sr_strbuf* (...) @sr_strbuf_new()
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %29, label %5
5: ; preds = %1, %25
%6 = phi i8 [ %27, %25 ], [ %3, %1 ]
%7 = phi i8* [ %26, %25 ], [ %0, %1 ]
%8 = sext i8 %6 to i32
switch i32 %8, label %23 [
i32 34, label %9
i32 10, label %11
i32 92, label %13
i32 13, label %15
i32 12, label %17
i32 8, label %19
i32 9, label %21
]
9: ; preds = %5
%10 = tail call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %25
11: ; preds = %5
%12 = tail call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %25
13: ; preds = %5
%14 = tail call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %25
15: ; preds = %5
%16 = tail call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %25
17: ; preds = %5
%18 = tail call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %25
19: ; preds = %5
%20 = tail call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %25
21: ; preds = %5
%22 = tail call i32 @sr_strbuf_append_str(%struct.sr_strbuf* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %25
23: ; preds = %5
%24 = tail call i32 @sr_strbuf_append_char(%struct.sr_strbuf* %2, i8 signext %6)
br label %25
25: ; preds = %23, %21, %19, %17, %15, %13, %11, %9
%26 = getelementptr inbounds i8, i8* %7, i64 1
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 0
br i1 %28, label %29, label %5
29: ; preds = %25, %1
%30 = tail call i8* @sr_strbuf_free_nobuf(%struct.sr_strbuf* %2)
ret i8* %30
}