#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);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
char *BitsToString(char *Bits, int len) {
  if (Bits == NULL || len <= 0)
    return NULL;
  char *ret = (char *)malloc(len * 2 + 1);
  int i = 0;
  for (i = 0; i < len; i++) {
    sprintf(&(ret[i * 2]), "%02x", (unsigned char)Bits[i]);
  }
  ret[len * 2] = 0x0;
  return ret;
}


x86 assembly (clang, -O0):
<BitsToString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1144 <BitsToString+0x24>
cmpl   $0x0,-0x14(%rbp)
jg     1151 <BitsToString+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11cb <BitsToString+0xab>
mov    -0x14(%rbp),%edi
shl    $0x1,%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b2 <BitsToString+0x92>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
lea    0xe65(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1040 <sprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1171 <BitsToString+0x51>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BitsToString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdi,%rdi
je     1187 <BitsToString+0x67>
mov    %esi,%ebp
test   %esi,%esi
jle    1187 <BitsToString+0x67>
mov    %rdi,%r13
movslq %ebp,%rcx
add    %rcx,%rcx
mov    %rcx,(%rsp)
lea    0x1(%rcx),%edi
call   1030 <malloc@plt>
mov    %rax,%r14
mov    %ebp,%ebx
xor    %r12d,%r12d
lea    0xea3(%rip),%r15
mov    %rax,%rbp
movzbl 0x0(%r13,%r12,1),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1040 <sprintf@plt>
add    $0x1,%r12
add    $0x2,%rbp
cmp    %r12,%rbx
jne    1160 <BitsToString+0x40>
mov    (%rsp),%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
define i8* @BitsToString(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* %5
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %10, %2
store i8* null, i8** %3
br label %46
14: ; preds = %10
%15 = load i32, i32* %5
%16 = mul nsw i32 %15, 2
%17 = add nsw i32 %16, 1
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %6
store i32 0, i32* %7
store i32 0, i32* %7
br label %20
20: ; preds = %36, %14
%21 = load i32, i32* %7
%22 = load i32, i32* %5
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %39
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = mul nsw i32 %26, 2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
%30 = load i8*, i8** %4
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %34)
br label %36
36: ; preds = %24
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %20
39: ; preds = %20
%40 = load i8*, i8** %6
%41 = load i32, i32* %5
%42 = mul nsw i32 %41, 2
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
store i8 0, i8* %44
%45 = load i8*, i8** %6
store i8* %45, i8** %3
br label %46
46: ; preds = %39, %13
%47 = load i8*, i8** %3
ret i8* %47
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i8* @BitsToString(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp slt i32 %1, 1
%5 = or i1 %3, %4
br i1 %5, label %24, label %6
6: ; preds = %2
%7 = shl nuw nsw i32 %1, 1
%8 = or i32 %7, 1
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = zext i32 %1 to i64
br label %12
12: ; preds = %6, %12
%13 = phi i64 [ 0, %6 ], [ %19, %12 ]
%14 = shl nuw nsw i64 %13, 1
%15 = getelementptr inbounds i8, i8* %10, i64 %14
%16 = getelementptr inbounds i8, i8* %0, i64 %13
%17 = load i8, i8* %16
%18 = tail call i32 @sprintf(i8* dereferenceable(1) %15, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %17)
%19 = add nuw nsw i64 %13, 1
%20 = icmp eq i64 %19, %11
br i1 %20, label %21, label %12
21: ; preds = %12
%22 = sext i32 %7 to i64
%23 = getelementptr inbounds i8, i8* %10, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %2, %21
%25 = phi i8* [ %10, %21 ], [ null, %2 ]
ret i8* %25
}