#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int snprintf(char *, size_t, char *, char *);
int /*<<< orphan*/ stderr;
size_t strlen(char *);
void xml_quote(char const *str, char *buffer, size_t max) {
  char *buf;
  char *quote;
  char *pos = (char *)str;
  size_t size;
  int ret;
  buffer[max - 1] = '\0';
  size = strlen(buffer);
  buf = &buffer[size];
  size = max - 1 - size;
  if (size < strlen(pos) + 2)
    goto Error;
  *buf = '"';
  buf++;
  size--;
  while (*pos) {
    switch (*pos) {
    case '&':
      quote = "&amp;";
      break;
    case '<':
      quote = "&lt;";
      break;
    case '>':
      quote = "&gt;";
      break;
    case '"':
      quote = "&quot;";
      break;
    default:
      *buf = *pos;
      pos++;
      buf++;
      size--;
      continue;
    }
    pos++;
    ret = snprintf(buf, size, "%s", quote);
    if (ret < 0 || ret >= (int)size)
      goto Error;
    buf += ret;
    size -= ret;
    if (size < strlen(pos))
      goto Error;
  }
  if (!size)
    goto Error;
  *buf = '"';
  buf++;
  *buf = '\0';
  return;
Error:
  fprintf(stderr, "quote_str: buffer overflow\n");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<xml_quote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    $0x2,%rcx
cmp    %rcx,%rax
jae    11bf <xml_quote+0x7f>
jmp    134d <xml_quote+0x20d>
mov    -0x20(%rbp),%rax
movb   $0x22,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     131d <xml_quote+0x1dd>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
add    $0xffffffde,%eax
mov    %eax,%ecx
mov    %rcx,-0x50(%rbp)
sub    $0x1c,%eax
ja     1258 <xml_quote+0x118>
mov    -0x50(%rbp),%rax
lea    0xdf1(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe55(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    128d <xml_quote+0x14d>
lea    0xe4b(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    128d <xml_quote+0x14d>
lea    0xe40(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    128d <xml_quote+0x14d>
lea    0xe35(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    128d <xml_quote+0x14d>
mov    -0x30(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
jmp    11de <xml_quote+0x9e>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rcx
lea    0xddf(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jl     12cd <xml_quote+0x18d>
mov    -0x3c(%rbp),%eax
mov    -0x38(%rbp),%rcx
cmp    %ecx,%eax
jl     12d2 <xml_quote+0x192>
jmp    134d <xml_quote+0x20d>
mov    -0x3c(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
movslq -0x3c(%rbp),%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
cmp    %rcx,%rax
jae    1318 <xml_quote+0x1d8>
jmp    134d <xml_quote+0x20d>
jmp    11de <xml_quote+0x9e>
cmpq   $0x0,-0x38(%rbp)
jne    132d <xml_quote+0x1ed>
jmp    134d <xml_quote+0x20d>
mov    -0x20(%rbp),%rax
movb   $0x22,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
add    $0x60,%rsp
pop    %rbp
ret
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
lea    0xd31(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<xml_quote>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
movb   $0x0,-0x1(%rsi,%rdx,1)
add    $0xffffffffffffffff,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
sub    %rax,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
add    $0x2,%rax
cmp    %rax,%rbx
jb     1257 <xml_quote+0x117>
add    %r12,%r14
movb   $0x22,(%r14)
add    $0x1,%r14
add    $0xffffffffffffffff,%rbx
mov    (%r15),%al
test   %al,%al
je     123d <xml_quote+0xfd>
lea    0xed3(%rip),%r13
lea    0xe58(%rip),%rbp
jmp    11c7 <xml_quote+0x87>
nopw   0x0(%rax,%rax,1)
mov    %al,(%r14)
add    $0x1,%r15
add    $0x1,%r14
add    $0xffffffffffffffff,%rbx
movzbl (%r15),%eax
test   %al,%al
je     123d <xml_quote+0xfd>
movsbl %al,%ecx
add    $0xffffffde,%ecx
cmp    $0x1c,%ecx
ja     11b0 <xml_quote+0x70>
movslq 0x0(%rbp,%rcx,4),%rdx
add    %rbp,%rdx
mov    %r13,%rcx
jmp    *%rdx
lea    0xe9e(%rip),%rcx
jmp    1207 <xml_quote+0xc7>
nopl   0x0(%rax,%rax,1)
lea    0xe83(%rip),%rcx
jmp    1207 <xml_quote+0xc7>
nopl   0x0(%rax)
lea    0xe78(%rip),%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
lea    0xe77(%rip),%rdx
call   1040 <snprintf@plt>
test   %eax,%eax
js     1257 <xml_quote+0x117>
cmp    %ebx,%eax
jge    1257 <xml_quote+0x117>
add    $0x1,%r15
mov    %eax,%r12d
sub    %r12,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
cmp    %rax,%rbx
jb     1257 <xml_quote+0x117>
add    %r12,%r14
jmp    11bf <xml_quote+0x7f>
test   %rbx,%rbx
je     1257 <xml_quote+0x117>
movw   $0x22,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"&amp;\00"
@.str.1 = [5 x i8] c"&lt;\00"
@.str.2 = [5 x i8] c"&gt;\00"
@.str.3 = [7 x i8] c"&quot;\00"
@.str.4 = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.5 = [28 x i8] c"quote_str: buffer overflow\0A\00"
@EXIT_FAILURE = global i32 0
define void @xml_quote(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%12 = load i8*, i8** %4
store i8* %12, i8** %9
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = sub i64 %14, 1
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 0, i8* %16
%17 = load i8*, i8** %5
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %10
%19 = load i8*, i8** %5
%20 = load i64, i64* %10
%21 = getelementptr inbounds i8, i8* %19, i64 %20
store i8* %21, i8** %7
%22 = load i64, i64* %6
%23 = sub i64 %22, 1
%24 = load i64, i64* %10
%25 = sub i64 %23, %24
store i64 %25, i64* %10
%26 = load i64, i64* %10
%27 = load i8*, i8** %9
%28 = call i64 @strlen(i8* %27)
%29 = add i64 %28, 2
%30 = icmp ult i64 %26, %29
br i1 %30, label %31, label %32
31: ; preds = %3
br label %99
32: ; preds = %3
%33 = load i8*, i8** %7
store i8 34, i8* %33
%34 = load i8*, i8** %7
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %7
%36 = load i64, i64* %10
%37 = add i64 %36, -1
store i64 %37, i64* %10
br label %38
38: ; preds = %89, %50, %32
%39 = load i8*, i8** %9
%40 = load i8, i8* %39
%41 = icmp ne i8 %40, 0
br i1 %41, label %42, label %90
42: ; preds = %38
%43 = load i8*, i8** %9
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
switch i32 %45, label %50 [
i32 38, label %46
i32 60, label %47
i32 62, label %48
i32 34, label %49
]
46: ; preds = %42
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %8
br label %60
47: ; preds = %42
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %8
br label %60
48: ; preds = %42
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %8
br label %60
49: ; preds = %42
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %8
br label %60
50: ; preds = %42
%51 = load i8*, i8** %9
%52 = load i8, i8* %51
%53 = load i8*, i8** %7
store i8 %52, i8* %53
%54 = load i8*, i8** %9
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %9
%56 = load i8*, i8** %7
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %7
%58 = load i64, i64* %10
%59 = add i64 %58, -1
store i64 %59, i64* %10
br label %38
60: ; preds = %49, %48, %47, %46
%61 = load i8*, i8** %9
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %9
%63 = load i8*, i8** %7
%64 = load i64, i64* %10
%65 = load i8*, i8** %8
%66 = call i32 @snprintf(i8* %63, i64 %64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %65)
store i32 %66, i32* %11
%67 = load i32, i32* %11
%68 = icmp slt i32 %67, 0
br i1 %68, label %74, label %69
69: ; preds = %60
%70 = load i32, i32* %11
%71 = load i64, i64* %10
%72 = trunc i64 %71 to i32
%73 = icmp sge i32 %70, %72
br i1 %73, label %74, label %75
74: ; preds = %69, %60
br label %99
75: ; preds = %69
%76 = load i32, i32* %11
%77 = load i8*, i8** %7
%78 = sext i32 %76 to i64
%79 = getelementptr inbounds i8, i8* %77, i64 %78
store i8* %79, i8** %7
%80 = load i32, i32* %11
%81 = sext i32 %80 to i64
%82 = load i64, i64* %10
%83 = sub i64 %82, %81
store i64 %83, i64* %10
%84 = load i64, i64* %10
%85 = load i8*, i8** %9
%86 = call i64 @strlen(i8* %85)
%87 = icmp ult i64 %84, %86
br i1 %87, label %88, label %89
88: ; preds = %75
br label %99
89: ; preds = %75
br label %38
90: ; preds = %38
%91 = load i64, i64* %10
%92 = icmp ne i64 %91, 0
br i1 %92, label %94, label %93
93: ; preds = %90
br label %99
94: ; preds = %90
%95 = load i8*, i8** %7
store i8 34, i8* %95
%96 = load i8*, i8** %7
%97 = getelementptr inbounds i8, i8* %96, i32 1
store i8* %97, i8** %7
%98 = load i8*, i8** %7
store i8 0, i8* %98
ret void
99: ; preds = %93, %88, %74, %31
%100 = load i32, i32* @stderr
%101 = call i32 @fprintf(i32 %100, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%102 = load i32, i32* @EXIT_FAILURE
%103 = call i32 @exit(i32 %102)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"&amp;\00"
@.str.1 = [5 x i8] c"&lt;\00"
@.str.2 = [5 x i8] c"&gt;\00"
@.str.3 = [7 x i8] c"&quot;\00"
@.str.4 = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.5 = [28 x i8] c"quote_str: buffer overflow\0A\00"
@EXIT_FAILURE = global i32 0
define void @xml_quote(i8* nocapture %0, i8* %1, i64 %2) {
%4 = add i64 %2, -1
%5 = getelementptr inbounds i8, i8* %1, i64 %4
store i8 0, i8* %5
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = sub i64 %4, %6
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%10 = add i64 %9, 2
%11 = icmp ult i64 %8, %10
br i1 %11, label %56, label %12
12: ; preds = %3
store i8 34, i8* %7
%13 = getelementptr inbounds i8, i8* %7, i64 1
%14 = add i64 %8, -1
%15 = load i8, i8* %0
%16 = icmp eq i8 %15, 0
br i1 %16, label %50, label %17
17: ; preds = %12, %30
%18 = phi i8 [ %34, %30 ], [ %15, %12 ]
%19 = phi i8* [ %33, %30 ], [ %13, %12 ]
%20 = phi i64 [ %32, %30 ], [ %14, %12 ]
%21 = phi i8* [ %31, %30 ], [ %0, %12 ]
%22 = sext i8 %18 to i32
switch i32 %22, label %26 [
i32 38, label %36
i32 60, label %23
i32 62, label %24
i32 34, label %25
]
23: ; preds = %17
br label %36
24: ; preds = %17
br label %36
25: ; preds = %17
br label %36
26: ; preds = %17
store i8 %18, i8* %19
%27 = getelementptr inbounds i8, i8* %21, i64 1
%28 = getelementptr inbounds i8, i8* %19, i64 1
%29 = add i64 %20, -1
br label %30
30: ; preds = %26, %44
%31 = phi i8* [ %27, %26 ], [ %38, %44 ]
%32 = phi i64 [ %29, %26 ], [ %47, %44 ]
%33 = phi i8* [ %28, %26 ], [ %46, %44 ]
%34 = load i8, i8* %31
%35 = icmp eq i8 %34, 0
br i1 %35, label %50, label %17
36: ; preds = %17, %25, %24, %23
%37 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), %25 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %24 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %23 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %17 ]
%38 = getelementptr inbounds i8, i8* %21, i64 1
%39 = tail call i32 @snprintf(i8* %19, i64 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %37)
%40 = icmp sgt i32 %39, -1
%41 = trunc i64 %20 to i32
%42 = icmp slt i32 %39, %41
%43 = select i1 %40, i1 %42, i1 false
br i1 %43, label %44, label %56
44: ; preds = %36
%45 = zext i32 %39 to i64
%46 = getelementptr inbounds i8, i8* %19, i64 %45
%47 = sub i64 %20, %45
%48 = tail call i64 @strlen(i8* %38)
%49 = icmp ult i64 %47, %48
br i1 %49, label %56, label %30
50: ; preds = %30, %12
%51 = phi i64 [ %14, %12 ], [ %32, %30 ]
%52 = phi i8* [ %13, %12 ], [ %33, %30 ]
%53 = icmp eq i64 %51, 0
br i1 %53, label %56, label %54
54: ; preds = %50
store i8 34, i8* %52
%55 = getelementptr inbounds i8, i8* %52, i64 1
store i8 0, i8* %55
ret void
56: ; preds = %44, %36, %50, %3
%57 = load i32, i32* @stderr
%58 = tail call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%59 = load i32, i32* @EXIT_FAILURE
%60 = tail call i32 @exit(i32 %59)
unreachable
}