#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*/ sprintf(char *, char *);
scalar_t__ strlen(char *);
void format(char *str, char *result) {
  int i;
  char *p;
  p = result;
  for (i = 0; i < (int)strlen(str); i++) {
    switch (str[i]) {
    case '\n':
      sprintf(p, "<br>\n");
      break;
    case '<':
      sprintf(p, "&lt;");
      break;
    case '>':
      sprintf(p, "&gt;");
      break;
    case '&':
      sprintf(p, "&amp;");
      break;
    case '\"':
      sprintf(p, "&quot;");
      break;
    default:
      *p = str[i];
      *(p + 1) = 0;
    }
    p += strlen(p);
  }
  *p = 0;
}


x86 assembly (clang, -O0):
<format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jge    125e <format+0x13e>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x28(%rbp)
sub    $0xa,%eax
je     11bd <format+0x9d>
jmp    1179 <format+0x59>
mov    -0x28(%rbp),%eax
sub    $0x22,%eax
je     1211 <format+0xf1>
jmp    118a <format+0x6a>
mov    -0x28(%rbp),%eax
sub    $0x26,%eax
je     11fc <format+0xdc>
jmp    119b <format+0x7b>
mov    -0x28(%rbp),%eax
sub    $0x3c,%eax
je     11d2 <format+0xb2>
jmp    11ac <format+0x8c>
mov    -0x28(%rbp),%eax
sub    $0x3e,%eax
je     11e7 <format+0xc7>
jmp    1226 <format+0x106>
mov    -0x20(%rbp),%rdi
lea    0xe38(%rip),%rsi
call   1040 <sprintf@plt>
jmp    123f <format+0x11f>
mov    -0x20(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1040 <sprintf@plt>
jmp    123f <format+0x11f>
mov    -0x20(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   1040 <sprintf@plt>
jmp    123f <format+0x11f>
mov    -0x20(%rbp),%rdi
lea    0xe09(%rip),%rsi
call   1040 <sprintf@plt>
jmp    123f <format+0x11f>
mov    -0x20(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1040 <sprintf@plt>
jmp    123f <format+0x11f>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    113f <format+0x1f>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %eax,%eax
jle    11d7 <format+0xc7>
xor    %ebx,%ebx
lea    0xecc(%rip),%r12
jmp    115e <format+0x4e>
mov    %al,(%r15)
movb   $0x0,0x1(%r15)
xchg   %ax,%ax
mov    %r15,%rdi
call   1030 <strlen@plt>
add    %rax,%r15
add    $0x1,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jge    11d7 <format+0xc7>
movzbl (%r14,%rbx,1),%eax
movsbl %al,%ecx
lea    -0x22(%rcx),%edx
cmp    $0x1c,%edx
ja     1190 <format+0x80>
movslq (%r12,%rdx,4),%rcx
add    %r12,%rcx
jmp    *%rcx
movl   $0x3b746f,0x3(%r15)
movl   $0x6f757126,(%r15)
jmp    1140 <format+0x30>
nopl   0x0(%rax,%rax,1)
cmp    $0xa,%ecx
jne    1136 <format+0x26>
movw   $0xa,0x4(%r15)
movl   $0x3e72623c,(%r15)
jmp    1140 <format+0x30>
movw   $0x3b,0x4(%r15)
movl   $0x706d6126,(%r15)
jmp    1140 <format+0x30>
movb   $0x0,0x4(%r15)
movl   $0x3b746c26,(%r15)
jmp    1140 <format+0x30>
movb   $0x0,0x4(%r15)
movl   $0x3b746726,(%r15)
jmp    1140 <format+0x30>
movb   $0x0,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"<br>\0A\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"&quot;\00"
define void @format(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
store i8* %7, i8** %6
store i32 0, i32* %5
br label %8
8: ; preds = %50, %2
%9 = load i32, i32* %5
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
%12 = trunc i64 %11 to i32
%13 = icmp slt i32 %9, %12
br i1 %13, label %14, label %53
14: ; preds = %8
%15 = load i8*, i8** %3
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
switch i32 %20, label %36 [
i32 10, label %21
i32 60, label %24
i32 62, label %27
i32 38, label %30
i32 34, label %33
]
21: ; preds = %14
%22 = load i8*, i8** %6
%23 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %45
24: ; preds = %14
%25 = load i8*, i8** %6
%26 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %45
27: ; preds = %14
%28 = load i8*, i8** %6
%29 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %45
30: ; preds = %14
%31 = load i8*, i8** %6
%32 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %45
33: ; preds = %14
%34 = load i8*, i8** %6
%35 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %45
36: ; preds = %14
%37 = load i8*, i8** %3
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i8, i8* %40
%42 = load i8*, i8** %6
store i8 %41, i8* %42
%43 = load i8*, i8** %6
%44 = getelementptr inbounds i8, i8* %43, i64 1
store i8 0, i8* %44
br label %45
45: ; preds = %36, %33, %30, %27, %24, %21
%46 = load i8*, i8** %6
%47 = call i64 @strlen(i8* %46)
%48 = load i8*, i8** %6
%49 = getelementptr inbounds i8, i8* %48, i64 %47
store i8* %49, i8** %6
br label %50
50: ; preds = %45
%51 = load i32, i32* %5
%52 = add nsw i32 %51, 1
store i32 %52, i32* %5
br label %8
53: ; preds = %8
%54 = load i8*, i8** %6
store i8 0, i8* %54
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"<br>\0A\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"&quot;\00"
define void @format(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = trunc i64 %3 to i32
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %27
6: ; preds = %2, %19
%7 = phi i64 [ %22, %19 ], [ 0, %2 ]
%8 = phi i8* [ %21, %19 ], [ %1, %2 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %7
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
switch i32 %11, label %17 [
i32 10, label %12
i32 60, label %13
i32 62, label %14
i32 38, label %15
i32 34, label %16
]
12: ; preds = %6
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(6) %8, i8* align 1 dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 6, i1 false)
br label %19
13: ; preds = %6
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(5) %8, i8* align 1 dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 5, i1 false)
br label %19
14: ; preds = %6
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(5) %8, i8* align 1 dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 5, i1 false)
br label %19
15: ; preds = %6
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(6) %8, i8* align 1 dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 6, i1 false)
br label %19
16: ; preds = %6
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(7) %8, i8* align 1 dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i64 7, i1 false)
br label %19
17: ; preds = %6
store i8 %10, i8* %8
%18 = getelementptr inbounds i8, i8* %8, i64 1
store i8 0, i8* %18
br label %19
19: ; preds = %17, %16, %15, %14, %13, %12
%20 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%21 = getelementptr inbounds i8, i8* %8, i64 %20
%22 = add nuw nsw i64 %7, 1
%23 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%24 = shl i64 %23, 32
%25 = ashr exact i64 %24, 32
%26 = icmp slt i64 %22, %25
br i1 %26, label %6, label %27
27: ; preds = %19, %2
%28 = phi i8* [ %1, %2 ], [ %21, %19 ]
store i8 0, i8* %28
ret void
}