#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ strcat(char *, char *);
size_t strcspn(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, size_t);
char *__replace_symbols2(char *str) {
  char *r = NULL;
  char *s = str;
  int len = strlen(s);
  unsigned toks = 0;
  char *p = s;
  while (p <= s + len) {
    size_t n = strcspn(p, "<>&\"'");
    p += (n + 1);
    toks++;
  }
  if (toks)
    r = (char *)calloc(len + (toks * 6), 1);
  p = s;
  if (r)
    while (p <= s + len) {
      size_t n = strcspn(p, "<>&\"'");
      strncat(r, p, n);
      p += n;
      switch (*p) {
      case '<':
        strcat(r, "&lt;");
        break;
      case '>':
        strcat(r, "&gt;");
        break;
      case '&':
        strcat(r, "&amp;");
        break;
      case '\'':
        strcat(r, "&apos;");
        break;
      case '\"':
        strcat(r, "&quot;");
        break;
      default:
        break;
      }
      p++;
    }
  return (char *)r;
}


x86 assembly (clang, -O0):
<__replace_symbols2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
ja     11d1 <__replace_symbols2+0x81>
mov    -0x28(%rbp),%rdi
lea    0xeca(%rip),%rsi
call   1050 <strcspn@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1187 <__replace_symbols2+0x37>
cmpl   $0x0,-0x20(%rbp)
je     11f2 <__replace_symbols2+0xa2>
mov    -0x1c(%rbp),%edi
imul   $0x6,-0x20(%rbp),%eax
add    %eax,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1307 <__replace_symbols2+0x1b7>
jmp    120a <__replace_symbols2+0xba>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
ja     1302 <__replace_symbols2+0x1b2>
mov    -0x28(%rbp),%rdi
lea    0xe47(%rip),%rsi
call   1050 <strcspn@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <strncat@plt>
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
add    $0xffffffde,%eax
mov    %eax,%ecx
mov    %rcx,-0x40(%rbp)
sub    $0x1c,%eax
ja     12ec <__replace_symbols2+0x19c>
mov    -0x40(%rbp),%rax
lea    0xd86(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   1070 <strcat@plt>
jmp    12f1 <__replace_symbols2+0x1a1>
mov    -0x10(%rbp),%rdi
lea    0xddc(%rip),%rsi
call   1070 <strcat@plt>
jmp    12f1 <__replace_symbols2+0x1a1>
mov    -0x10(%rbp),%rdi
lea    0xdcc(%rip),%rsi
call   1070 <strcat@plt>
jmp    12f1 <__replace_symbols2+0x1a1>
mov    -0x10(%rbp),%rdi
lea    0xdbd(%rip),%rsi
call   1070 <strcat@plt>
jmp    12f1 <__replace_symbols2+0x1a1>
mov    -0x10(%rbp),%rdi
lea    0xdaf(%rip),%rsi
call   1070 <strcat@plt>
jmp    12f1 <__replace_symbols2+0x1a1>
jmp    12f1 <__replace_symbols2+0x1a1>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    120a <__replace_symbols2+0xba>
jmp    1307 <__replace_symbols2+0x1b7>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__replace_symbols2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
xor    %r13d,%r13d
test   %eax,%eax
js     1261 <__replace_symbols2+0x111>
movslq %eax,%r15
add    %r14,%r15
lea    0xef9(%rip),%r12
mov    %eax,0x4(%rsp)
mov    %eax,%ebp
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <strcspn@plt>
add    %rax,%rbx
add    $0x1,%rbx
add    $0xffffffff,%r13d
add    $0x6,%ebp
cmp    %r15,%rbx
jbe    1190 <__replace_symbols2+0x40>
test   %r13d,%r13d
je     125e <__replace_symbols2+0x10e>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r13
test   %rax,%rax
je     1261 <__replace_symbols2+0x111>
cmpl   $0x0,0x4(%rsp)
js     1261 <__replace_symbols2+0x111>
lea    0xe1f(%rip),%r12
jmp    1201 <__replace_symbols2+0xb1>
lea    0xea7(%rip),%rsi
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1070 <strcat@plt>
add    $0x1,%r14
cmp    %r15,%r14
ja     1261 <__replace_symbols2+0x111>
mov    %r14,%rbx
mov    %r14,%rdi
lea    0xe66(%rip),%rsi
call   1050 <strcspn@plt>
mov    %rax,%rbp
mov    %r13,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <strncat@plt>
add    %rbp,%r14
movsbl (%rbx,%rbp,1),%eax
add    $0xffffffde,%eax
cmp    $0x1c,%eax
ja     11f8 <__replace_symbols2+0xa8>
movslq (%r12,%rax,4),%rax
add    %r12,%rax
lea    0xe39(%rip),%rsi
jmp    *%rax
lea    0xe3a(%rip),%rsi
jmp    11f0 <__replace_symbols2+0xa0>
lea    0xe37(%rip),%rsi
jmp    11f0 <__replace_symbols2+0xa0>
lea    0xe23(%rip),%rsi
jmp    11f0 <__replace_symbols2+0xa0>
xor    %r13d,%r13d
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"<>&\22'\00"
@.str.1 = [5 x i8] c"&lt;\00"
@.str.2 = [5 x i8] c"&gt;\00"
@.str.3 = [6 x i8] c"&amp;\00"
@.str.4 = [7 x i8] c"&apos;\00"
@.str.5 = [7 x i8] c"&quot;\00"
define i8* @__replace_symbols2(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = sext i32 %2 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = icmp slt i32 %2, 0
br i1 %5, label %40, label %6
6: ; preds = %1, %6
%7 = phi i32 [ %12, %6 ], [ 0, %1 ]
%8 = phi i8* [ %11, %6 ], [ %0, %1 ]
%9 = tail call i64 @strcspn(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%10 = add i64 %9, 1
%11 = getelementptr inbounds i8, i8* %8, i64 %10
%12 = add i32 %7, 1
%13 = icmp ugt i8* %11, %4
br i1 %13, label %14, label %6
14: ; preds = %6
%15 = icmp eq i32 %12, 0
br i1 %15, label %40, label %16
16: ; preds = %14
%17 = mul i32 %12, 6
%18 = add i32 %17, %2
%19 = tail call i64 @calloc(i32 %18, i32 1)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
%22 = select i1 %21, i1 true, i1 %5
br i1 %22, label %40, label %23
23: ; preds = %16, %37
%24 = phi i8* [ %38, %37 ], [ %0, %16 ]
%25 = tail call i64 @strcspn(i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%26 = tail call i32 @strncat(i8* %20, i8* %24, i64 %25)
%27 = getelementptr inbounds i8, i8* %24, i64 %25
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
switch i32 %29, label %37 [
i32 60, label %34
i32 62, label %30
i32 38, label %31
i32 39, label %32
i32 34, label %33
]
30: ; preds = %23
br label %34
31: ; preds = %23
br label %34
32: ; preds = %23
br label %34
33: ; preds = %23
br label %34
34: ; preds = %23, %30, %31, %32, %33
%35 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %33 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), %32 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %31 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %30 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %23 ]
%36 = tail call i32 @strcat(i8* %20, i8* %35)
br label %37
37: ; preds = %34, %23
%38 = getelementptr inbounds i8, i8* %27, i64 1
%39 = icmp ugt i8* %38, %4
br i1 %39, label %40, label %23
40: ; preds = %37, %16, %1, %14
%41 = phi i8* [ %20, %16 ], [ null, %14 ], [ null, %1 ], [ %20, %37 ]
ret i8* %41
}