void byte2bin(char *buf, unsigned char byte) {
  int i;
  buf[0] = '\0';
  for (i = 0; i < 8; i++) {
    if ((byte >> i) & 0x1)
      buf[8 - i - 1] = '1';
    else
      buf[8 - i - 1] = '0';
  }
  buf[8] = '\0';
}


x86 assembly (clang, -O0):
<byte2bin>:
push   %rbp
mov    %rsp,%rbp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x10(%rbp)
cmpl   $0x8,-0x10(%rbp)
jge    117f <byte2bin+0x7f>
movzbl -0x9(%rbp),%eax
mov    -0x10(%rbp),%ecx
sar    %cl,%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1156 <byte2bin+0x56>
mov    -0x8(%rbp),%rax
mov    $0x8,%ecx
sub    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x31,(%rax,%rcx,1)
jmp    116c <byte2bin+0x6c>
mov    -0x8(%rbp),%rax
mov    $0x8,%ecx
sub    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x30,(%rax,%rcx,1)
jmp    1171 <byte2bin+0x71>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    111c <byte2bin+0x1c>
mov    -0x8(%rbp),%rax
movb   $0x0,0x8(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<byte2bin>:
movd   %esi,%xmm0
pshufd $0x0,%xmm0,%xmm0
movdqa 0xeef(%rip),%xmm1
pand   %xmm0,%xmm1
pand   0xef3(%rip),%xmm0
pxor   %xmm2,%xmm2
pcmpeqd %xmm2,%xmm0
pcmpeqd %xmm2,%xmm1
packssdw %xmm0,%xmm1
packsswb %xmm1,%xmm1
pmovmskb %xmm1,%esi
mov    %esi,%r11d
and    $0x2,%r11b
shr    %r11b
mov    %esi,%r8d
and    $0x4,%r8b
shr    $0x2,%r8b
mov    %esi,%r9d
and    $0x8,%r9b
shr    $0x3,%r9b
mov    %esi,%r10d
and    $0x10,%r10b
shr    $0x4,%r10b
mov    %esi,%ecx
and    $0x20,%cl
shr    $0x5,%cl
mov    %esi,%edx
and    $0x40,%dl
shr    $0x6,%dl
mov    %esi,%eax
sar    $0x7,%al
add    $0x31,%al
mov    %al,0x7(%rdi)
xor    $0x31,%dl
mov    %dl,0x6(%rdi)
xor    $0x31,%cl
mov    %cl,0x5(%rdi)
xor    $0x31,%r10b
mov    %r10b,0x4(%rdi)
xor    $0x31,%r9b
mov    %r9b,0x3(%rdi)
xor    $0x31,%r8b
mov    %r8b,0x2(%rdi)
xor    $0x31,%r11b
mov    %r11b,0x1(%rdi)
and    $0x1,%sil
xor    $0x31,%sil
mov    %sil,(%rdi)
movb   $0x0,0x8(%rdi)
ret

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

LLVM IR (-O2):
define void @byte2bin(i8* nocapture writeonly %0, i8 zeroext %1) {
%3 = zext i8 %1 to i32
%4 = insertelement <8 x i32> poison, i32 %3, i64 0
%5 = shufflevector <8 x i32> %4, <8 x i32> poison, <8 x i32> zeroinitializer
%6 = and <8 x i32> %5, <i32 128, i32 64, i32 32, i32 16, i32 8, i32 4, i32 2, i32 1>
%7 = icmp eq <8 x i32> %6, zeroinitializer
%8 = extractelement <8 x i1> %7, i64 7
%9 = select i1 %8, i8 48, i8 49
%10 = getelementptr inbounds i8, i8* %0, i64 7
store i8 %9, i8* %10
%11 = extractelement <8 x i1> %7, i64 6
%12 = select i1 %11, i8 48, i8 49
%13 = getelementptr inbounds i8, i8* %0, i64 6
store i8 %12, i8* %13
%14 = extractelement <8 x i1> %7, i64 5
%15 = select i1 %14, i8 48, i8 49
%16 = getelementptr inbounds i8, i8* %0, i64 5
store i8 %15, i8* %16
%17 = extractelement <8 x i1> %7, i64 4
%18 = select i1 %17, i8 48, i8 49
%19 = getelementptr inbounds i8, i8* %0, i64 4
store i8 %18, i8* %19
%20 = extractelement <8 x i1> %7, i64 3
%21 = select i1 %20, i8 48, i8 49
%22 = getelementptr inbounds i8, i8* %0, i64 3
store i8 %21, i8* %22
%23 = extractelement <8 x i1> %7, i64 2
%24 = select i1 %23, i8 48, i8 49
%25 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %24, i8* %25
%26 = extractelement <8 x i1> %7, i64 1
%27 = select i1 %26, i8 48, i8 49
%28 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %27, i8* %28
%29 = extractelement <8 x i1> %7, i64 0
%30 = select i1 %29, i8 48, i8 49
store i8 %30, i8* %0
%31 = getelementptr inbounds i8, i8* %0, i64 8
store i8 0, i8* %31
ret void
}