#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ bin;

/* Variables and functions */
char *malloc(int);
long *realloc(long *, int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, long);
long *bin2bits(long bin, long *bits) {
  size_t i = 0;
  size_t length = sizeof(bin) * 8;
  char *buffer = malloc(sizeof(*buffer) * length);
  bits = realloc(bits, sizeof(*bits) * length);
  if ((buffer != NULL) && (bits != NULL)) {
    snprintf(buffer, length, "%ld", bin);
    while (*buffer) {
      bits[i] = *buffer - '0';
      i++;
      buffer++;
    }
  }
  return bits;
}


x86 assembly (clang, -O0):
<bin2bits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x40,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f0 <bin2bits+0xc0>
cmpq   $0x0,-0x10(%rbp)
je     11f0 <bin2bits+0xc0>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rcx
lea    0xe5d(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     11eb <bin2bits+0xbb>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
sub    $0x30,%eax
movslq %eax,%rdx
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11a8 <bin2bits+0x78>
jmp    11f0 <bin2bits+0xc0>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin2bits>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r15
mov    $0x40,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x200,%esi
call   1050 <realloc@plt>
mov    %rax,%rbx
test   %r14,%r14
je     11aa <bin2bits+0x7a>
test   %rbx,%rbx
je     11aa <bin2bits+0x7a>
lea    0xe97(%rip),%rdx
mov    $0x40,%esi
mov    %r14,%rdi
mov    %r15,%rcx
call   1030 <snprintf@plt>
mov    (%r14),%al
test   %al,%al
je     11aa <bin2bits+0x7a>
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbq %al,%rax
add    $0xffffffffffffffd0,%rax
mov    %rax,(%rbx,%rcx,8)
movzbl 0x1(%r14,%rcx,1),%eax
add    $0x1,%rcx
test   %al,%al
jne    1190 <bin2bits+0x60>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%ld\00"
define i64* @bin2bits(i64 %0, i64* %1) {
%3 = alloca i64
%4 = alloca i64*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i64 %0, i64* %3
store i64* %1, i64** %4
store i64 0, i64* %5
store i64 64, i64* %6
%8 = load i64, i64* %6
%9 = mul i64 1, %8
%10 = trunc i64 %9 to i32
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %7
%12 = load i64*, i64** %4
%13 = load i64, i64* %6
%14 = mul i64 8, %13
%15 = trunc i64 %14 to i32
%16 = call i64* @realloc(i64* %12, i32 %15)
store i64* %16, i64** %4
%17 = load i8*, i8** %7
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %45
19: ; preds = %2
%20 = load i64*, i64** %4
%21 = icmp ne i64* %20, null
br i1 %21, label %22, label %45
22: ; preds = %19
%23 = load i8*, i8** %7
%24 = load i64, i64* %6
%25 = load i64, i64* %3
%26 = call i32 @snprintf(i8* %23, i64 %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %25)
br label %27
27: ; preds = %31, %22
%28 = load i8*, i8** %7
%29 = load i8, i8* %28
%30 = icmp ne i8 %29, 0
br i1 %30, label %31, label %44
31: ; preds = %27
%32 = load i8*, i8** %7
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = sub nsw i32 %34, 48
%36 = sext i32 %35 to i64
%37 = load i64*, i64** %4
%38 = load i64, i64* %5
%39 = getelementptr inbounds i64, i64* %37, i64 %38
store i64 %36, i64* %39
%40 = load i64, i64* %5
%41 = add i64 %40, 1
store i64 %41, i64* %5
%42 = load i8*, i8** %7
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %7
br label %27
44: ; preds = %27
br label %45
45: ; preds = %44, %19, %2
%46 = load i64*, i64** %4
ret i64* %46
}

LLVM IR (-O2):
@.str = [4 x i8] c"%ld\00"
define i64* @bin2bits(i64 %0, i64* %1) {
%3 = tail call dereferenceable_or_null(64) i8* @malloc(i32 64)
%4 = tail call i64* @realloc(i64* %1, i32 512)
%5 = icmp ne i8* %3, null
%6 = icmp ne i64* %4, null
%7 = select i1 %5, i1 %6, i1 false
br i1 %7, label %8, label %23
8: ; preds = %2
%9 = tail call i32 @snprintf(i8* %3, i64 64, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %0)
%10 = load i8, i8* %3
%11 = icmp eq i8 %10, 0
br i1 %11, label %23, label %12
12: ; preds = %8, %12
%13 = phi i8 [ %21, %12 ], [ %10, %8 ]
%14 = phi i8* [ %20, %12 ], [ %3, %8 ]
%15 = phi i64 [ %19, %12 ], [ 0, %8 ]
%16 = sext i8 %13 to i64
%17 = add nsw i64 %16, -48
%18 = getelementptr inbounds i64, i64* %4, i64 %15
store i64 %17, i64* %18
%19 = add i64 %15, 1
%20 = getelementptr inbounds i8, i8* %14, i64 1
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %12
23: ; preds = %12, %8, %2
ret i64* %4
}