#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*/ free(char *);
char *safe_malloc(size_t);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
void escape_string(char *buffer, size_t buffsize) {
  char *tmp;
  char *ptr;
  char tmp_array[3];
  tmp = safe_malloc(buffsize * 2);
  tmp[0] = 0;
  tmp_array[2] = 0;
  for (ptr = buffer; *ptr != 0; ptr++) {
    tmp_array[0] = '\\';
    if (*ptr == '\r') {
      tmp_array[1] = 'r';
    } else if (*ptr == '\n') {
      tmp_array[1] = 'n';
    } else if (*ptr == '"') {
      tmp_array[0] = '"';
      tmp_array[1] = '"';
    } else {
      tmp_array[0] = *ptr;
      tmp_array[1] = 0;
    }
    strcat(tmp, tmp_array);
  }
  strncpy(buffer, tmp, buffsize);
  free(tmp);
}


x86 assembly (clang, -O0):
<escape_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x1,%rdi
call   1050 <safe_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movb   $0x0,-0x21(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     120c <escape_string+0xcc>
movb   $0x5c,-0x23(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
jne    11a1 <escape_string+0x61>
movb   $0x72,-0x22(%rbp)
jmp    11ee <escape_string+0xae>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    11ba <escape_string+0x7a>
movb   $0x6e,-0x22(%rbp)
jmp    11e9 <escape_string+0xa9>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
jne    11d7 <escape_string+0x97>
movb   $0x22,-0x23(%rbp)
movb   $0x22,-0x22(%rbp)
jmp    11e4 <escape_string+0xa4>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x23(%rbp)
movb   $0x0,-0x22(%rbp)
jmp    11e9 <escape_string+0xa9>
jmp    11ee <escape_string+0xae>
mov    -0x18(%rbp),%rdi
lea    -0x23(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1174 <escape_string+0x34>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_string>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    (%rsi,%rsi,1),%rdi
call   1050 <safe_malloc@plt>
mov    %rax,%r13
movb   $0x0,(%rax)
movb   $0x0,0xf(%rsp)
mov    (%r15),%al
test   %al,%al
je     11d8 <escape_string+0x98>
lea    0x1(%r15),%rbx
lea    0xd(%rsp),%r12
jmp    119c <escape_string+0x5c>
nopl   0x0(%rax)
mov    $0x72,%al
mov    %al,0xe(%rsp)
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <strcat@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11d8 <escape_string+0x98>
movb   $0x5c,0xd(%rsp)
cmp    $0x22,%al
je     11c0 <escape_string+0x80>
cmp    $0xd,%al
je     1180 <escape_string+0x40>
cmp    $0xa,%al
jne    11d0 <escape_string+0x90>
mov    $0x6e,%al
jmp    1182 <escape_string+0x42>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movb   $0x22,0xd(%rsp)
mov    $0x22,%al
jmp    1182 <escape_string+0x42>
nopl   0x0(%rax)
mov    %al,0xd(%rsp)
xor    %eax,%eax
jmp    1182 <escape_string+0x42>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   1040 <strncpy@plt>
mov    %r13,%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @escape_string(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [3 x i8]
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = load i64, i64* %4
%9 = mul i64 %8, 2
%10 = call i8* @safe_malloc(i64 %9)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = getelementptr inbounds i8, i8* %11, i64 0
store i8 0, i8* %12
%13 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 2
store i8 0, i8* %13
%14 = load i8*, i8** %3
store i8* %14, i8** %6
br label %15
15: ; preds = %54, %2
%16 = load i8*, i8** %6
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %57
20: ; preds = %15
%21 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
store i8 92, i8* %21
%22 = load i8*, i8** %6
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 13
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 1
store i8 114, i8* %27
br label %50
28: ; preds = %20
%29 = load i8*, i8** %6
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 10
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 1
store i8 110, i8* %34
br label %49
35: ; preds = %28
%36 = load i8*, i8** %6
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 34
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
store i8 34, i8* %41
%42 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 1
store i8 34, i8* %42
br label %48
43: ; preds = %35
%44 = load i8*, i8** %6
%45 = load i8, i8* %44
%46 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
store i8 %45, i8* %46
%47 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 1
store i8 0, i8* %47
br label %48
48: ; preds = %43, %40
br label %49
49: ; preds = %48, %33
br label %50
50: ; preds = %49, %26
%51 = load i8*, i8** %5
%52 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%53 = call i32 @strcat(i8* %51, i8* %52)
br label %54
54: ; preds = %50
%55 = load i8*, i8** %6
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %6
br label %15
57: ; preds = %15
%58 = load i8*, i8** %3
%59 = load i8*, i8** %5
%60 = load i64, i64* %4
%61 = call i32 @strncpy(i8* %58, i8* %59, i64 %60)
%62 = load i8*, i8** %5
%63 = call i32 @free(i8* %62)
ret void
}

LLVM IR (-O2):
define void @escape_string(i8* %0, i64 %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%5 = shl i64 %1, 1
%6 = tail call i8* @safe_malloc(i64 %5)
store i8 0, i8* %6
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 2
store i8 0, i8* %7
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %2
%11 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
br label %12
12: ; preds = %10, %18
%13 = phi i8 [ %8, %10 ], [ %22, %18 ]
%14 = phi i8* [ %0, %10 ], [ %21, %18 ]
store i8 92, i8* %4
switch i8 %13, label %17 [
i8 13, label %18
i8 10, label %15
i8 34, label %16
]
15: ; preds = %12
br label %18
16: ; preds = %12
store i8 34, i8* %4
br label %18
17: ; preds = %12
store i8 %13, i8* %4
br label %18
18: ; preds = %12, %15, %17, %16
%19 = phi i8 [ 110, %15 ], [ 0, %17 ], [ 34, %16 ], [ 114, %12 ]
store i8 %19, i8* %11
%20 = call i32 @strcat(i8* %6, i8* %4)
%21 = getelementptr inbounds i8, i8* %14, i64 1
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %12
24: ; preds = %18, %2
%25 = call i32 @strncpy(i8* %0, i8* %6, i64 %1)
%26 = call i32 @free(i8* %6)
ret void
}