const char *elf_type(unsigned char code) {
  if (code == 0)
    return "NONE";
  else if (code == 1)
    return "REL (Relocatable file)";
  else if (code == 2)
    return "EXEC (Executable file)";
  else if (code == 3)
    return "DYN (Shared object file)";
  else if (code == 4)
    return "CORE (Core dump file)";
  else
    return "Unkown";
}


x86 assembly (clang, -O0):
<elf_type>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x9(%rbp)
movzbl -0x9(%rbp),%eax
cmp    $0x0,%eax
jne    1127 <elf_type+0x27>
lea    0xee2(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a6 <elf_type+0xa6>
movzbl -0x9(%rbp),%eax
cmp    $0x1,%eax
jne    1144 <elf_type+0x44>
lea    0xeca(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a6 <elf_type+0xa6>
movzbl -0x9(%rbp),%eax
cmp    $0x2,%eax
jne    1161 <elf_type+0x61>
lea    0xec4(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a6 <elf_type+0xa6>
movzbl -0x9(%rbp),%eax
cmp    $0x3,%eax
jne    117e <elf_type+0x7e>
lea    0xebe(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a6 <elf_type+0xa6>
movzbl -0x9(%rbp),%eax
cmp    $0x4,%eax
jne    119b <elf_type+0x9b>
lea    0xeba(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a6 <elf_type+0xa6>
lea    0xec0(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<elf_type>:
cmp    $0x4,%dil
ja     1119 <elf_type+0x19>
movsbq %dil,%rax
lea    0xf5b(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
ret
lea    0xf42(%rip),%rax
ret

LLVM IR (-O0):
@.str = [5 x i8] c"NONE\00"
@.str.1 = [23 x i8] c"REL (Relocatable file)\00"
@.str.2 = [23 x i8] c"EXEC (Executable file)\00"
@.str.3 = [25 x i8] c"DYN (Shared object file)\00"
@.str.4 = [22 x i8] c"CORE (Core dump file)\00"
@.str.5 = [7 x i8] c"Unkown\00"
define i8* @elf_type(i8 zeroext %0) {
%2 = alloca i8*
%3 = alloca i8
store i8 %0, i8* %3
%4 = load i8, i8* %3
%5 = zext i8 %4 to i32
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %2
br label %29
8: ; preds = %1
%9 = load i8, i8* %3
%10 = zext i8 %9 to i32
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %13
12: ; preds = %8
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %29
13: ; preds = %8
%14 = load i8, i8* %3
%15 = zext i8 %14 to i32
%16 = icmp eq i32 %15, 2
br i1 %16, label %17, label %18
17: ; preds = %13
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %29
18: ; preds = %13
%19 = load i8, i8* %3
%20 = zext i8 %19 to i32
%21 = icmp eq i32 %20, 3
br i1 %21, label %22, label %23
22: ; preds = %18
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %29
23: ; preds = %18
%24 = load i8, i8* %3
%25 = zext i8 %24 to i32
%26 = icmp eq i32 %25, 4
br i1 %26, label %27, label %28
27: ; preds = %23
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8** %2
br label %29
28: ; preds = %23
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8** %2
br label %29
29: ; preds = %28, %27, %22, %17, %12, %7
%30 = load i8*, i8** %2
ret i8* %30
}

LLVM IR (-O2):
@.str = [5 x i8] c"NONE\00"
@.str.1 = [23 x i8] c"REL (Relocatable file)\00"
@.str.2 = [23 x i8] c"EXEC (Executable file)\00"
@.str.3 = [25 x i8] c"DYN (Shared object file)\00"
@.str.4 = [22 x i8] c"CORE (Core dump file)\00"
@.str.5 = [7 x i8] c"Unkown\00"
@reltable.elf_type = [5 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str to i64), i64 ptrtoint ([5 x i32]* @reltable.elf_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([23 x i8]* @.str.1 to i64), i64 ptrtoint ([5 x i32]* @reltable.elf_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([23 x i8]* @.str.2 to i64), i64 ptrtoint ([5 x i32]* @reltable.elf_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([25 x i8]* @.str.3 to i64), i64 ptrtoint ([5 x i32]* @reltable.elf_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([22 x i8]* @.str.4 to i64), i64 ptrtoint ([5 x i32]* @reltable.elf_type to i64)) to i32)]
define i8* @elf_type(i8 zeroext %0) {
%2 = icmp ult i8 %0, 5
br i1 %2, label %3, label %7
3: ; preds = %1
%4 = sext i8 %0 to i64
%5 = shl i64 %4, 2
%6 = call i8* @llvm.load.relative.i64(i8* bitcast ([5 x i32]* @reltable.elf_type to i8*), i64 %5)
br label %7
7: ; preds = %1, %3
%8 = phi i8* [ %6, %3 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %1 ]
ret i8* %8
}