#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 */
int strlen(char *);
char *bin2hex(char *data) {
  char const hex_chars[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
                              '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  char string[strlen(data) * 2];
  int i, j;
  for (i = 0; i < strlen(data); ++i) {
    string[i] = 0;
  }
  for (i = 0, j = 0; i < strlen(data); ++i, j += 2) {
    char const byte = data[i];
    string[j] = hex_chars[(byte & 0xF0) >> 4];
    string[j + 1] = hex_chars[(byte & 0x0F) >> 0];
  }
  string[strlen(data) * 2] = '\0';
  char *result = string;
  return result;
}


x86 assembly (clang, -O0):
<bin2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
movaps 0xedd(%rip),%xmm0
movaps %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
cmp    %ecx,%eax
jge    1192 <bin2hex+0x82>
mov    -0x50(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    115c <bin2hex+0x4c>
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%eax
cmp    %ecx,%eax
jge    121d <bin2hex+0x10d>
mov    -0x50(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0x34(%rbp),%rdx
mov    (%rcx,%rdx,1),%cl
mov    %cl,-0x39(%rbp)
movsbl -0x39(%rbp),%ecx
and    $0xf0,%ecx
sar    $0x4,%ecx
movslq %ecx,%rcx
mov    -0x20(%rbp,%rcx,1),%dl
movslq -0x38(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x39(%rbp),%ecx
and    $0xf,%ecx
sar    $0x0,%ecx
movslq %ecx,%rcx
mov    -0x20(%rbp,%rcx,1),%dl
mov    -0x38(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x38(%rbp)
jmp    11a0 <bin2hex+0x90>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
shl    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin2hex>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
add    %eax,%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1168 <bin2hex+0x58>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movb   $0x0,(%r15,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1150 <bin2hex+0x40>
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    11b5 <bin2hex+0xa5>
xor    %ebx,%ebx
lea    0xe83(%rip),%r12
nopl   (%rax)
movzbl (%r14,%rbx,1),%eax
mov    %rax,%rcx
shr    $0x4,%rcx
movzbl (%rcx,%r12,1),%ecx
mov    %cl,(%r15,%rbx,2)
and    $0xf,%eax
movzbl (%rax,%r12,1),%eax
mov    %al,0x1(%r15,%rbx,2)
add    $0x1,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1180 <bin2hex+0x70>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %r15,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.bin2hex.hex_chars = [16 x i8] c"0123456789ABCDEF"
define i8* @bin2hex(i8* %0) {
%2 = alloca i8*
%3 = alloca [16 x i8]
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8*
store i8* %0, i8** %2
%10 = bitcast [16 x i8]* %3 to i8*
%11 = load i8*, i8** %2
%12 = call i32 @strlen(i8* %11)
%13 = mul nsw i32 %12, 2
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %4
%16 = alloca i8, i64 %14
store i64 %14, i64* %5
store i32 0, i32* %6
br label %17
17: ; preds = %26, %1
%18 = load i32, i32* %6
%19 = load i8*, i8** %2
%20 = call i32 @strlen(i8* %19)
%21 = icmp slt i32 %18, %20
br i1 %21, label %22, label %29
22: ; preds = %17
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %16, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %22
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
br label %17
29: ; preds = %17
store i32 0, i32* %6
store i32 0, i32* %7
br label %30
30: ; preds = %62, %29
%31 = load i32, i32* %6
%32 = load i8*, i8** %2
%33 = call i32 @strlen(i8* %32)
%34 = icmp slt i32 %31, %33
br i1 %34, label %35, label %67
35: ; preds = %30
%36 = load i8*, i8** %2
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
store i8 %40, i8* %8
%41 = load i8, i8* %8
%42 = sext i8 %41 to i32
%43 = and i32 %42, 240
%44 = ashr i32 %43, 4
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = load i32, i32* %7
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %16, i64 %49
store i8 %47, i8* %50
%51 = load i8, i8* %8
%52 = sext i8 %51 to i32
%53 = and i32 %52, 15
%54 = ashr i32 %53, 0
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = load i32, i32* %7
%59 = add nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %16, i64 %60
store i8 %57, i8* %61
br label %62
62: ; preds = %35
%63 = load i32, i32* %6
%64 = add nsw i32 %63, 1
store i32 %64, i32* %6
%65 = load i32, i32* %7
%66 = add nsw i32 %65, 2
store i32 %66, i32* %7
br label %30
67: ; preds = %30
%68 = load i8*, i8** %2
%69 = call i32 @strlen(i8* %68)
%70 = mul nsw i32 %69, 2
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %16, i64 %71
store i8 0, i8* %72
store i8* %16, i8** %9
%73 = load i8*, i8** %9
%74 = load i8*, i8** %4
ret i8* %73
}

LLVM IR (-O2):
@__const.bin2hex.hex_chars = [16 x i8] c"0123456789ABCDEF"
define noalias i8* @bin2hex(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = shl nsw i32 %2, 1
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %11, %1
%9 = tail call i32 @strlen(i8* %0)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %18, label %40
11: ; preds = %1, %11
%12 = phi i64 [ %14, %11 ], [ 0, %1 ]
%13 = getelementptr inbounds i8, i8* %5, i64 %12
store i8 0, i8* %13
%14 = add nuw nsw i64 %12, 1
%15 = tail call i32 @strlen(i8* %0)
%16 = sext i32 %15 to i64
%17 = icmp slt i64 %14, %16
br i1 %17, label %11, label %8
18: ; preds = %8, %18
%19 = phi i64 [ %36, %18 ], [ 0, %8 ]
%20 = phi i64 [ %35, %18 ], [ 0, %8 ]
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = lshr i32 %23, 4
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds [16 x i8], [16 x i8]* @__const.bin2hex.hex_chars, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = getelementptr inbounds i8, i8* %5, i64 %19
store i8 %27, i8* %28
%29 = and i32 %23, 15
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds [16 x i8], [16 x i8]* @__const.bin2hex.hex_chars, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = or i64 %19, 1
%34 = getelementptr inbounds i8, i8* %5, i64 %33
store i8 %32, i8* %34
%35 = add nuw nsw i64 %20, 1
%36 = add nuw nsw i64 %19, 2
%37 = tail call i32 @strlen(i8* %0)
%38 = sext i32 %37 to i64
%39 = icmp slt i64 %35, %38
br i1 %39, label %18, label %40
40: ; preds = %18, %8
%41 = tail call i32 @strlen(i8* %0)
ret i8* %5
}