#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 int uint8_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
const char *byte_to_bin(uint8_t byte, int bits) {
  static bool allocated = false;
  static char *b;
  if (!allocated) {
    free(b);
  }
  b = (char *)malloc(sizeof(char) * bits);
  allocated = true;
  b[0] = '\0';
  for (int i = 1 << (bits - 1); i > 0; i >>= 1) {
    strcat(b, ((byte & i) == i) ? "1" : "0");
  }
  return b;
}


x86 assembly (clang, -O0):
<byte_to_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x0,0x2efb(%rip)
jne    1157 <byte_to_bin+0x27>
mov    0x2ef6(%rip),%rdi
call   1030 <free@plt>
movslq -0x8(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,0x2edb(%rip)
movl   $0x1,0x2ec9(%rip)
mov    0x2eca(%rip),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11d2 <byte_to_bin+0xa2>
mov    0x2ea6(%rip),%rdi
mov    -0x4(%rbp),%ecx
and    -0xc(%rbp),%ecx
mov    -0xc(%rbp),%edx
lea    0xe50(%rip),%rsi
lea    0xe47(%rip),%rax
cmp    %edx,%ecx
cmove  %rax,%rsi
call   1050 <strcat@plt>
mov    -0xc(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1191 <byte_to_bin+0x61>
mov    0x2e6f(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<byte_to_bin>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %edi,%r12d
cmpb   $0x0,0x2efb(%rip)
jne    1153 <byte_to_bin+0x23>
mov    0x2efa(%rip),%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   1040 <malloc@plt>
mov    %rax,0x2ee6(%rip)
movb   $0x1,0x2ed7(%rip)
movb   $0x0,(%rax)
add    $0xffffffff,%r14d
cmp    $0x1f,%r14d
je     11bf <byte_to_bin+0x8f>
mov    $0x1,%ebx
mov    %r14d,%ecx
shl    %cl,%ebx
not    %r12d
lea    0xe76(%rip),%r14
lea    0xe71(%rip),%r15
mov    %ebx,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x2ea1(%rip),%rdi
test   %r12d,%ebx
mov    %r15,%rsi
cmove  %r14,%rsi
call   1050 <strcat@plt>
shr    %ebp
cmp    $0x2,%ebx
mov    %ebp,%ebx
jae    11a0 <byte_to_bin+0x70>
mov    0x2e82(%rip),%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@byte_to_bin.allocated = internal global i32 0
@byte_to_bin.b = internal global i8* null
@.str = [2 x i8] c"1\00"
@.str.1 = [2 x i8] c"0\00"
define i8* @byte_to_bin(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* @byte_to_bin.allocated
%7 = icmp ne i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i8*, i8** @byte_to_bin.b
%10 = call i32 @free(i8* %9)
br label %11
11: ; preds = %8, %2
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = mul i64 1, %13
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** @byte_to_bin.b
store i32 1, i32* @byte_to_bin.allocated
%18 = load i8*, i8** @byte_to_bin.b
%19 = getelementptr inbounds i8, i8* %18, i64 0
store i8 0, i8* %19
%20 = load i32, i32* %4
%21 = sub nsw i32 %20, 1
%22 = shl i32 1, %21
store i32 %22, i32* %5
br label %23
23: ; preds = %36, %11
%24 = load i32, i32* %5
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %23
%27 = load i8*, i8** @byte_to_bin.b
%28 = load i32, i32* %3
%29 = load i32, i32* %5
%30 = and i32 %28, %29
%31 = load i32, i32* %5
%32 = icmp eq i32 %30, %31
%33 = zext i1 %32 to i64
%34 = select i1 %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%35 = call i32 @strcat(i8* %27, i8* %34)
br label %36
36: ; preds = %26
%37 = load i32, i32* %5
%38 = ashr i32 %37, 1
store i32 %38, i32* %5
br label %23
39: ; preds = %23
%40 = load i8*, i8** @byte_to_bin.b
ret i8* %40
}

LLVM IR (-O2):
@byte_to_bin.allocated = internal unnamed_addr global i1 false
@byte_to_bin.b = internal unnamed_addr global i8* null
@.str = [2 x i8] c"1\00"
@.str.1 = [2 x i8] c"0\00"
define i8* @byte_to_bin(i32 %0, i32 %1) {
%3 = load i1, i1* @byte_to_bin.allocated
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** @byte_to_bin.b
%6 = tail call i32 @free(i8* %5)
br label %7
7: ; preds = %4, %2
%8 = tail call i64 @malloc(i32 %1)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** @byte_to_bin.b
store i1 true, i1* @byte_to_bin.allocated
store i8 0, i8* %9
%10 = add nsw i32 %1, -1
%11 = icmp eq i32 %10, 31
br i1 %11, label %14, label %12
12: ; preds = %7
%13 = shl nuw i32 1, %10
br label %16
14: ; preds = %16, %7
%15 = load i8*, i8** @byte_to_bin.b
ret i8* %15
16: ; preds = %12, %16
%17 = phi i32 [ %23, %16 ], [ %13, %12 ]
%18 = load i8*, i8** @byte_to_bin.b
%19 = and i32 %17, %0
%20 = icmp eq i32 %19, %17
%21 = select i1 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%22 = tail call i32 @strcat(i8* %18, i8* %21)
%23 = lshr i32 %17, 1
%24 = icmp ult i32 %17, 2
br i1 %24, label %14, label %16
}