#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 strbuf {
  int dummy;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ isprint(int);
int strbuf_addchar(struct strbuf *, char const);
int strbuf_addmem(struct strbuf *, char *, int);
ssize_t strbuf_addmem2json(struct strbuf *sb, const char *mem, size_t l) {
  const char *end = mem + l;
  const char *p;
  int rv;
  for (p = mem; p < end; p++) {
    switch (*p) {
    case '\b':
      rv = strbuf_addmem(sb, "\\b", 2);
      break;
    case '\t':
      rv = strbuf_addmem(sb, "\\t", 2);
      break;
    case '\n':
      rv = strbuf_addmem(sb, "\\n", 2);
      break;
    case '\f':
      rv = strbuf_addmem(sb, "\\f", 2);
      break;
    case '\r':
      rv = strbuf_addmem(sb, "\\r", 2);
      break;
    case '\"':
      rv = strbuf_addmem(sb, "\\\"", 2);
      break;
    case '/':
      rv = strbuf_addmem(sb, "\\/", 2);
      break;
    case '\\':
      rv = strbuf_addmem(sb, "\\\\", 2);
      break;
    default:
      rv = isprint((int)*p) ? strbuf_addchar(sb, *p) : -1;
      break;
    }
    if (rv == -1)
      return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<strbuf_addmem2json>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1349 <strbuf_addmem2json+0x219>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x38(%rbp)
sub    $0x8,%eax
je     11f5 <strbuf_addmem2json+0xc5>
jmp    117e <strbuf_addmem2json+0x4e>
mov    -0x38(%rbp),%eax
sub    $0x9,%eax
je     1212 <strbuf_addmem2json+0xe2>
jmp    118f <strbuf_addmem2json+0x5f>
mov    -0x38(%rbp),%eax
sub    $0xa,%eax
je     122f <strbuf_addmem2json+0xff>
jmp    11a0 <strbuf_addmem2json+0x70>
mov    -0x38(%rbp),%eax
sub    $0xc,%eax
je     124c <strbuf_addmem2json+0x11c>
jmp    11b1 <strbuf_addmem2json+0x81>
mov    -0x38(%rbp),%eax
sub    $0xd,%eax
je     1269 <strbuf_addmem2json+0x139>
jmp    11c2 <strbuf_addmem2json+0x92>
mov    -0x38(%rbp),%eax
sub    $0x22,%eax
je     1286 <strbuf_addmem2json+0x156>
jmp    11d3 <strbuf_addmem2json+0xa3>
mov    -0x38(%rbp),%eax
sub    $0x2f,%eax
je     12a3 <strbuf_addmem2json+0x173>
jmp    11e4 <strbuf_addmem2json+0xb4>
mov    -0x38(%rbp),%eax
sub    $0x5c,%eax
je     12c0 <strbuf_addmem2json+0x190>
jmp    12dd <strbuf_addmem2json+0x1ad>
mov    -0x10(%rbp),%rdi
lea    0xe00(%rip),%rsi
mov    $0x2,%edx
call   1050 <strbuf_addmem@plt>
mov    %eax,-0x34(%rbp)
jmp    131d <strbuf_addmem2json+0x1ed>
mov    -0x10(%rbp),%rdi
lea    0xde6(%rip),%rsi
mov    $0x2,%edx
call   1050 <strbuf_addmem@plt>
mov    %eax,-0x34(%rbp)
jmp    131d <strbuf_addmem2json+0x1ed>
mov    -0x10(%rbp),%rdi
lea    0xdcc(%rip),%rsi
mov    $0x2,%edx
call   1050 <strbuf_addmem@plt>
mov    %eax,-0x34(%rbp)
jmp    131d <strbuf_addmem2json+0x1ed>
mov    -0x10(%rbp),%rdi
lea    0xdb2(%rip),%rsi
mov    $0x2,%edx
call   1050 <strbuf_addmem@plt>
mov    %eax,-0x34(%rbp)
jmp    131d <strbuf_addmem2json+0x1ed>
mov    -0x10(%rbp),%rdi
lea    0xd98(%rip),%rsi
mov    $0x2,%edx
call   1050 <strbuf_addmem@plt>
mov    %eax,-0x34(%rbp)
jmp    131d <strbuf_addmem2json+0x1ed>
mov    -0x10(%rbp),%rdi
lea    0xd7e(%rip),%rsi
mov    $0x2,%edx
call   1050 <strbuf_addmem@plt>
mov    %eax,-0x34(%rbp)
jmp    131d <strbuf_addmem2json+0x1ed>
mov    -0x10(%rbp),%rdi
lea    0xd64(%rip),%rsi
mov    $0x2,%edx
call   1050 <strbuf_addmem@plt>
mov    %eax,-0x34(%rbp)
jmp    131d <strbuf_addmem2json+0x1ed>
mov    -0x10(%rbp),%rdi
lea    0xd4a(%rip),%rsi
mov    $0x2,%edx
call   1050 <strbuf_addmem@plt>
mov    %eax,-0x34(%rbp)
jmp    131d <strbuf_addmem2json+0x1ed>
mov    -0x30(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isprint@plt>
cmp    $0x0,%eax
je     130a <strbuf_addmem2json+0x1da>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
movsbl (%rax),%esi
call   1040 <strbuf_addchar@plt>
mov    %eax,-0x3c(%rbp)
jmp    1317 <strbuf_addmem2json+0x1e7>
mov    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
jmp    1317 <strbuf_addmem2json+0x1e7>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1333 <strbuf_addmem2json+0x203>
movl   $0xffffffff,-0x4(%rbp)
jmp    1350 <strbuf_addmem2json+0x220>
jmp    1338 <strbuf_addmem2json+0x208>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1158 <strbuf_addmem2json+0x28>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strbuf_addmem2json>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
jle    1224 <strbuf_addmem2json+0xf4>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%r15
add    %rsi,%rbx
lea    0xea6(%rip),%r13
nopw   0x0(%rax,%rax,1)
movsbl 0x0(%rbp),%r12d
lea    -0x8(%r12),%eax
cmp    $0x27,%eax
ja     1185 <strbuf_addmem2json+0x55>
movslq 0x0(%r13,%rax,4),%rax
add    %r13,%rax
jmp    *%rax
mov    %r15,%rdi
lea    0xf1d(%rip),%rsi
jmp    1200 <strbuf_addmem2json+0xd0>
cmp    $0x5c,%r12d
jne    1197 <strbuf_addmem2json+0x67>
mov    %r15,%rdi
lea    0xf20(%rip),%rsi
jmp    1200 <strbuf_addmem2json+0xd0>
mov    %r12d,%edi
call   1030 <isprint@plt>
test   %eax,%eax
je     121e <strbuf_addmem2json+0xee>
mov    %r15,%rdi
mov    %r12d,%esi
call   1040 <strbuf_addchar@plt>
cmp    $0xffffffff,%eax
jne    120f <strbuf_addmem2json+0xdf>
jmp    121e <strbuf_addmem2json+0xee>
mov    %r15,%rdi
lea    0xee4(%rip),%rsi
jmp    1200 <strbuf_addmem2json+0xd0>
mov    %r15,%rdi
lea    0xedb(%rip),%rsi
jmp    1200 <strbuf_addmem2json+0xd0>
mov    %r15,%rdi
lea    0xed2(%rip),%rsi
jmp    1200 <strbuf_addmem2json+0xd0>
mov    %r15,%rdi
lea    0xec9(%rip),%rsi
jmp    1200 <strbuf_addmem2json+0xd0>
mov    %r15,%rdi
lea    0xec0(%rip),%rsi
jmp    1200 <strbuf_addmem2json+0xd0>
mov    %r15,%rdi
lea    0xeb7(%rip),%rsi
nopl   0x0(%rax,%rax,1)
mov    $0x2,%edx
call   1050 <strbuf_addmem@plt>
cmp    $0xffffffff,%eax
je     121e <strbuf_addmem2json+0xee>
add    $0x1,%rbp
cmp    %rbx,%rbp
jb     1160 <strbuf_addmem2json+0x30>
jmp    1224 <strbuf_addmem2json+0xf4>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.strbuf = type { i32 }
@.str = [3 x i8] c"\\b\00"
@.str.1 = [3 x i8] c"\\t\00"
@.str.2 = [3 x i8] c"\\n\00"
@.str.3 = [3 x i8] c"\\f\00"
@.str.4 = [3 x i8] c"\\r\00"
@.str.5 = [3 x i8] c"\\\22\00"
@.str.6 = [3 x i8] c"\\/\00"
@.str.7 = [3 x i8] c"\\\\\00"
define i32 @strbuf_addmem2json(%struct.strbuf* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.strbuf*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store %struct.strbuf* %0, %struct.strbuf** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
%12 = load i64, i64* %7
%13 = getelementptr inbounds i8, i8* %11, i64 %12
store i8* %13, i8** %8
%14 = load i8*, i8** %6
store i8* %14, i8** %9
br label %15
15: ; preds = %66, %3
%16 = load i8*, i8** %9
%17 = load i8*, i8** %8
%18 = icmp ult i8* %16, %17
br i1 %18, label %19, label %69
19: ; preds = %15
%20 = load i8*, i8** %9
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
switch i32 %22, label %47 [
i32 8, label %23
i32 9, label %26
i32 10, label %29
i32 12, label %32
i32 13, label %35
i32 34, label %38
i32 47, label %41
i32 92, label %44
]
23: ; preds = %19
%24 = load %struct.strbuf*, %struct.strbuf** %5
%25 = call i32 @strbuf_addmem(%struct.strbuf* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
store i32 %25, i32* %10
br label %61
26: ; preds = %19
%27 = load %struct.strbuf*, %struct.strbuf** %5
%28 = call i32 @strbuf_addmem(%struct.strbuf* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
store i32 %28, i32* %10
br label %61
29: ; preds = %19
%30 = load %struct.strbuf*, %struct.strbuf** %5
%31 = call i32 @strbuf_addmem(%struct.strbuf* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
store i32 %31, i32* %10
br label %61
32: ; preds = %19
%33 = load %struct.strbuf*, %struct.strbuf** %5
%34 = call i32 @strbuf_addmem(%struct.strbuf* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
store i32 %34, i32* %10
br label %61
35: ; preds = %19
%36 = load %struct.strbuf*, %struct.strbuf** %5
%37 = call i32 @strbuf_addmem(%struct.strbuf* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
store i32 %37, i32* %10
br label %61
38: ; preds = %19
%39 = load %struct.strbuf*, %struct.strbuf** %5
%40 = call i32 @strbuf_addmem(%struct.strbuf* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
store i32 %40, i32* %10
br label %61
41: ; preds = %19
%42 = load %struct.strbuf*, %struct.strbuf** %5
%43 = call i32 @strbuf_addmem(%struct.strbuf* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 2)
store i32 %43, i32* %10
br label %61
44: ; preds = %19
%45 = load %struct.strbuf*, %struct.strbuf** %5
%46 = call i32 @strbuf_addmem(%struct.strbuf* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 2)
store i32 %46, i32* %10
br label %61
47: ; preds = %19
%48 = load i8*, i8** %9
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = call i32 @isprint(i32 %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %47
%54 = load %struct.strbuf*, %struct.strbuf** %5
%55 = load i8*, i8** %9
%56 = load i8, i8* %55
%57 = call i32 @strbuf_addchar(%struct.strbuf* %54, i8 signext %56)
br label %59
58: ; preds = %47
br label %59
59: ; preds = %58, %53
%60 = phi i32 [ %57, %53 ], [ -1, %58 ]
store i32 %60, i32* %10
br label %61
61: ; preds = %59, %44, %41, %38, %35, %32, %29, %26, %23
%62 = load i32, i32* %10
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %65
64: ; preds = %61
store i32 -1, i32* %4
br label %70
65: ; preds = %61
br label %66
66: ; preds = %65
%67 = load i8*, i8** %9
%68 = getelementptr inbounds i8, i8* %67, i32 1
store i8* %68, i8** %9
br label %15
69: ; preds = %15
store i32 0, i32* %4
br label %70
70: ; preds = %69, %64
%71 = load i32, i32* %4
ret i32 %71
}

LLVM IR (-O2):
%struct.strbuf = type { i32 }
@.str = [3 x i8] c"\\b\00"
@.str.1 = [3 x i8] c"\\t\00"
@.str.2 = [3 x i8] c"\\n\00"
@.str.3 = [3 x i8] c"\\f\00"
@.str.4 = [3 x i8] c"\\r\00"
@.str.5 = [3 x i8] c"\\\22\00"
@.str.6 = [3 x i8] c"\\/\00"
@.str.7 = [3 x i8] c"\\\\\00"
define i32 @strbuf_addmem2json(%struct.strbuf* %0, i8* %1, i64 %2) {
%4 = getelementptr inbounds i8, i8* %1, i64 %2
%5 = icmp sgt i64 %2, 0
br i1 %5, label %8, label %37
6: ; preds = %33
%7 = icmp ult i8* %36, %4
br i1 %7, label %8, label %37
8: ; preds = %3, %6
%9 = phi i8* [ %36, %6 ], [ %1, %3 ]
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
switch i32 %11, label %28 [
i32 8, label %12
i32 9, label %14
i32 10, label %16
i32 12, label %18
i32 13, label %20
i32 34, label %22
i32 47, label %24
i32 92, label %26
]
12: ; preds = %8
%13 = tail call i32 @strbuf_addmem(%struct.strbuf* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
br label %33
14: ; preds = %8
%15 = tail call i32 @strbuf_addmem(%struct.strbuf* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
br label %33
16: ; preds = %8
%17 = tail call i32 @strbuf_addmem(%struct.strbuf* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %33
18: ; preds = %8
%19 = tail call i32 @strbuf_addmem(%struct.strbuf* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
br label %33
20: ; preds = %8
%21 = tail call i32 @strbuf_addmem(%struct.strbuf* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
br label %33
22: ; preds = %8
%23 = tail call i32 @strbuf_addmem(%struct.strbuf* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
br label %33
24: ; preds = %8
%25 = tail call i32 @strbuf_addmem(%struct.strbuf* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 2)
br label %33
26: ; preds = %8
%27 = tail call i32 @strbuf_addmem(%struct.strbuf* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 2)
br label %33
28: ; preds = %8
%29 = tail call i32 @isprint(i32 %11)
%30 = icmp eq i32 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %28
%32 = tail call i32 @strbuf_addchar(%struct.strbuf* %0, i8 signext %10)
br label %33
33: ; preds = %31, %26, %24, %22, %20, %18, %16, %14, %12
%34 = phi i32 [ %27, %26 ], [ %25, %24 ], [ %23, %22 ], [ %21, %20 ], [ %19, %18 ], [ %17, %16 ], [ %15, %14 ], [ %13, %12 ], [ %32, %31 ]
%35 = icmp eq i32 %34, -1
%36 = getelementptr inbounds i8, i8* %9, i64 1
br i1 %35, label %37, label %6
37: ; preds = %33, %6, %28, %3
%38 = phi i32 [ 0, %3 ], [ -1, %28 ], [ 0, %6 ], [ -1, %33 ]
ret i32 %38
}