#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 format1(char *str, char *result) {
  int i;
  char *p;
  p = result;
  for (i = 0; i < (int)strlen(str); i++) {
    switch (str[i]) {
    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):
<format1>:
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    121a <format1+0xfa>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
add    $0xffffffde,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x1c,%eax
ja     11e2 <format1+0xc2>
mov    -0x30(%rbp),%rax
lea    0xe7b(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x20(%rbp),%rdi
lea    0xedb(%rip),%rsi
call   1040 <sprintf@plt>
jmp    11fb <format1+0xdb>
mov    -0x20(%rbp),%rdi
lea    0xecb(%rip),%rsi
call   1040 <sprintf@plt>
jmp    11fb <format1+0xdb>
mov    -0x20(%rbp),%rdi
lea    0xebb(%rip),%rsi
call   1040 <sprintf@plt>
jmp    11fb <format1+0xdb>
mov    -0x20(%rbp),%rdi
lea    0xeac(%rip),%rsi
call   1040 <sprintf@plt>
jmp    11fb <format1+0xdb>
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 <format1+0x1f>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format1>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %eax,%eax
jle    11ce <format1+0xbe>
xor    %ebx,%ebx
lea    0xecc(%rip),%r12
jmp    116e <format1+0x5e>
cs nopw 0x0(%rax,%rax,1)
mov    %al,(%r15)
movb   $0x0,0x1(%r15)
nopl   0x0(%rax,%rax,1)
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    11ce <format1+0xbe>
movsbl (%r14,%rbx,1),%eax
lea    -0x22(%rax),%ecx
cmp    $0x1c,%ecx
ja     1140 <format1+0x30>
movslq (%r12,%rcx,4),%rcx
add    %r12,%rcx
jmp    *%rcx
movl   $0x3b746f,0x3(%r15)
movl   $0x6f757126,(%r15)
jmp    1150 <format1+0x40>
cs nopw 0x0(%rax,%rax,1)
nop
movw   $0x3b,0x4(%r15)
movl   $0x706d6126,(%r15)
jmp    1150 <format1+0x40>
movb   $0x0,0x4(%r15)
movl   $0x3b746c26,(%r15)
jmp    1150 <format1+0x40>
xchg   %ax,%ax
movb   $0x0,0x4(%r15)
movl   $0x3b746726,(%r15)
jmp    1150 <format1+0x40>
movb   $0x0,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"&lt;\00"
@.str.1 = [5 x i8] c"&gt;\00"
@.str.2 = [6 x i8] c"&amp;\00"
@.str.3 = [7 x i8] c"&quot;\00"
define void @format1(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 = %47, %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 %50
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 %33 [
i32 60, label %21
i32 62, label %24
i32 38, label %27
i32 34, label %30
]
21: ; preds = %14
%22 = load i8*, i8** %6
%23 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %42
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 %42
27: ; preds = %14
%28 = load i8*, i8** %6
%29 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %42
30: ; preds = %14
%31 = load i8*, i8** %6
%32 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %42
33: ; preds = %14
%34 = load i8*, i8** %3
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = load i8*, i8** %6
store i8 %38, i8* %39
%40 = load i8*, i8** %6
%41 = getelementptr inbounds i8, i8* %40, i64 1
store i8 0, i8* %41
br label %42
42: ; preds = %33, %30, %27, %24, %21
%43 = load i8*, i8** %6
%44 = call i64 @strlen(i8* %43)
%45 = load i8*, i8** %6
%46 = getelementptr inbounds i8, i8* %45, i64 %44
store i8* %46, i8** %6
br label %47
47: ; preds = %42
%48 = load i32, i32* %5
%49 = add nsw i32 %48, 1
store i32 %49, i32* %5
br label %8
50: ; preds = %8
%51 = load i8*, i8** %6
store i8 0, i8* %51
ret void
}

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