#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 */
typedef int /*<<< orphan*/ table;

/* Variables and functions */
char *malloc(size_t);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strdup_escape_xml(const char *p_str) {
  struct {
    char ch;
    const char *str;
    size_t len;
  } table[] = {{'&', "&amp;", 5},
               {'<', "&lt;", 4},
               {'>', "&gt;", 4},
               {'"', "&quot;", 6},
               {'\'', "&apos;", 6}};
  size_t n = sizeof(table) / sizeof(table[0]);
  size_t i = 0;
  size_t len = 0;
  char *p_res = NULL;
  char *p = NULL;
  size_t res_len = 0;
  char ch = 0;
  while ((ch = p_str[len]) != 0) {
    ++res_len;
    for (i = 0; i < n; ++i) {
      if (ch == table[i].ch) {
        res_len += (table[i].len - 1);
        break;
      }
    }
    ++len;
  }
  p = p_res = malloc(res_len + 1);
  for (size_t j = 0; j < len; ++j) {
    ch = p_str[j];
    for (i = 0; i < n; ++i) {
      if (ch == table[i].ch) {
        strncpy(p, table[i].str, table[i].len);
        p += table[i].len;
        break;
      }
    }
    if (i == n) {
      *p = ch;
      ++p;
    }
  }
  *p = 0;
  return p_res;
}


