char *print_prot(int prot) {
  char *prot_array[8] = {
      "PROT_NONE",
      "PROT_READ",
      "PROT_WRITE",
      "PROT_READ|PROT_WRITE",
      "PROT_EXEC",
      "PROT_EXEC|PROT_READ",
      "PROT_EXEC|PROT_WRITE",
      "PROT_EXEC|PROT_READ|PROT_WRITE",
  };
  prot &= 7;
  return prot_array[prot];
}


x86 assembly (clang, -O0):
<print_prot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
lea    0x2cba(%rip),%rsi
mov    $0x40,%edx
call   1030 <memcpy@plt>
mov    -0x4(%rbp),%eax
and    $0x7,%eax
mov    %eax,-0x4(%rbp)
movslq -0x4(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_prot>:
and    $0x7,%edi
lea    0xf52(%rip),%rcx
movslq (%rcx,%rdi,4),%rax
add    %rcx,%rax
ret

LLVM IR (-O0):
@.str = [10 x i8] c"PROT_NONE\00"
@.str.1 = [10 x i8] c"PROT_READ\00"
@.str.2 = [11 x i8] c"PROT_WRITE\00"
@.str.3 = [21 x i8] c"PROT_READ|PROT_WRITE\00"
@.str.4 = [10 x i8] c"PROT_EXEC\00"
@.str.5 = [20 x i8] c"PROT_EXEC|PROT_READ\00"
@.str.6 = [21 x i8] c"PROT_EXEC|PROT_WRITE\00"
@.str.7 = [31 x i8] c"PROT_EXEC|PROT_READ|PROT_WRITE\00"
@__const.print_prot.prot_array = [8 x i8*] [i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i32 0, i32 0)]
define i8* @print_prot(i32 %0) {
%2 = alloca i32
%3 = alloca [8 x i8*]
store i32 %0, i32* %2
%4 = bitcast [8 x i8*]* %3 to i8*
%5 = load i32, i32* %2
%6 = and i32 %5, 7
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds [8 x i8*], [8 x i8*]* %3, i64 0, i64 %8
%10 = load i8*, i8** %9
ret i8* %10
}

LLVM IR (-O2):
@.str = [10 x i8] c"PROT_NONE\00"
@.str.1 = [10 x i8] c"PROT_READ\00"
@.str.2 = [11 x i8] c"PROT_WRITE\00"
@.str.3 = [21 x i8] c"PROT_READ|PROT_WRITE\00"
@.str.4 = [10 x i8] c"PROT_EXEC\00"
@.str.5 = [20 x i8] c"PROT_EXEC|PROT_READ\00"
@.str.6 = [21 x i8] c"PROT_EXEC|PROT_WRITE\00"
@.str.7 = [31 x i8] c"PROT_EXEC|PROT_READ|PROT_WRITE\00"
@reltable.print_prot = [8 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([10 x i8]* @.str to i64), i64 ptrtoint ([8 x i32]* @reltable.print_prot to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([10 x i8]* @.str.1 to i64), i64 ptrtoint ([8 x i32]* @reltable.print_prot to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([11 x i8]* @.str.2 to i64), i64 ptrtoint ([8 x i32]* @reltable.print_prot to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([21 x i8]* @.str.3 to i64), i64 ptrtoint ([8 x i32]* @reltable.print_prot to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([10 x i8]* @.str.4 to i64), i64 ptrtoint ([8 x i32]* @reltable.print_prot to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([20 x i8]* @.str.5 to i64), i64 ptrtoint ([8 x i32]* @reltable.print_prot to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([21 x i8]* @.str.6 to i64), i64 ptrtoint ([8 x i32]* @reltable.print_prot to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([31 x i8]* @.str.7 to i64), i64 ptrtoint ([8 x i32]* @reltable.print_prot to i64)) to i32)]
define i8* @print_prot(i32 %0) {
%2 = and i32 %0, 7
%3 = zext i32 %2 to i64
%4 = shl i64 %3, 2
%5 = call i8* @llvm.load.relative.i64(i8* bitcast ([8 x i32]* @reltable.print_prot to i8*), i64 %4)
ret i8* %5
}