#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*/ free(char *);
char *malloc(int const);
char *realloc(char *, size_t);
char *strdup(char const *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *escape_predefined_character_entities(const char *string) {
  const int MAX_ESCAPE_LEN = 6;
  char *s = strdup(string);
  char *result = malloc(MAX_ESCAPE_LEN * strlen(string) * sizeof(char));
  if (NULL == result)
    return NULL;
  char *c;
  char *result_end = result;
  for (c = s; *c != '\0'; c++) {
    switch (*c) {
    case '&':
      strncpy(result_end, "&amp;", 5);
      result_end += 5;
      break;
    case '<':
      strncpy(result_end, "&lt;", 4);
      result_end += 4;
      break;
    case '>':
      strncpy(result_end, "&gt;", 4);
      result_end += 4;
      break;
    case '\'':
      strncpy(result_end, "&apos;", 6);
      result_end += 6;
      break;
    case '"':
      strncpy(result_end, "&quot;", 6);
      result_end += 6;
      break;
    default:
      *result_end = *c;
      result_end++;
      break;
    }
  }
  *result_end = '\0';
  free(s);
  size_t new_length = strlen(result) + 1;
  result = realloc(result, new_length);
  return result;
}


x86 assembly (clang, -O0):
<escape_predefined_character_entities>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x6,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
imul   $0x6,%eax,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    11b6 <escape_predefined_character_entities+0x56>
movq   $0x0,-0x8(%rbp)
jmp    132a <escape_predefined_character_entities+0x1ca>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12ef <escape_predefined_character_entities+0x18f>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
add    $0xffffffde,%eax
mov    %eax,%ecx
mov    %rcx,-0x48(%rbp)
sub    $0x1c,%eax
ja     12c1 <escape_predefined_character_entities+0x161>
mov    -0x48(%rbp),%rax
lea    0xe06(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x38(%rbp),%rdi
lea    0xe66(%rip),%rsi
mov    $0x5,%edx
call   1040 <strncpy@plt>
mov    -0x38(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x38(%rbp)
jmp    12d9 <escape_predefined_character_entities+0x179>
mov    -0x38(%rbp),%rdi
lea    0xe46(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
jmp    12d9 <escape_predefined_character_entities+0x179>
mov    -0x38(%rbp),%rdi
lea    0xe25(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
jmp    12d9 <escape_predefined_character_entities+0x179>
mov    -0x38(%rbp),%rdi
lea    0xe04(%rip),%rsi
mov    $0x6,%edx
call   1040 <strncpy@plt>
mov    -0x38(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x38(%rbp)
jmp    12d9 <escape_predefined_character_entities+0x179>
mov    -0x38(%rbp),%rdi
lea    0xde5(%rip),%rsi
mov    $0x6,%edx
call   1040 <strncpy@plt>
mov    -0x38(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x38(%rbp)
jmp    12d9 <escape_predefined_character_entities+0x179>
mov    -0x30(%rbp),%rax
mov    (%rax),%cl
mov    -0x38(%rbp),%rax
mov    %cl,(%rax)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    12de <escape_predefined_character_entities+0x17e>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11c6 <escape_predefined_character_entities+0x66>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_predefined_character_entities>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1080 <strdup@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    %eax,%eax
lea    (%rax,%rax,2),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     128b <escape_predefined_character_entities+0x12b>
mov    %rax,%r15
mov    0x0(%r13),%al
mov    %r15,%rbx
test   %al,%al
je     125c <escape_predefined_character_entities+0xfc>
mov    %r13,%r14
add    $0x1,%r14
lea    0xe4f(%rip),%rbp
lea    0xec7(%rip),%r12
mov    %r15,%rbx
jmp    11df <escape_predefined_character_entities+0x7f>
mov    %al,(%rbx)
mov    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %rax,%rbx
movzbl (%r14),%eax
add    $0x1,%r14
test   %al,%al
je     125c <escape_predefined_character_entities+0xfc>
movsbl %al,%ecx
add    $0xffffffde,%ecx
cmp    $0x1c,%ecx
ja     11bd <escape_predefined_character_entities+0x5d>
movslq 0x0(%rbp,%rcx,4),%rcx
add    %rbp,%rcx
jmp    *%rcx
mov    %rbx,%rdi
lea    0xe8d(%rip),%rsi
jmp    1225 <escape_predefined_character_entities+0xc5>
mov    %rbx,%rdi
lea    0xe6a(%rip),%rsi
mov    $0x5,%edx
call   1040 <strncpy@plt>
mov    $0x5,%eax
jmp    11d0 <escape_predefined_character_entities+0x70>
mov    %rbx,%rdi
lea    0xe5f(%rip),%rsi
mov    $0x6,%edx
call   1040 <strncpy@plt>
mov    $0x6,%eax
jmp    11d0 <escape_predefined_character_entities+0x70>
mov    %rbx,%rdi
lea    0xe3a(%rip),%rsi
jmp    1248 <escape_predefined_character_entities+0xe8>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    $0x4,%eax
jmp    11d0 <escape_predefined_character_entities+0x70>
movb   $0x0,(%rbx)
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rsi
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <realloc@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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"&apos;\00"
@.str.4 = [7 x i8] c"&quot;\00"
define i8* @escape_predefined_character_entities(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %3
store i32 6, i32* %4
%10 = load i8*, i8** %3
%11 = call i8* @strdup(i8* %10)
store i8* %11, i8** %5
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
%14 = mul nsw i32 6, %13
%15 = sext i32 %14 to i64
%16 = mul i64 %15, 1
%17 = trunc i64 %16 to i32
%18 = call i8* @malloc(i32 %17)
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = icmp eq i8* null, %19
br i1 %20, label %21, label %22
21: ; preds = %1
store i8* null, i8** %2
br label %81
22: ; preds = %1
%23 = load i8*, i8** %6
store i8* %23, i8** %8
%24 = load i8*, i8** %5
store i8* %24, i8** %7
br label %25
25: ; preds = %66, %22
%26 = load i8*, i8** %7
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %69
30: ; preds = %25
%31 = load i8*, i8** %7
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
switch i32 %33, label %59 [
i32 38, label %34
i32 60, label %39
i32 62, label %44
i32 39, label %49
i32 34, label %54
]
34: ; preds = %30
%35 = load i8*, i8** %8
%36 = call i32 @strncpy(i8* %35, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%37 = load i8*, i8** %8
%38 = getelementptr inbounds i8, i8* %37, i64 5
store i8* %38, i8** %8
br label %65
39: ; preds = %30
%40 = load i8*, i8** %8
%41 = call i32 @strncpy(i8* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%42 = load i8*, i8** %8
%43 = getelementptr inbounds i8, i8* %42, i64 4
store i8* %43, i8** %8
br label %65
44: ; preds = %30
%45 = load i8*, i8** %8
%46 = call i32 @strncpy(i8* %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%47 = load i8*, i8** %8
%48 = getelementptr inbounds i8, i8* %47, i64 4
store i8* %48, i8** %8
br label %65
49: ; preds = %30
%50 = load i8*, i8** %8
%51 = call i32 @strncpy(i8* %50, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%52 = load i8*, i8** %8
%53 = getelementptr inbounds i8, i8* %52, i64 6
store i8* %53, i8** %8
br label %65
54: ; preds = %30
%55 = load i8*, i8** %8
%56 = call i32 @strncpy(i8* %55, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 6)
%57 = load i8*, i8** %8
%58 = getelementptr inbounds i8, i8* %57, i64 6
store i8* %58, i8** %8
br label %65
59: ; preds = %30
%60 = load i8*, i8** %7
%61 = load i8, i8* %60
%62 = load i8*, i8** %8
store i8 %61, i8* %62
%63 = load i8*, i8** %8
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %8
br label %65
65: ; preds = %59, %54, %49, %44, %39, %34
br label %66
66: ; preds = %65
%67 = load i8*, i8** %7
%68 = getelementptr inbounds i8, i8* %67, i32 1
store i8* %68, i8** %7
br label %25
69: ; preds = %25
%70 = load i8*, i8** %8
store i8 0, i8* %70
%71 = load i8*, i8** %5
%72 = call i32 @free(i8* %71)
%73 = load i8*, i8** %6
%74 = call i32 @strlen(i8* %73)
%75 = add nsw i32 %74, 1
%76 = sext i32 %75 to i64
store i64 %76, i64* %9
%77 = load i8*, i8** %6
%78 = load i64, i64* %9
%79 = call i8* @realloc(i8* %77, i64 %78)
store i8* %79, i8** %6
%80 = load i8*, i8** %6
store i8* %80, i8** %2
br label %81
81: ; preds = %69, %21
%82 = load i8*, i8** %2
ret i8* %82
}

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"&apos;\00"
@.str.4 = [7 x i8] c"&quot;\00"
define noalias i8* @escape_predefined_character_entities(i8* %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = tail call i32 @strlen(i8* %0)
%4 = mul nsw i32 %3, 6
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %39, label %7
7: ; preds = %1
%8 = load i8, i8* %2
%9 = icmp eq i8 %8, 0
br i1 %9, label %32, label %10
10: ; preds = %7, %26
%11 = phi i8 [ %30, %26 ], [ %8, %7 ]
%12 = phi i8* [ %28, %26 ], [ %5, %7 ]
%13 = phi i8* [ %29, %26 ], [ %2, %7 ]
%14 = sext i8 %11 to i32
switch i32 %14, label %25 [
i32 38, label %15
i32 60, label %17
i32 62, label %19
i32 39, label %21
i32 34, label %23
]
15: ; preds = %10
%16 = tail call i32 @strncpy(i8* %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
br label %26
17: ; preds = %10
%18 = tail call i32 @strncpy(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
br label %26
19: ; preds = %10
%20 = tail call i32 @strncpy(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
br label %26
21: ; preds = %10
%22 = tail call i32 @strncpy(i8* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
br label %26
23: ; preds = %10
%24 = tail call i32 @strncpy(i8* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 6)
br label %26
25: ; preds = %10
store i8 %11, i8* %12
br label %26
26: ; preds = %15, %17, %19, %21, %23, %25
%27 = phi i64 [ 5, %15 ], [ 4, %17 ], [ 4, %19 ], [ 6, %21 ], [ 6, %23 ], [ 1, %25 ]
%28 = getelementptr inbounds i8, i8* %12, i64 %27
%29 = getelementptr inbounds i8, i8* %13, i64 1
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %10
32: ; preds = %26, %7
%33 = phi i8* [ %5, %7 ], [ %28, %26 ]
store i8 0, i8* %33
%34 = tail call i32 @free(i8* %2)
%35 = tail call i32 @strlen(i8* %5)
%36 = add nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = tail call i8* @realloc(i8* %5, i64 %37)
br label %39
39: ; preds = %1, %32
%40 = phi i8* [ %38, %32 ], [ null, %1 ]
ret i8* %40
}