char *getrateunit(int unit, int index) {
  static char *bunit[] = {"na",     "KiB/s",  "MiB/s", "GiB/s", "TiB/s",
                          "KB/s",   "MB/s",   "GB/s",  "TB/s",  "kbit/s",
                          "Mbit/s", "Gbit/s", "Tbit/s"};
  if (index > 4) {
    return bunit[0];
  } else {
    return bunit[(unit * 4) + index];
  }
}


x86 assembly (clang, -O0):
<getrateunit>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
cmpl   $0x4,-0x10(%rbp)
jle    1124 <getrateunit+0x24>
mov    0x2f15(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    113f <getrateunit+0x3f>
mov    -0xc(%rbp),%eax
shl    $0x2,%eax
add    -0x10(%rbp),%eax
movslq %eax,%rcx
lea    0x2ef9(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<getrateunit>:
cmp    $0x4,%esi
jle    110d <getrateunit+0xd>
lea    0xf28(%rip),%rax
ret
lea    (%rsi,%rdi,4),%eax
cltq
lea    0xee7(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
ret

LLVM IR (-O0):
@getrateunit.bunit = internal global [13 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.12, i32 0, i32 0)]
@.str = [3 x i8] c"na\00"
@.str.1 = [6 x i8] c"KiB/s\00"
@.str.2 = [6 x i8] c"MiB/s\00"
@.str.3 = [6 x i8] c"GiB/s\00"
@.str.4 = [6 x i8] c"TiB/s\00"
@.str.5 = [5 x i8] c"KB/s\00"
@.str.6 = [5 x i8] c"MB/s\00"
@.str.7 = [5 x i8] c"GB/s\00"
@.str.8 = [5 x i8] c"TB/s\00"
@.str.9 = [7 x i8] c"kbit/s\00"
@.str.10 = [7 x i8] c"Mbit/s\00"
@.str.11 = [7 x i8] c"Gbit/s\00"
@.str.12 = [7 x i8] c"Tbit/s\00"
define i8* @getrateunit(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%6 = load i32, i32* %5
%7 = icmp sgt i32 %6, 4
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = load i8*, i8** getelementptr inbounds ([13 x i8*], [13 x i8*]* @getrateunit.bunit, i64 0, i64 0)
store i8* %9, i8** %3
br label %18
10: ; preds = %2
%11 = load i32, i32* %4
%12 = mul nsw i32 %11, 4
%13 = load i32, i32* %5
%14 = add nsw i32 %12, %13
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [13 x i8*], [13 x i8*]* @getrateunit.bunit, i64 0, i64 %15
%17 = load i8*, i8** %16
store i8* %17, i8** %3
br label %18
18: ; preds = %10, %8
%19 = load i8*, i8** %3
ret i8* %19
}

LLVM IR (-O2):
@reltable.getrateunit = internal unnamed_addr constant [13 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str.1 to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str.2 to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str.3 to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str.4 to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.5 to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.6 to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.7 to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.8 to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([7 x i8]* @.str.9 to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([7 x i8]* @.str.10 to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([7 x i8]* @.str.11 to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([7 x i8]* @.str.12 to i64), i64 ptrtoint ([13 x i32]* @reltable.getrateunit to i64)) to i32)]
@.str = [3 x i8] c"na\00"
@.str.1 = [6 x i8] c"KiB/s\00"
@.str.2 = [6 x i8] c"MiB/s\00"
@.str.3 = [6 x i8] c"GiB/s\00"
@.str.4 = [6 x i8] c"TiB/s\00"
@.str.5 = [5 x i8] c"KB/s\00"
@.str.6 = [5 x i8] c"MB/s\00"
@.str.7 = [5 x i8] c"GB/s\00"
@.str.8 = [5 x i8] c"TB/s\00"
@.str.9 = [7 x i8] c"kbit/s\00"
@.str.10 = [7 x i8] c"Mbit/s\00"
@.str.11 = [7 x i8] c"Gbit/s\00"
@.str.12 = [7 x i8] c"Tbit/s\00"
define i8* @getrateunit(i32 %0, i32 %1) {
%3 = icmp sgt i32 %1, 4
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = shl nsw i32 %0, 2
%6 = add nsw i32 %5, %1
%7 = sext i32 %6 to i64
%8 = shl i64 %7, 2
%9 = call i8* @llvm.load.relative.i64(i8* bitcast ([13 x i32]* @reltable.getrateunit to i8*), i64 %8)
br label %10
10: ; preds = %2, %4
%11 = phi i8* [ %9, %4 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %2 ]
ret i8* %11
}