#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*/ snprintf(char *, size_t const, char *, char);
size_t xml_flush(size_t, char *);
void write_xml_attribute_escaped(const char *string) {
  static const size_t escape_size = 6;
  if (!string)
    return;
  char buff[1024];
  const size_t buffer_size = sizeof buff;
  size_t i = 0;
  for (char c = *string; c; c = *++string) {
    switch (c) {
    case '\t':
    case '\n':
    case '\r':
    case '"':
    case '&':
    case '<':
      if (buffer_size - i < escape_size) {
        i = xml_flush(i, buff);
      }
      snprintf(buff + i, escape_size, "&#%02d;", c);
      i += escape_size - 1;
      break;
    default:
      buff[i++] = c;
      break;
    }
    if (i == buffer_size - 1) {
      i = xml_flush(i, buff);
    }
  }
  if (i > 0) {
    xml_flush(i, buff);
  }
}


x86 assembly (clang, -O0):
<write_xml_attribute_escaped>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    113f <write_xml_attribute_escaped+0x1f>
jmp    12d9 <write_xml_attribute_escaped+0x1b9>
movq   $0x400,-0x418(%rbp)
movq   $0x0,-0x420(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x421(%rbp)
cmpb   $0x0,-0x421(%rbp)
je     12b8 <write_xml_attribute_escaped+0x198>
movsbl -0x421(%rbp),%eax
mov    %eax,-0x428(%rbp)
add    $0xfffffff7,%eax
sub    $0x2,%eax
jb     11dc <write_xml_attribute_escaped+0xbc>
jmp    118c <write_xml_attribute_escaped+0x6c>
mov    -0x428(%rbp),%eax
sub    $0xd,%eax
je     11dc <write_xml_attribute_escaped+0xbc>
jmp    11a0 <write_xml_attribute_escaped+0x80>
mov    -0x428(%rbp),%eax
sub    $0x22,%eax
je     11dc <write_xml_attribute_escaped+0xbc>
jmp    11b4 <write_xml_attribute_escaped+0x94>
mov    -0x428(%rbp),%eax
sub    $0x26,%eax
je     11dc <write_xml_attribute_escaped+0xbc>
jmp    11c8 <write_xml_attribute_escaped+0xa8>
mov    -0x428(%rbp),%eax
sub    $0x3c,%eax
jne    1249 <write_xml_attribute_escaped+0x129>
jmp    11dc <write_xml_attribute_escaped+0xbc>
mov    $0x400,%eax
sub    -0x420(%rbp),%rax
cmp    $0x6,%rax
jae    120c <write_xml_attribute_escaped+0xec>
mov    -0x420(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1040 <xml_flush@plt>
mov    %rax,-0x420(%rbp)
lea    -0x410(%rbp),%rdi
add    -0x420(%rbp),%rdi
mov    $0x6,%esi
lea    0xde2(%rip),%rdx
movsbl -0x421(%rbp),%ecx
call   1030 <snprintf@plt>
mov    -0x420(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x420(%rbp)
jmp    126b <write_xml_attribute_escaped+0x14b>
mov    -0x421(%rbp),%cl
mov    -0x420(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x420(%rbp)
mov    %cl,-0x410(%rbp,%rax,1)
cmpq   $0x3ff,-0x420(%rbp)
jne    1296 <write_xml_attribute_escaped+0x176>
mov    -0x420(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1040 <xml_flush@plt>
mov    %rax,-0x420(%rbp)
jmp    129b <write_xml_attribute_escaped+0x17b>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    0x1(%rax),%al
mov    %al,-0x421(%rbp)
jmp    1161 <write_xml_attribute_escaped+0x41>
cmpq   $0x0,-0x420(%rbp)
jbe    12d9 <write_xml_attribute_escaped+0x1b9>
mov    -0x420(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1040 <xml_flush@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_xml_attribute_escaped>:
test   %rdi,%rdi
je     120c <write_xml_attribute_escaped+0xec>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r13
mov    (%rdi),%al
test   %al,%al
je     11fb <write_xml_attribute_escaped+0xdb>
add    $0x1,%r13
xor    %r14d,%r14d
mov    %rsp,%r15
movabs $0x1000004400002600,%rbp
lea    0xe9e(%rip),%r12
jmp    117d <write_xml_attribute_escaped+0x5d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl 0x0(%r13),%eax
add    $0x1,%r13
test   %al,%al
je     11eb <write_xml_attribute_escaped+0xcb>
movsbl %al,%ebx
cmp    $0x3c,%ebx
ja     11d8 <write_xml_attribute_escaped+0xb8>
bt     %rbx,%rbp
jae    11d8 <write_xml_attribute_escaped+0xb8>
lea    -0x3fb(%r14),%rax
cmp    $0x5,%rax
ja     11a6 <write_xml_attribute_escaped+0x86>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <xml_flush@plt>
mov    %rax,%r14
lea    (%rsp,%r14,1),%rdi
mov    $0x6,%esi
mov    %r12,%rdx
mov    %ebx,%ecx
call   1030 <snprintf@plt>
add    $0x5,%r14
cmp    $0x3ff,%r14
jne    1170 <write_xml_attribute_escaped+0x50>
mov    $0x3ff,%edi
mov    %r15,%rsi
call   1040 <xml_flush@plt>
mov    %rax,%r14
jmp    1170 <write_xml_attribute_escaped+0x50>
mov    %al,(%rsp,%r14,1)
add    $0x1,%r14
cmp    $0x3ff,%r14
jne    1170 <write_xml_attribute_escaped+0x50>
jmp    11c6 <write_xml_attribute_escaped+0xa6>
test   %r14,%r14
je     11fb <write_xml_attribute_escaped+0xdb>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1040 <xml_flush@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@write_xml_attribute_escaped.escape_size = internal constant i64 6
@.str = [8 x i8] c"&#%02d;\00"
define void @write_xml_attribute_escaped(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
br label %59
10: ; preds = %1
store i64 1024, i64* %4
store i64 0, i64* %5
%11 = load i8*, i8** %2
%12 = load i8, i8* %11
store i8 %12, i8* %6
br label %13
13: ; preds = %48, %10
%14 = load i8, i8* %6
%15 = icmp ne i8 %14, 0
br i1 %15, label %16, label %52
16: ; preds = %13
%17 = load i8, i8* %6
%18 = sext i8 %17 to i32
switch i32 %18, label %35 [
i32 9, label %19
i32 10, label %19
i32 13, label %19
i32 34, label %19
i32 38, label %19
i32 60, label %19
]
19: ; preds = %16, %16, %16, %16, %16, %16
%20 = load i64, i64* %5
%21 = sub i64 1024, %20
%22 = icmp ult i64 %21, 6
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i64, i64* %5
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%26 = call i64 @xml_flush(i64 %24, i8* %25)
store i64 %26, i64* %5
br label %27
27: ; preds = %23, %19
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%29 = load i64, i64* %5
%30 = getelementptr inbounds i8, i8* %28, i64 %29
%31 = load i8, i8* %6
%32 = call i32 @snprintf(i8* %30, i64 6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8 signext %31)
%33 = load i64, i64* %5
%34 = add i64 %33, 5
store i64 %34, i64* %5
br label %40
35: ; preds = %16
%36 = load i8, i8* %6
%37 = load i64, i64* %5
%38 = add i64 %37, 1
store i64 %38, i64* %5
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %37
store i8 %36, i8* %39
br label %40
40: ; preds = %35, %27
%41 = load i64, i64* %5
%42 = icmp eq i64 %41, 1023
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i64, i64* %5
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%46 = call i64 @xml_flush(i64 %44, i8* %45)
store i64 %46, i64* %5
br label %47
47: ; preds = %43, %40
br label %48
48: ; preds = %47
%49 = load i8*, i8** %2
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %2
%51 = load i8, i8* %50
store i8 %51, i8* %6
br label %13
52: ; preds = %13
%53 = load i64, i64* %5
%54 = icmp ugt i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = load i64, i64* %5
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%58 = call i64 @xml_flush(i64 %56, i8* %57)
br label %59
59: ; preds = %9, %55, %52
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"&#%02d;\00"
define void @write_xml_attribute_escaped(i8* %0) {
%2 = alloca [1024 x i8]
%3 = icmp eq i8* %0, null
br i1 %3, label %41, label %4
4: ; preds = %1
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %40, label %10
8: ; preds = %33
%9 = icmp eq i64 %34, 0
br i1 %9, label %40, label %38
10: ; preds = %4, %33
%11 = phi i8 [ %36, %33 ], [ %6, %4 ]
%12 = phi i64 [ %34, %33 ], [ 0, %4 ]
%13 = phi i8* [ %35, %33 ], [ %0, %4 ]
%14 = sext i8 %11 to i32
switch i32 %14, label %25 [
i32 9, label %15
i32 10, label %15
i32 13, label %15
i32 34, label %15
i32 38, label %15
i32 60, label %15
]
15: ; preds = %10, %10, %10, %10, %10, %10
%16 = add i64 %12, -1019
%17 = icmp ult i64 %16, 6
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i64 @xml_flush(i64 %12, i8* %5)
br label %20
20: ; preds = %18, %15
%21 = phi i64 [ %19, %18 ], [ %12, %15 ]
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %21
%23 = call i32 @snprintf(i8* %22, i64 6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8 signext %11)
%24 = add i64 %21, 5
br label %28
25: ; preds = %10
%26 = add i64 %12, 1
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %12
store i8 %11, i8* %27
br label %28
28: ; preds = %25, %20
%29 = phi i64 [ %26, %25 ], [ %24, %20 ]
%30 = icmp eq i64 %29, 1023
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i64 @xml_flush(i64 1023, i8* %5)
br label %33
33: ; preds = %28, %31
%34 = phi i64 [ %32, %31 ], [ %29, %28 ]
%35 = getelementptr inbounds i8, i8* %13, i64 1
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 0
br i1 %37, label %8, label %10
38: ; preds = %8
%39 = call i64 @xml_flush(i64 %34, i8* %5)
br label %40
40: ; preds = %4, %38, %8
br label %41
41: ; preds = %1, %40
ret void
}