#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 */
scalar_t__ calloc(size_t, int);
int /*<<< orphan*/ sprintf(char *, char *, int const);
const char *Hex(const void *buf, size_t length) {
  char *id = (char *)calloc(length * 2 + 1, sizeof(char));
  for (int kH = 0; kH < length; kH++)
    sprintf(&(id[kH * 2]), "%02x", ((const uint8_t *)buf)[kH]);
  return id;
}


x86 assembly (clang, -O0):
<Hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x1,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1193 <Hex+0x73>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe80(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1151 <Hex+0x31>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%r14
lea    (%rsi,%rsi,1),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,%r15
test   %r13,%r13
je     117c <Hex+0x5c>
xor    %ebp,%ebp
lea    0xeac(%rip),%r12
mov    %r15,%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r14,%rbp,4),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <sprintf@plt>
add    $0x1,%rbp
add    $0x2,%rbx
cmp    %rbp,%r13
jne    1160 <Hex+0x40>
mov    %r15,%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* @Hex(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = mul i64 %7, 2
%9 = add i64 %8, 1
%10 = call i64 @calloc(i64 %9, i32 1)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
store i32 0, i32* %6
br label %12
12: ; preds = %30, %2
%13 = load i32, i32* %6
%14 = sext i32 %13 to i64
%15 = load i64, i64* %4
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %33
17: ; preds = %12
%18 = load i8*, i8** %5
%19 = load i32, i32* %6
%20 = mul nsw i32 %19, 2
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
%23 = load i8*, i8** %3
%24 = bitcast i8* %23 to i32*
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %17
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
store i32 %32, i32* %6
br label %12
33: ; preds = %12
%34 = load i8*, i8** %5
ret i8* %34
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i8* @Hex(i8* nocapture %0, i64 %1) {
%3 = shl i64 %1, 1
%4 = or i64 %3, 1
%5 = tail call i64 @calloc(i64 %4, i32 1)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %1, 0
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = bitcast i8* %0 to i32*
br label %11
10: ; preds = %11, %2
ret i8* %6
11: ; preds = %8, %11
%12 = phi i64 [ 0, %8 ], [ %18, %11 ]
%13 = shl nuw nsw i64 %12, 1
%14 = getelementptr inbounds i8, i8* %6, i64 %13
%15 = getelementptr inbounds i32, i32* %9, i64 %12
%16 = load i32, i32* %15
%17 = tail call i32 @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = add nuw i64 %12, 1
%19 = icmp eq i64 %18, %1
br i1 %19, label %10, label %11
}