const char *cardinal(float course) {
  static const char *directions[] = {"N",  "NNE", "NE", "ENE", "E",  "ESE",
                                     "SE", "SSE", "S",  "SSW", "SW", "WSW",
                                     "W",  "WNW", "NW", "NNW"};
  int direction = (int)((course + 11.25f) / 22.5f);
  return directions[direction % 16];
}


x86 assembly (clang, -O0):
<cardinal>:
push   %rbp
mov    %rsp,%rbp
movss  %xmm0,-0x4(%rbp)
movss  0xef3(%rip),%xmm0
addss  -0x4(%rbp),%xmm0
movss  0xee2(%rip),%xmm1
divss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
movslq %edx,%rcx
lea    0x2ef2(%rip),%rax
mov    (%rax,%rcx,8),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<cardinal>:
addss  0xef8(%rip),%xmm0
divss  0xef4(%rip),%xmm0
cvttss2si %xmm0,%eax
lea    0xf(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffff0,%ecx
sub    %ecx,%eax
cltq
lea    0xede(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
ret

LLVM IR (-O0):
@cardinal.directions = internal global [16 x i8*] [i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.14, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.15, i32 0, i32 0)]
@.str = [2 x i8] c"N\00"
@.str.1 = [4 x i8] c"NNE\00"
@.str.2 = [3 x i8] c"NE\00"
@.str.3 = [4 x i8] c"ENE\00"
@.str.4 = [2 x i8] c"E\00"
@.str.5 = [4 x i8] c"ESE\00"
@.str.6 = [3 x i8] c"SE\00"
@.str.7 = [4 x i8] c"SSE\00"
@.str.8 = [2 x i8] c"S\00"
@.str.9 = [4 x i8] c"SSW\00"
@.str.10 = [3 x i8] c"SW\00"
@.str.11 = [4 x i8] c"WSW\00"
@.str.12 = [2 x i8] c"W\00"
@.str.13 = [4 x i8] c"WNW\00"
@.str.14 = [3 x i8] c"NW\00"
@.str.15 = [4 x i8] c"NNW\00"
define i8* @cardinal(float %0) {
%2 = alloca float
%3 = alloca i32
store float %0, float* %2
%4 = load float, float* %2
%5 = fadd float %4, 1.125000e+01
%6 = fdiv float %5, 2.250000e+01
%7 = fptosi float %6 to i32
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = srem i32 %8, 16
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [16 x i8*], [16 x i8*]* @cardinal.directions, i64 0, i64 %10
%12 = load i8*, i8** %11
ret i8* %12
}

LLVM IR (-O2):
@reltable.cardinal = internal unnamed_addr constant [16 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.1 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.2 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.3 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.4 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.5 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.6 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.7 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.8 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.9 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.10 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.11 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.12 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.13 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.14 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.15 to i64), i64 ptrtoint ([16 x i32]* @reltable.cardinal to i64)) to i32)]
@.str = [2 x i8] c"N\00"
@.str.1 = [4 x i8] c"NNE\00"
@.str.2 = [3 x i8] c"NE\00"
@.str.3 = [4 x i8] c"ENE\00"
@.str.4 = [2 x i8] c"E\00"
@.str.5 = [4 x i8] c"ESE\00"
@.str.6 = [3 x i8] c"SE\00"
@.str.7 = [4 x i8] c"SSE\00"
@.str.8 = [2 x i8] c"S\00"
@.str.9 = [4 x i8] c"SSW\00"
@.str.10 = [3 x i8] c"SW\00"
@.str.11 = [4 x i8] c"WSW\00"
@.str.12 = [2 x i8] c"W\00"
@.str.13 = [4 x i8] c"WNW\00"
@.str.14 = [3 x i8] c"NW\00"
@.str.15 = [4 x i8] c"NNW\00"
define i8* @cardinal(float %0) {
%2 = fadd float %0, 1.125000e+01
%3 = fdiv float %2, 2.250000e+01
%4 = fptosi float %3 to i32
%5 = srem i32 %4, 16
%6 = sext i32 %5 to i64
%7 = shl i64 %6, 2
%8 = call i8* @llvm.load.relative.i64(i8* bitcast ([16 x i32]* @reltable.cardinal to i8*), i64 %7)
ret i8* %8
}