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


x86 assembly (clang, -O0):
<escape_message>:
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    117e <escape_message+0x3e>
lea    0xe92(%rip),%rdi
call   1050 <toc_debug_printf@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     1233 <escape_message+0xf3>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x20(%rbp)
add    $0xffffffdd,%eax
sub    $0x2,%eax
jb     11f1 <escape_message+0xb1>
jmp    11bb <escape_message+0x7b>
mov    -0x20(%rbp),%eax
add    $0xffffffd8,%eax
sub    $0x2,%eax
jb     11f1 <escape_message+0xb1>
jmp    11cf <escape_message+0x8f>
mov    -0x20(%rbp),%eax
sub    $0x5b,%eax
je     11f1 <escape_message+0xb1>
jmp    11e0 <escape_message+0xa0>
mov    -0x20(%rbp),%eax
sub    $0x5d,%eax
jne    1207 <escape_message+0xc7>
jmp    11f1 <escape_message+0xb1>
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    1193 <escape_message+0x53>
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_message>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
cmp    $0x801,%eax
jl     1166 <escape_message+0x26>
lea    0xea6(%rip),%rdi
call   1050 <toc_debug_printf@plt>
movb   $0x0,0x7ff(%rbx)
mov    %rbx,%rdi
call   1060 <strdup@plt>
mov    (%rax),%dl
test   %dl,%dl
je     11c1 <escape_message+0x81>
mov    %rax,%rsi
add    $0x1,%rsi
xor    %ecx,%ecx
movabs $0x500000000000063,%r8
jmp    11a4 <escape_message+0x64>
nopl   0x0(%rax)
movslq %ecx,%rdi
add    $0x1,%ecx
mov    %dl,(%rbx,%rdi,1)
movzbl (%rsi),%edx
add    $0x1,%rsi
test   %dl,%dl
je     11c3 <escape_message+0x83>
movsbl %dl,%edi
add    $0xffffffdd,%edi
cmp    $0x3a,%edi
ja     1190 <escape_message+0x50>
bt     %rdi,%r8
jae    1190 <escape_message+0x50>
movslq %ecx,%rdi
add    $0x1,%ecx
movb   $0x5c,(%rbx,%rdi,1)
jmp    1190 <escape_message+0x50>
xor    %ecx,%ecx
movslq %ecx,%r14
movb   $0x0,(%rbx,%r14,1)
mov    %rax,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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