#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 */
char *calloc(int, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *bin2hex(const char *input) {
  char *output = calloc(9, sizeof(char));
  output[8] = 0;
  unsigned int hexnum;
  char temp[4];
  for (int i = 0; i < 8; ++i) {
    hexnum = 0;
    strncpy(temp, input + (i * 4), 4);
    for (int j = 0; j < 4; ++j) {
      hexnum += (temp[j] - '0') << (3 - j);
    }
    sprintf(output + i, "%X", hexnum);
  }
  return output;
}


x86 assembly (clang, -O0):
<bin2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x9,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,0x8(%rax)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x8,-0x1c(%rbp)
jge    11ef <bin2hex+0xbf>
movl   $0x0,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%eax
shl    $0x2,%eax
cltq
add    %rax,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x20(%rbp)
cmpl   $0x4,-0x20(%rbp)
jge    11c7 <bin2hex+0x97>
movslq -0x20(%rbp),%rax
movsbl -0x18(%rbp,%rax,1),%eax
sub    $0x30,%eax
mov    $0x3,%ecx
sub    -0x20(%rbp),%ecx
shl    %cl,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1193 <bin2hex+0x63>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%edx
lea    0xe24(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115e <bin2hex+0x2e>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin2hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r13
mov    $0x9,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%r14
xor    %ebx,%ebx
lea    0xc(%rsp),%r15
lea    0xea0(%rip),%r12
mov    %r15,%rdi
mov    %r13,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
movsbl 0xc(%rsp),%eax
shl    $0x3,%eax
movsbl 0xd(%rsp),%ecx
lea    (%rax,%rcx,4),%eax
movsbl 0xe(%rsp),%ecx
lea    (%rax,%rcx,2),%eax
movsbl 0xf(%rsp),%ecx
lea    (%rcx,%rax,1),%edx
add    $0xfffffd30,%edx
lea    (%r14,%rbx,1),%rdi
mov    %r12,%rsi
call   1050 <sprintf@plt>
add    $0x1,%rbx
add    $0x4,%r13
cmp    $0x8,%rbx
jne    1160 <bin2hex+0x30>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%X\00"
define noalias i8* @bin2hex(i8* %0) {
%2 = alloca [4 x i8]
%3 = tail call dereferenceable_or_null(9) i8* @calloc(i32 9, i32 1)
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 1
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 2
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 3
br label %9
8: ; preds = %9
ret i8* %3
9: ; preds = %1, %9
%10 = phi i64 [ 0, %1 ], [ %31, %9 ]
%11 = shl nuw nsw i64 %10, 2
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = call i32 @strncpy(i8* %4, i8* %12, i32 4)
%14 = load i8, i8* %4
%15 = sext i8 %14 to i32
%16 = shl nsw i32 %15, 3
%17 = load i8, i8* %5
%18 = sext i8 %17 to i32
%19 = shl nsw i32 %18, 2
%20 = add nsw i32 %19, %16
%21 = load i8, i8* %6
%22 = sext i8 %21 to i32
%23 = shl nsw i32 %22, 1
%24 = add nsw i32 %23, %20
%25 = load i8, i8* %7
%26 = sext i8 %25 to i32
%27 = add nsw i32 %24, %26
%28 = add nsw i32 %27, -720
%29 = getelementptr inbounds i8, i8* %3, i64 %10
%30 = call i32 @sprintf(i8* dereferenceable(1) %29, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %28)
%31 = add nuw nsw i64 %10, 1
%32 = icmp eq i64 %31, 8
br i1 %32, label %8, label %9
}