unsigned short me_encode(unsigned char c) {
  unsigned short tmp = 0;
  unsigned char i, t;
  for (i = 0; i < 8; i++) {
    t = c & 1;
    tmp >>= 2;
    if (t)
      tmp |= 0x8000;
    else
      tmp |= 0x4000;
    c >>= 1;
  }
  return tmp;
}


x86 assembly (clang, -O0):
<me_encode>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x1(%rbp)
movw   $0x0,-0x4(%rbp)
movb   $0x0,-0x5(%rbp)
movzbl -0x5(%rbp),%eax
cmp    $0x8,%eax
jge    1176 <me_encode+0x76>
movzbl -0x1(%rbp),%eax
and    $0x1,%eax
mov    %al,-0x6(%rbp)
movzwl -0x4(%rbp),%eax
sar    $0x2,%eax
mov    %ax,-0x4(%rbp)
cmpb   $0x0,-0x6(%rbp)
je     1152 <me_encode+0x52>
movzwl -0x4(%rbp),%eax
or     $0x8000,%eax
mov    %ax,-0x4(%rbp)
jmp    115f <me_encode+0x5f>
movzwl -0x4(%rbp),%eax
or     $0x4000,%eax
mov    %ax,-0x4(%rbp)
movzbl -0x1(%rbp),%eax
sar    $0x1,%eax
mov    %al,-0x1(%rbp)
mov    -0x5(%rbp),%al
add    $0x1,%al
mov    %al,-0x5(%rbp)
jmp    1114 <me_encode+0x14>
movzwl -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<me_encode>:
mov    %edi,%eax
and    $0x1,%eax
add    $0x1,%eax
mov    %edi,%ecx
and    $0x2,%cl
shr    %cl
movzbl %cl,%ecx
lea    0x4(,%rcx,4),%edx
mov    %edi,%ecx
and    $0x4,%cl
shr    $0x2,%cl
movzbl %cl,%ecx
shl    $0x4,%ecx
add    $0x10,%ecx
or     %edx,%ecx
movd   %edi,%xmm0
punpcklbw %xmm0,%xmm0
pshuflw $0x0,%xmm0,%xmm0
pand   0xebf(%rip),%xmm0
pxor   %xmm1,%xmm1
pcmpeqb %xmm0,%xmm1
punpcklbw %xmm1,%xmm0
psraw  $0x8,%xmm0
movdqa %xmm0,%xmm1
pandn  0xeb2(%rip),%xmm1
or     %eax,%ecx
pand   0xeb8(%rip),%xmm0
por    %xmm1,%xmm0
pshufd $0x55,%xmm0,%xmm1
por    %xmm0,%xmm1
movdqa %xmm1,%xmm0
psrld  $0x10,%xmm0
por    %xmm1,%xmm0
movd   %xmm0,%edx
test   %dil,%dil
mov    $0x4000,%esi
mov    $0xffff8000,%eax
cmovns %esi,%eax
or     %ecx,%eax
or     %edx,%eax
ret

LLVM IR (-O0):
define zeroext i16 @me_encode(i8 zeroext %0) {
%2 = alloca i8
%3 = alloca i16
%4 = alloca i8
%5 = alloca i8
store i8 %0, i8* %2
store i16 0, i16* %3
store i8 0, i8* %4
br label %6
6: ; preds = %36, %1
%7 = load i8, i8* %4
%8 = zext i8 %7 to i32
%9 = icmp slt i32 %8, 8
br i1 %9, label %10, label %39
10: ; preds = %6
%11 = load i8, i8* %2
%12 = zext i8 %11 to i32
%13 = and i32 %12, 1
%14 = trunc i32 %13 to i8
store i8 %14, i8* %5
%15 = load i16, i16* %3
%16 = zext i16 %15 to i32
%17 = ashr i32 %16, 2
%18 = trunc i32 %17 to i16
store i16 %18, i16* %3
%19 = load i8, i8* %5
%20 = icmp ne i8 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %10
%22 = load i16, i16* %3
%23 = zext i16 %22 to i32
%24 = or i32 %23, 32768
%25 = trunc i32 %24 to i16
store i16 %25, i16* %3
br label %31
26: ; preds = %10
%27 = load i16, i16* %3
%28 = zext i16 %27 to i32
%29 = or i32 %28, 16384
%30 = trunc i32 %29 to i16
store i16 %30, i16* %3
br label %31
31: ; preds = %26, %21
%32 = load i8, i8* %2
%33 = zext i8 %32 to i32
%34 = ashr i32 %33, 1
%35 = trunc i32 %34 to i8
store i8 %35, i8* %2
br label %36
36: ; preds = %31
%37 = load i8, i8* %4
%38 = add i8 %37, 1
store i8 %38, i8* %4
br label %6
39: ; preds = %6
%40 = load i16, i16* %3
ret i16 %40
}

LLVM IR (-O2):
define zeroext i16 @me_encode(i8 zeroext %0) {
%2 = and i8 %0, 1
%3 = icmp eq i8 %2, 0
%4 = select i1 %3, i16 1, i16 2
%5 = and i8 %0, 2
%6 = icmp eq i8 %5, 0
%7 = select i1 %6, i16 4, i16 8
%8 = and i8 %0, 4
%9 = icmp eq i8 %8, 0
%10 = select i1 %9, i16 16, i16 32
%11 = insertelement <4 x i8> poison, i8 %0, i64 0
%12 = shufflevector <4 x i8> %11, <4 x i8> poison, <4 x i32> zeroinitializer
%13 = and <4 x i8> %12, <i8 64, i8 32, i8 16, i8 8>
%14 = icmp eq <4 x i8> %13, zeroinitializer
%15 = select <4 x i1> %14, <4 x i16> <i16 4096, i16 1024, i16 256, i16 64>, <4 x i16> <i16 8192, i16 2048, i16 512, i16 128>
%16 = call i16 @llvm.vector.reduce.or.v4i16(<4 x i16> %15)
%17 = or i16 %16, %10
%18 = or i16 %17, %7
%19 = or i16 %18, %4
%20 = icmp sgt i8 %0, -1
%21 = select i1 %20, i16 16384, i16 -32768
%22 = or i16 %21, %19
ret i16 %22
}