#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);
int /*<<< orphan*/ stderr;
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *xml_quote(char const *str, char *quote_buf) {
  int i;
  int pos = 0;
  int len = strlen(str);
  quote_buf[pos++] = '"';
  for (i = 0; i < len; i++) {
    if (pos >= 2048 - 10) {
      fprintf(stderr, "quote_str: buffer overflow %d\n", pos);
      exit(EXIT_FAILURE);
    }
    switch (str[i]) {
    case '&':
      strncpy(quote_buf + pos, "&amp;", 5);
      pos += 5;
      break;
    case '<':
      strncpy(quote_buf + pos, "&lt;", 4);
      pos += 4;
      break;
    case '>':
      strncpy(quote_buf + pos, "&gt;", 4);
      pos += 4;
      break;
    case '"':
      strncpy(quote_buf + pos, "&quot;", 6);
      pos += 6;
      break;
    default:
      quote_buf[pos++] = str[i];
      break;
    }
  }
  quote_buf[pos++] = '"';
  quote_buf[pos++] = '\0';
  return quote_buf;
}


x86 assembly (clang, -O0):
<xml_quote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
movb   $0x22,(%rax,%rcx,1)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12cc <xml_quote+0x18c>
cmpl   $0x7f6,-0x18(%rbp)
jl     11bf <xml_quote+0x7f>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xec8(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
add    $0xffffffde,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x1c,%eax
ja     1299 <xml_quote+0x159>
mov    -0x28(%rbp),%rax
lea    0xe18(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
lea    0xe90(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0x18(%rbp)
jmp    12b9 <xml_quote+0x179>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
lea    0xe6c(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x18(%rbp)
jmp    12b9 <xml_quote+0x179>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
lea    0xe47(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x18(%rbp)
jmp    12b9 <xml_quote+0x179>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
lea    0xe22(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%eax
add    $0x6,%eax
mov    %eax,-0x18(%rbp)
jmp    12b9 <xml_quote+0x179>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    12be <xml_quote+0x17e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1180 <xml_quote+0x40>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
movb   $0x22,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xml_quote>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <strlen@plt>
movb   $0x22,(%rbx)
test   %eax,%eax
jle    120f <xml_quote+0xcf>
mov    %eax,%r12d
mov    $0x1,%ebp
xor    %r13d,%r13d
lea    0xe8d(%rip),%r15
jmp    1198 <xml_quote+0x58>
movslq %ebp,%rdi
add    %rbx,%rdi
lea    0xf17(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
add    $0x4,%ebp
add    $0x1,%r13
cmp    %r13,%r12
je     1214 <xml_quote+0xd4>
cmp    $0x7f6,%ebp
jge    122f <xml_quote+0xef>
movsbl (%r14,%r13,1),%eax
lea    -0x22(%rax),%ecx
cmp    $0x1c,%ecx
ja     11d6 <xml_quote+0x96>
movslq (%r15,%rcx,4),%rcx
add    %r15,%rcx
jmp    *%rcx
movslq %ebp,%rdi
add    %rbx,%rdi
lea    0xedc(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncpy@plt>
add    $0x6,%ebp
jmp    118f <xml_quote+0x4f>
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %al,(%rbx,%rcx,1)
jmp    118f <xml_quote+0x4f>
movslq %ebp,%rdi
add    %rbx,%rdi
lea    0xea5(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncpy@plt>
add    $0x5,%ebp
jmp    118f <xml_quote+0x4f>
movslq %ebp,%rdi
add    %rbx,%rdi
lea    0xe94(%rip),%rsi
jmp    1182 <xml_quote+0x42>
mov    $0x1,%ebp
movslq %ebp,%rax
movw   $0x22,(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
mov    %ebp,%edx
call   1050 <fprintf@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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

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