#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 */
char *calloc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
uint8_t *bin_to_ascii(uint8_t *hash, int len) {
  int idx_hash;
  char *token, *token_ptr;
  if (!hash || len <= 0) {
    fprintf(stderr, "error: Bad parameter(s)\n");
    return NULL;
  }
  token = calloc(len * 2 + 1, sizeof(*token));
  if (!token) {
    fprintf(stderr, "error: bin_to_ascii(): Failed allocating token\n");
    return NULL;
  }
  token_ptr = token;
  for (idx_hash = 0; idx_hash < len; idx_hash++) {
    snprintf(token_ptr, 3, "%02x", hash[idx_hash]);
    token_ptr += 2;
  }
  return token;
}


x86 assembly (clang, -O0):
<bin_to_ascii>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1154 <bin_to_ascii+0x24>
cmpl   $0x0,-0x14(%rbp)
jg     1176 <bin_to_ascii+0x46>
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
lea    0xe9c(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1217 <bin_to_ascii+0xe7>
mov    -0x14(%rbp),%edi
shl    $0x1,%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ba <bin_to_ascii+0x8a>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1217 <bin_to_ascii+0xe7>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    120f <bin_to_ascii+0xdf>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    $0x3,%esi
lea    0xe59(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c9 <bin_to_ascii+0x99>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin_to_ascii>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rdi,%rdi
je     1190 <bin_to_ascii+0x60>
mov    %esi,%r15d
test   %esi,%esi
jle    1190 <bin_to_ascii+0x60>
mov    %rdi,%r14
lea    (%r15,%r15,1),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     11a2 <bin_to_ascii+0x72>
mov    %rax,%r12
mov    %r15d,%r13d
add    %r13,%r13
xor    %ebx,%ebx
lea    0xed9(%rip),%r15
lea    (%r12,%rbx,1),%rdi
mov    (%r14,%rbx,2),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1030 <snprintf@plt>
add    $0x2,%rbx
cmp    %rbx,%r13
jne    1170 <bin_to_ascii+0x40>
jmp    11ba <bin_to_ascii+0x8a>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
jmp    11b2 <bin_to_ascii+0x82>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1050 <fprintf@plt>
xor    %r12d,%r12d
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"error: Bad parameter(s)\0A\00"
@.str.1 = [48 x i8] c"error: bin_to_ascii(): Failed allocating token\0A\00"
@.str.2 = [5 x i8] c"%02x\00"
define i32* @bin_to_ascii(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i32* %0, i32** %4
store i32 %1, i32* %5
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11, %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %3
br label %49
17: ; preds = %11
%18 = load i32, i32* %5
%19 = mul nsw i32 %18, 2
%20 = add nsw i32 %19, 1
%21 = call i8* @calloc(i32 %20, i32 1)
store i8* %21, i8** %7
%22 = load i8*, i8** %7
%23 = icmp ne i8* %22, null
br i1 %23, label %27, label %24
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
store i32* null, i32** %3
br label %49
27: ; preds = %17
%28 = load i8*, i8** %7
store i8* %28, i8** %8
store i32 0, i32* %6
br label %29
29: ; preds = %43, %27
%30 = load i32, i32* %6
%31 = load i32, i32* %5
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %46
33: ; preds = %29
%34 = load i8*, i8** %8
%35 = load i32*, i32** %4
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = load i32, i32* %38
%40 = call i32 @snprintf(i8* %34, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = load i8*, i8** %8
%42 = getelementptr inbounds i8, i8* %41, i64 2
store i8* %42, i8** %8
br label %43
43: ; preds = %33
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %29
46: ; preds = %29
%47 = load i8*, i8** %7
%48 = bitcast i8* %47 to i32*
store i32* %48, i32** %3
br label %49
49: ; preds = %46, %24, %14
%50 = load i32*, i32** %3
ret i32* %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"error: Bad parameter(s)\0A\00"
@.str.1 = [48 x i8] c"error: bin_to_ascii(): Failed allocating token\0A\00"
@.str.2 = [5 x i8] c"%02x\00"
define i32* @bin_to_ascii(i32* %0, i32 %1) {
%3 = icmp eq i32* %0, null
%4 = icmp slt i32 %1, 1
%5 = or i1 %3, %4
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %30
9: ; preds = %2
%10 = shl nuw nsw i32 %1, 1
%11 = or i32 %10, 1
%12 = tail call i8* @calloc(i32 %11, i32 1)
%13 = icmp eq i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = zext i32 %1 to i64
br label %19
16: ; preds = %9
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
br label %30
19: ; preds = %14, %19
%20 = phi i64 [ 0, %14 ], [ %26, %19 ]
%21 = phi i8* [ %12, %14 ], [ %25, %19 ]
%22 = getelementptr inbounds i32, i32* %0, i64 %20
%23 = load i32, i32* %22
%24 = tail call i32 @snprintf(i8* %21, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds i8, i8* %21, i64 2
%26 = add nuw nsw i64 %20, 1
%27 = icmp eq i64 %26, %15
br i1 %27, label %28, label %19
28: ; preds = %19
%29 = bitcast i8* %12 to i32*
br label %30
30: ; preds = %28, %16, %6
%31 = phi i32* [ null, %6 ], [ %29, %28 ], [ null, %16 ]
ret i32* %31
}