#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*/ assert(char const *);
char *cbmyfatal(char *);
char *malloc(int);
int strlen(char const *);
char *cbcsvescape(const char *str) {
  char *buf, *wp;
  int i;
  assert(str);
  (((buf) = malloc(strlen(str) * 2 + 3)) ? (buf) : cbmyfatal("out of memory"));
  wp = buf;
  *(wp++) = '"';
  for (i = 0; str[i] != '\0'; i++) {
    if (str[i] == '"')
      *(wp++) = '"';
    *(wp++) = str[i];
  }
  *(wp++) = '"';
  *wp = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<cbcsvescape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
shl    $0x1,%edi
add    $0x3,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1182 <cbcsvescape+0x42>
mov    -0x10(%rbp),%rax
jmp    118e <cbcsvescape+0x4e>
lea    0xe77(%rip),%rdi
call   1060 <cbmyfatal@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x22,(%rax)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1215 <cbcsvescape+0xd5>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x22,%eax
jne    11eb <cbcsvescape+0xab>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x22,(%rax)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11af <cbcsvescape+0x6f>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x22,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cbcsvescape>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <assert@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%edi
add    $0x3,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1173 <cbcsvescape+0x33>
lea    0xe92(%rip),%rdi
call   1060 <cbmyfatal@plt>
movb   $0x22,(%r14)
mov    %r14,%rax
jmp    1191 <cbcsvescape+0x51>
nopl   0x0(%rax)
movb   $0x22,0x1(%rax)
add    $0x2,%rax
movzbl (%rbx),%ecx
mov    %cl,(%rax)
add    $0x1,%rbx
movzbl (%rbx),%ecx
cmp    $0x22,%cl
je     1180 <cbcsvescape+0x40>
test   %cl,%cl
je     11a3 <cbcsvescape+0x63>
add    $0x1,%rax
jmp    118b <cbcsvescape+0x4b>
movw   $0x22,0x1(%rax)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"out of memory\00"
define i8* @cbcsvescape(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @assert(i8* %6)
%8 = load i8*, i8** %2
%9 = call i32 @strlen(i8* %8)
%10 = mul nsw i32 %9, 2
%11 = add nsw i32 %10, 3
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = load i8*, i8** %3
br label %18
16: ; preds = %1
%17 = call i8* @cbmyfatal(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
%19 = phi i8* [ %15, %14 ], [ %17, %16 ]
%20 = load i8*, i8** %3
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %4
store i8 34, i8* %21
store i32 0, i32* %5
br label %23
23: ; preds = %50, %18
%24 = load i8*, i8** %2
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %53
31: ; preds = %23
%32 = load i8*, i8** %2
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 34
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %4
store i8 34, i8* %40
br label %42
42: ; preds = %39, %31
%43 = load i8*, i8** %2
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i8, i8* %46
%48 = load i8*, i8** %4
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %4
store i8 %47, i8* %48
br label %50
50: ; preds = %42
%51 = load i32, i32* %5
%52 = add nsw i32 %51, 1
store i32 %52, i32* %5
br label %23
53: ; preds = %23
%54 = load i8*, i8** %4
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %4
store i8 34, i8* %54
%56 = load i8*, i8** %4
store i8 0, i8* %56
%57 = load i8*, i8** %3
ret i8* %57
}

LLVM IR (-O2):
@.str = [14 x i8] c"out of memory\00"
define noalias i8* @cbcsvescape(i8* %0) {
%2 = tail call i32 @assert(i8* %0)
%3 = tail call i32 @strlen(i8* %0)
%4 = shl nsw i32 %3, 1
%5 = add nsw i32 %4, 3
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i8* @cbmyfatal(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %1, %8
store i8 34, i8* %6
br label %11
11: ; preds = %20, %10
%12 = phi i64 [ %23, %20 ], [ 0, %10 ]
%13 = phi i8* [ %22, %20 ], [ %6, %10 ]
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = getelementptr inbounds i8, i8* %0, i64 %12
%16 = load i8, i8* %15
switch i8 %16, label %20 [
i8 0, label %24
i8 34, label %17
]
17: ; preds = %11
%18 = getelementptr inbounds i8, i8* %13, i64 2
store i8 34, i8* %14
%19 = load i8, i8* %15
br label %20
20: ; preds = %11, %17
%21 = phi i8 [ %19, %17 ], [ %16, %11 ]
%22 = phi i8* [ %18, %17 ], [ %14, %11 ]
store i8 %21, i8* %22
%23 = add nuw i64 %12, 1
br label %11
24: ; preds = %11
%25 = getelementptr inbounds i8, i8* %13, i64 2
store i8 34, i8* %14
store i8 0, i8* %25
ret i8* %6
}