#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*/ sprintf(char *, char *, ...);
size_t strlen(char const *);
char *json_serialize_string(const char *string, char *buf) {
  size_t i = 0, len = strlen(string);
  char c = '\0';
  buf += sprintf(buf, "\"");
  for (i = 0; i < len; i++) {
    c = string[i];
    switch (c) {
    case '\"':
      buf += sprintf(buf, "\\\"");
      ;
      break;
    case '\\':
      buf += sprintf(buf, "\\\\");
      ;
      break;
    case '\b':
      buf += sprintf(buf, "\\b");
      ;
      break;
    case '\f':
      buf += sprintf(buf, "\\f");
      ;
      break;
    case '\n':
      buf += sprintf(buf, "\\n");
      ;
      break;
    case '\r':
      buf += sprintf(buf, "\\r");
      ;
      break;
    case '\t':
      buf += sprintf(buf, "\\t");
      ;
      break;
    default:
      buf += sprintf(buf, "%c", c);
      ;
      break;
    }
  }
  buf += sprintf(buf, "\"");
  ;
  return buf;
}


x86 assembly (clang, -O0):
<json_serialize_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xead(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1357 <json_serialize_string+0x237>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x8,%eax
je     1258 <json_serialize_string+0x138>
jmp    11a4 <json_serialize_string+0x84>
mov    -0x28(%rbp),%eax
sub    $0x9,%eax
je     12f4 <json_serialize_string+0x1d4>
jmp    11b5 <json_serialize_string+0x95>
mov    -0x28(%rbp),%eax
sub    $0xa,%eax
je     12a6 <json_serialize_string+0x186>
jmp    11c6 <json_serialize_string+0xa6>
mov    -0x28(%rbp),%eax
sub    $0xc,%eax
je     127f <json_serialize_string+0x15f>
jmp    11d7 <json_serialize_string+0xb7>
mov    -0x28(%rbp),%eax
sub    $0xd,%eax
je     12cd <json_serialize_string+0x1ad>
jmp    11e8 <json_serialize_string+0xc8>
mov    -0x28(%rbp),%eax
sub    $0x22,%eax
je     120a <json_serialize_string+0xea>
jmp    11f9 <json_serialize_string+0xd9>
mov    -0x28(%rbp),%eax
sub    $0x5c,%eax
je     1231 <json_serialize_string+0x111>
jmp    131b <json_serialize_string+0x1fb>
mov    -0x10(%rbp),%rdi
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1341 <json_serialize_string+0x221>
mov    -0x10(%rbp),%rdi
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1341 <json_serialize_string+0x221>
mov    -0x10(%rbp),%rdi
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1341 <json_serialize_string+0x221>
mov    -0x10(%rbp),%rdi
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1341 <json_serialize_string+0x221>
mov    -0x10(%rbp),%rdi
lea    0xd5b(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1341 <json_serialize_string+0x221>
mov    -0x10(%rbp),%rdi
lea    0xd37(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1341 <json_serialize_string+0x221>
mov    -0x10(%rbp),%rdi
lea    0xd13(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1341 <json_serialize_string+0x221>
mov    -0x10(%rbp),%rdi
movsbl -0x21(%rbp),%edx
lea    0xceb(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1346 <json_serialize_string+0x226>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1173 <json_serialize_string+0x53>
mov    -0x10(%rbp),%rdi
lea    0xc9f(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<json_serialize_string>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
movw   $0x22,(%rbx)
add    $0x1,%rbx
test   %rax,%rax
je     11ea <json_serialize_string+0xda>
xor    %ecx,%ecx
lea    0xec6(%rip),%r8
jmp    115d <json_serialize_string+0x4d>
mov    %dl,(%rbx)
movb   $0x0,0x1(%rbx)
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rcx
cmp    %rcx,%rax
je     11ea <json_serialize_string+0xda>
movzbl (%r14,%rcx,1),%edx
movsbl %dl,%edi
lea    -0x8(%rdi),%esi
cmp    $0x1a,%esi
ja     1185 <json_serialize_string+0x75>
movslq (%r8,%rsi,4),%rsi
add    %r8,%rsi
jmp    *%rsi
movb   $0x0,0x2(%rbx)
movw   $0x625c,(%rbx)
add    $0x2,%rbx
jmp    1150 <json_serialize_string+0x40>
cmp    $0x5c,%edi
jne    113c <json_serialize_string+0x2c>
movb   $0x0,0x2(%rbx)
movw   $0x5c5c,(%rbx)
add    $0x2,%rbx
jmp    1150 <json_serialize_string+0x40>
movb   $0x0,0x2(%rbx)
movw   $0x745c,(%rbx)
add    $0x2,%rbx
jmp    1150 <json_serialize_string+0x40>
movb   $0x0,0x2(%rbx)
movw   $0x6e5c,(%rbx)
add    $0x2,%rbx
jmp    1150 <json_serialize_string+0x40>
movb   $0x0,0x2(%rbx)
movw   $0x665c,(%rbx)
add    $0x2,%rbx
jmp    1150 <json_serialize_string+0x40>
movb   $0x0,0x2(%rbx)
movw   $0x725c,(%rbx)
add    $0x2,%rbx
jmp    1150 <json_serialize_string+0x40>
movb   $0x0,0x2(%rbx)
movw   $0x225c,(%rbx)
add    $0x2,%rbx
jmp    1150 <json_serialize_string+0x40>
movw   $0x22,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\22\00"
@.str.1 = [3 x i8] c"\\\22\00"
@.str.2 = [3 x i8] c"\\\\\00"
@.str.3 = [3 x i8] c"\\b\00"
@.str.4 = [3 x i8] c"\\f\00"
@.str.5 = [3 x i8] c"\\n\00"
@.str.6 = [3 x i8] c"\\r\00"
@.str.7 = [3 x i8] c"\\t\00"
@.str.8 = [3 x i8] c"%c\00"
define i8* @json_serialize_string(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8
store i8* %0, i8** %3
store i8* %1, i8** %4
store i64 0, i64* %5
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %6
store i8 0, i8* %7
%10 = load i8*, i8** %4
%11 = call i32 (i8*, i8*, ...) @sprintf(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = load i8*, i8** %4
%13 = sext i32 %11 to i64
%14 = getelementptr inbounds i8, i8* %12, i64 %13
store i8* %14, i8** %4
store i64 0, i64* %5
br label %15
15: ; preds = %77, %2
%16 = load i64, i64* %5
%17 = load i64, i64* %6
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %80
19: ; preds = %15
%20 = load i8*, i8** %3
%21 = load i64, i64* %5
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i8, i8* %22
store i8 %23, i8* %7
%24 = load i8, i8* %7
%25 = sext i8 %24 to i32
switch i32 %25, label %68 [
i32 34, label %26
i32 92, label %32
i32 8, label %38
i32 12, label %44
i32 10, label %50
i32 13, label %56
i32 9, label %62
]
26: ; preds = %19
%27 = load i8*, i8** %4
%28 = call i32 (i8*, i8*, ...) @sprintf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%29 = load i8*, i8** %4
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8* %31, i8** %4
br label %76
32: ; preds = %19
%33 = load i8*, i8** %4
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%35 = load i8*, i8** %4
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %4
br label %76
38: ; preds = %19
%39 = load i8*, i8** %4
%40 = call i32 (i8*, i8*, ...) @sprintf(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%41 = load i8*, i8** %4
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8* %43, i8** %4
br label %76
44: ; preds = %19
%45 = load i8*, i8** %4
%46 = call i32 (i8*, i8*, ...) @sprintf(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%47 = load i8*, i8** %4
%48 = sext i32 %46 to i64
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8* %49, i8** %4
br label %76
50: ; preds = %19
%51 = load i8*, i8** %4
%52 = call i32 (i8*, i8*, ...) @sprintf(i8* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%53 = load i8*, i8** %4
%54 = sext i32 %52 to i64
%55 = getelementptr inbounds i8, i8* %53, i64 %54
store i8* %55, i8** %4
br label %76
56: ; preds = %19
%57 = load i8*, i8** %4
%58 = call i32 (i8*, i8*, ...) @sprintf(i8* %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%59 = load i8*, i8** %4
%60 = sext i32 %58 to i64
%61 = getelementptr inbounds i8, i8* %59, i64 %60
store i8* %61, i8** %4
br label %76
62: ; preds = %19
%63 = load i8*, i8** %4
%64 = call i32 (i8*, i8*, ...) @sprintf(i8* %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%65 = load i8*, i8** %4
%66 = sext i32 %64 to i64
%67 = getelementptr inbounds i8, i8* %65, i64 %66
store i8* %67, i8** %4
br label %76
68: ; preds = %19
%69 = load i8*, i8** %4
%70 = load i8, i8* %7
%71 = sext i8 %70 to i32
%72 = call i32 (i8*, i8*, ...) @sprintf(i8* %69, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %71)
%73 = load i8*, i8** %4
%74 = sext i32 %72 to i64
%75 = getelementptr inbounds i8, i8* %73, i64 %74
store i8* %75, i8** %4
br label %76
76: ; preds = %68, %62, %56, %50, %44, %38, %32, %26
br label %77
77: ; preds = %76
%78 = load i64, i64* %5
%79 = add i64 %78, 1
store i64 %79, i64* %5
br label %15
80: ; preds = %15
%81 = load i8*, i8** %4
%82 = call i32 (i8*, i8*, ...) @sprintf(i8* %81, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%83 = load i8*, i8** %4
%84 = sext i32 %82 to i64
%85 = getelementptr inbounds i8, i8* %83, i64 %84
store i8* %85, i8** %4
%86 = load i8*, i8** %4
ret i8* %86
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"\\\22\00"
@.str.2 = [3 x i8] c"\\\\\00"
@.str.3 = [3 x i8] c"\\b\00"
@.str.4 = [3 x i8] c"\\f\00"
@.str.5 = [3 x i8] c"\\n\00"
@.str.6 = [3 x i8] c"\\r\00"
@.str.7 = [3 x i8] c"\\t\00"
define i8* @json_serialize_string(i8* nocapture %0, i8* writeonly %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = bitcast i8* %1 to i16*
store i16 34, i16* %4
%5 = getelementptr inbounds i8, i8* %1, i64 1
%6 = icmp eq i64 %3, 0
br i1 %6, label %33, label %7
7: ; preds = %2, %29
%8 = phi i8* [ %30, %29 ], [ %5, %2 ]
%9 = phi i64 [ %31, %29 ], [ 0, %2 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
switch i32 %12, label %27 [
i32 34, label %13
i32 92, label %15
i32 8, label %17
i32 12, label %19
i32 10, label %21
i32 13, label %23
i32 9, label %25
]
13: ; preds = %7
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %8, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64 3, i1 false)
%14 = getelementptr inbounds i8, i8* %8, i64 2
br label %29
15: ; preds = %7
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %8, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 3, i1 false)
%16 = getelementptr inbounds i8, i8* %8, i64 2
br label %29
17: ; preds = %7
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %8, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i64 3, i1 false)
%18 = getelementptr inbounds i8, i8* %8, i64 2
br label %29
19: ; preds = %7
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %8, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i64 3, i1 false)
%20 = getelementptr inbounds i8, i8* %8, i64 2
br label %29
21: ; preds = %7
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %8, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i64 3, i1 false)
%22 = getelementptr inbounds i8, i8* %8, i64 2
br label %29
23: ; preds = %7
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %8, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i64 3, i1 false)
%24 = getelementptr inbounds i8, i8* %8, i64 2
br label %29
25: ; preds = %7
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %8, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i64 3, i1 false)
%26 = getelementptr inbounds i8, i8* %8, i64 2
br label %29
27: ; preds = %7
store i8 %11, i8* %8
%28 = getelementptr i8, i8* %8, i64 1
store i8 0, i8* %28
br label %29
29: ; preds = %13, %15, %17, %19, %21, %23, %25, %27
%30 = phi i8* [ %28, %27 ], [ %26, %25 ], [ %24, %23 ], [ %22, %21 ], [ %20, %19 ], [ %18, %17 ], [ %16, %15 ], [ %14, %13 ]
%31 = add nuw i64 %9, 1
%32 = icmp eq i64 %31, %3
br i1 %32, label %33, label %7
33: ; preds = %29, %2
%34 = phi i8* [ %5, %2 ], [ %30, %29 ]
%35 = bitcast i8* %34 to i16*
store i16 34, i16* %35
%36 = getelementptr inbounds i8, i8* %34, i64 1
ret i8* %36
}