#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 estring {
  int dummy;
};

/* Variables and functions */
scalar_t__ estring_append(struct estring *, char const *, int);
char *memchr(char const *, char, int);
int estring_append_escape(struct estring *e, const char *src, int len,
                          char escape) {
  const char *p = src;
  const char *endp = src + len;
  while (p < endp) {
    const char *next;
    next = memchr(p, escape, endp - p);
    if (!next) {
      return estring_append(e, p, endp - p);
    }
    if (estring_append(e, p, next - p) < 0)
      return -1;
    if (escape == '\'') {
      if (estring_append(e, "'\"'\"'", 5) < 0)
        return -1;
    } else {
      if (estring_append(e, "\\\"", 2) < 0)
        return -1;
    }
    p = next + 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<estring_append_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %cl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1258 <estring_append_escape+0x138>
mov    -0x28(%rbp),%rdi
mov    -0x1d(%rbp),%al
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rdx
sub    %rdx,%rcx
mov    %ecx,%edx
movsbl %al,%esi
call   1030 <memchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11aa <estring_append_escape+0x8a>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <estring_append@plt>
mov    %eax,-0x4(%rbp)
jmp    125f <estring_append_escape+0x13f>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <estring_append@plt>
cmp    $0x0,%rax
jge    11da <estring_append_escape+0xba>
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <estring_append_escape+0x13f>
movsbl -0x1d(%rbp),%eax
cmp    $0x27,%eax
jne    1217 <estring_append_escape+0xf7>
mov    -0x10(%rbp),%rdi
lea    0xe0e(%rip),%rsi
mov    $0x5,%edx
call   1040 <estring_append@plt>
cmp    $0x0,%rax
jge    1212 <estring_append_escape+0xf2>
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <estring_append_escape+0x13f>
jmp    1247 <estring_append_escape+0x127>
mov    -0x10(%rbp),%rdi
lea    0xde4(%rip),%rsi
mov    $0x2,%edx
call   1040 <estring_append@plt>
cmp    $0x0,%rax
jge    1242 <estring_append_escape+0x122>
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <estring_append_escape+0x13f>
jmp    1247 <estring_append_escape+0x127>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    114f <estring_append_escape+0x2f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<estring_append_escape>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    11c5 <estring_append_escape+0xa5>
mov    %rsi,%r15
mov    %rdi,%r12
movslq %edx,%r14
add    %rsi,%r14
mov    %ecx,0x4(%rsp)
movsbl %cl,%r13d
nopw   0x0(%rax,%rax,1)
mov    %r14,%rbx
sub    %r15,%rbx
mov    %r15,%rdi
mov    %r13d,%esi
mov    %ebx,%edx
call   1030 <memchr@plt>
test   %rax,%rax
je     11c9 <estring_append_escape+0xa9>
mov    %rax,%rbp
mov    %ebp,%edx
sub    %r15d,%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <estring_append@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     11d9 <estring_append_escape+0xb9>
cmpb   $0x27,0x4(%rsp)
jne    11a0 <estring_append_escape+0x80>
mov    %r12,%rdi
lea    0xe6a(%rip),%rsi
mov    $0x5,%edx
jmp    11af <estring_append_escape+0x8f>
nopl   (%rax)
mov    %r12,%rdi
lea    0xe5c(%rip),%rsi
mov    $0x2,%edx
call   1040 <estring_append@plt>
test   %rax,%rax
js     11d9 <estring_append_escape+0xb9>
add    $0x1,%rbp
mov    %rbp,%r15
cmp    %r14,%rbp
jb     1150 <estring_append_escape+0x30>
xor    %ebx,%ebx
jmp    11d9 <estring_append_escape+0xb9>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <estring_append@plt>
mov    %rax,%rbx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.estring = type { i32 }
@.str = [6 x i8] c"'\22'\22'\00"
@.str.1 = [3 x i8] c"\\\22\00"
define i32 @estring_append_escape(%struct.estring* %0, i8* %1, i32 %2, i8 signext %3) {
%5 = sext i32 %2 to i64
%6 = getelementptr inbounds i8, i8* %1, i64 %5
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %38
8: ; preds = %4
%9 = ptrtoint i8* %6 to i64
%10 = icmp eq i8 %3, 39
br label %11
11: ; preds = %8, %35
%12 = phi i8* [ %1, %8 ], [ %36, %35 ]
%13 = ptrtoint i8* %12 to i64
%14 = sub i64 %9, %13
%15 = trunc i64 %14 to i32
%16 = tail call i8* @memchr(i8* %12, i8 signext %3, i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %11
%19 = trunc i64 %14 to i32
%20 = tail call i64 @estring_append(%struct.estring* %0, i8* %12, i32 %19)
%21 = trunc i64 %20 to i32
br label %38
22: ; preds = %11
%23 = ptrtoint i8* %16 to i64
%24 = sub i64 %23, %13
%25 = trunc i64 %24 to i32
%26 = tail call i64 @estring_append(%struct.estring* %0, i8* %12, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %38, label %28
28: ; preds = %22
br i1 %10, label %29, label %32
29: ; preds = %28
%30 = tail call i64 @estring_append(%struct.estring* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%31 = icmp slt i64 %30, 0
br i1 %31, label %38, label %35
32: ; preds = %28
%33 = tail call i64 @estring_append(%struct.estring* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%34 = icmp slt i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %29, %32
%36 = getelementptr inbounds i8, i8* %16, i64 1
%37 = icmp ult i8* %36, %6
br i1 %37, label %11, label %38
38: ; preds = %35, %22, %29, %32, %4, %18
%39 = phi i32 [ %21, %18 ], [ 0, %4 ], [ 0, %35 ], [ -1, %22 ], [ -1, %29 ], [ -1, %32 ]
ret i32 %39
}