#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 *malloc(size_t);
char nibbleToChar(int);
char *bintohex(uint8_t *bytes, size_t buflen) {
  char *retval;
  int i;
  buflen = buflen * 2;
  retval = malloc(buflen * 2 + 1);
  for (i = 0; i < buflen; i++) {
    retval[i * 2] = nibbleToChar(bytes[i] >> 4);
    retval[i * 2 + 1] = nibbleToChar(bytes[i] & 0x0f);
  }
  retval[i] = '\0';
  return retval;
}


x86 assembly (clang, -O0):
<bintohex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x1,%rdi
add    $0x1,%rdi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11c1 <bintohex+0xa1>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
sar    $0x4,%edi
call   1040 <nibbleToChar@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
and    $0xf,%edi
call   1040 <nibbleToChar@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1158 <bintohex+0x38>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bintohex>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
lea    0x1(,%rsi,4),%rdi
add    %rsi,%r14
call   1030 <malloc@plt>
mov    %rax,%r15
test   %r14,%r14
je     1180 <bintohex+0x60>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r12,%rbx,4),%edi
sar    $0x4,%edi
call   1040 <nibbleToChar@plt>
mov    %al,(%r15,%rbx,2)
mov    (%r12,%rbx,4),%edi
and    $0xf,%edi
call   1040 <nibbleToChar@plt>
mov    %al,0x1(%r15,%rbx,2)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1150 <bintohex+0x30>
and    $0xfffffffe,%r14d
jmp    1183 <bintohex+0x63>
xor    %r14d,%r14d
movb   $0x0,(%r15,%r14,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @bintohex(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
store i32* %0, i32** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = mul i64 %7, 2
store i64 %8, i64* %4
%9 = load i64, i64* %4
%10 = mul i64 %9, 2
%11 = add i64 %10, 1
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %5
store i32 0, i32* %6
br label %13
13: ; preds = %44, %2
%14 = load i32, i32* %6
%15 = sext i32 %14 to i64
%16 = load i64, i64* %4
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %47
18: ; preds = %13
%19 = load i32*, i32** %3
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = load i32, i32* %22
%24 = ashr i32 %23, 4
%25 = call signext i8 @nibbleToChar(i32 %24)
%26 = load i8*, i8** %5
%27 = load i32, i32* %6
%28 = mul nsw i32 %27, 2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
store i8 %25, i8* %30
%31 = load i32*, i32** %3
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
%35 = load i32, i32* %34
%36 = and i32 %35, 15
%37 = call signext i8 @nibbleToChar(i32 %36)
%38 = load i8*, i8** %5
%39 = load i32, i32* %6
%40 = mul nsw i32 %39, 2
%41 = add nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %38, i64 %42
store i8 %37, i8* %43
br label %44
44: ; preds = %18
%45 = load i32, i32* %6
%46 = add nsw i32 %45, 1
store i32 %46, i32* %6
br label %13
47: ; preds = %13
%48 = load i8*, i8** %5
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %5
ret i8* %52
}

LLVM IR (-O2):
define noalias i8* @bintohex(i32* nocapture %0, i64 %1) {
%3 = shl i64 %1, 1
%4 = shl i64 %1, 2
%5 = or i64 %4, 1
%6 = tail call i8* @malloc(i64 %5)
%7 = icmp eq i64 %3, 0
br i1 %7, label %25, label %8
8: ; preds = %2, %8
%9 = phi i64 [ %21, %8 ], [ 0, %2 ]
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = load i32, i32* %10
%12 = ashr i32 %11, 4
%13 = tail call signext i8 @nibbleToChar(i32 %12)
%14 = shl nuw nsw i64 %9, 1
%15 = getelementptr inbounds i8, i8* %6, i64 %14
store i8 %13, i8* %15
%16 = load i32, i32* %10
%17 = and i32 %16, 15
%18 = tail call signext i8 @nibbleToChar(i32 %17)
%19 = or i64 %14, 1
%20 = getelementptr inbounds i8, i8* %6, i64 %19
store i8 %18, i8* %20
%21 = add nuw i64 %9, 1
%22 = icmp eq i64 %21, %3
br i1 %22, label %23, label %8
23: ; preds = %8
%24 = and i64 %3, 4294967294
br label %25
25: ; preds = %23, %2
%26 = phi i64 [ 0, %2 ], [ %24, %23 ]
%27 = getelementptr inbounds i8, i8* %6, i64 %26
store i8 0, i8* %27
ret i8* %6
}