void init_cmap(unsigned short *cmap) {
  for (unsigned i = 0; i < 65536; i++) {
    unsigned char c = i >> (16 - 5);
    cmap[i] = (c << 10) | (c << 5) | c;
  }
}


x86 assembly (clang, -O0):
<init_cmap>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x10000,-0xc(%rbp)
jae    1157 <init_cmap+0x57>
mov    -0xc(%rbp),%eax
shr    $0xb,%eax
mov    %al,-0xd(%rbp)
movzbl -0xd(%rbp),%eax
shl    $0xa,%eax
movzbl -0xd(%rbp),%ecx
shl    $0x5,%ecx
or     %ecx,%eax
movzbl -0xd(%rbp),%ecx
or     %ecx,%eax
mov    %ax,%dx
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %dx,(%rax,%rcx,2)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    110f <init_cmap+0xf>
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_cmap>:
movdqa 0xef8(%rip),%xmm0
movdqa 0xf00(%rip),%xmm1
xor    %eax,%eax
movdqa 0xf06(%rip),%xmm2
movdqa 0xf0e(%rip),%xmm3
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movdqa %xmm0,%xmm4
psrld  $0xb,%xmm4
movdqa %xmm1,%xmm5
psrld  $0xb,%xmm5
movdqa %xmm5,%xmm6
pslld  $0x5,%xmm6
por    %xmm5,%xmm6
pslld  $0xa,%xmm5
movdqa %xmm4,%xmm7
pslld  $0x5,%xmm7
por    %xmm4,%xmm7
pslld  $0xa,%xmm4
por    %xmm5,%xmm6
por    %xmm4,%xmm7
pslld  $0x10,%xmm6
psrad  $0x10,%xmm6
pslld  $0x10,%xmm7
psrad  $0x10,%xmm7
packssdw %xmm7,%xmm6
movdqu %xmm6,(%rdi,%rax,2)
movdqa %xmm1,%xmm4
paddd  %xmm2,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm2,%xmm5
psrld  $0xb,%xmm5
psrld  $0xb,%xmm4
movdqa %xmm4,%xmm6
pslld  $0x5,%xmm6
por    %xmm4,%xmm6
pslld  $0xa,%xmm4
movdqa %xmm5,%xmm7
pslld  $0x5,%xmm7
por    %xmm5,%xmm7
pslld  $0xa,%xmm5
por    %xmm4,%xmm6
por    %xmm5,%xmm7
pslld  $0x10,%xmm6
psrad  $0x10,%xmm6
pslld  $0x10,%xmm7
psrad  $0x10,%xmm7
packssdw %xmm7,%xmm6
movdqu %xmm6,0x10(%rdi,%rax,2)
add    $0x10,%rax
paddd  %xmm3,%xmm1
paddd  %xmm3,%xmm0
cmp    $0x10000,%rax
jne    1130 <init_cmap+0x30>
ret

LLVM IR (-O0):
define void @init_cmap(i16* %0) {
%2 = alloca i16*
%3 = alloca i32
%4 = alloca i8
store i16* %0, i16** %2
store i32 0, i32* %3
br label %5
5: ; preds = %27, %1
%6 = load i32, i32* %3
%7 = icmp ult i32 %6, 65536
br i1 %7, label %8, label %30
8: ; preds = %5
%9 = load i32, i32* %3
%10 = lshr i32 %9, 11
%11 = trunc i32 %10 to i8
store i8 %11, i8* %4
%12 = load i8, i8* %4
%13 = zext i8 %12 to i32
%14 = shl i32 %13, 10
%15 = load i8, i8* %4
%16 = zext i8 %15 to i32
%17 = shl i32 %16, 5
%18 = or i32 %14, %17
%19 = load i8, i8* %4
%20 = zext i8 %19 to i32
%21 = or i32 %18, %20
%22 = trunc i32 %21 to i16
%23 = load i16*, i16** %2
%24 = load i32, i32* %3
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i16, i16* %23, i64 %25
store i16 %22, i16* %26
br label %27
27: ; preds = %8
%28 = load i32, i32* %3
%29 = add i32 %28, 1
store i32 %29, i32* %3
br label %5
30: ; preds = %5
ret void
}

LLVM IR (-O2):
define void @init_cmap(i16* nocapture writeonly %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i64 [ 0, %1 ], [ %23, %2 ]
%4 = phi <8 x i32> [ <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>, %1 ], [ %24, %2 ]
%5 = lshr <8 x i32> %4, <i32 11, i32 11, i32 11, i32 11, i32 11, i32 11, i32 11, i32 11>
%6 = shl nuw nsw <8 x i32> %5, <i32 10, i32 10, i32 10, i32 10, i32 10, i32 10, i32 10, i32 10>
%7 = shl nuw nsw <8 x i32> %5, <i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5>
%8 = or <8 x i32> %7, %5
%9 = or <8 x i32> %8, %6
%10 = trunc <8 x i32> %9 to <8 x i16>
%11 = getelementptr inbounds i16, i16* %0, i64 %3
%12 = bitcast i16* %11 to <8 x i16>*
store <8 x i16> %10, <8 x i16>* %12
%13 = or i64 %3, 8
%14 = add <8 x i32> %4, <i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8>
%15 = lshr <8 x i32> %14, <i32 11, i32 11, i32 11, i32 11, i32 11, i32 11, i32 11, i32 11>
%16 = shl nuw nsw <8 x i32> %15, <i32 10, i32 10, i32 10, i32 10, i32 10, i32 10, i32 10, i32 10>
%17 = shl nuw nsw <8 x i32> %15, <i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5>
%18 = or <8 x i32> %17, %15
%19 = or <8 x i32> %18, %16
%20 = trunc <8 x i32> %19 to <8 x i16>
%21 = getelementptr inbounds i16, i16* %0, i64 %13
%22 = bitcast i16* %21 to <8 x i16>*
store <8 x i16> %20, <8 x i16>* %22
%23 = add nuw nsw i64 %3, 16
%24 = add <8 x i32> %4, <i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16>
%25 = icmp eq i64 %23, 65536
br i1 %25, label %26, label %2
26: ; preds = %2
ret void
}