void to_bin(int nbr, char *str) {
  int c, k;
  int i = 0;
  for (c = 31; c >= 0; c--) {
    k = nbr >> c;
    if (k & 1)
      str[i++] = '1';
    else
      str[i++] = '0';
  }
  str[i] = '\0';
}


x86 assembly (clang, -O0):
<to_bin>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1f,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1181 <to_bin+0x81>
mov    -0x4(%rbp),%eax
mov    -0x14(%rbp),%ecx
sar    %cl,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1158 <to_bin+0x58>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
movb   $0x31,(%rax,%rcx,1)
jmp    116e <to_bin+0x6e>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
movb   $0x30,(%rax,%rcx,1)
jmp    1173 <to_bin+0x73>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1119 <to_bin+0x19>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_bin>:
movd   %edi,%xmm0
pshufd $0x0,%xmm0,%xmm0
movdqa 0xeef(%rip),%xmm2
pand   %xmm0,%xmm2
movdqa 0xef3(%rip),%xmm3
pand   %xmm0,%xmm3
movdqa 0xef7(%rip),%xmm4
pand   %xmm0,%xmm4
movdqa 0xefb(%rip),%xmm5
pand   %xmm0,%xmm5
pxor   %xmm1,%xmm1
pcmpeqd %xmm1,%xmm5
pcmpeqd %xmm1,%xmm4
packssdw %xmm5,%xmm4
pcmpeqd %xmm1,%xmm3
pcmpeqd %xmm1,%xmm2
packssdw %xmm3,%xmm2
packsswb %xmm4,%xmm2
movdqa 0xedf(%rip),%xmm3
paddb  %xmm3,%xmm2
movdqu %xmm2,(%rsi)
movdqa 0xedf(%rip),%xmm2
pand   %xmm0,%xmm2
movdqa 0xee3(%rip),%xmm4
pand   %xmm0,%xmm4
movdqa 0xee7(%rip),%xmm5
pand   %xmm0,%xmm5
pand   0xeeb(%rip),%xmm0
pcmpeqd %xmm1,%xmm0
pcmpeqd %xmm1,%xmm5
packssdw %xmm0,%xmm5
pcmpeqd %xmm1,%xmm4
pcmpeqd %xmm1,%xmm2
packssdw %xmm4,%xmm2
packsswb %xmm5,%xmm2
paddb  %xmm3,%xmm2
movdqu %xmm2,0x10(%rsi)
movb   $0x0,0x20(%rsi)
ret

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

LLVM IR (-O2):
define void @to_bin(i32 %0, i8* nocapture writeonly %1) {
%3 = insertelement <16 x i32> poison, i32 %0, i64 0
%4 = shufflevector <16 x i32> %3, <16 x i32> poison, <16 x i32> zeroinitializer
%5 = and <16 x i32> %4, <i32 -2147483648, i32 1073741824, i32 536870912, i32 268435456, i32 134217728, i32 67108864, i32 33554432, i32 16777216, i32 8388608, i32 4194304, i32 2097152, i32 1048576, i32 524288, i32 262144, i32 131072, i32 65536>
%6 = icmp eq <16 x i32> %5, zeroinitializer
%7 = select <16 x i1> %6, <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>
%8 = bitcast i8* %1 to <16 x i8>*
store <16 x i8> %7, <16 x i8>* %8
%9 = and <16 x i32> %4, <i32 32768, i32 16384, i32 8192, i32 4096, i32 2048, i32 1024, i32 512, i32 256, i32 128, i32 64, i32 32, i32 16, i32 8, i32 4, i32 2, i32 1>
%10 = icmp eq <16 x i32> %9, zeroinitializer
%11 = select <16 x i1> %10, <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>
%12 = getelementptr inbounds i8, i8* %1, i64 16
%13 = bitcast i8* %12 to <16 x i8>*
store <16 x i8> %11, <16 x i8>* %13
%14 = getelementptr inbounds i8, i8* %1, i64 32
store i8 0, i8* %14
ret void
}