const char *macho_strerror(int err) {
  int num;
  num = 0;
  while (err > 0) {
    err >>= 1;
    num++;
  }
  static char *errors[] = {
      "Success",
      "Error opening or reading file",
      "Error mapping file into memory",
      "Error allocating memory",
      "Premature end of data, possibly corrupt file",
      "Not a Mach-O file",
  };
  return errors[num];
}


x86 assembly (clang, -O0):
<macho_strerror>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    112f <macho_strerror+0x2f>
mov    -0x4(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    110e <macho_strerror+0xe>
movslq -0x8(%rbp),%rcx
lea    0x2ef6(%rip),%rax
mov    (%rax,%rcx,8),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<macho_strerror>:
xor    %eax,%eax
test   %edi,%edi
jle    111d <macho_strerror+0x1d>
mov    %edi,%ecx
nopl   0x0(%rax,%rax,1)
shr    %ecx
add    $0x1,%rax
cmp    $0x1,%edi
mov    %ecx,%edi
ja     1110 <macho_strerror+0x10>
lea    0xedc(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
ret

LLVM IR (-O0):
@macho_strerror.errors = internal global [6 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i32 0, i32 0)]
@.str = [8 x i8] c"Success\00"
@.str.1 = [30 x i8] c"Error opening or reading file\00"
@.str.2 = [31 x i8] c"Error mapping file into memory\00"
@.str.3 = [24 x i8] c"Error allocating memory\00"
@.str.4 = [45 x i8] c"Premature end of data, possibly corrupt file\00"
@.str.5 = [18 x i8] c"Not a Mach-O file\00"
define i8* @macho_strerror(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
br label %4
4: ; preds = %7, %1
%5 = load i32, i32* %2
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = load i32, i32* %2
%9 = ashr i32 %8, 1
store i32 %9, i32* %2
%10 = load i32, i32* %3
%11 = add nsw i32 %10, 1
store i32 %11, i32* %3
br label %4
12: ; preds = %4
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [6 x i8*], [6 x i8*]* @macho_strerror.errors, i64 0, i64 %14
%16 = load i8*, i8** %15
ret i8* %16
}

LLVM IR (-O2):
@reltable.macho_strerror = internal unnamed_addr constant [6 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str to i64), i64 ptrtoint ([6 x i32]* @reltable.macho_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([30 x i8]* @.str.1 to i64), i64 ptrtoint ([6 x i32]* @reltable.macho_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([31 x i8]* @.str.2 to i64), i64 ptrtoint ([6 x i32]* @reltable.macho_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([24 x i8]* @.str.3 to i64), i64 ptrtoint ([6 x i32]* @reltable.macho_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([45 x i8]* @.str.4 to i64), i64 ptrtoint ([6 x i32]* @reltable.macho_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([18 x i8]* @.str.5 to i64), i64 ptrtoint ([6 x i32]* @reltable.macho_strerror to i64)) to i32)]
@.str = [8 x i8] c"Success\00"
@.str.1 = [30 x i8] c"Error opening or reading file\00"
@.str.2 = [31 x i8] c"Error mapping file into memory\00"
@.str.3 = [24 x i8] c"Error allocating memory\00"
@.str.4 = [45 x i8] c"Premature end of data, possibly corrupt file\00"
@.str.5 = [18 x i8] c"Not a Mach-O file\00"
define i8* @macho_strerror(i32 %0) {
%2 = icmp sgt i32 %0, 0
br i1 %2, label %3, label %11
3: ; preds = %1, %3
%4 = phi i32 [ %7, %3 ], [ 0, %1 ]
%5 = phi i32 [ %6, %3 ], [ %0, %1 ]
%6 = lshr i32 %5, 1
%7 = add nuw nsw i32 %4, 1
%8 = icmp ult i32 %5, 2
br i1 %8, label %9, label %3
9: ; preds = %3
%10 = zext i32 %7 to i64
br label %11
11: ; preds = %9, %1
%12 = phi i64 [ 0, %1 ], [ %10, %9 ]
%13 = shl i64 %12, 2
%14 = call i8* @llvm.load.relative.i64(i8* bitcast ([6 x i32]* @reltable.macho_strerror to i8*), i64 %13)
ret i8* %14
}