#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 */
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
char escape_as(char);
size_t json_calc_escaped_len(char *, size_t);
ssize_t json_escape(char *str, size_t *len, size_t buf_size) {
  char *next;
  char *dest;
  size_t escaped_len = json_calc_escaped_len(str, *len);
  if (escaped_len == *len) {
    return 0;
  }
  if (escaped_len >= buf_size) {
    return -ENOMEM;
  }
  str[escaped_len] = '\0';
  for (next = &str[*len], dest = &str[escaped_len]; next != str;) {
    char next_c = *(--next);
    char escape = escape_as(next_c);
    if (escape) {
      *(--dest) = escape;
      *(--dest) = '\\';
    } else {
      *(--dest) = next_c;
    }
  }
  *len = escaped_len;
  return 0;
}


x86 assembly (clang, -O0):
<json_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1030 <json_calc_escaped_len@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jne    1165 <json_escape+0x45>
movl   $0x0,-0x4(%rbp)
jmp    123f <json_escape+0x11f>
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1186 <json_escape+0x66>
xor    %eax,%eax
mov    0x2e64(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    123f <json_escape+0x11f>
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    (%rcx),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     122d <json_escape+0x10d>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x28(%rbp)
mov    -0x1(%rax),%al
mov    %al,-0x39(%rbp)
movsbl -0x39(%rbp),%edi
call   1040 <escape_as@plt>
mov    %al,-0x3a(%rbp)
cmpb   $0x0,-0x3a(%rbp)
je     1213 <json_escape+0xf3>
mov    -0x3a(%rbp),%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x5c,-0x1(%rax)
jmp    1228 <json_escape+0x108>
mov    -0x39(%rbp),%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,-0x1(%rax)
jmp    11ad <json_escape+0x8d>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<json_escape>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%r14
mov    (%rsi),%rsi
call   1030 <json_calc_escaped_len@plt>
mov    %rax,%r15
xor    %eax,%eax
cmp    0x0(%r13),%r15
je     11b5 <json_escape+0x95>
cmp    %r12,%r15
jae    11aa <json_escape+0x8a>
movb   $0x0,(%r14,%r15,1)
mov    0x0(%r13),%rbx
test   %rbx,%rbx
je     11a2 <json_escape+0x82>
mov    %r15,%rbp
add    %r14,%rbp
jmp    117e <json_escape+0x5e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0xffffffffffffffff,%rbp
mov    %r12b,0x0(%rbp)
add    $0xffffffffffffffff,%rbx
je     11a2 <json_escape+0x82>
movzbl -0x1(%r14,%rbx,1),%r12d
movsbl %r12b,%edi
call   1040 <escape_as@plt>
test   %al,%al
je     1170 <json_escape+0x50>
mov    %al,-0x1(%rbp)
movb   $0x5c,-0x2(%rbp)
add    $0xfffffffffffffffe,%rbp
add    $0xffffffffffffffff,%rbx
jne    117e <json_escape+0x5e>
mov    %r15,0x0(%r13)
xor    %eax,%eax
jmp    11b5 <json_escape+0x95>
mov    0x2e2f(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
define i32 @json_escape(i8* %0, i64* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8
%12 = alloca i8
store i8* %0, i8** %5
store i64* %1, i64** %6
store i64 %2, i64* %7
%13 = load i8*, i8** %5
%14 = load i64*, i64** %6
%15 = load i64, i64* %14
%16 = call i64 @json_calc_escaped_len(i8* %13, i64 %15)
store i64 %16, i64* %10
%17 = load i64, i64* %10
%18 = load i64*, i64** %6
%19 = load i64, i64* %18
%20 = icmp eq i64 %17, %19
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 0, i32* %4
br label %66
22: ; preds = %3
%23 = load i64, i64* %10
%24 = load i64, i64* %7
%25 = icmp uge i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* @ENOMEM
%28 = sub nsw i32 0, %27
store i32 %28, i32* %4
br label %66
29: ; preds = %22
%30 = load i8*, i8** %5
%31 = load i64, i64* %10
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8 0, i8* %32
%33 = load i8*, i8** %5
%34 = load i64*, i64** %6
%35 = load i64, i64* %34
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8* %36, i8** %8
%37 = load i8*, i8** %5
%38 = load i64, i64* %10
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8* %39, i8** %9
br label %40
40: ; preds = %62, %29
%41 = load i8*, i8** %8
%42 = load i8*, i8** %5
%43 = icmp ne i8* %41, %42
br i1 %43, label %44, label %63
44: ; preds = %40
%45 = load i8*, i8** %8
%46 = getelementptr inbounds i8, i8* %45, i32 -1
store i8* %46, i8** %8
%47 = load i8, i8* %46
store i8 %47, i8* %11
%48 = load i8, i8* %11
%49 = call signext i8 @escape_as(i8 signext %48)
store i8 %49, i8* %12
%50 = load i8, i8* %12
%51 = icmp ne i8 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %44
%53 = load i8, i8* %12
%54 = load i8*, i8** %9
%55 = getelementptr inbounds i8, i8* %54, i32 -1
store i8* %55, i8** %9
store i8 %53, i8* %55
%56 = load i8*, i8** %9
%57 = getelementptr inbounds i8, i8* %56, i32 -1
store i8* %57, i8** %9
store i8 92, i8* %57
br label %62
58: ; preds = %44
%59 = load i8, i8* %11
%60 = load i8*, i8** %9
%61 = getelementptr inbounds i8, i8* %60, i32 -1
store i8* %61, i8** %9
store i8 %59, i8* %61
br label %62
62: ; preds = %58, %52
br label %40
63: ; preds = %40
%64 = load i64, i64* %10
%65 = load i64*, i64** %6
store i64 %64, i64* %65
store i32 0, i32* %4
br label %66
66: ; preds = %63, %26, %21
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
@ENOMEM = global i32 0
define i32 @json_escape(i8* %0, i64* nocapture %1, i64 %2) {
%4 = load i64, i64* %1
%5 = tail call i64 @json_calc_escaped_len(i8* %0, i64 %4)
%6 = load i64, i64* %1
%7 = icmp eq i64 %5, %6
br i1 %7, label %34, label %8
8: ; preds = %3
%9 = icmp ult i64 %5, %2
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = load i32, i32* @ENOMEM
%12 = sub nsw i32 0, %11
br label %34
13: ; preds = %8
%14 = getelementptr inbounds i8, i8* %0, i64 %5
store i8 0, i8* %14
%15 = load i64, i64* %1
%16 = icmp eq i64 %15, 0
br i1 %16, label %33, label %17
17: ; preds = %13
%18 = getelementptr inbounds i8, i8* %0, i64 %15
br label %19
19: ; preds = %17, %30
%20 = phi i8* [ %22, %30 ], [ %18, %17 ]
%21 = phi i8* [ %31, %30 ], [ %14, %17 ]
%22 = getelementptr inbounds i8, i8* %20, i64 -1
%23 = load i8, i8* %22
%24 = tail call signext i8 @escape_as(i8 signext %23)
%25 = icmp eq i8 %24, 0
%26 = getelementptr inbounds i8, i8* %21, i64 -1
br i1 %25, label %29, label %27
27: ; preds = %19
store i8 %24, i8* %26
%28 = getelementptr inbounds i8, i8* %21, i64 -2
store i8 92, i8* %28
br label %30
29: ; preds = %19
store i8 %23, i8* %26
br label %30
30: ; preds = %29, %27
%31 = phi i8* [ %28, %27 ], [ %26, %29 ]
%32 = icmp eq i8* %22, %0
br i1 %32, label %33, label %19
33: ; preds = %30, %13
store i64 %5, i64* %1
br label %34
34: ; preds = %3, %33, %10
%35 = phi i32 [ %12, %10 ], [ 0, %33 ], [ 0, %3 ]
ret i32 %35
}