#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 strlen(char *);
void escape(char *input, char **output) {
  char c;
  int pos = 0;
  for (int i = 0; i < strlen(input); i++) {
    c = input[i];
    if (c == '"' || c == '\\') {
      (*output)[pos++] = '\\';
    }
    (*output)[pos++] = c;
  }
}


x86 assembly (clang, -O0):
<escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jge    11b4 <escape+0xa4>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x22,%eax
je     1172 <escape+0x62>
movsbl -0x11(%rbp),%eax
cmp    $0x5c,%eax
jne    118b <escape+0x7b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
movb   $0x5c,(%rax,%rcx,1)
mov    -0x11(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112e <escape+0x1e>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
test   %eax,%eax
jle    116b <escape+0x5b>
xor    %ebx,%ebx
xor    %ebp,%ebp
jmp    115c <escape+0x4c>
nopl   0x0(%rax)
mov    (%r14),%rcx
movslq %ebp,%rdx
add    $0x1,%ebp
movb   $0x5c,(%rcx,%rdx,1)
mov    (%r14),%rcx
movslq %ebp,%rdx
add    $0x1,%ebp
mov    %al,(%rcx,%rdx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jge    116b <escape+0x5b>
movzbl (%r15,%rbx,1),%eax
cmp    $0x5c,%al
je     1130 <escape+0x20>
cmp    $0x22,%al
je     1130 <escape+0x20>
jmp    113d <escape+0x2d>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @escape(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8** %1, i8*** %4
store i32 0, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %41, %2
%9 = load i32, i32* %7
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = icmp slt i32 %9, %11
br i1 %12, label %13, label %44
13: ; preds = %8
%14 = load i8*, i8** %3
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
store i8 %18, i8* %5
%19 = load i8, i8* %5
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 34
br i1 %21, label %26, label %22
22: ; preds = %13
%23 = load i8, i8* %5
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 92
br i1 %25, label %26, label %33
26: ; preds = %22, %13
%27 = load i8**, i8*** %4
%28 = load i8*, i8** %27
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
store i32 %30, i32* %6
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 92, i8* %32
br label %33
33: ; preds = %26, %22
%34 = load i8, i8* %5
%35 = load i8**, i8*** %4
%36 = load i8*, i8** %35
%37 = load i32, i32* %6
%38 = add nsw i32 %37, 1
store i32 %38, i32* %6
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 %34, i8* %40
br label %41
41: ; preds = %33
%42 = load i32, i32* %7
%43 = add nsw i32 %42, 1
store i32 %43, i32* %7
br label %8
44: ; preds = %8
ret void
}

LLVM IR (-O2):
define void @escape(i8* %0, i8** nocapture %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %6, label %5
5: ; preds = %16, %2
ret void
6: ; preds = %2, %16
%7 = phi i64 [ %22, %16 ], [ 0, %2 ]
%8 = phi i32 [ %19, %16 ], [ 0, %2 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %7
%10 = load i8, i8* %9
switch i8 %10, label %16 [
i8 92, label %11
i8 34, label %11
]
11: ; preds = %6, %6
%12 = load i8*, i8** %1
%13 = add nsw i32 %8, 1
%14 = sext i32 %8 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
store i8 92, i8* %15
br label %16
16: ; preds = %6, %11
%17 = phi i32 [ %13, %11 ], [ %8, %6 ]
%18 = load i8*, i8** %1
%19 = add nsw i32 %17, 1
%20 = sext i32 %17 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 %10, i8* %21
%22 = add nuw nsw i64 %7, 1
%23 = tail call i32 @strlen(i8* %0)
%24 = sext i32 %23 to i64
%25 = icmp slt i64 %22, %24
br i1 %25, label %6, label %5
}