char *xml_encode(char ch) {
  static char text[2];
  switch (ch) {
  case '&':
    return ("&amp;");
    break;
  case '>':
    return ("&gt;");
    break;
  case '<':
    return ("&lt;");
    break;
  case '\"':
    return ("&quot;");
    break;
  case '\'':
    return ("&apos;");
    break;
  default:
    text[0] = ch;
    text[1] = 0;
    return text;
    break;
  }
}


x86 assembly (clang, -O0):
<xml_encode>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
add    $0xffffffde,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x1c,%eax
ja     1184 <xml_encode+0x84>
mov    -0x18(%rbp),%rax
lea    0xed5(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xf39(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <xml_encode+0x9f>
lea    0xf2f(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <xml_encode+0x9f>
lea    0xf24(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <xml_encode+0x9f>
lea    0xf19(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <xml_encode+0x9f>
lea    0xf10(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <xml_encode+0x9f>
mov    -0x9(%rbp),%al
mov    %al,0x2e94(%rip)
movb   $0x0,0x2e8e(%rip)
lea    0x2e86(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<xml_encode>:
lea    -0x22(%rdi),%ecx
cmp    $0x1c,%ecx
ja     1127 <xml_encode+0x27>
lea    0xf65(%rip),%rax
lea    0xeea(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
lea    0xf5e(%rip),%rax
ret
mov    %dil,0x2ef3(%rip)
lea    0x2eec(%rip),%rax
movb   $0x0,0x2ee6(%rip)
ret
lea    0xf47(%rip),%rax
ret
lea    0xf33(%rip),%rax
ret
lea    0xf26(%rip),%rax
ret

LLVM IR (-O0):
@xml_encode.text = internal global [2 x i8] zeroinitializer
@.str = [6 x i8] c"&amp;\00"
@.str.1 = [5 x i8] c"&gt;\00"
@.str.2 = [5 x i8] c"&lt;\00"
@.str.3 = [7 x i8] c"&quot;\00"
@.str.4 = [7 x i8] c"&apos;\00"
define i8* @xml_encode(i8 signext %0) {
%2 = alloca i8*
%3 = alloca i8
store i8 %0, i8* %3
%4 = load i8, i8* %3
%5 = sext i8 %4 to i32
switch i32 %5, label %11 [
i32 38, label %6
i32 62, label %7
i32 60, label %8
i32 34, label %9
i32 39, label %10
]
6: ; preds = %1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %2
br label %13
7: ; preds = %1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %13
8: ; preds = %1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %13
9: ; preds = %1
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %13
10: ; preds = %1
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8** %2
br label %13
11: ; preds = %1
%12 = load i8, i8* %3
store i8 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @xml_encode.text, i64 0, i64 0)
store i8 0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @xml_encode.text, i64 0, i64 1)
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @xml_encode.text, i64 0, i64 0), i8** %2
br label %13
13: ; preds = %11, %10, %9, %8, %7, %6
%14 = load i8*, i8** %2
ret i8* %14
}

LLVM IR (-O2):
@xml_encode.text = internal global [2 x i8] zeroinitializer
@.str = [6 x i8] c"&amp;\00"
@.str.1 = [5 x i8] c"&gt;\00"
@.str.2 = [5 x i8] c"&lt;\00"
@.str.3 = [7 x i8] c"&quot;\00"
@.str.4 = [7 x i8] c"&apos;\00"
define i8* @xml_encode(i8 signext %0) {
%2 = sext i8 %0 to i32
switch i32 %2, label %7 [
i32 38, label %8
i32 62, label %3
i32 60, label %4
i32 34, label %5
i32 39, label %6
]
3: ; preds = %1
br label %8
4: ; preds = %1
br label %8
5: ; preds = %1
br label %8
6: ; preds = %1
br label %8
7: ; preds = %1
store i8 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @xml_encode.text, i64 0, i64 0)
store i8 0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @xml_encode.text, i64 0, i64 1)
br label %8
8: ; preds = %1, %7, %6, %5, %4, %3
%9 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @xml_encode.text, i64 0, i64 0), %7 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), %6 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), %5 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %4 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %3 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %1 ]
ret i8* %9
}