#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 long long uint64_t;

/* Variables and functions */
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
void print_binary(uint64_t val, int bits) {
  int bit;
  int leading = 1;
  while (--bits >= 0) {
    bit = ((val & (1LL << bits)) != 0LL);
    if (leading && (bits != 0) && !bit)
      continue;
    leading = 0;
    fputc('0' + bit, stdout);
  }
}


x86 assembly (clang, -O0):
<print_binary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jl     119f <print_binary+0x8f>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%rdx
mov    %rdx,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     117f <print_binary+0x6f>
cmpl   $0x0,-0xc(%rbp)
je     117f <print_binary+0x6f>
cmpl   $0x0,-0x10(%rbp)
jne    117f <print_binary+0x6f>
jmp    1126 <print_binary+0x16>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%edi
add    $0x30,%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1030 <fputc@plt>
jmp    1126 <print_binary+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_binary>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
xor    %eax,%eax
mov    0x2ebd(%rip),%r15
mov    %ebx,%ebx
add    $0xffffffffffffffff,%rbx
nopl   0x0(%rax)
lea    0x1(%rbx),%ecx
test   %ecx,%ecx
jle    1172 <print_binary+0x62>
mov    $0x1,%edx
mov    %ebx,%ecx
shl    %cl,%rdx
and    %r14,%rdx
sub    $0x1,%rbx
setb   %cl
or     %al,%cl
test   $0x1,%cl
jne    1157 <print_binary+0x47>
test   %rdx,%rdx
je     1130 <print_binary+0x20>
xor    %edi,%edi
test   %rdx,%rdx
setne  %dil
add    $0x1,%ebx
or     $0x30,%edi
mov    (%r15),%esi
call   1030 <fputc@plt>
mov    $0x1,%al
jmp    1123 <print_binary+0x13>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdout = global i32 0
define void @print_binary(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i64 %0, i64* %3
store i32 %1, i32* %4
store i32 1, i32* %6
br label %7
7: ; preds = %28, %27, %2
%8 = load i32, i32* %4
%9 = add nsw i32 %8, -1
store i32 %9, i32* %4
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %33
11: ; preds = %7
%12 = load i64, i64* %3
%13 = load i32, i32* %4
%14 = zext i32 %13 to i64
%15 = shl i64 1, %14
%16 = and i64 %12, %15
%17 = icmp ne i64 %16, 0
%18 = zext i1 %17 to i32
store i32 %18, i32* %5
%19 = load i32, i32* %6
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %11
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %24
br label %7
28: ; preds = %24, %21, %11
store i32 0, i32* %6
%29 = load i32, i32* %5
%30 = add nsw i32 48, %29
%31 = load i32, i32* @stdout
%32 = call i32 @fputc(i32 %30, i32 %31)
br label %7
33: ; preds = %7
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
define void @print_binary(i64 %0, i32 %1) {
br label %3
3: ; preds = %19, %2
%4 = phi i32 [ %20, %19 ], [ %1, %2 ]
%5 = phi i1 [ true, %19 ], [ false, %2 ]
%6 = zext i32 %4 to i64
br label %7
7: ; preds = %3, %12
%8 = phi i64 [ %6, %3 ], [ %9, %12 ]
%9 = add nsw i64 %8, -1
%10 = trunc i64 %8 to i32
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %7
%13 = shl nuw i64 1, %9
%14 = and i64 %13, %0
%15 = icmp ne i64 %14, 0
%16 = icmp eq i64 %9, 0
%17 = select i1 %5, i1 true, i1 %16
%18 = select i1 %17, i1 true, i1 %15
br i1 %18, label %19, label %7
19: ; preds = %12
%20 = trunc i64 %9 to i32
%21 = select i1 %15, i32 49, i32 48
%22 = load i32, i32* @stdout
%23 = tail call i32 @fputc(i32 %21, i32 %22)
br label %3
24: ; preds = %7
ret void
}