#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 */

/* Variables and functions */
int getbinarydigit(unsigned long);
char *getbinary(unsigned long n, int d) {
  static char str[1024] = {0};
  int i;
  if (d == 0)
    d = getbinarydigit(n);
  for (i = d - 1; i >= 0; --i) {
    if (n & 1)
      str[i] = '1';
    else
      str[i] = '0';
    n >>= 1;
  }
  str[d] = 0;
  return str;
}


x86 assembly (clang, -O0):
<getbinary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1135 <getbinary+0x25>
mov    -0x8(%rbp),%rdi
call   1030 <getbinarydigit@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1197 <getbinary+0x87>
mov    -0x8(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     116e <getbinary+0x5e>
movslq -0x10(%rbp),%rcx
lea    0x2edb(%rip),%rax
movb   $0x31,(%rax,%rcx,1)
jmp    117d <getbinary+0x6d>
movslq -0x10(%rbp),%rcx
lea    0x2ec7(%rip),%rax
movb   $0x30,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    113e <getbinary+0x2e>
movslq -0xc(%rbp),%rcx
lea    0x2e9e(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e93(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbinary>:
push   %rbx
mov    %rdi,%rbx
test   %esi,%esi
jne    1122 <getbinary+0x12>
mov    %rbx,%rdi
call   1030 <getbinarydigit@plt>
mov    %eax,%esi
test   %esi,%esi
jle    1196 <getbinary+0x86>
mov    %esi,%eax
test   $0x1,%al
je     1147 <getbinary+0x37>
add    $0xffffffffffffffff,%rax
mov    %eax,%ecx
lea    0x2f07(%rip),%rdi
mov    %ebx,%edx
and    $0x1,%dl
or     $0x30,%dl
mov    %dl,(%rcx,%rdi,1)
shr    %rbx
cmp    $0x1,%esi
je     1196 <getbinary+0x86>
add    $0xfffffffffffffffe,%rax
lea    0x2ee9(%rip),%r8
mov    %rbx,%rdx
nopw   0x0(%rax,%rax,1)
lea    0x1(%rax),%r9
mov    %r9d,%edi
mov    %ebx,%ecx
and    $0x1,%cl
or     $0x30,%cl
mov    %cl,(%rdi,%r8,1)
shr    $0x2,%rdx
and    $0x2,%ebx
cmp    $0x1,%rbx
mov    $0x31,%cl
sbb    $0x0,%cl
mov    %eax,%edi
mov    %cl,(%rdi,%r8,1)
add    $0xfffffffffffffffe,%rax
mov    %rdx,%rbx
cmp    $0x1,%r9
ja     1160 <getbinary+0x50>
movslq %esi,%rcx
lea    0x2ea0(%rip),%rax
movb   $0x0,(%rcx,%rax,1)
pop    %rbx
ret

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

LLVM IR (-O2):
@getbinary.str = internal global [1024 x i8] zeroinitializer
define i8* @getbinary(i64 %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @getbinarydigit(i64 %0)
br label %6
6: ; preds = %4, %2
%7 = phi i32 [ %5, %4 ], [ %1, %2 ]
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %42
9: ; preds = %6
%10 = zext i32 %7 to i64
%11 = and i64 %10, 1
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %9
%14 = add nsw i64 %10, -1
%15 = and i64 %0, 1
%16 = icmp eq i64 %15, 0
%17 = and i64 %14, 4294967295
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* @getbinary.str, i64 0, i64 %17
%19 = select i1 %16, i8 48, i8 49
store i8 %19, i8* %18
%20 = lshr i64 %0, 1
br label %21
21: ; preds = %13, %9
%22 = phi i64 [ %10, %9 ], [ %14, %13 ]
%23 = phi i64 [ %0, %9 ], [ %20, %13 ]
%24 = icmp eq i32 %7, 1
br i1 %24, label %42, label %25
25: ; preds = %21, %25
%26 = phi i64 [ %35, %25 ], [ %22, %21 ]
%27 = phi i64 [ %34, %25 ], [ %23, %21 ]
%28 = add nsw i64 %26, -1
%29 = and i64 %27, 1
%30 = icmp eq i64 %29, 0
%31 = and i64 %28, 4294967295
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* @getbinary.str, i64 0, i64 %31
%33 = select i1 %30, i8 48, i8 49
store i8 %33, i8* %32
%34 = lshr i64 %27, 2
%35 = add nsw i64 %26, -2
%36 = and i64 %27, 2
%37 = icmp eq i64 %36, 0
%38 = and i64 %35, 4294967295
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* @getbinary.str, i64 0, i64 %38
%40 = select i1 %37, i8 48, i8 49
store i8 %40, i8* %39
%41 = icmp ugt i64 %28, 1
br i1 %41, label %25, label %42
42: ; preds = %21, %25, %6
%43 = sext i32 %7 to i64
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* @getbinary.str, i64 0, i64 %43
store i8 0, i8* %44
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @getbinary.str, i64 0, i64 0)
}