#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*/ debug_out(char *, ...);
int /*<<< orphan*/ memmove(char *, char *, int);
int strlen(char *);
int escape_string(char *text) {
  int found_error = 0;
  if (!text || text[0] == 0) {
    return 0;
  }
  for (int i = 0; i < strlen(text); ++i) {
    if (text[i] == '\\') {
      int escape_char = text[i + 1];
      switch (escape_char) {
      case 0:
        debug_out("escape_string: incomplete escape at end of string\n");
        found_error = 1;
        break;
      case 'n':
        memmove(&text[i], &text[i + 1], strlen(text) - i);
        text[i] = '\n';
        break;
      default:
        memmove(&text[i], &text[i + 1], strlen(text) - i);
        debug_out("escape_string: unrecognized escape \\%c\n", escape_char);
        found_error = 1;
      }
    }
  }
  return found_error;
}


x86 assembly (clang, -O0):
<escape_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     115e <escape_string+0x2e>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    116a <escape_string+0x3a>
movl   $0x0,-0x4(%rbp)
jmp    12b3 <escape_string+0x183>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jge    12ad <escape_string+0x17d>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5c,%eax
jne    129a <escape_string+0x16a>
mov    -0x10(%rbp),%rcx
movslq -0x18(%rbp),%rax
movsbl 0x1(%rax,%rcx,1),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
test   %eax,%eax
je     11d6 <escape_string+0xa6>
jmp    11c5 <escape_string+0x95>
mov    -0x24(%rbp),%eax
sub    $0x6e,%eax
je     11f0 <escape_string+0xc0>
jmp    123f <escape_string+0x10f>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <debug_out@plt>
movl   $0x1,-0x14(%rbp)
jmp    1295 <escape_string+0x165>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
sub    -0x18(%rbp),%edx
call   1050 <memmove@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0xa,(%rax,%rcx,1)
jmp    1295 <escape_string+0x165>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
sub    -0x18(%rbp),%edx
call   1050 <memmove@plt>
mov    -0x1c(%rbp),%esi
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1030 <debug_out@plt>
movl   $0x1,-0x14(%rbp)
jmp    129a <escape_string+0x16a>
jmp    129f <escape_string+0x16f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1171 <escape_string+0x41>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     1226 <escape_string+0xf6>
mov    %rdi,%rbx
movl   $0x0,0xc(%rsp)
cmpb   $0x0,(%rdi)
je     122e <escape_string+0xfe>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    122e <escape_string+0xfe>
lea    0x1(%rbx),%rax
mov    %rax,0x10(%rsp)
xor    %r12d,%r12d
xor    %r13d,%r13d
movl   $0x0,0xc(%rsp)
jmp    11ca <escape_string+0x9a>
mov    %rbx,%rdi
mov    %rcx,%r15
call   1040 <strlen@plt>
lea    (%rax,%r12,1),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1050 <memmove@plt>
lea    0xe8e(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1030 <debug_out@plt>
movl   $0x1,0xc(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
add    $0xffffffff,%r12d
cmp    %rax,%r13
jge    122e <escape_string+0xfe>
mov    %r13,%r15
add    $0x1,%r13
cmpb   $0x5c,(%rbx,%r15,1)
jne    11b7 <escape_string+0x87>
lea    (%rbx,%r15,1),%rbp
mov    0x10(%rsp),%rax
lea    (%rax,%r15,1),%rcx
movsbl (%rax,%r15,1),%r14d
cmp    $0x6e,%r14d
je     1205 <escape_string+0xd5>
test   %r14d,%r14d
jne    1184 <escape_string+0x54>
lea    0xe04(%rip),%rdi
xor    %eax,%eax
call   1030 <debug_out@plt>
jmp    11af <escape_string+0x7f>
mov    %rbx,%rdi
mov    %rcx,%r14
call   1040 <strlen@plt>
lea    (%rax,%r12,1),%edx
mov    %rbp,%rdi
mov    %r14,%rsi
call   1050 <memmove@plt>
movb   $0xa,(%rbx,%r15,1)
jmp    11b7 <escape_string+0x87>
movl   $0x0,0xc(%rsp)
mov    0xc(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [51 x i8] c"escape_string: incomplete escape at end of string\0A\00"
@.str.1 = [40 x i8] c"escape_string: unrecognized escape \\%c\0A\00"
define i32 @escape_string(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %9, %1
store i32 0, i32* %2
br label %84
16: ; preds = %9
store i32 0, i32* %5
br label %17
17: ; preds = %79, %16
%18 = load i32, i32* %5
%19 = load i8*, i8** %3
%20 = call i32 @strlen(i8* %19)
%21 = icmp slt i32 %18, %20
br i1 %21, label %22, label %82
22: ; preds = %17
%23 = load i8*, i8** %3
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 92
br i1 %29, label %30, label %78
30: ; preds = %22
%31 = load i8*, i8** %3
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
store i32 %37, i32* %6
%38 = load i32, i32* %6
switch i32 %38, label %60 [
i32 0, label %39
i32 110, label %41
]
39: ; preds = %30
%40 = call i32 (i8*, ...) @debug_out(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %77
41: ; preds = %30
%42 = load i8*, i8** %3
%43 = load i32, i32* %5
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8*, i8** %3
%47 = load i32, i32* %5
%48 = add nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
%51 = load i8*, i8** %3
%52 = call i32 @strlen(i8* %51)
%53 = load i32, i32* %5
%54 = sub nsw i32 %52, %53
%55 = call i32 @memmove(i8* %45, i8* %50, i32 %54)
%56 = load i8*, i8** %3
%57 = load i32, i32* %5
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 10, i8* %59
br label %77
60: ; preds = %30
%61 = load i8*, i8** %3
%62 = load i32, i32* %5
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = load i8*, i8** %3
%66 = load i32, i32* %5
%67 = add nsw i32 %66, 1
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %65, i64 %68
%70 = load i8*, i8** %3
%71 = call i32 @strlen(i8* %70)
%72 = load i32, i32* %5
%73 = sub nsw i32 %71, %72
%74 = call i32 @memmove(i8* %64, i8* %69, i32 %73)
%75 = load i32, i32* %6
%76 = call i32 (i8*, ...) @debug_out(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %75)
store i32 1, i32* %4
br label %77
77: ; preds = %60, %41, %39
br label %78
78: ; preds = %77, %22
br label %79
79: ; preds = %78
%80 = load i32, i32* %5
%81 = add nsw i32 %80, 1
store i32 %81, i32* %5
br label %17
82: ; preds = %17
%83 = load i32, i32* %4
store i32 %83, i32* %2
br label %84
84: ; preds = %82, %15
%85 = load i32, i32* %2
ret i32 %85
}

LLVM IR (-O2):
@.str = [51 x i8] c"escape_string: incomplete escape at end of string\0A\00"
@.str.1 = [40 x i8] c"escape_string: unrecognized escape \\%c\0A\00"
define i32 @escape_string(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %38, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %38, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %38
9: ; preds = %6, %33
%10 = phi i64 [ %15, %33 ], [ 0, %6 ]
%11 = phi i32 [ %34, %33 ], [ 0, %6 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %10
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 92
%15 = add nuw nsw i64 %10, 1
br i1 %14, label %16, label %33
16: ; preds = %9
%17 = getelementptr inbounds i8, i8* %0, i64 %15
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
switch i32 %19, label %27 [
i32 0, label %20
i32 110, label %22
]
20: ; preds = %16
%21 = tail call i32 (i8*, ...) @debug_out(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %33
22: ; preds = %16
%23 = tail call i32 @strlen(i8* %0)
%24 = trunc i64 %10 to i32
%25 = sub nsw i32 %23, %24
%26 = tail call i32 @memmove(i8* %12, i8* %17, i32 %25)
store i8 10, i8* %12
br label %33
27: ; preds = %16
%28 = tail call i32 @strlen(i8* %0)
%29 = trunc i64 %10 to i32
%30 = sub nsw i32 %28, %29
%31 = tail call i32 @memmove(i8* %12, i8* %17, i32 %30)
%32 = tail call i32 (i8*, ...) @debug_out(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %33
33: ; preds = %9, %20, %22, %27
%34 = phi i32 [ 1, %20 ], [ %11, %22 ], [ 1, %27 ], [ %11, %9 ]
%35 = tail call i32 @strlen(i8* %0)
%36 = sext i32 %35 to i64
%37 = icmp slt i64 %15, %36
br i1 %37, label %9, label %38
38: ; preds = %33, %6, %1, %3
%39 = phi i32 [ 0, %3 ], [ 0, %1 ], [ 0, %6 ], [ %34, %33 ]
ret i32 %39
}