x86 assembly (clang, -O0):
<strdup_escape_xml>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x80(%rbp),%rdi
lea    0x2c56(%rip),%rsi
mov    $0x78,%edx
call   1040 <memcpy@plt>
movq   $0x5,-0x88(%rbp)
movq   $0x0,-0x90(%rbp)
movq   $0x0,-0x98(%rbp)
movq   $0x0,-0xa0(%rbp)
movq   $0x0,-0xa8(%rbp)
movq   $0x0,-0xb0(%rbp)
movb   $0x0,-0xb1(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x98(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0xb1(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
je     126c <strdup_escape_xml+0x13c>
mov    -0xb0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xb0(%rbp)
movq   $0x0,-0x90(%rbp)
mov    -0x90(%rbp),%rax
cmp    -0x88(%rbp),%rax
jae    1255 <strdup_escape_xml+0x125>
movsbl -0xb1(%rbp),%eax
lea    -0x80(%rbp),%rcx
imul   $0x18,-0x90(%rbp),%rdx
add    %rdx,%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1239 <strdup_escape_xml+0x109>
lea    -0x80(%rbp),%rax
imul   $0x18,-0x90(%rbp),%rcx
18
add    %rcx,%rax
mov    0x10(%rax),%rax
sub    $0x1,%rax
add    -0xb0(%rbp),%rax
mov    %rax,-0xb0(%rbp)
jmp    1255 <strdup_escape_xml+0x125>
jmp    123e <strdup_escape_xml+0x10e>
mov    -0x90(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x90(%rbp)
jmp    11da <strdup_escape_xml+0xaa>
mov    -0x98(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x98(%rbp)
jmp    119d <strdup_escape_xml+0x6d>
mov    -0xb0(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0xa0(%rbp)
mov    %rax,-0xa8(%rbp)
movq   $0x0,-0xc0(%rbp)
mov    -0xc0(%rbp),%rax
cmp    -0x98(%rbp),%rax
jae    13c4 <strdup_escape_xml+0x294>
mov    -0x8(%rbp),%rax
mov    -0xc0(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0xb1(%rbp)
movq   $0x0,-0x90(%rbp)
mov    -0x90(%rbp),%rax
cmp    -0x88(%rbp),%rax
jae    1373 <strdup_escape_xml+0x243>
movsbl -0xb1(%rbp),%eax
lea    -0x80(%rbp),%rcx
imul   $0x18,-0x90(%rbp),%rdx
18
add    %rdx,%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1357 <strdup_escape_xml+0x227>
mov    -0xa8(%rbp),%rdi
lea    -0x80(%rbp),%rax
imul   $0x18,-0x90(%rbp),%rcx
18
add    %rcx,%rax
mov    0x8(%rax),%rsi
lea    -0x80(%rbp),%rax
imul   $0x18,-0x90(%rbp),%rcx
18
add    %rcx,%rax
mov    0x10(%rax),%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
lea    -0x80(%rbp),%rax
imul   $0x18,-0x90(%rbp),%rcx
18
add    %rcx,%rax
mov    0x10(%rax),%rax
add    -0xa8(%rbp),%rax
mov    %rax,-0xa8(%rbp)
jmp    1373 <strdup_escape_xml+0x243>
jmp    135c <strdup_escape_xml+0x22c>
mov    -0x90(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x90(%rbp)
jmp    12c8 <strdup_escape_xml+0x198>
mov    -0x90(%rbp),%rax
cmp    -0x88(%rbp),%rax
jne    13a8 <strdup_escape_xml+0x278>
mov    -0xb1(%rbp),%cl
mov    -0xa8(%rbp),%rax
mov    %cl,(%rax)
mov    -0xa8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xa8(%rbp)
jmp    13ad <strdup_escape_xml+0x27d>
mov    -0xc0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xc0(%rbp)
jmp    1295 <strdup_escape_xml+0x165>
mov    -0xa8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0xa0(%rbp),%rax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strdup_escape_xml>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    (%rdi),%bl
test   %bl,%bl
je     1243 <strdup_escape_xml+0x123>
mov    %rdi,%r15
mov    $0xffffffffffffffff,%r13
xor    %edi,%edi
lea    0xeb8(%rip),%rax
lea    0x2c51(%rip),%rbp
mov    %ebx,%ecx
jmp    117a <strdup_escape_xml+0x5a>
mov    $0x4,%ecx
nopl   0x0(%rax,%rax,1)
lea    (%rcx,%rcx,2),%rcx
mov    0x10(%rbp,%rcx,8),%rdx
add    %rdx,%rdi
movzbl 0x2(%r15,%r13,1),%ecx
add    $0x1,%r13
test   %cl,%cl
je     11aa <strdup_escape_xml+0x8a>
mov    $0x1,%edx
add    $0xde,%cl
cmp    $0x1c,%cl
ja     1169 <strdup_escape_xml+0x49>
movzbl %cl,%ecx
movslq (%rax,%rcx,4),%rsi
add    %rax,%rsi
xor    %ecx,%ecx
jmp    *%rsi
mov    $0x3,%ecx
jmp    1160 <strdup_escape_xml+0x40>
mov    $0x1,%ecx
jmp    1160 <strdup_escape_xml+0x40>
mov    $0x2,%ecx
jmp    1160 <strdup_escape_xml+0x40>
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,%r12
cmp    $0xffffffffffffffff,%r13
je     1256 <strdup_escape_xml+0x136>
xor    %r14d,%r14d
lea    0xeaa(%rip),%rbp
mov    %rax,(%rsp)
xchg   %ax,%ax
lea    -0x22(%rbx),%eax
cmp    $0x1c,%al
ja     11ed <strdup_escape_xml+0xcd>
movzbl %al,%eax
movslq 0x0(%rbp,%rax,4),%rcx
add    %rbp,%rcx
xor    %eax,%eax
jmp    *%rcx
mov    $0x3,%eax
jmp    1210 <strdup_escape_xml+0xf0>
mov    %bl,(%r12)
mov    $0x1,%ebx
jmp    122f <strdup_escape_xml+0x10f>
mov    $0x4,%eax
jmp    1210 <strdup_escape_xml+0xf0>
mov    $0x1,%eax
jmp    1210 <strdup_escape_xml+0xf0>
mov    $0x2,%eax
nopl   0x0(%rax,%rax,1)
lea    (%rax,%rax,2),%rax
lea    0x2b85(%rip),%rcx
mov    0x8(%rcx,%rax,8),%rsi
mov    0x10(%rcx,%rax,8),%rbx
mov    %r12,%rdi
mov    %ebx,%edx
call   1030 <strncpy@plt>
add    %rbx,%r12
cmp    %r14,%r13
je     1252 <strdup_escape_xml+0x132>
movzbl 0x1(%r15,%r14,1),%ebx
add    $0x1,%r14
jmp    11d0 <strdup_escape_xml+0xb0>
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
jmp    1256 <strdup_escape_xml+0x136>
mov    (%rsp),%rax
movb   $0x0,(%r12)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.anon = type { i8, i8*, i64 }
@.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 = [7 x i8] c"&apos;\00"
@__const.strdup_escape_xml.table = [5 x %struct.anon] [%struct.anon { i8 38, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i64 5 }, %struct.anon { i8 60, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i32 0, i32 0), i64 4 }, %struct.anon { i8 62, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0), i64 4 }, %struct.anon { i8 34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i32 0, i32 0), i64 6 }, %struct.anon { i8 39, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i32 0, i32 0), i64 6 }]
define i8* @strdup_escape_xml(i8* %0) {
%2 = alloca i8*
%3 = alloca [5 x %struct.anon]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8
%11 = alloca i64
store i8* %0, i8** %2
%12 = bitcast [5 x %struct.anon]* %3 to i8*
store i64 5, i64* %4
store i64 0, i64* %5
store i64 0, i64* %6
store i8* null, i8** %7
store i8* null, i8** %8
store i64 0, i64* %9
store i8 0, i8* %10
br label %13
13: ; preds = %48, %1
%14 = load i8*, i8** %2
%15 = load i64, i64* %6
%16 = getelementptr inbounds i8, i8* %14, i64 %15
%17 = load i8, i8* %16
store i8 %17, i8* %10
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %51
20: ; preds = %13
%21 = load i64, i64* %9
%22 = add i64 %21, 1
store i64 %22, i64* %9
store i64 0, i64* %5
br label %23
23: ; preds = %45, %20
%24 = load i64, i64* %5
%25 = load i64, i64* %4
%26 = icmp ult i64 %24, %25
br i1 %26, label %27, label %48
27: ; preds = %23
%28 = load i8, i8* %10
%29 = sext i8 %28 to i32
%30 = load i64, i64* %5
%31 = getelementptr inbounds [5 x %struct.anon], [5 x %struct.anon]* %3, i64 0, i64 %30
%32 = getelementptr inbounds %struct.anon, %struct.anon* %31, i32 0, i32 0
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %29, %34
br i1 %35, label %36, label %44
36: ; preds = %27
%37 = load i64, i64* %5
%38 = getelementptr inbounds [5 x %struct.anon], [5 x %struct.anon]* %3, i64 0, i64 %37
%39 = getelementptr inbounds %struct.anon, %struct.anon* %38, i32 0, i32 2
%40 = load i64, i64* %39
%41 = sub i64 %40, 1
%42 = load i64, i64* %9
%43 = add i64 %42, %41
store i64 %43, i64* %9
br label %48
44: ; preds = %27
br label %45
45: ; preds = %44
%46 = load i64, i64* %5
%47 = add i64 %46, 1
store i64 %47, i64* %5
br label %23
48: ; preds = %36, %23
%49 = load i64, i64* %6
%50 = add i64 %49, 1
store i64 %50, i64* %6
br label %13
51: ; preds = %13
%52 = load i64, i64* %9
%53 = add i64 %52, 1
%54 = call i8* @malloc(i64 %53)
store i8* %54, i8** %7
store i8* %54, i8** %8
store i64 0, i64* %11
br label %55
55: ; preds = %109, %51
%56 = load i64, i64* %11
%57 = load i64, i64* %6
%58 = icmp ult i64 %56, %57
br i1 %58, label %59, label %112
59: ; preds = %55
%60 = load i8*, i8** %2
%61 = load i64, i64* %11
%62 = getelementptr inbounds i8, i8* %60, i64 %61
%63 = load i8, i8* %62
store i8 %63, i8* %10
store i64 0, i64* %5
br label %64
64: ; preds = %96, %59
%65 = load i64, i64* %5
%66 = load i64, i64* %4
%67 = icmp ult i64 %65, %66
br i1 %67, label %68, label %99
68: ; preds = %64
%69 = load i8, i8* %10
%70 = sext i8 %69 to i32
%71 = load i64, i64* %5
%72 = getelementptr inbounds [5 x %struct.anon], [5 x %struct.anon]* %3, i64 0, i64 %71
%73 = getelementptr inbounds %struct.anon, %struct.anon* %72, i32 0, i32 0
%74 = load i8, i8* %73
%75 = sext i8 %74 to i32
%76 = icmp eq i32 %70, %75
br i1 %76, label %77, label %95
77: ; preds = %68
%78 = load i8*, i8** %8
%79 = load i64, i64* %5
%80 = getelementptr inbounds [5 x %struct.anon], [5 x %struct.anon]* %3, i64 0, i64 %79
%81 = getelementptr inbounds %struct.anon, %struct.anon* %80, i32 0, i32 1
%82 = load i8*, i8** %81
%83 = load i64, i64* %5
%84 = getelementptr inbounds [5 x %struct.anon], [5 x %struct.anon]* %3, i64 0, i64 %83
%85 = getelementptr inbounds %struct.anon, %struct.anon* %84, i32 0, i32 2
%86 = load i64, i64* %85
%87 = trunc i64 %86 to i32
%88 = call i32 @strncpy(i8* %78, i8* %82, i32 %87)
%89 = load i64, i64* %5
%90 = getelementptr inbounds [5 x %struct.anon], [5 x %struct.anon]* %3, i64 0, i64 %89
%91 = getelementptr inbounds %struct.anon, %struct.anon* %90, i32 0, i32 2
%92 = load i64, i64* %91
%93 = load i8*, i8** %8
%94 = getelementptr inbounds i8, i8* %93, i64 %92
store i8* %94, i8** %8
br label %99
95: ; preds = %68
br label %96
96: ; preds = %95
%97 = load i64, i64* %5
%98 = add i64 %97, 1
store i64 %98, i64* %5
br label %64
99: ; preds = %77, %64
%100 = load i64, i64* %5
%101 = load i64, i64* %4
%102 = icmp eq i64 %100, %101
br i1 %102, label %103, label %108
103: ; preds = %99
%104 = load i8, i8* %10
%105 = load i8*, i8** %8
store i8 %104, i8* %105
%106 = load i8*, i8** %8
%107 = getelementptr inbounds i8, i8* %106, i32 1
store i8* %107, i8** %8
br label %108
108: ; preds = %103, %99
br label %109
109: ; preds = %108
%110 = load i64, i64* %11
%111 = add i64 %110, 1
store i64 %111, i64* %11
br label %55
112: ; preds = %55
%113 = load i8*, i8** %8
store i8 0, i8* %113
%114 = load i8*, i8** %7
ret i8* %114
}

LLVM IR (-O2):
%struct.anon = type { i8, i8*, i64 }
@.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 = [7 x i8] c"&apos;\00"
@__const.strdup_escape_xml.table = [5 x %struct.anon] [%struct.anon { i8 38, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i64 5 }, %struct.anon { i8 60, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i32 0, i32 0), i64 4 }, %struct.anon { i8 62, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0), i64 4 }, %struct.anon { i8 34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i32 0, i32 0), i64 6 }, %struct.anon { i8 39, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i32 0, i32 0), i64 6 }]
define i8* @strdup_escape_xml(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call dereferenceable_or_null(1) i8* @malloc(i64 1)
br label %29
6: ; preds = %1, %18
%7 = phi i8 [ %23, %18 ], [ %2, %1 ]
%8 = phi i64 [ %20, %18 ], [ 0, %1 ]
%9 = phi i64 [ %21, %18 ], [ 0, %1 ]
switch i8 %7, label %18 [
i8 38, label %14
i8 60, label %10
i8 62, label %11
i8 34, label %12
i8 39, label %13
]
10: ; preds = %6
br label %14
11: ; preds = %6
br label %14
12: ; preds = %6
br label %14
13: ; preds = %6
br label %14
14: ; preds = %6, %13, %12, %11, %10
%15 = phi i64 [ 0, %6 ], [ 1, %10 ], [ 2, %11 ], [ 3, %12 ], [ 4, %13 ]
%16 = getelementptr inbounds [5 x %struct.anon], [5 x %struct.anon]* @__const.strdup_escape_xml.table, i64 0, i64 %15, i32 2
%17 = load i64, i64* %16
br label %18
18: ; preds = %6, %14
%19 = phi i64 [ %17, %14 ], [ 1, %6 ]
%20 = add i64 %19, %8
%21 = add i64 %9, 1
%22 = getelementptr inbounds i8, i8* %0, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %6
25: ; preds = %18
%26 = add i64 %20, 1
%27 = tail call i8* @malloc(i64 %26)
%28 = icmp eq i64 %21, 0
br i1 %28, label %29, label %32
29: ; preds = %49, %4, %25
%30 = phi i8* [ %27, %25 ], [ %5, %4 ], [ %27, %49 ]
%31 = phi i8* [ %27, %25 ], [ %5, %4 ], [ %51, %49 ]
store i8 0, i8* %31
ret i8* %30
32: ; preds = %25, %54
%33 = phi i8 [ %56, %54 ], [ %2, %25 ]
%34 = phi i64 [ %52, %54 ], [ 0, %25 ]
%35 = phi i8* [ %51, %54 ], [ %27, %25 ]
switch i8 %33, label %48 [
i8 38, label %40
i8 60, label %36
i8 62, label %37
i8 34, label %38
i8 39, label %39
]
36: ; preds = %32
br label %40
37: ; preds = %32
br label %40
38: ; preds = %32
br label %40
39: ; preds = %32
br label %40
40: ; preds = %32, %39, %38, %37, %36
%41 = phi i64 [ 0, %32 ], [ 1, %36 ], [ 2, %37 ], [ 3, %38 ], [ 4, %39 ]
%42 = getelementptr inbounds [5 x %struct.anon], [5 x %struct.anon]* @__const.strdup_escape_xml.table, i64 0, i64 %41, i32 1
%43 = load i8*, i8** %42
%44 = getelementptr inbounds [5 x %struct.anon], [5 x %struct.anon]* @__const.strdup_escape_xml.table, i64 0, i64 %41, i32 2
%45 = load i64, i64* %44
%46 = trunc i64 %45 to i32
%47 = tail call i32 @strncpy(i8* %35, i8* %43, i32 %46)
br label %49
48: ; preds = %32
store i8 %33, i8* %35
br label %49
49: ; preds = %40, %48
%50 = phi i64 [ %45, %40 ], [ 1, %48 ]
%51 = getelementptr inbounds i8, i8* %35, i64 %50
%52 = add nuw i64 %34, 1
%53 = icmp eq i64 %34, %9
br i1 %53, label %29, label %54
54: ; preds = %49
%55 = getelementptr inbounds i8, i8* %0, i64 %52
%56 = load i8, i8* %55
br label %32
}