#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 */
struct str {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ isprint(char const);
int str_append(struct str *, char *);
int str_appendf(struct str *, char *, char const);
int str_appendn(struct str *, char const *, int);
int str_append_escaped(struct str *str, const char *buf, size_t len) {
  int ret;
  size_t i;
  for (i = 0; i < len; i++) {
    switch (buf[i]) {
    case '\0':
      ret = str_append(str, "\\0");
      break;
    case '\a':
      ret = str_append(str, "\\a");
      break;
    case '\b':
      ret = str_append(str, "\\b");
      break;
    case '\t':
      ret = str_append(str, "\\t");
      break;
    case '\n':
      ret = str_append(str, "\\n");
      break;
    case '\v':
      ret = str_append(str, "\\v");
      break;
    case '\f':
      ret = str_append(str, "\\f");
      break;
    case '\r':
      ret = str_append(str, "\\r");
      break;
    case '\\':
      ret = str_append(str, "\\\\");
      break;
    default:
      if (isprint(buf[i]))
        ret = str_appendn(str, &buf[i], 1);
      else
        ret = str_appendf(str, "\\x%x", buf[i]);
      break;
    }
    if (ret)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<str_append_escaped>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    136e <str_append_escaped+0x22e>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x34(%rbp)
test   %eax,%eax
je     120e <str_append_escaped+0xce>
jmp    1186 <str_append_escaped+0x46>
mov    -0x34(%rbp),%eax
sub    $0x7,%eax
je     1226 <str_append_escaped+0xe6>
jmp    1197 <str_append_escaped+0x57>
mov    -0x34(%rbp),%eax
sub    $0x8,%eax
je     123e <str_append_escaped+0xfe>
jmp    11a8 <str_append_escaped+0x68>
mov    -0x34(%rbp),%eax
sub    $0x9,%eax
je     1256 <str_append_escaped+0x116>
jmp    11b9 <str_append_escaped+0x79>
mov    -0x34(%rbp),%eax
sub    $0xa,%eax
je     126e <str_append_escaped+0x12e>
jmp    11ca <str_append_escaped+0x8a>
mov    -0x34(%rbp),%eax
sub    $0xb,%eax
je     1286 <str_append_escaped+0x146>
jmp    11db <str_append_escaped+0x9b>
mov    -0x34(%rbp),%eax
sub    $0xc,%eax
je     129e <str_append_escaped+0x15e>
jmp    11ec <str_append_escaped+0xac>
mov    -0x34(%rbp),%eax
sub    $0xd,%eax
je     12b6 <str_append_escaped+0x176>
jmp    11fd <str_append_escaped+0xbd>
mov    -0x34(%rbp),%eax
sub    $0x5c,%eax
je     12ce <str_append_escaped+0x18e>
jmp    12e6 <str_append_escaped+0x1a6>
mov    -0x10(%rbp),%rdi
lea    0xde7(%rip),%rsi
call   1030 <str_append@plt>
mov    %eax,-0x24(%rbp)
jmp    1342 <str_append_escaped+0x202>
mov    -0x10(%rbp),%rdi
lea    0xdd2(%rip),%rsi
call   1030 <str_append@plt>
mov    %eax,-0x24(%rbp)
jmp    1342 <str_append_escaped+0x202>
mov    -0x10(%rbp),%rdi
lea    0xdbd(%rip),%rsi
call   1030 <str_append@plt>
mov    %eax,-0x24(%rbp)
jmp    1342 <str_append_escaped+0x202>
mov    -0x10(%rbp),%rdi
lea    0xda8(%rip),%rsi
call   1030 <str_append@plt>
mov    %eax,-0x24(%rbp)
jmp    1342 <str_append_escaped+0x202>
mov    -0x10(%rbp),%rdi
lea    0xd93(%rip),%rsi
call   1030 <str_append@plt>
mov    %eax,-0x24(%rbp)
jmp    1342 <str_append_escaped+0x202>
mov    -0x10(%rbp),%rdi
lea    0xd7e(%rip),%rsi
call   1030 <str_append@plt>
mov    %eax,-0x24(%rbp)
jmp    1342 <str_append_escaped+0x202>
mov    -0x10(%rbp),%rdi
lea    0xd69(%rip),%rsi
call   1030 <str_append@plt>
mov    %eax,-0x24(%rbp)
jmp    1342 <str_append_escaped+0x202>
mov    -0x10(%rbp),%rdi
lea    0xd54(%rip),%rsi
call   1030 <str_append@plt>
mov    %eax,-0x24(%rbp)
jmp    1342 <str_append_escaped+0x202>
mov    -0x10(%rbp),%rdi
lea    0xd3f(%rip),%rsi
call   1030 <str_append@plt>
mov    %eax,-0x24(%rbp)
jmp    1342 <str_append_escaped+0x202>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isprint@plt>
cmp    $0x0,%eax
je     131e <str_append_escaped+0x1de>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1050 <str_appendn@plt>
mov    %eax,-0x24(%rbp)
jmp    133d <str_append_escaped+0x1fd>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
lea    0xcea(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1060 <str_appendf@plt>
mov    %eax,-0x24(%rbp)
jmp    1342 <str_append_escaped+0x202>
cmpl   $0x0,-0x24(%rbp)
je     1358 <str_append_escaped+0x218>
movl   $0xffffffff,-0x4(%rbp)
jmp    1375 <str_append_escaped+0x235>
jmp    135d <str_append_escaped+0x21d>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    115c <str_append_escaped+0x1c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_append_escaped>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
je     1246 <str_append_escaped+0x106>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%r15
lea    0xe99(%rip),%r13
lea    0xedf(%rip),%r12
xchg   %ax,%ax
movsbl 0x0(%rbp),%edi
cmp    $0xd,%edi
ja     1192 <str_append_escaped+0x52>
movslq 0x0(%r13,%rdi,4),%rax
add    %r13,%rax
jmp    *%rax
mov    %r15,%rdi
lea    0xeab(%rip),%rsi
jmp    1210 <str_append_escaped+0xd0>
cmp    $0x5c,%edi
jne    11a3 <str_append_escaped+0x63>
mov    %r15,%rdi
lea    0xeaf(%rip),%rsi
jmp    1210 <str_append_escaped+0xd0>
call   1040 <isprint@plt>
test   %eax,%eax
je     1229 <str_append_escaped+0xe9>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <str_appendn@plt>
test   %eax,%eax
je     1219 <str_append_escaped+0xd9>
jmp    1240 <str_append_escaped+0x100>
mov    %r15,%rdi
lea    0xe6f(%rip),%rsi
jmp    1210 <str_append_escaped+0xd0>
mov    %r15,%rdi
lea    0xe66(%rip),%rsi
jmp    1210 <str_append_escaped+0xd0>
mov    %r15,%rdi
lea    0xe5d(%rip),%rsi
jmp    1210 <str_append_escaped+0xd0>
mov    %r15,%rdi
lea    0xe54(%rip),%rsi
jmp    1210 <str_append_escaped+0xd0>
mov    %r15,%rdi
lea    0xe4b(%rip),%rsi
jmp    1210 <str_append_escaped+0xd0>
mov    %r15,%rdi
lea    0xe42(%rip),%rsi
jmp    1210 <str_append_escaped+0xd0>
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <str_append@plt>
test   %eax,%eax
jne    1240 <str_append_escaped+0x100>
add    $0x1,%rbp
add    $0xffffffffffffffff,%rbx
jne    1170 <str_append_escaped+0x30>
jmp    1246 <str_append_escaped+0x106>
movsbl 0x0(%rbp),%edx
mov    %r15,%rdi
lea    0xe1c(%rip),%rsi
call   1060 <str_appendf@plt>
test   %eax,%eax
je     1219 <str_append_escaped+0xd9>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.str = type { i32 }
@.str = [3 x i8] c"\\0\00"
@.str.1 = [3 x i8] c"\\a\00"
@.str.2 = [3 x i8] c"\\b\00"
@.str.3 = [3 x i8] c"\\t\00"
@.str.4 = [3 x i8] c"\\n\00"
@.str.5 = [3 x i8] c"\\v\00"
@.str.6 = [3 x i8] c"\\f\00"
@.str.7 = [3 x i8] c"\\r\00"
@.str.8 = [3 x i8] c"\\\\\00"
@.str.9 = [5 x i8] c"\\x%x\00"
define i32 @str_append_escaped(%struct.str* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.str*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
store %struct.str* %0, %struct.str** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %9
br label %10
10: ; preds = %73, %3
%11 = load i64, i64* %9
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %76
14: ; preds = %10
%15 = load i8*, i8** %6
%16 = load i64, i64* %9
%17 = getelementptr inbounds i8, i8* %15, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
switch i32 %19, label %47 [
i32 0, label %20
i32 7, label %23
i32 8, label %26
i32 9, label %29
i32 10, label %32
i32 11, label %35
i32 12, label %38
i32 13, label %41
i32 92, label %44
]
20: ; preds = %14
%21 = load %struct.str*, %struct.str** %5
%22 = call i32 @str_append(%struct.str* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %22, i32* %8
br label %68
23: ; preds = %14
%24 = load %struct.str*, %struct.str** %5
%25 = call i32 @str_append(%struct.str* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 %25, i32* %8
br label %68
26: ; preds = %14
%27 = load %struct.str*, %struct.str** %5
%28 = call i32 @str_append(%struct.str* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32 %28, i32* %8
br label %68
29: ; preds = %14
%30 = load %struct.str*, %struct.str** %5
%31 = call i32 @str_append(%struct.str* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32 %31, i32* %8
br label %68
32: ; preds = %14
%33 = load %struct.str*, %struct.str** %5
%34 = call i32 @str_append(%struct.str* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32 %34, i32* %8
br label %68
35: ; preds = %14
%36 = load %struct.str*, %struct.str** %5
%37 = call i32 @str_append(%struct.str* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32 %37, i32* %8
br label %68
38: ; preds = %14
%39 = load %struct.str*, %struct.str** %5
%40 = call i32 @str_append(%struct.str* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32 %40, i32* %8
br label %68
41: ; preds = %14
%42 = load %struct.str*, %struct.str** %5
%43 = call i32 @str_append(%struct.str* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32 %43, i32* %8
br label %68
44: ; preds = %14
%45 = load %struct.str*, %struct.str** %5
%46 = call i32 @str_append(%struct.str* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
store i32 %46, i32* %8
br label %68
47: ; preds = %14
%48 = load i8*, i8** %6
%49 = load i64, i64* %9
%50 = getelementptr inbounds i8, i8* %48, i64 %49
%51 = load i8, i8* %50
%52 = call i32 @isprint(i8 signext %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %47
%55 = load %struct.str*, %struct.str** %5
%56 = load i8*, i8** %6
%57 = load i64, i64* %9
%58 = getelementptr inbounds i8, i8* %56, i64 %57
%59 = call i32 @str_appendn(%struct.str* %55, i8* %58, i32 1)
store i32 %59, i32* %8
br label %67
60: ; preds = %47
%61 = load %struct.str*, %struct.str** %5
%62 = load i8*, i8** %6
%63 = load i64, i64* %9
%64 = getelementptr inbounds i8, i8* %62, i64 %63
%65 = load i8, i8* %64
%66 = call i32 @str_appendf(%struct.str* %61, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i8 signext %65)
store i32 %66, i32* %8
br label %67
67: ; preds = %60, %54
br label %68
68: ; preds = %67, %44, %41, %38, %35, %32, %29, %26, %23, %20
%69 = load i32, i32* %8
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %68
store i32 -1, i32* %4
br label %77
72: ; preds = %68
br label %73
73: ; preds = %72
%74 = load i64, i64* %9
%75 = add i64 %74, 1
store i64 %75, i64* %9
br label %10
76: ; preds = %10
store i32 0, i32* %4
br label %77
77: ; preds = %76, %71
%78 = load i32, i32* %4
ret i32 %78
}

LLVM IR (-O2):
%struct.str = type { i32 }
@.str = [3 x i8] c"\\0\00"
@.str.1 = [3 x i8] c"\\a\00"
@.str.2 = [3 x i8] c"\\b\00"
@.str.3 = [3 x i8] c"\\t\00"
@.str.4 = [3 x i8] c"\\n\00"
@.str.5 = [3 x i8] c"\\v\00"
@.str.6 = [3 x i8] c"\\f\00"
@.str.7 = [3 x i8] c"\\r\00"
@.str.8 = [3 x i8] c"\\\\\00"
@.str.9 = [5 x i8] c"\\x%x\00"
define i32 @str_append_escaped(%struct.str* %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %42, label %7
5: ; preds = %38
%6 = icmp eq i64 %41, %2
br i1 %6, label %42, label %7
7: ; preds = %3, %5
%8 = phi i64 [ %41, %5 ], [ 0, %3 ]
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
switch i32 %11, label %30 [
i32 0, label %12
i32 7, label %14
i32 8, label %16
i32 9, label %18
i32 10, label %20
i32 11, label %22
i32 12, label %24
i32 13, label %26
i32 92, label %28
]
12: ; preds = %7
%13 = tail call i32 @str_append(%struct.str* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %38
14: ; preds = %7
%15 = tail call i32 @str_append(%struct.str* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %38
16: ; preds = %7
%17 = tail call i32 @str_append(%struct.str* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %38
18: ; preds = %7
%19 = tail call i32 @str_append(%struct.str* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %38
20: ; preds = %7
%21 = tail call i32 @str_append(%struct.str* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %38
22: ; preds = %7
%23 = tail call i32 @str_append(%struct.str* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %38
24: ; preds = %7
%25 = tail call i32 @str_append(%struct.str* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %38
26: ; preds = %7
%27 = tail call i32 @str_append(%struct.str* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
br label %38
28: ; preds = %7
%29 = tail call i32 @str_append(%struct.str* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
br label %38
30: ; preds = %7
%31 = tail call i32 @isprint(i8 signext %10)
%32 = icmp eq i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = tail call i32 @str_appendn(%struct.str* %0, i8* %9, i32 1)
br label %38
35: ; preds = %30
%36 = load i8, i8* %9
%37 = tail call i32 @str_appendf(%struct.str* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i8 signext %36)
br label %38
38: ; preds = %33, %35, %28, %26, %24, %22, %20, %18, %16, %14, %12
%39 = phi i32 [ %34, %33 ], [ %37, %35 ], [ %29, %28 ], [ %27, %26 ], [ %25, %24 ], [ %23, %22 ], [ %21, %20 ], [ %19, %18 ], [ %17, %16 ], [ %15, %14 ], [ %13, %12 ]
%40 = icmp eq i32 %39, 0
%41 = add nuw i64 %8, 1
br i1 %40, label %5, label %42
42: ; preds = %38, %5, %3
%43 = phi i32 [ 0, %3 ], [ 0, %5 ], [ -1, %38 ]
ret i32 %43
}