void long_to_binary(const long x, char b[66]) {
  char *p = b;
  unsigned long z;
  for (z = (1l << 63); z > 0; z >>= 1) {
    *p++ = (x & z) ? '1' : '0';
  }
  *p = '\0';
}


x86 assembly (clang, -O0):
<long_to_binary>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movabs $0x8000000000000000,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    116a <long_to_binary+0x6a>
mov    -0x8(%rbp),%rdx
and    -0x20(%rbp),%rdx
mov    $0x30,%eax
mov    $0x31,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1122 <long_to_binary+0x22>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<long_to_binary>:
movabs $0x8000000000000000,%rax
mov    %rax,%r9
nopl   (%rax)
mov    %rax,%rdx
and    %rdi,%rdx
cmp    $0x1,%rdx
mov    $0x31,%r8b
sbb    $0x0,%r8b
mov    %rax,%rdx
shr    %rdx
and    %rdi,%rdx
cmp    $0x1,%rdx
mov    $0x31,%r10b
sbb    $0x0,%r10b
mov    %rax,%rdx
shr    $0x2,%rdx
and    %rdi,%rdx
cmp    $0x1,%rdx
mov    %rsi,%rdx
mov    %r8b,(%rsi)
mov    $0x31,%cl
sbb    $0x0,%cl
mov    %r10b,0x1(%rsi)
mov    %cl,0x2(%rsi)
mov    %rax,%rcx
shr    $0x3,%rcx
and    %rdi,%rcx
cmp    $0x1,%rcx
mov    $0x31,%cl
sbb    $0x0,%cl
mov    %cl,0x3(%rsi)
shr    $0x4,%r9
add    $0x4,%rsi
cmp    $0xf,%rax
mov    %r9,%rax
ja     1110 <long_to_binary+0x10>
movb   $0x0,0x4(%rdx)
ret

LLVM IR (-O0):
define void @long_to_binary(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store i64 %0, i64* %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
store i8* %7, i8** %5
store i64 -9223372036854775808, i64* %6
br label %8
8: ; preds = %21, %2
%9 = load i64, i64* %6
%10 = icmp ugt i64 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %8
%12 = load i64, i64* %3
%13 = load i64, i64* %6
%14 = and i64 %12, %13
%15 = icmp ne i64 %14, 0
%16 = zext i1 %15 to i64
%17 = select i1 %15, i32 49, i32 48
%18 = trunc i32 %17 to i8
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %5
store i8 %18, i8* %19
br label %21
21: ; preds = %11
%22 = load i64, i64* %6
%23 = lshr i64 %22, 1
store i64 %23, i64* %6
br label %8
24: ; preds = %8
%25 = load i8*, i8** %5
store i8 0, i8* %25
ret void
}

LLVM IR (-O2):
define void @long_to_binary(i64 %0, i8* nocapture writeonly %1) {
br label %3
3: ; preds = %3, %2
%4 = phi i64 [ -9223372036854775808, %2 ], [ %25, %3 ]
%5 = phi i8* [ %1, %2 ], [ %24, %3 ]
%6 = and i64 %4, %0
%7 = icmp eq i64 %6, 0
%8 = select i1 %7, i8 48, i8 49
%9 = getelementptr inbounds i8, i8* %5, i64 1
store i8 %8, i8* %5
%10 = lshr i64 %4, 1
%11 = and i64 %10, %0
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i8 48, i8 49
%14 = getelementptr inbounds i8, i8* %5, i64 2
store i8 %13, i8* %9
%15 = lshr i64 %4, 2
%16 = and i64 %15, %0
%17 = icmp eq i64 %16, 0
%18 = select i1 %17, i8 48, i8 49
%19 = getelementptr inbounds i8, i8* %5, i64 3
store i8 %18, i8* %14
%20 = lshr i64 %4, 3
%21 = and i64 %20, %0
%22 = icmp eq i64 %21, 0
%23 = select i1 %22, i8 48, i8 49
%24 = getelementptr inbounds i8, i8* %5, i64 4
store i8 %23, i8* %19
%25 = lshr i64 %4, 4
%26 = icmp ult i64 %4, 16
br i1 %26, label %27, label %3
27: ; preds = %3
store i8 0, i8* %24
ret void
}