char *tobin(int n, int x) {
  int i;
  int mask;
  static char buffer[64];
  for (i = 0; i < n; i++) {
    mask = 1 << i;
    buffer[i] = (x & mask) ? '1' : '0';
  }
  buffer[n] = 0;
  return buffer;
}


x86 assembly (clang, -O0):
<tobin>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    115e <tobin+0x5e>
mov    -0xc(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edx
and    -0x10(%rbp),%edx
mov    $0x30,%eax
mov    $0x31,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%dl
movslq -0xc(%rbp),%rcx
lea    0x2ee3(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1111 <tobin+0x11>
movslq -0x4(%rbp),%rcx
lea    0x2ec7(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2ebc(%rip),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<tobin>:
test   %edi,%edi
jle    1504 <tobin+0x404>
mov    %edi,%r9d
cmp    $0x10,%edi
jae    1117 <tobin+0x17>
xor    %ecx,%ecx
jmp    14e1 <tobin+0x3e1>
mov    %r9d,%ecx
and    $0xfffffff0,%ecx
movd   %esi,%xmm0
pshufd $0x0,%xmm0,%xmm13
lea    -0x10(%rcx),%rax
mov    %rax,%r8
shr    $0x4,%r8
add    $0x1,%r8
test   %rax,%rax
je     1513 <tobin+0x413>
mov    %r8,%rax
and    $0xfffffffffffffffe,%rax
movdqa 0xeb1(%rip),%xmm14
movdqa 0xeb8(%rip),%xmm15
movdqa 0xebf(%rip),%xmm9
movdqa 0xec7(%rip),%xmm4
xor    %edx,%edx
movdqa 0xecc(%rip),%xmm11
movdqa 0xed4(%rip),%xmm6
pxor   %xmm12,%xmm12
lea    0x2ea8(%rip),%r10
movdqa 0xecf(%rip),%xmm10
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movdqa %xmm4,%xmm0
pslld  $0x17,%xmm0
paddd  %xmm11,%xmm0
cvttps2dq %xmm0,%xmm0
pshufd $0xf5,%xmm0,%xmm1
pmuludq %xmm6,%xmm0
pshufd $0xe8,%xmm0,%xmm5
pmuludq %xmm6,%xmm1
pshufd $0xe8,%xmm1,%xmm0
punpckldq %xmm0,%xmm5
movdqa %xmm9,%xmm0
pslld  $0x17,%xmm0
paddd  %xmm11,%xmm0
cvttps2dq %xmm0,%xmm0
pshufd $0xf5,%xmm0,%xmm7
pmuludq %xmm6,%xmm0
pshufd $0xe8,%xmm0,%xmm1
pmuludq %xmm6,%xmm7
pshufd $0xe8,%xmm7,%xmm0
punpckldq %xmm0,%xmm1
movdqa %xmm15,%xmm0
pslld  $0x17,%xmm0
paddd  %xmm11,%xmm0
cvttps2dq %xmm0,%xmm0
pshufd $0xf5,%xmm0,%xmm7
pmuludq %xmm6,%xmm0
pshufd $0xe8,%xmm0,%xmm0
pmuludq %xmm6,%xmm7
pshufd $0xe8,%xmm7,%xmm7
punpckldq %xmm7,%xmm0
movdqa %xmm14,%xmm7
pslld  $0x17,%xmm7
paddd  %xmm11,%xmm7
cvttps2dq %xmm7,%xmm7
pshufd $0xf5,%xmm7,%xmm2
pmuludq %xmm6,%xmm7
pshufd $0xe8,%xmm7,%xmm7
pmuludq %xmm6,%xmm2
pshufd $0xe8,%xmm2,%xmm2
punpckldq %xmm2,%xmm7
pand   %xmm13,%xmm7
pand   %xmm13,%xmm0
pand   %xmm13,%xmm1
pand   %xmm13,%xmm5
pcmpeqd %xmm12,%xmm5
pcmpeqd %xmm12,%xmm1
packssdw %xmm1,%xmm5
pcmpeqd %xmm12,%xmm0
pcmpeqd %xmm12,%xmm7
packssdw %xmm7,%xmm0
packsswb %xmm0,%xmm5
paddb  %xmm10,%xmm5
movdqa %xmm5,(%rdx,%r10,1)
movdqa %xmm14,%xmm1
movdqa 0xdcd(%rip),%xmm3
paddd  %xmm3,%xmm1
movdqa %xmm15,%xmm7
paddd  %xmm3,%xmm7
movdqa %xmm9,%xmm0
paddd  %xmm3,%xmm0
movdqa %xmm4,%xmm2
paddd  %xmm3,%xmm2
pslld  $0x17,%xmm2
paddd  %xmm11,%xmm2
cvttps2dq %xmm2,%xmm2
pshufd $0xf5,%xmm2,%xmm8
pmuludq %xmm6,%xmm2
pshufd $0xe8,%xmm2,%xmm5
pmuludq %xmm6,%xmm8
pshufd $0xe8,%xmm8,%xmm2
punpckldq %xmm2,%xmm5
pslld  $0x17,%xmm0
paddd  %xmm11,%xmm0
cvttps2dq %xmm0,%xmm0
pshufd $0xf5,%xmm0,%xmm2
pmuludq %xmm6,%xmm0
pshufd $0xe8,%xmm0,%xmm0
pmuludq %xmm6,%xmm2
pshufd $0xe8,%xmm2,%xmm2
punpckldq %xmm2,%xmm0
pslld  $0x17,%xmm7
paddd  %xmm11,%xmm7
cvttps2dq %xmm7,%xmm2
pshufd $0xf5,%xmm2,%xmm3
pmuludq %xmm6,%xmm2
pshufd $0xe8,%xmm2,%xmm7
pmuludq %xmm6,%xmm3
pshufd $0xe8,%xmm3,%xmm2
punpckldq %xmm2,%xmm7
pslld  $0x17,%xmm1
paddd  %xmm11,%xmm1
cvttps2dq %xmm1,%xmm1
pshufd $0xf5,%xmm1,%xmm2
pmuludq %xmm6,%xmm1
pshufd $0xe8,%xmm1,%xmm1
pmuludq %xmm6,%xmm2
pshufd $0xe8,%xmm2,%xmm2
punpckldq %xmm2,%xmm1
pand   %xmm13,%xmm1
pand   %xmm13,%xmm7
pand   %xmm13,%xmm0
pand   %xmm13,%xmm5
pcmpeqd %xmm12,%xmm5
pcmpeqd %xmm12,%xmm0
packssdw %xmm0,%xmm5
pcmpeqd %xmm12,%xmm7
pcmpeqd %xmm12,%xmm1
packssdw %xmm1,%xmm7
packsswb %xmm7,%xmm5
paddb  %xmm10,%xmm5
movdqa %xmm5,0x10(%rdx,%r10,1)
add    $0x20,%rdx
movdqa 0xccc(%rip),%xmm0
paddd  %xmm0,%xmm4
paddd  %xmm0,%xmm9
paddd  %xmm0,%xmm15
paddd  %xmm0,%xmm14
add    $0xfffffffffffffffe,%rax
jne    11a0 <tobin+0xa0>
test   $0x1,%r8b
je     14dc <tobin+0x3dc>
pslld  $0x17,%xmm4
movdqa 0xc58(%rip),%xmm0
paddd  %xmm0,%xmm4
cvttps2dq %xmm4,%xmm2
movdqa 0xc58(%rip),%xmm1
pshufd $0xf5,%xmm2,%xmm3
pmuludq %xmm1,%xmm2
pshufd $0xe8,%xmm2,%xmm4
pmuludq %xmm1,%xmm3
pshufd $0xe8,%xmm3,%xmm2
punpckldq %xmm2,%xmm4
pslld  $0x17,%xmm9
paddd  %xmm0,%xmm9
cvttps2dq %xmm9,%xmm2
pshufd $0xf5,%xmm2,%xmm5
pmuludq %xmm1,%xmm2
pshufd $0xe8,%xmm2,%xmm3
pmuludq %xmm1,%xmm5
pshufd $0xe8,%xmm5,%xmm2
punpckldq %xmm2,%xmm3
pslld  $0x17,%xmm15
paddd  %xmm0,%xmm15
cvttps2dq %xmm15,%xmm2
pshufd $0xf5,%xmm2,%xmm5
pmuludq %xmm1,%xmm2
pshufd $0xe8,%xmm2,%xmm2
pmuludq %xmm1,%xmm5
pshufd $0xe8,%xmm5,%xmm5
punpckldq %xmm5,%xmm2
pslld  $0x17,%xmm14
paddd  %xmm0,%xmm14
cvttps2dq %xmm14,%xmm0
pshufd $0xf5,%xmm0,%xmm5
pmuludq %xmm1,%xmm0
pshufd $0xe8,%xmm0,%xmm0
pmuludq %xmm1,%xmm5
pshufd $0xe8,%xmm5,%xmm1
punpckldq %xmm1,%xmm0
pand   %xmm13,%xmm0
pand   %xmm13,%xmm2
pand   %xmm13,%xmm3
pand   %xmm13,%xmm4
pxor   %xmm1,%xmm1
pcmpeqd %xmm1,%xmm4
pcmpeqd %xmm1,%xmm3
packssdw %xmm3,%xmm4
pcmpeqd %xmm1,%xmm2
pcmpeqd %xmm1,%xmm0
packssdw %xmm0,%xmm2
packsswb %xmm2,%xmm4
paddb  0xb90(%rip),%xmm4
lea    0x2b59(%rip),%rax
movdqa %xmm4,(%rdx,%rax,1)
cmp    %r9,%rcx
je     1504 <tobin+0x404>
lea    0x2b48(%rip),%rax
nopl   0x0(%rax,%rax,1)
bt     %ecx,%esi
mov    $0x31,%dl
adc    $0xff,%dl
mov    %dl,(%rcx,%rax,1)
add    $0x1,%rcx
cmp    %rcx,%r9
jne    14f0 <tobin+0x3f0>
movslq %edi,%rcx
lea    0x2b22(%rip),%rax
movb   $0x0,(%rcx,%rax,1)
ret
movdqa 0xae4(%rip),%xmm14
movdqa 0xaeb(%rip),%xmm15
movdqa 0xaf2(%rip),%xmm9
movdqa 0xafa(%rip),%xmm4
xor    %edx,%edx
test   $0x1,%r8b
jne    13db <tobin+0x2db>
jmp    14dc <tobin+0x3dc>

LLVM IR (-O0):
@tobin.buffer = internal global [64 x i8] zeroinitializer
define i8* @tobin(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %24, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %3
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %27
11: ; preds = %7
%12 = load i32, i32* %5
%13 = shl i32 1, %12
store i32 %13, i32* %6
%14 = load i32, i32* %4
%15 = load i32, i32* %6
%16 = and i32 %14, %15
%17 = icmp ne i32 %16, 0
%18 = zext i1 %17 to i64
%19 = select i1 %17, i32 49, i32 48
%20 = trunc i32 %19 to i8
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [64 x i8], [64 x i8]* @tobin.buffer, i64 0, i64 %22
store i8 %20, i8* %23
br label %24
24: ; preds = %11
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
br label %7
27: ; preds = %7
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [64 x i8], [64 x i8]* @tobin.buffer, i64 0, i64 %29
store i8 0, i8* %30
ret i8* getelementptr inbounds ([64 x i8], [64 x i8]* @tobin.buffer, i64 0, i64 0)
}

LLVM IR (-O2):
@tobin.buffer = internal global [64 x i8] zeroinitializer
define i8* @tobin(i32 %0, i32 %1) {
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %65
4: ; preds = %2
%5 = zext i32 %0 to i64
%6 = icmp ult i32 %0, 16
br i1 %6, label %53, label %7
7: ; preds = %4
%8 = and i64 %5, 4294967280
%9 = insertelement <16 x i32> poison, i32 %1, i64 0
%10 = shufflevector <16 x i32> %9, <16 x i32> poison, <16 x i32> zeroinitializer
%11 = add nsw i64 %8, -16
%12 = lshr exact i64 %11, 4
%13 = add nuw nsw i64 %12, 1
%14 = and i64 %13, 1
%15 = icmp eq i64 %11, 0
br i1 %15, label %40, label %16
16: ; preds = %7
%17 = and i64 %13, 2305843009213693950
br label %18
18: ; preds = %18, %16
%19 = phi i64 [ 0, %16 ], [ %36, %18 ]
%20 = phi <16 x i32> [ <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>, %16 ], [ %37, %18 ]
%21 = phi i64 [ 0, %16 ], [ %38, %18 ]
%22 = shl nuw <16 x i32> <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>, %20
%23 = and <16 x i32> %22, %10
%24 = icmp eq <16 x i32> %23, zeroinitializer
%25 = select <16 x i1> %24, <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> <i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49>
%26 = getelementptr inbounds [64 x i8], [64 x i8]* @tobin.buffer, i64 0, i64 %19
%27 = bitcast i8* %26 to <16 x i8>*
store <16 x i8> %25, <16 x i8>* %27
%28 = or i64 %19, 16
%29 = add <16 x i32> %20, <i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16>
%30 = shl nuw <16 x i32> <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>, %29
%31 = and <16 x i32> %30, %10
%32 = icmp eq <16 x i32> %31, zeroinitializer
%33 = select <16 x i1> %32, <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> <i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49>
%34 = getelementptr inbounds [64 x i8], [64 x i8]* @tobin.buffer, i64 0, i64 %28
%35 = bitcast i8* %34 to <16 x i8>*
store <16 x i8> %33, <16 x i8>* %35
%36 = add nuw i64 %19, 32
%37 = add <16 x i32> %20, <i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32>
%38 = add i64 %21, 2
%39 = icmp eq i64 %38, %17
br i1 %39, label %40, label %18
40: ; preds = %18, %7
%41 = phi i64 [ 0, %7 ], [ %36, %18 ]
%42 = phi <16 x i32> [ <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>, %7 ], [ %37, %18 ]
%43 = icmp eq i64 %14, 0
br i1 %43, label %51, label %44
44: ; preds = %40
%45 = shl nuw <16 x i32> <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>, %42
%46 = and <16 x i32> %45, %10
%47 = icmp eq <16 x i32> %46, zeroinitializer
%48 = select <16 x i1> %47, <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> <i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49, i8 49>
%49 = getelementptr inbounds [64 x i8], [64 x i8]* @tobin.buffer, i64 0, i64 %41
%50 = bitcast i8* %49 to <16 x i8>*
store <16 x i8> %48, <16 x i8>* %50
br label %51
51: ; preds = %40, %44
%52 = icmp eq i64 %8, %5
br i1 %52, label %65, label %53
53: ; preds = %4, %51
%54 = phi i64 [ 0, %4 ], [ %8, %51 ]
br label %55
55: ; preds = %53, %55
%56 = phi i64 [ %63, %55 ], [ %54, %53 ]
%57 = trunc i64 %56 to i32
%58 = shl nuw i32 1, %57
%59 = and i32 %58, %1
%60 = icmp eq i32 %59, 0
%61 = select i1 %60, i8 48, i8 49
%62 = getelementptr inbounds [64 x i8], [64 x i8]* @tobin.buffer, i64 0, i64 %56
store i8 %61, i8* %62
%63 = add nuw nsw i64 %56, 1
%64 = icmp eq i64 %63, %5
br i1 %64, label %65, label %55
65: ; preds = %55, %51, %2
%66 = sext i32 %0 to i64
%67 = getelementptr inbounds [64 x i8], [64 x i8]* @tobin.buffer, i64 0, i64 %66
store i8 0, i8* %67
ret i8* getelementptr inbounds ([64 x i8], [64 x i8]* @tobin.buffer, i64 0, i64 0)
}