#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 */
scalar_t__ malloc(int);
char *btostr(unsigned int n, int numBits) {
  unsigned int m;
  char *result, *t;
  t = (char *)malloc(numBits + 1);
  result = t;
  m = 1 << (numBits - 1);
  while (m) {
    *t++ = (n & m) ? '1' : '0';
    m = m >> 1;
  }
  *t = '\0';
  return result;
}


x86 assembly (clang, -O0):
<btostr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1186 <btostr+0x76>
mov    -0x4(%rbp),%edx
and    -0xc(%rbp),%edx
mov    $0x30,%eax
mov    $0x31,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1145 <btostr+0x35>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<btostr>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
lea    0x1(%r14),%edi
call   1030 <malloc@plt>
add    $0xff,%r14b
mov    $0x1,%edx
mov    %r14d,%ecx
shl    %cl,%edx
mov    %rax,%rcx
mov    %edx,%esi
cs nopw 0x0(%rax,%rax,1)
nop
mov    %edx,%edi
and    %ebp,%edi
cmp    $0x1,%edi
mov    $0x31,%bl
sbb    $0x0,%bl
mov    %bl,(%rcx)
add    $0x1,%rcx
shr    %esi
cmp    $0x1,%edx
mov    %esi,%edx
ja     1140 <btostr+0x30>
movb   $0x0,(%rcx)
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @btostr(i32 %0, i32 %1) {
%3 = add nsw i32 %1, 1
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i8*
%6 = add nsw i32 %1, -1
%7 = shl nuw i32 1, %6
br label %8
8: ; preds = %2, %8
%9 = phi i8* [ %5, %2 ], [ %14, %8 ]
%10 = phi i32 [ %7, %2 ], [ %15, %8 ]
%11 = and i32 %10, %0
%12 = icmp eq i32 %11, 0
%13 = select i1 %12, i8 48, i8 49
%14 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %13, i8* %9
%15 = lshr i32 %10, 1
%16 = icmp ult i32 %10, 2
br i1 %16, label %17, label %8
17: ; preds = %8
store i8 0, i8* %14
ret i8* %5
}