#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 /*<<< orphan*/ printf(char *, unsigned int);
void printbits(unsigned n) {
  int k = 1;
  for (k = 1; n / k; k *= 2)
    ;
  while (k > 0) {
    printf("%d", n / k);
    n %= k;
    k /= 2;
  }
}


x86 assembly (clang, -O0):
<printbits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
movl   $0x1,-0x8(%rbp)
mov    -0x4(%rbp),%eax
xor    %edx,%edx
divl   -0x8(%rbp)
cmp    $0x0,%eax
je     114d <printbits+0x3d>
jmp    113f <printbits+0x2f>
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1129 <printbits+0x19>
jmp    1152 <printbits+0x42>
cmpl   $0x0,-0x8(%rbp)
jle    1192 <printbits+0x82>
mov    -0x4(%rbp),%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %eax,%esi
lea    0xe93(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%ecx
mov    -0x4(%rbp),%eax
xor    %edx,%edx
div    %ecx
mov    %edx,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
jmp    1152 <printbits+0x42>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printbits>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
mov    $0x1,%eax
nop
mov    %eax,%ebx
lea    (%rbx,%rbx,1),%eax
cmp    %r15d,%ebx
jbe    1120 <printbits+0x10>
test   %ebx,%ebx
jle    115d <printbits+0x4d>
lea    0xecb(%rip),%r14
mov    %ebx,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r15d,%eax
xor    %edx,%edx
div    %ebx
mov    %edx,%r15d
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
shr    %ebp
cmp    $0x1,%ebx
mov    %ebp,%ebx
ja     1140 <printbits+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define void @printbits(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %3
store i32 1, i32* %3
br label %4
4: ; preds = %10, %1
%5 = load i32, i32* %2
%6 = load i32, i32* %3
%7 = udiv i32 %5, %6
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %4
br label %10
10: ; preds = %9
%11 = load i32, i32* %3
%12 = mul nsw i32 %11, 2
store i32 %12, i32* %3
br label %4
13: ; preds = %4
br label %14
14: ; preds = %17, %13
%15 = load i32, i32* %3
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %14
%18 = load i32, i32* %2
%19 = load i32, i32* %3
%20 = udiv i32 %18, %19
%21 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %3
%23 = load i32, i32* %2
%24 = urem i32 %23, %22
store i32 %24, i32* %2
%25 = load i32, i32* %3
%26 = sdiv i32 %25, 2
store i32 %26, i32* %3
br label %14
27: ; preds = %14
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define void @printbits(i32 %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i32 [ 1, %1 ], [ %5, %2 ]
%4 = icmp ugt i32 %3, %0
%5 = shl nsw i32 %3, 1
br i1 %4, label %6, label %2
6: ; preds = %2
%7 = icmp sgt i32 %3, 0
br i1 %7, label %8, label %16
8: ; preds = %6, %8
%9 = phi i32 [ %14, %8 ], [ %3, %6 ]
%10 = phi i32 [ %13, %8 ], [ %0, %6 ]
%11 = udiv i32 %10, %9
%12 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = urem i32 %10, %9
%14 = lshr i32 %9, 1
%15 = icmp ult i32 %9, 2
br i1 %15, label %16, label %8
16: ; preds = %8, %6
ret void
}