#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 *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 = malloc(buffsize * 2);
  tmp[0] = 0;
  tmp_array[2] = 0;
  for (ptr = buffer; *ptr != 0; ptr++) {
    tmp_array[0] = '\\';
    if (*ptr == '\t') {
      tmp_array[1] = 't';
    } else if (*ptr == '\r') {
      tmp_array[1] = 'r';
    } else if (*ptr == '\n') {
      tmp_array[1] = 'n';
    } else if (*ptr == '"') {
      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 <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     1226 <escape_string+0xe6>
movb   $0x5c,-0x23(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x9,%eax
jne    11a1 <escape_string+0x61>
movb   $0x74,-0x22(%rbp)
jmp    1208 <escape_string+0xc8>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
jne    11ba <escape_string+0x7a>
movb   $0x72,-0x22(%rbp)
jmp    1203 <escape_string+0xc3>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    11d3 <escape_string+0x93>
movb   $0x6e,-0x22(%rbp)
jmp    11fe <escape_string+0xbe>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
jne    11ec <escape_string+0xac>
movb   $0x22,-0x22(%rbp)
jmp    11f9 <escape_string+0xb9>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x23(%rbp)
movb   $0x0,-0x22(%rbp)
jmp    11fe <escape_string+0xbe>
jmp    1203 <escape_string+0xc3>
jmp    1208 <escape_string+0xc8>
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   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    (%rsi,%rsi,1),%rdi
call   1050 <malloc@plt>
mov    %rax,%r13
movb   $0x0,(%rax)
movb   $0x0,0x7(%rsp)
mov    (%r15),%al
test   %al,%al
je     11e4 <escape_string+0xa4>
lea    0x1(%r15),%rbx
lea    0x5(%rsp),%r12
lea    0xe84(%rip),%rbp
jmp    11aa <escape_string+0x6a>
xchg   %ax,%ax
mov    %al,0x5(%rsp)
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
mov    %cl,0x6(%rsp)
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <strcat@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11e4 <escape_string+0xa4>
movb   $0x5c,0x5(%rsp)
lea    -0x9(%rax),%edx
cmp    $0x19,%dl
ja     1180 <escape_string+0x40>
mov    $0x74,%cl
movzbl %dl,%edx
movslq 0x0(%rbp,%rdx,4),%rdx
add    %rbp,%rdx
jmp    *%rdx
mov    $0x6e,%cl
jmp    1190 <escape_string+0x50>
nopw   0x0(%rax,%rax,1)
mov    $0x72,%cl
jmp    1190 <escape_string+0x50>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x22,%cl
jmp    1190 <escape_string+0x50>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   1040 <strncpy@plt>
mov    %r13,%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
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* @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 = %61, %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 %64
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, 9
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 116, i8* %27
br label %57
28: ; preds = %20
%29 = load i8*, i8** %6
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 13
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 114, i8* %34
br label %56
35: ; preds = %28
%36 = load i8*, i8** %6
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 10
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 1
store i8 110, i8* %41
br label %55
42: ; preds = %35
%43 = load i8*, i8** %6
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 34
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 1
store i8 34, i8* %48
br label %54
49: ; preds = %42
%50 = load i8*, i8** %6
%51 = load i8, i8* %50
%52 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
store i8 %51, i8* %52
%53 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 1
store i8 0, i8* %53
br label %54
54: ; preds = %49, %47
br label %55
55: ; preds = %54, %40
br label %56
56: ; preds = %55, %33
br label %57
57: ; preds = %56, %26
%58 = load i8*, i8** %5
%59 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%60 = call i32 @strcat(i8* %58, i8* %59)
br label %61
61: ; preds = %57
%62 = load i8*, i8** %6
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %6
br label %15
64: ; preds = %15
%65 = load i8*, i8** %3
%66 = load i8*, i8** %5
%67 = load i64, i64* %4
%68 = call i32 @strncpy(i8* %65, i8* %66, i64 %67)
%69 = load i8*, i8** %5
%70 = call i32 @free(i8* %69)
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* @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 %25, 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, %19
%13 = phi i8 [ %8, %10 ], [ %23, %19 ]
%14 = phi i8* [ %0, %10 ], [ %22, %19 ]
store i8 92, i8* %4
switch i8 %13, label %18 [
i8 9, label %19
i8 13, label %15
i8 10, label %16
i8 34, label %17
]
15: ; preds = %12
br label %19
16: ; preds = %12
br label %19
17: ; preds = %12
br label %19
18: ; preds = %12
store i8 %13, i8* %4
br label %19
19: ; preds = %12, %15, %17, %18, %16
%20 = phi i8 [ 114, %15 ], [ 34, %17 ], [ 0, %18 ], [ 110, %16 ], [ 116, %12 ]
store i8 %20, i8* %11
%21 = call i32 @strcat(i8* %6, i8* %4)
%22 = getelementptr inbounds i8, i8* %14, i64 1
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %12
25: ; preds = %19, %2
%26 = call i32 @strncpy(i8* %0, i8* %6, i64 %1)
%27 = call i32 @free(i8* %6)
ret void
}