double getsample(void *p, unsigned long n, unsigned short m) {
  double v;
  switch (m) {
  case 1:
    v = (double)(*((char *)p + n));
    break;
  case 2:
    v = (double)*((short *)p + n);
    break;
  case 4:
    v = (double)*((long *)p + n);
  default:
    v = 0.0;
  }
  return (v);
}


x86 assembly (clang, -O0):
<getsample>:
push   %rbp
mov    %rsp,%rbp
mov    %dx,%ax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
movzwl -0x12(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x1,%eax
je     114a <getsample+0x4a>
jmp    1128 <getsample+0x28>
mov    -0x24(%rbp),%eax
sub    $0x2,%eax
je     1164 <getsample+0x64>
jmp    1139 <getsample+0x39>
mov    -0x24(%rbp),%eax
sub    $0x4,%eax
je     117e <getsample+0x7e>
jmp    1191 <getsample+0x91>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1199 <getsample+0x99>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movswl (%rax,%rcx,2),%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1199 <getsample+0x99>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
cvtsi2sdq (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsample>:
cmp    $0x2,%dx
je     1116 <getsample+0x16>
xorpd  %xmm0,%xmm0
cmp    $0x1,%dx
jne    1121 <getsample+0x21>
movsbl (%rdi,%rsi,1),%eax
jmp    111a <getsample+0x1a>
movswl (%rdi,%rsi,2),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
ret

LLVM IR (-O0):
define double @getsample(i8* %0, i64 %1, i16 zeroext %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i16
%7 = alloca double
store i8* %0, i8** %4
store i64 %1, i64* %5
store i16 %2, i16* %6
%8 = load i16, i16* %6
%9 = zext i16 %8 to i32
switch i32 %9, label %30 [
i32 1, label %10
i32 2, label %16
i32 4, label %23
]
10: ; preds = %3
%11 = load i8*, i8** %4
%12 = load i64, i64* %5
%13 = getelementptr inbounds i8, i8* %11, i64 %12
%14 = load i8, i8* %13
%15 = sitofp i8 %14 to double
store double %15, double* %7
br label %31
16: ; preds = %3
%17 = load i8*, i8** %4
%18 = bitcast i8* %17 to i16*
%19 = load i64, i64* %5
%20 = getelementptr inbounds i16, i16* %18, i64 %19
%21 = load i16, i16* %20
%22 = sitofp i16 %21 to double
store double %22, double* %7
br label %31
23: ; preds = %3
%24 = load i8*, i8** %4
%25 = bitcast i8* %24 to i64*
%26 = load i64, i64* %5
%27 = getelementptr inbounds i64, i64* %25, i64 %26
%28 = load i64, i64* %27
%29 = sitofp i64 %28 to double
store double %29, double* %7
br label %30
30: ; preds = %3, %23
store double 0.000000e+00, double* %7
br label %31
31: ; preds = %30, %16, %10
%32 = load double, double* %7
ret double %32
}

LLVM IR (-O2):
define double @getsample(i8* nocapture %0, i64 %1, i16 zeroext %2) {
switch i16 %2, label %13 [
i16 1, label %4
i16 2, label %8
]
4: ; preds = %3
%5 = getelementptr inbounds i8, i8* %0, i64 %1
%6 = load i8, i8* %5
%7 = sitofp i8 %6 to double
br label %13
8: ; preds = %3
%9 = bitcast i8* %0 to i16*
%10 = getelementptr inbounds i16, i16* %9, i64 %1
%11 = load i16, i16* %10
%12 = sitofp i16 %11 to double
br label %13
13: ; preds = %3, %8, %4
%14 = phi double [ %12, %8 ], [ %7, %4 ], [ 0.000000e+00, %3 ]
ret double %14
}