#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*/ check_capacity(char **, size_t *, size_t, char **, size_t);
char *c_quote(char *str, size_t ilen, size_t *len) {
  static char *buf = NULL;
  static size_t buf_cap = 0;
  const char DQ = '"';
  const char E = '\\';
  check_capacity(&buf, &buf_cap, ilen, 0, 0);
  if (len)
    *len = 0;
  if (buf) {
    size_t i = 0;
    char *p = buf;
    p[i++] = DQ;
    while (ilen > 0) {
      check_capacity(&buf, &buf_cap, i + 8, &p, i);
      if (!buf)
        return buf;
      if (*str == DQ) {
        p[i++] = E;
        p[i++] = DQ;
      } else if (*str == E) {
        p[i++] = E;
        p[i++] = E;
      } else {
        p[i++] = *str;
      }
      str++;
      ilen--;
    }
    p[i++] = DQ;
    p[i] = 0;
    if (len)
      *len = i;
  }
  return buf;
}


x86 assembly (clang, -O0):
<c_quote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movb   $0x22,-0x21(%rbp)
movb   $0x5c,-0x22(%rbp)
mov    -0x18(%rbp),%rdx
lea    0x2ef9(%rip),%rdi
lea    0x2efa(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   1030 <check_capacity@plt>
cmpq   $0x0,-0x20(%rbp)
je     1161 <c_quote+0x51>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,0x2ec7(%rip)
je     12e7 <c_quote+0x1d7>
movq   $0x0,-0x30(%rbp)
mov    0x2eb2(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x22,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
jbe    12a9 <c_quote+0x199>
mov    -0x30(%rbp),%rdx
add    $0x8,%rdx
mov    -0x30(%rbp),%r8
lea    0x2e79(%rip),%rdi
lea    0x2e7a(%rip),%rsi
lea    -0x38(%rbp),%rcx
call   1030 <check_capacity@plt>
cmpq   $0x0,0x2e61(%rip)
jne    11e5 <c_quote+0xd5>
mov    0x2e54(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12f2 <c_quote+0x1e2>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
jne    1228 <c_quote+0x118>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x5c,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x22,(%rax,%rcx,1)
jmp    128c <c_quote+0x17c>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
jne    126b <c_quote+0x15b>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x5c,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x5c,(%rax,%rcx,1)
jmp    1287 <c_quote+0x177>
mov    -0x10(%rbp),%rax
mov    (%rax),%dl
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x30(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    128c <c_quote+0x17c>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1199 <c_quote+0x89>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x22,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x20(%rbp)
je     12e2 <c_quote+0x1d2>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    12e7 <c_quote+0x1d7>
mov    0x2d42(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c_quote>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r15
lea    0x2f02(%rip),%rdi
lea    0x2f03(%rip),%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1030 <check_capacity@plt>
test   %rbx,%rbx
je     114e <c_quote+0x3e>
movq   $0x0,(%rbx)
mov    0x2edb(%rip),%rax
test   %rax,%rax
je     1209 <c_quote+0xf9>
mov    %rax,0x8(%rsp)
movb   $0x22,(%rax)
test   %r12,%r12
mov    %rbx,0x10(%rsp)
mov    $0x1,%ebx
je     120d <c_quote+0xfd>
xor    %r13d,%r13d
lea    0x2eb5(%rip),%rbp
lea    0x8(%rsp),%r14
jmp    11a5 <c_quote+0x95>
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rcx
mov    %al,(%rcx,%rbx,1)
add    $0x1,%rbx
add    $0x1,%r13
cmp    %r13,%r12
je     120d <c_quote+0xfd>
lea    0x8(%rbx),%rdx
lea    0x2e80(%rip),%rdi
mov    %rbp,%rsi
mov    %r14,%rcx
mov    %rbx,%r8
call   1030 <check_capacity@plt>
cmpq   $0x0,0x2e6a(%rip)
je     1209 <c_quote+0xf9>
movzbl (%r15,%r13,1),%eax
cmp    $0x5c,%al
je     11f0 <c_quote+0xe0>
cmp    $0x22,%al
jne    1190 <c_quote+0x80>
mov    0x8(%rsp),%rax
movb   $0x5c,(%rax,%rbx,1)
mov    0x8(%rsp),%rax
movb   $0x22,0x1(%rax,%rbx,1)
add    $0x1,%rbx
jmp    1198 <c_quote+0x88>
xchg   %ax,%ax
mov    0x8(%rsp),%rax
movb   $0x5c,(%rax,%rbx,1)
mov    0x8(%rsp),%rax
movb   $0x5c,0x1(%rax,%rbx,1)
add    $0x1,%rbx
jmp    1198 <c_quote+0x88>
xor    %eax,%eax
jmp    1238 <c_quote+0x128>
mov    0x8(%rsp),%rax
movb   $0x22,(%rax,%rbx,1)
mov    0x8(%rsp),%rax
movb   $0x0,0x1(%rax,%rbx,1)
mov    0x10(%rsp),%rax
test   %rax,%rax
je     1231 <c_quote+0x121>
add    $0x1,%rbx
mov    %rbx,(%rax)
mov    0x2df8(%rip),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@c_quote.buf = internal global i8* null
@c_quote.buf_cap = internal global i64 0
define i8* @c_quote(i8* %0, i64 %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i8
%9 = alloca i8
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
store i8 34, i8* %8
store i8 92, i8* %9
%12 = load i64, i64* %6
%13 = call i32 @check_capacity(i8** @c_quote.buf, i64* @c_quote.buf_cap, i64 %12, i8** null, i64 0)
%14 = load i64*, i64** %7
%15 = icmp ne i64* %14, null
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i64*, i64** %7
store i64 0, i64* %17
br label %18
18: ; preds = %16, %3
%19 = load i8*, i8** @c_quote.buf
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %94
21: ; preds = %18
store i64 0, i64* %10
%22 = load i8*, i8** @c_quote.buf
store i8* %22, i8** %11
%23 = load i8*, i8** %11
%24 = load i64, i64* %10
%25 = add i64 %24, 1
store i64 %25, i64* %10
%26 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 34, i8* %26
br label %27
27: ; preds = %75, %21
%28 = load i64, i64* %6
%29 = icmp ugt i64 %28, 0
br i1 %29, label %30, label %80
30: ; preds = %27
%31 = load i64, i64* %10
%32 = add i64 %31, 8
%33 = load i64, i64* %10
%34 = call i32 @check_capacity(i8** @c_quote.buf, i64* @c_quote.buf_cap, i64 %32, i8** %11, i64 %33)
%35 = load i8*, i8** @c_quote.buf
%36 = icmp ne i8* %35, null
br i1 %36, label %39, label %37
37: ; preds = %30
%38 = load i8*, i8** @c_quote.buf
store i8* %38, i8** %4
br label %96
39: ; preds = %30
%40 = load i8*, i8** %5
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 34
br i1 %43, label %44, label %53
44: ; preds = %39
%45 = load i8*, i8** %11
%46 = load i64, i64* %10
%47 = add i64 %46, 1
store i64 %47, i64* %10
%48 = getelementptr inbounds i8, i8* %45, i64 %46
store i8 92, i8* %48
%49 = load i8*, i8** %11
%50 = load i64, i64* %10
%51 = add i64 %50, 1
store i64 %51, i64* %10
%52 = getelementptr inbounds i8, i8* %49, i64 %50
store i8 34, i8* %52
br label %75
53: ; preds = %39
%54 = load i8*, i8** %5
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 92
br i1 %57, label %58, label %67
58: ; preds = %53
%59 = load i8*, i8** %11
%60 = load i64, i64* %10
%61 = add i64 %60, 1
store i64 %61, i64* %10
%62 = getelementptr inbounds i8, i8* %59, i64 %60
store i8 92, i8* %62
%63 = load i8*, i8** %11
%64 = load i64, i64* %10
%65 = add i64 %64, 1
store i64 %65, i64* %10
%66 = getelementptr inbounds i8, i8* %63, i64 %64
store i8 92, i8* %66
br label %74
67: ; preds = %53
%68 = load i8*, i8** %5
%69 = load i8, i8* %68
%70 = load i8*, i8** %11
%71 = load i64, i64* %10
%72 = add i64 %71, 1
store i64 %72, i64* %10
%73 = getelementptr inbounds i8, i8* %70, i64 %71
store i8 %69, i8* %73
br label %74
74: ; preds = %67, %58
br label %75
75: ; preds = %74, %44
%76 = load i8*, i8** %5
%77 = getelementptr inbounds i8, i8* %76, i32 1
store i8* %77, i8** %5
%78 = load i64, i64* %6
%79 = add i64 %78, -1
store i64 %79, i64* %6
br label %27
80: ; preds = %27
%81 = load i8*, i8** %11
%82 = load i64, i64* %10
%83 = add i64 %82, 1
store i64 %83, i64* %10
%84 = getelementptr inbounds i8, i8* %81, i64 %82
store i8 34, i8* %84
%85 = load i8*, i8** %11
%86 = load i64, i64* %10
%87 = getelementptr inbounds i8, i8* %85, i64 %86
store i8 0, i8* %87
%88 = load i64*, i64** %7
%89 = icmp ne i64* %88, null
br i1 %89, label %90, label %93
90: ; preds = %80
%91 = load i64, i64* %10
%92 = load i64*, i64** %7
store i64 %91, i64* %92
br label %93
93: ; preds = %90, %80
br label %94
94: ; preds = %93, %18
%95 = load i8*, i8** @c_quote.buf
store i8* %95, i8** %4
br label %96
96: ; preds = %94, %37
%97 = load i8*, i8** %4
ret i8* %97
}

LLVM IR (-O2):
@c_quote.buf = internal global i8* null
@c_quote.buf_cap = internal global i64 0
define i8* @c_quote(i8* nocapture %0, i64 %1, i64* writeonly %2) {
%4 = alloca i8*
%5 = tail call i32 @check_capacity(i8** @c_quote.buf, i64* @c_quote.buf_cap, i64 %1, i8** null, i64 0)
%6 = icmp eq i64* %2, null
br i1 %6, label %8, label %7
7: ; preds = %3
store i64 0, i64* %2
br label %8
8: ; preds = %7, %3
%9 = load i8*, i8** @c_quote.buf
%10 = icmp eq i8* %9, null
br i1 %10, label %56, label %11
11: ; preds = %8
%12 = bitcast i8** %4 to i8*
store i8* %9, i8** %4
store i8 34, i8* %9
%13 = icmp eq i64 %1, 0
br i1 %13, label %45, label %14
14: ; preds = %11, %39
%15 = phi i64 [ %41, %39 ], [ 1, %11 ]
%16 = phi i8* [ %42, %39 ], [ %0, %11 ]
%17 = phi i64 [ %43, %39 ], [ %1, %11 ]
%18 = add i64 %15, 8
%19 = call i32 @check_capacity(i8** @c_quote.buf, i64* @c_quote.buf_cap, i64 %18, i8** %4, i64 %15)
%20 = load i8*, i8** @c_quote.buf
%21 = icmp eq i8* %20, null
br i1 %21, label %55, label %22
22: ; preds = %14
%23 = load i8, i8* %16
switch i8 %23, label %36 [
i8 34, label %24
i8 92, label %30
]
24: ; preds = %22
%25 = load i8*, i8** %4
%26 = add i64 %15, 1
%27 = getelementptr inbounds i8, i8* %25, i64 %15
store i8 92, i8* %27
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i64 %26
store i8 34, i8* %29
br label %39
30: ; preds = %22
%31 = load i8*, i8** %4
%32 = add i64 %15, 1
%33 = getelementptr inbounds i8, i8* %31, i64 %15
store i8 92, i8* %33
%34 = load i8*, i8** %4
%35 = getelementptr inbounds i8, i8* %34, i64 %32
store i8 92, i8* %35
br label %39
36: ; preds = %22
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 %15
store i8 %23, i8* %38
br label %39
39: ; preds = %30, %36, %24
%40 = phi i64 [ %26, %24 ], [ %32, %30 ], [ %15, %36 ]
%41 = add i64 %40, 1
%42 = getelementptr inbounds i8, i8* %16, i64 1
%43 = add i64 %17, -1
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %14
45: ; preds = %39, %11
%46 = phi i64 [ 1, %11 ], [ %41, %39 ]
%47 = load i8*, i8** %4
%48 = add i64 %46, 1
%49 = getelementptr inbounds i8, i8* %47, i64 %46
store i8 34, i8* %49
%50 = load i8*, i8** %4
%51 = getelementptr inbounds i8, i8* %50, i64 %48
store i8 0, i8* %51
br i1 %6, label %53, label %52
52: ; preds = %45
store i64 %48, i64* %2
br label %53
53: ; preds = %45, %52
%54 = load i8*, i8** @c_quote.buf
br label %56
55: ; preds = %14
br label %56
56: ; preds = %8, %53, %55
%57 = phi i8* [ null, %55 ], [ %54, %53 ], [ null, %8 ]
ret i8* %57
}