char *bin(unsigned int x) {
  static char binval[32];
  unsigned int y = x;
  binval[0] = '\0';
  int z;
  for (z = 31; z >= 0; z--) {
    if (y % 2)
      binval[z] = '1';
    else
      binval[z] = '0';
    y /= 2;
  }
  return binval;
}


x86 assembly (clang, -O0):
<bin>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
movb   $0x0,0x2f1c(%rip)
movl   $0x1f,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     116e <bin+0x6e>
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1148 <bin+0x48>
movslq -0xc(%rbp),%rcx
lea    0x2ef1(%rip),%rax
movb   $0x31,(%rax,%rcx,1)
jmp    1157 <bin+0x57>
movslq -0xc(%rbp),%rcx
lea    0x2edd(%rip),%rax
movb   $0x30,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    111b <bin+0x1b>
lea    0x2ebb(%rip),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin>:
movb   $0x0,0x2f29(%rip)
mov    $0x20,%eax
lea    0x2f1d(%rip),%rsi
mov    %edi,%edx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %edi,%ecx
and    $0x1,%cl
or     $0x30,%cl
mov    %cl,-0x1(%rax,%rsi,1)
shr    $0x2,%edx
and    $0x2,%edi
cmp    $0x1,%edi
mov    $0x31,%cl
sbb    $0x0,%cl
mov    %cl,-0x2(%rax,%rsi,1)
mov    %edx,%edi
add    $0xfffffffffffffffe,%rax
jne    1120 <bin+0x20>
lea    0x2ee3(%rip),%rax
ret

LLVM IR (-O0):
@bin.binval = internal global [32 x i8] zeroinitializer
define i8* @bin(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
store i32 %5, i32* %3
store i8 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @bin.binval, i64 0, i64 0)
store i32 31, i32* %4
br label %6
6: ; preds = %24, %1
%7 = load i32, i32* %4
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %27
9: ; preds = %6
%10 = load i32, i32* %3
%11 = urem i32 %10, 2
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [32 x i8], [32 x i8]* @bin.binval, i64 0, i64 %15
store i8 49, i8* %16
br label %21
17: ; preds = %9
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [32 x i8], [32 x i8]* @bin.binval, i64 0, i64 %19
store i8 48, i8* %20
br label %21
21: ; preds = %17, %13
%22 = load i32, i32* %3
%23 = udiv i32 %22, 2
store i32 %23, i32* %3
br label %24
24: ; preds = %21
%25 = load i32, i32* %4
%26 = add nsw i32 %25, -1
store i32 %26, i32* %4
br label %6
27: ; preds = %6
ret i8* getelementptr inbounds ([32 x i8], [32 x i8]* @bin.binval, i64 0, i64 0)
}

LLVM IR (-O2):
@bin.binval = internal global [32 x i8] zeroinitializer
define i8* @bin(i32 %0) {
store i8 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @bin.binval, i64 0, i64 0)
br label %2
2: ; preds = %2, %1
%3 = phi i64 [ 31, %1 ], [ %15, %2 ]
%4 = phi i32 [ %0, %1 ], [ %9, %2 ]
%5 = and i32 %4, 1
%6 = icmp eq i32 %5, 0
%7 = select i1 %6, i8 48, i8 49
%8 = getelementptr inbounds [32 x i8], [32 x i8]* @bin.binval, i64 0, i64 %3
store i8 %7, i8* %8
%9 = lshr i32 %4, 2
%10 = add nsw i64 %3, -1
%11 = and i32 %4, 2
%12 = icmp eq i32 %11, 0
%13 = select i1 %12, i8 48, i8 49
%14 = getelementptr inbounds [32 x i8], [32 x i8]* @bin.binval, i64 0, i64 %10
store i8 %13, i8* %14
%15 = add nsw i64 %3, -2
%16 = icmp eq i64 %10, 0
br i1 %16, label %17, label %2
17: ; preds = %2
ret i8* getelementptr inbounds ([32 x i8], [32 x i8]* @bin.binval, i64 0, i64 0)
}