const char *mp_error_to_string(int code) {
  switch (code) {
  case 0:
    return "Successful";
  case -2:
    return "Out of heap";
  case -3:
    return "Value out of range";
  case -4:
    return "Max. iterations reached";
  default:
    return "Invalid error code";
  }
}


x86 assembly (clang, -O0):
<mp_error_to_string>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x4,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x4,%eax
ja     1170 <mp_error_to_string+0x70>
mov    -0x18(%rbp),%rax
lea    0xed9(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xedd(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    117b <mp_error_to_string+0x7b>
lea    0xed8(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    117b <mp_error_to_string+0x7b>
lea    0xed4(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    117b <mp_error_to_string+0x7b>
lea    0xed7(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    117b <mp_error_to_string+0x7b>
lea    0xedf(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<mp_error_to_string>:
add    $0x4,%edi
cmp    $0x4,%edi
ja     111a <mp_error_to_string+0x1a>
movslq %edi,%rax
lea    0xf46(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
ret
lea    0xf21(%rip),%rax
ret

LLVM IR (-O0):
@.str = [11 x i8] c"Successful\00"
@.str.1 = [12 x i8] c"Out of heap\00"
@.str.2 = [19 x i8] c"Value out of range\00"
@.str.3 = [24 x i8] c"Max. iterations reached\00"
@.str.4 = [19 x i8] c"Invalid error code\00"
define i8* @mp_error_to_string(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
switch i32 %4, label %9 [
i32 0, label %5
i32 -2, label %6
i32 -3, label %7
i32 -4, label %8
]
5: ; preds = %1
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %2
br label %10
6: ; preds = %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %10
7: ; preds = %1
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %10
8: ; preds = %1
store i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %10
9: ; preds = %1
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8** %2
br label %10
10: ; preds = %9, %8, %7, %6, %5
%11 = load i8*, i8** %2
ret i8* %11
}

LLVM IR (-O2):
@.str = [11 x i8] c"Successful\00"
@.str.1 = [12 x i8] c"Out of heap\00"
@.str.2 = [19 x i8] c"Value out of range\00"
@.str.3 = [24 x i8] c"Max. iterations reached\00"
@.str.4 = [19 x i8] c"Invalid error code\00"
@reltable.mp_error_to_string = [5 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([24 x i8]* @.str.3 to i64), i64 ptrtoint ([5 x i32]* @reltable.mp_error_to_string to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([19 x i8]* @.str.2 to i64), i64 ptrtoint ([5 x i32]* @reltable.mp_error_to_string to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([12 x i8]* @.str.1 to i64), i64 ptrtoint ([5 x i32]* @reltable.mp_error_to_string to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([19 x i8]* @.str.4 to i64), i64 ptrtoint ([5 x i32]* @reltable.mp_error_to_string to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([11 x i8]* @.str to i64), i64 ptrtoint ([5 x i32]* @reltable.mp_error_to_string to i64)) to i32)]
define i8* @mp_error_to_string(i32 %0) {
%2 = add i32 %0, 4
%3 = icmp ult i32 %2, 5
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = sext i32 %2 to i64
%6 = shl i64 %5, 2
%7 = call i8* @llvm.load.relative.i64(i8* bitcast ([5 x i32]* @reltable.mp_error_to_string to i8*), i64 %6)
br label %8
8: ; preds = %1, %4
%9 = phi i8* [ %7, %4 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), %1 ]
ret i8* %9
}