void bin32(unsigned short n, char *s) {
  int i;
  for (i = 31; i >= 0; i--) {
    *s = '0' + ((n >> i) & 1);
    s++;
  }
  *s = '\0';
}


x86 assembly (clang, -O0):
<bin32>:
push   %rbp
mov    %rsp,%rbp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1f,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1151 <bin32+0x51>
movzwl -0x2(%rbp),%eax
mov    -0x14(%rbp),%ecx
sar    %cl,%eax
and    $0x1,%eax
add    $0x30,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1116 <bin32+0x16>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin32>:
movd   %edi,%xmm0
pshufd $0x0,%xmm0,%xmm2
movdqa 0xeef(%rip),%xmm0
movdqa %xmm2,%xmm1
psrld  $0xc,%xmm1
movdqa %xmm2,%xmm3
psrld  $0xd,%xmm3
punpckhqdq %xmm1,%xmm3
movdqa %xmm2,%xmm4
psrld  $0xe,%xmm4
movdqa %xmm2,%xmm1
psrld  $0xf,%xmm1
punpcklqdq %xmm4,%xmm1
shufps $0xcc,%xmm3,%xmm1
movdqa %xmm2,%xmm3
psrld  $0x8,%xmm3
movdqa %xmm2,%xmm4
psrld  $0x9,%xmm4
punpckhqdq %xmm3,%xmm4
movdqa %xmm2,%xmm5
psrld  $0xa,%xmm5
movdqa %xmm2,%xmm3
psrld  $0xb,%xmm3
punpcklqdq %xmm5,%xmm3
shufps $0xcc,%xmm4,%xmm3
movdqa %xmm2,%xmm4
psrld  $0x4,%xmm4
movdqa %xmm2,%xmm5
psrld  $0x5,%xmm5
punpckhqdq %xmm4,%xmm5
movdqa %xmm2,%xmm4
psrld  $0x6,%xmm4
movdqa %xmm2,%xmm6
psrld  $0x7,%xmm6
punpcklqdq %xmm4,%xmm6
shufps $0xcc,%xmm5,%xmm6
movdqa %xmm2,%xmm4
psrld  $0x1,%xmm4
punpckhqdq %xmm2,%xmm4
movdqa %xmm2,%xmm5
psrld  $0x2,%xmm5
psrld  $0x3,%xmm2
punpcklqdq %xmm5,%xmm2
shufps $0xcc,%xmm4,%xmm2
movaps 0xe45(%rip),%xmm4
andps  %xmm4,%xmm2
andps  %xmm4,%xmm6
packuswb %xmm2,%xmm6
andps  %xmm4,%xmm3
andps  %xmm4,%xmm1
packuswb %xmm3,%xmm1
packuswb %xmm6,%xmm1
pand   0xe35(%rip),%xmm1
movdqu %xmm0,(%rsi)
por    %xmm0,%xmm1
movdqu %xmm1,0x10(%rsi)
movb   $0x0,0x20(%rsi)
ret

LLVM IR (-O0):
define void @bin32(i16 zeroext %0, i8* %1) {
%3 = alloca i16
%4 = alloca i8*
%5 = alloca i32
store i16 %0, i16* %3
store i8* %1, i8** %4
store i32 31, i32* %5
br label %6
6: ; preds = %20, %2
%7 = load i32, i32* %5
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %23
9: ; preds = %6
%10 = load i16, i16* %3
%11 = zext i16 %10 to i32
%12 = load i32, i32* %5
%13 = ashr i32 %11, %12
%14 = and i32 %13, 1
%15 = add nsw i32 48, %14
%16 = trunc i32 %15 to i8
%17 = load i8*, i8** %4
store i8 %16, i8* %17
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %4
br label %20
20: ; preds = %9
%21 = load i32, i32* %5
%22 = add nsw i32 %21, -1
store i32 %22, i32* %5
br label %6
23: ; preds = %6
%24 = load i8*, i8** %4
store i8 0, i8* %24
ret void
}

LLVM IR (-O2):
define void @bin32(i16 zeroext %0, i8* nocapture writeonly %1) {
%3 = zext i16 %0 to i32
%4 = insertelement <16 x i32> poison, i32 %3, i64 0
%5 = shufflevector <16 x i32> %4, <16 x i32> poison, <16 x i32> zeroinitializer
%6 = bitcast i8* %1 to <16 x i8>*
store <16 x i8> <i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48>, <16 x i8>* %6
%7 = getelementptr i8, i8* %1, i64 16
%8 = lshr <16 x i32> %5, <i32 15, i32 14, i32 13, i32 12, i32 11, i32 10, i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
%9 = trunc <16 x i32> %8 to <16 x i8>
%10 = and <16 x i8> %9, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
%11 = or <16 x i8> %10, <i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48>
%12 = bitcast i8* %7 to <16 x i8>*
store <16 x i8> %11, <16 x i8>* %12
%13 = getelementptr i8, i8* %1, i64 32
store i8 0, i8* %13
ret void
}