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


x86 assembly (clang, -O0):
<shell_quote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movb   $0x27,-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 <shell_quote+0x51>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,0x2ec7(%rip)
je     12cd <shell_quote+0x1bd>
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   $0x27,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
jbe    128f <shell_quote+0x17f>
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 <shell_quote+0xd5>
mov    0x2e54(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12d8 <shell_quote+0x1c8>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x27,%eax
je     1216 <shell_quote+0x106>
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    1272 <shell_quote+0x162>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x27,(%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)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x27,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x27,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1199 <shell_quote+0x89>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x27,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x20(%rbp)
je     12c8 <shell_quote+0x1b8>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    12cd <shell_quote+0x1bd>
mov    0x2d5c(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shell_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 <shell_quote+0x3e>
movq   $0x0,(%rbx)
mov    0x2edb(%rip),%rax
test   %rax,%rax
je     11f9 <shell_quote+0xe9>
mov    %rax,0x8(%rsp)
movb   $0x27,(%rax)
test   %r12,%r12
mov    %rbx,0x10(%rsp)
mov    $0x1,%ebx
je     11fd <shell_quote+0xed>
xor    %r13d,%r13d
lea    0x2eb5(%rip),%rbp
lea    0x8(%rsp),%r14
jmp    11bf <shell_quote+0xaf>
nopw   0x0(%rax,%rax,1)
movb   $0x27,(%rcx,%rbx,1)
mov    0x8(%rsp),%rax
movb   $0x5c,0x1(%rax,%rbx,1)
mov    0x8(%rsp),%rax
movb   $0x27,0x2(%rbx,%rax,1)
mov    0x8(%rsp),%rax
movb   $0x27,0x3(%rbx,%rax,1)
add    $0x4,%rbx
add    $0x1,%r13
cmp    %r13,%r12
je     11fd <shell_quote+0xed>
lea    0x8(%rbx),%rdx
lea    0x2e66(%rip),%rdi
mov    %rbp,%rsi
mov    %r14,%rcx
mov    %rbx,%r8
call   1030 <check_capacity@plt>
cmpq   $0x0,0x2e50(%rip)
je     11f9 <shell_quote+0xe9>
movzbl (%r15,%r13,1),%eax
mov    0x8(%rsp),%rcx
cmp    $0x27,%al
je     1190 <shell_quote+0x80>
mov    %al,(%rcx,%rbx,1)
add    $0x1,%rbx
jmp    11b6 <shell_quote+0xa6>
xor    %eax,%eax
jmp    1228 <shell_quote+0x118>
mov    0x8(%rsp),%rax
movb   $0x27,(%rax,%rbx,1)
mov    0x8(%rsp),%rax
movb   $0x0,0x1(%rax,%rbx,1)
mov    0x10(%rsp),%rax
test   %rax,%rax
je     1221 <shell_quote+0x111>
add    $0x1,%rbx
mov    %rbx,(%rax)
mov    0x2e08(%rip),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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