#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ stderr;
char *strdup(char *);
int strlen(char *);
int escape_text(char *msg) {
  char *c, *cpy;
  int cnt = 0;
  if (strlen(msg) > 2048) {
    fprintf(stderr, "Warning:  truncating message to 2048 bytes\n");
    msg[2047] = '\0';
  }
  cpy = strdup(msg);
  c = cpy;
  while (*c) {
    switch (*c) {
    case '{':
    case '}':
    case '\\':
    case '"':
      msg[cnt++] = '\\';
    default:
      msg[cnt++] = *c;
    }
    c++;
  }
  msg[cnt] = '\0';
  free(cpy);
  return cnt;
}


x86 assembly (clang, -O0):
<escape_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x800,%eax
jle    1187 <escape_text+0x47>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
lea    0xe89(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%rax
movb   $0x0,0x7ff(%rax)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1236 <escape_text+0xf6>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x20(%rbp)
sub    $0x22,%eax
je     11f4 <escape_text+0xb4>
jmp    11c1 <escape_text+0x81>
mov    -0x20(%rbp),%eax
sub    $0x5c,%eax
je     11f4 <escape_text+0xb4>
jmp    11d2 <escape_text+0x92>
mov    -0x20(%rbp),%eax
sub    $0x7b,%eax
je     11f4 <escape_text+0xb4>
jmp    11e3 <escape_text+0xa3>
mov    -0x20(%rbp),%eax
sub    $0x7d,%eax
jne    120a <escape_text+0xca>
jmp    11f4 <escape_text+0xb4>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
movb   $0x5c,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    (%rax),%dl
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    119c <escape_text+0x5c>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_text>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1040 <strlen@plt>
cmp    $0x801,%eax
jl     1171 <escape_text+0x31>
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
lea    0xe9c(%rip),%rsi
call   1050 <fprintf@plt>
movb   $0x0,0x7ff(%r15)
mov    %r15,%rdi
call   1060 <strdup@plt>
mov    (%rax),%cl
test   %cl,%cl
je     11d8 <escape_text+0x98>
mov    %rax,%rsi
add    $0x1,%rsi
xor    %edx,%edx
movabs $0x280000001,%r8
jmp    11c6 <escape_text+0x86>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
bt     %rbx,%r8
jae    11d1 <escape_text+0x91>
movslq %edx,%rdi
add    $0x1,%edx
movb   $0x5c,(%r15,%rdi,1)
movslq %edx,%rdi
add    $0x1,%edx
mov    %cl,(%r15,%rdi,1)
movzbl (%rsi),%ecx
add    $0x1,%rsi
test   %cl,%cl
je     11da <escape_text+0x9a>
movsbl %cl,%edi
lea    -0x5c(%rdi),%ebx
cmp    $0x21,%ebx
jbe    11a0 <escape_text+0x60>
cmp    $0x22,%edi
je     11a6 <escape_text+0x66>
jmp    11b1 <escape_text+0x71>
xor    %edx,%edx
movslq %edx,%r14
movb   $0x0,(%r15,%r14,1)
mov    %rax,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [44 x i8] c"Warning: truncating message to 2048 bytes\0A\00"
define i32 @escape_text(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
%8 = icmp sgt i32 %7, 2048
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%12 = load i8*, i8** %2
%13 = getelementptr inbounds i8, i8* %12, i64 2047
store i8 0, i8* %13
br label %14
14: ; preds = %9, %1
%15 = load i8*, i8** %2
%16 = call i8* @strdup(i8* %15)
store i8* %16, i8** %4
%17 = load i8*, i8** %4
store i8* %17, i8** %3
br label %18
18: ; preds = %40, %14
%19 = load i8*, i8** %3
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %43
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
switch i32 %25, label %32 [
i32 123, label %26
i32 125, label %26
i32 92, label %26
i32 34, label %26
]
26: ; preds = %22, %22, %22, %22
%27 = load i8*, i8** %2
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
store i8 92, i8* %31
br label %32
32: ; preds = %22, %26
%33 = load i8*, i8** %3
%34 = load i8, i8* %33
%35 = load i8*, i8** %2
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8 %34, i8* %39
br label %40
40: ; preds = %32
%41 = load i8*, i8** %3
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %3
br label %18
43: ; preds = %18
%44 = load i8*, i8** %2
%45 = load i32, i32* %5
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %4
%49 = call i32 @free(i8* %48)
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [44 x i8] c"Warning: truncating message to 2048 bytes\0A\00"
define i32 @escape_text(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 2048
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds i8, i8* %0, i64 2047
store i8 0, i8* %7
br label %8
8: ; preds = %4, %1
%9 = tail call i8* @strdup(i8* %0)
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %29, label %12
12: ; preds = %8, %21
%13 = phi i8 [ %27, %21 ], [ %10, %8 ]
%14 = phi i32 [ %23, %21 ], [ 0, %8 ]
%15 = phi i8* [ %26, %21 ], [ %9, %8 ]
%16 = sext i8 %13 to i32
switch i32 %16, label %21 [
i32 123, label %17
i32 125, label %17
i32 92, label %17
i32 34, label %17
]
17: ; preds = %12, %12, %12, %12
%18 = add nsw i32 %14, 1
%19 = sext i32 %14 to i64
%20 = getelementptr inbounds i8, i8* %0, i64 %19
store i8 92, i8* %20
br label %21
21: ; preds = %12, %17
%22 = phi i32 [ %14, %12 ], [ %18, %17 ]
%23 = add nsw i32 %22, 1
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 %13, i8* %25
%26 = getelementptr inbounds i8, i8* %15, i64 1
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 0
br i1 %28, label %29, label %12
29: ; preds = %21, %8
%30 = phi i32 [ 0, %8 ], [ %23, %21 ]
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %0, i64 %31
store i8 0, i8* %32
%33 = tail call i32 @free(i8* %9)
ret i32 %30
}