#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 *);
int /*<<< orphan*/ printf(char *, char *);
char *str_replace(char *, char const *, char const *);
char *escape(char *str) {
  printf("", str);
  char *temp1, *temp2;
  int i, numChars = 2;
  temp1 = str;
  const char *echars[] = {"&", "<"};
  const char *rchars[] = {"&amp;", "&lt;"};
  for (i = 0; i < numChars; i++) {
    temp2 = str_replace(temp1, echars[i], rchars[i]);
    if (temp1 != str)
      free(temp1);
    temp1 = temp2;
  }
  return temp1;
}


x86 assembly (clang, -O0):
<escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xeba(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2c9e(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c9b(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c98(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c95(%rip),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11e6 <escape+0xb6>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rsi
movslq -0x1c(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdx
call   1050 <str_replace@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     11d0 <escape+0xa0>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118e <escape+0x5e>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
lea    0xec2(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
lea    0xeb2(%rip),%rsi
lea    0xeaf(%rip),%rdx
mov    %r15,%rdi
call   1050 <str_replace@plt>
mov    %rax,%rbx
lea    0xe9b(%rip),%rsi
lea    0xe9c(%rip),%rdx
mov    %rax,%rdi
call   1050 <str_replace@plt>
mov    %rax,%r14
cmp    %r15,%rbx
je     1186 <escape+0x56>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"&\00"
@.str.2 = [2 x i8] c"<\00"
@__const.escape.echars = [2 x i8*] [i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i32 0, i32 0)]
@.str.3 = [6 x i8] c"&amp;\00"
@.str.4 = [5 x i8] c"&lt;\00"
@__const.escape.rchars = [2 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i32 0, i32 0)]
define i8* @escape(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [2 x i8*]
%8 = alloca [2 x i8*]
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 @printf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %9)
store i32 2, i32* %6
%11 = load i8*, i8** %2
store i8* %11, i8** %3
%12 = bitcast [2 x i8*]* %7 to i8*
%13 = bitcast [2 x i8*]* %8 to i8*
store i32 0, i32* %5
br label %14
14: ; preds = %37, %1
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %40
18: ; preds = %14
%19 = load i8*, i8** %3
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [2 x i8*], [2 x i8*]* %7, i64 0, i64 %21
%23 = load i8*, i8** %22
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [2 x i8*], [2 x i8*]* %8, i64 0, i64 %25
%27 = load i8*, i8** %26
%28 = call i8* @str_replace(i8* %19, i8* %23, i8* %27)
store i8* %28, i8** %4
%29 = load i8*, i8** %3
%30 = load i8*, i8** %2
%31 = icmp ne i8* %29, %30
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = load i8*, i8** %3
%34 = call i32 @free(i8* %33)
br label %35
35: ; preds = %32, %18
%36 = load i8*, i8** %4
store i8* %36, i8** %3
br label %37
37: ; preds = %35
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 1
store i32 %39, i32* %5
br label %14
40: ; preds = %14
%41 = load i8*, i8** %3
ret i8* %41
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"&\00"
@.str.2 = [2 x i8] c"<\00"
@.str.3 = [6 x i8] c"&amp;\00"
@.str.4 = [5 x i8] c"&lt;\00"
define i8* @escape(i8* %0) {
%2 = tail call i32 @printf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = tail call i8* @str_replace(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%4 = tail call i8* @str_replace(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%5 = icmp eq i8* %3, %0
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = tail call i32 @free(i8* %3)
br label %8
8: ; preds = %6, %1
ret i8* %4
}