#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 */
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
char *xml_escape(const char *str) {
  int len = 0;
  for (const char *cur = str; *cur; ++cur) {
    if (*cur == '&') {
      len += 5;
    } else if (*cur == '<' || *cur == '>') {
      len += 4;
    } else {
      len++;
    }
  }
  char *result = malloc(len + 1);
  char *end = result;
  for (const char *cur = str; *cur; ++cur) {
    if (*cur == '&') {
      strcpy(end, "&amp;");
      end += 5;
    } else if (*cur == '<') {
      strcpy(end, "&lt;");
      end += 4;
    } else if (*cur == '>') {
      strcpy(end, "&gt;");
      end += 4;
    } else {
      *end++ = *cur;
    }
  }
  *end = 0;
  return result;
}


x86 assembly (clang, -O0):
<xml_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11b8 <xml_escape+0x98>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x26,%eax
jne    1166 <xml_escape+0x46>
mov    -0xc(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0xc(%rbp)
jmp    11a2 <xml_escape+0x82>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3c,%eax
je     1186 <xml_escape+0x66>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3e,%eax
jne    1194 <xml_escape+0x74>
mov    -0xc(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0xc(%rbp)
jmp    119d <xml_escape+0x7d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a2 <xml_escape+0x82>
jmp    11a7 <xml_escape+0x87>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    113b <xml_escape+0x1b>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     12ae <xml_escape+0x18e>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x26,%eax
jne    1215 <xml_escape+0xf5>
mov    -0x28(%rbp),%rdi
lea    0xe01(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x28(%rbp)
jmp    1298 <xml_escape+0x178>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3c,%eax
jne    1246 <xml_escape+0x126>
mov    -0x28(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
jmp    1293 <xml_escape+0x173>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3e,%eax
jne    1277 <xml_escape+0x157>
mov    -0x28(%rbp),%rdi
lea    0xdaa(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
jmp    128e <xml_escape+0x16e>
mov    -0x30(%rbp),%rax
mov    (%rax),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
jmp    1293 <xml_escape+0x173>
jmp    1298 <xml_escape+0x178>
jmp    129d <xml_escape+0x17d>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11d7 <xml_escape+0xb7>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xml_escape>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
xor    %edi,%edi
mov    %r13,%rax
jmp    114b <xml_escape+0x2b>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x4,%edx
add    %edx,%edi
add    $0x1,%rax
movzbl (%rax),%ecx
cmp    $0x3b,%cl
jle    1160 <xml_escape+0x40>
cmp    $0x3c,%cl
je     1140 <xml_escape+0x20>
cmp    $0x3e,%cl
je     1140 <xml_escape+0x20>
jmp    116e <xml_escape+0x4e>
nop
mov    $0x5,%edx
cmp    $0x26,%cl
je     1145 <xml_escape+0x25>
test   %cl,%cl
je     1175 <xml_escape+0x55>
mov    $0x1,%edx
jmp    1145 <xml_escape+0x25>
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
lea    0xe79(%rip),%r15
lea    0xe78(%rip),%r12
lea    0xe76(%rip),%rbp
mov    %rax,%rbx
jmp    11b3 <xml_escape+0x93>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
add    $0x4,%rbx
add    $0x1,%r13
movzbl 0x0(%r13),%eax
cmp    $0x3b,%al
jle    11d0 <xml_escape+0xb0>
cmp    $0x3c,%al
je     11a0 <xml_escape+0x80>
cmp    $0x3e,%al
jne    11ed <xml_escape+0xcd>
mov    %rbx,%rdi
mov    %rbp,%rsi
jmp    11a6 <xml_escape+0x86>
nopl   0x0(%rax)
cmp    $0x26,%al
jne    11e9 <xml_escape+0xc9>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
add    $0x5,%rbx
add    $0x1,%r13
jmp    11b3 <xml_escape+0x93>
test   %al,%al
je     11f9 <xml_escape+0xd9>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0x1,%r13
jmp    11b3 <xml_escape+0x93>
movb   $0x0,(%rbx)
mov    %r14,%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"&amp;\00"
@.str.1 = [5 x i8] c"&lt;\00"
@.str.2 = [5 x i8] c"&gt;\00"
define i8* @xml_escape(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
%8 = load i8*, i8** %2
store i8* %8, i8** %4
br label %9
9: ; preds = %39, %1
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = icmp ne i8 %11, 0
br i1 %12, label %13, label %42
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 38
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* %3
%20 = add nsw i32 %19, 5
store i32 %20, i32* %3
br label %38
21: ; preds = %13
%22 = load i8*, i8** %4
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 60
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = load i8*, i8** %4
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 62
br i1 %30, label %31, label %34
31: ; preds = %26, %21
%32 = load i32, i32* %3
%33 = add nsw i32 %32, 4
store i32 %33, i32* %3
br label %37
34: ; preds = %26
%35 = load i32, i32* %3
%36 = add nsw i32 %35, 1
store i32 %36, i32* %3
br label %37
37: ; preds = %34, %31
br label %38
38: ; preds = %37, %18
br label %39
39: ; preds = %38
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %4
br label %9
42: ; preds = %9
%43 = load i32, i32* %3
%44 = add nsw i32 %43, 1
%45 = call i8* @malloc(i32 %44)
store i8* %45, i8** %5
%46 = load i8*, i8** %5
store i8* %46, i8** %6
%47 = load i8*, i8** %2
store i8* %47, i8** %7
br label %48
48: ; preds = %90, %42
%49 = load i8*, i8** %7
%50 = load i8, i8* %49
%51 = icmp ne i8 %50, 0
br i1 %51, label %52, label %93
52: ; preds = %48
%53 = load i8*, i8** %7
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 38
br i1 %56, label %57, label %62
57: ; preds = %52
%58 = load i8*, i8** %6
%59 = call i32 @strcpy(i8* %58, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%60 = load i8*, i8** %6
%61 = getelementptr inbounds i8, i8* %60, i64 5
store i8* %61, i8** %6
br label %89
62: ; preds = %52
%63 = load i8*, i8** %7
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = icmp eq i32 %65, 60
br i1 %66, label %67, label %72
67: ; preds = %62
%68 = load i8*, i8** %6
%69 = call i32 @strcpy(i8* %68, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%70 = load i8*, i8** %6
%71 = getelementptr inbounds i8, i8* %70, i64 4
store i8* %71, i8** %6
br label %88
72: ; preds = %62
%73 = load i8*, i8** %7
%74 = load i8, i8* %73
%75 = sext i8 %74 to i32
%76 = icmp eq i32 %75, 62
br i1 %76, label %77, label %82
77: ; preds = %72
%78 = load i8*, i8** %6
%79 = call i32 @strcpy(i8* %78, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%80 = load i8*, i8** %6
%81 = getelementptr inbounds i8, i8* %80, i64 4
store i8* %81, i8** %6
br label %87
82: ; preds = %72
%83 = load i8*, i8** %7
%84 = load i8, i8* %83
%85 = load i8*, i8** %6
%86 = getelementptr inbounds i8, i8* %85, i32 1
store i8* %86, i8** %6
store i8 %84, i8* %85
br label %87
87: ; preds = %82, %77
br label %88
88: ; preds = %87, %67
br label %89
89: ; preds = %88, %57
br label %90
90: ; preds = %89
%91 = load i8*, i8** %7
%92 = getelementptr inbounds i8, i8* %91, i32 1
store i8* %92, i8** %7
br label %48
93: ; preds = %48
%94 = load i8*, i8** %6
store i8 0, i8* %94
%95 = load i8*, i8** %5
ret i8* %95
}

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"
define i8* @xml_escape(i8* nocapture %0) {
br label %2
2: ; preds = %11, %1
%3 = phi i8* [ %0, %1 ], [ %14, %11 ]
%4 = phi i32 [ 0, %1 ], [ %13, %11 ]
%5 = load i8, i8* %3
switch i8 %5, label %10 [
i8 0, label %6
i8 38, label %11
i8 60, label %9
i8 62, label %9
]
6: ; preds = %2
%7 = add nuw nsw i32 %4, 1
%8 = tail call i8* @malloc(i32 %7)
br label %15
9: ; preds = %2, %2
br label %11
10: ; preds = %2
br label %11
11: ; preds = %2, %10, %9
%12 = phi i32 [ 1, %10 ], [ 4, %9 ], [ 5, %2 ]
%13 = add nuw nsw i32 %4, %12
%14 = getelementptr inbounds i8, i8* %3, i64 1
br label %2
15: ; preds = %31, %6
%16 = phi i8* [ %8, %6 ], [ %32, %31 ]
%17 = phi i8* [ %0, %6 ], [ %33, %31 ]
%18 = load i8, i8* %17
switch i8 %18, label %29 [
i8 0, label %19
i8 38, label %20
i8 60, label %23
i8 62, label %26
]
19: ; preds = %15
store i8 0, i8* %16
ret i8* %8
20: ; preds = %15
%21 = tail call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%22 = getelementptr inbounds i8, i8* %16, i64 5
br label %31
23: ; preds = %15
%24 = tail call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = getelementptr inbounds i8, i8* %16, i64 4
br label %31
26: ; preds = %15
%27 = tail call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%28 = getelementptr inbounds i8, i8* %16, i64 4
br label %31
29: ; preds = %15
%30 = getelementptr inbounds i8, i8* %16, i64 1
store i8 %18, i8* %16
br label %31
31: ; preds = %20, %26, %29, %23
%32 = phi i8* [ %22, %20 ], [ %25, %23 ], [ %28, %26 ], [ %30, %29 ]
%33 = getelementptr inbounds i8, i8* %17, i64 1
br label %15
}