#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*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int JsonEscapeString(char *str) {
  char buffer[3 * 256];
  int i = 0, n = 0;
  for (i = 0; i < strlen(str); ++i) {
    if (str[i] == '"') {
      sprintf(buffer + i + n, "\\%c", str[i]);
      ++n;
    } else if (str[i] == ',') {
      str[i] = '.';
    } else {
      sprintf(buffer + i + n, "%c", str[i]);
    }
  }
  if (n > 0) {
    memset(str, 0, strlen(str));
    strcpy(str, buffer);
  }
  return 0;
}


x86 assembly (clang, -O0):
<JsonEscapeString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x330,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x314(%rbp)
movl   $0x0,-0x318(%rbp)
movl   $0x0,-0x314(%rbp)
mov    -0x314(%rbp),%eax
mov    %eax,-0x31c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x31c(%rbp),%eax
cmp    %ecx,%eax
jge    1274 <JsonEscapeString+0x134>
mov    -0x8(%rbp),%rax
movslq -0x314(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x22,%eax
jne    11f4 <JsonEscapeString+0xb4>
lea    -0x310(%rbp),%rdi
movslq -0x314(%rbp),%rax
add    %rax,%rdi
movslq -0x318(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movslq -0x314(%rbp),%rcx
lea    0xe29(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1060 <sprintf@plt>
mov    -0x318(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x318(%rbp)
jmp    125b <JsonEscapeString+0x11b>
mov    -0x8(%rbp),%rax
movslq -0x314(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2c,%eax
jne    1220 <JsonEscapeString+0xe0>
mov    -0x8(%rbp),%rax
movslq -0x314(%rbp),%rcx
movb   $0x2e,(%rax,%rcx,1)
jmp    1256 <JsonEscapeString+0x116>
lea    -0x310(%rbp),%rdi
movslq -0x314(%rbp),%rax
add    %rax,%rdi
movslq -0x318(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movslq -0x314(%rbp),%rcx
lea    0xdb4(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1060 <sprintf@plt>
jmp    125b <JsonEscapeString+0x11b>
jmp    1260 <JsonEscapeString+0x120>
mov    -0x314(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x314(%rbp)
jmp    116d <JsonEscapeString+0x2d>
cmpl   $0x0,-0x318(%rbp)
jle    12b5 <JsonEscapeString+0x175>
mov    -0x8(%rbp),%rax
mov    %rax,-0x328(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x328(%rbp),%rdi
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
lea    -0x310(%rbp),%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x330,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<JsonEscapeString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x308,%rsp
mov    %rdi,%r15
call   1040 <strlen@plt>
test   %eax,%eax
jle    11f8 <JsonEscapeString+0xb8>
xor    %r12d,%r12d
mov    %rsp,%r13
lea    0xe92(%rip),%r14
mov    %r15,%rbx
xor    %ebp,%ebp
jmp    11a9 <JsonEscapeString+0x69>
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebp,%rcx
mov    %al,0x0(%r13,%rcx,1)
movb   $0x0,0x1(%r13,%rcx,1)
add    $0x1,%r12
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
add    $0x1,%rbx
add    $0x1,%r13
cmp    %rax,%r12
jge    11d5 <JsonEscapeString+0x95>
movzbl (%rbx),%eax
cmp    $0x2c,%al
je     11d0 <JsonEscapeString+0x90>
cmp    $0x22,%al
jne    1180 <JsonEscapeString+0x40>
movslq %ebp,%rbp
mov    %r13,%rdi
add    %rbp,%rdi
mov    %r14,%rsi
mov    $0x22,%edx
call   1060 <sprintf@plt>
add    $0x1,%ebp
jmp    118e <JsonEscapeString+0x4e>
nop
movb   $0x2e,(%rbx)
jmp    118e <JsonEscapeString+0x4e>
test   %ebp,%ebp
jle    11f8 <JsonEscapeString+0xb8>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1050 <memset@plt>
mov    %rsp,%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x308,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"\\%c\00"
define i32 @JsonEscapeString(i8* %0) {
%2 = alloca [768 x i8]
%3 = getelementptr inbounds [768 x i8], [768 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %35
6: ; preds = %1, %23
%7 = phi i64 [ %25, %23 ], [ 0, %1 ]
%8 = phi i32 [ %24, %23 ], [ 0, %1 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %7
%10 = load i8, i8* %9
switch i8 %10, label %18 [
i8 34, label %11
i8 44, label %17
]
11: ; preds = %6
%12 = getelementptr inbounds [768 x i8], [768 x i8]* %2, i64 0, i64 %7
%13 = sext i32 %8 to i64
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 signext 34)
%16 = add nsw i32 %8, 1
br label %23
17: ; preds = %6
store i8 46, i8* %9
br label %23
18: ; preds = %6
%19 = getelementptr inbounds [768 x i8], [768 x i8]* %2, i64 0, i64 %7
%20 = sext i32 %8 to i64
%21 = getelementptr inbounds i8, i8* %19, i64 %20
store i8 %10, i8* %21
%22 = getelementptr i8, i8* %21, i64 1
store i8 0, i8* %22
br label %23
23: ; preds = %11, %18, %17
%24 = phi i32 [ %16, %11 ], [ %8, %17 ], [ %8, %18 ]
%25 = add nuw nsw i64 %7, 1
%26 = tail call i32 @strlen(i8* %0)
%27 = sext i32 %26 to i64
%28 = icmp slt i64 %25, %27
br i1 %28, label %6, label %29
29: ; preds = %23
%30 = icmp sgt i32 %24, 0
br i1 %30, label %31, label %35
31: ; preds = %29
%32 = tail call i32 @strlen(i8* %0)
%33 = tail call i32 @memset(i8* %0, i32 0, i32 %32)
%34 = call i32 @strcpy(i8* %0, i8* %3)
br label %35
35: ; preds = %1, %31, %29
ret i32 0
}