#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
char *hex_image(const void *base, size_t size) {
  static char *hex = "0123456789abcdef";
  size_t i;
  const unsigned char *data = base;
  char *result = malloc(2 * size + 1);
  int out_index = 0;
  if (result == NULL) {
    fprintf(stderr, "malloc failed\n");
    exit(EXIT_FAILURE);
  }
  for (i = 0; i < size; i++) {
    result[out_index++] = hex[data[i] / 16];
    result[out_index++] = hex[data[i] % 16];
  }
  result[out_index] = '\0';
  return result;
}


x86 assembly (clang, -O0):
<hex_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1194 <hex_image+0x64>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1239 <hex_image+0x109>
mov    0x2e87(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    0x2e47(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    -0x38(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    119c <hex_image+0x6c>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_image>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    (%rsi,%rsi,1),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     119a <hex_image+0x6a>
xor    %ecx,%ecx
test   %r15,%r15
je     1190 <hex_image+0x60>
lea    0xea7(%rip),%r8
xor    %esi,%esi
nopl   0x0(%rax,%rax,1)
movzbl (%r14,%rsi,1),%ebx
mov    %rbx,%rdi
shr    $0x4,%rdi
movzbl (%rdi,%r8,1),%edx
mov    %dl,(%rax,%rcx,1)
and    $0xf,%ebx
movzbl (%rbx,%r8,1),%edx
mov    %dl,0x1(%rax,%rcx,1)
add    $0x2,%rcx
add    $0x1,%rsi
cmp    %rsi,%r15
jne    1160 <hex_image+0x30>
and    $0xfffffffe,%ecx
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@hex_image.hex = internal global i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i32 0, i32 0)
@.str = [17 x i8] c"0123456789abcdef\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"malloc failed\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @hex_image(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%9 = load i8*, i8** %3
store i8* %9, i8** %6
%10 = load i64, i64* %4
%11 = mul i64 2, %10
%12 = add i64 %11, 1
%13 = trunc i64 %12 to i32
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %7
store i32 0, i32* %8
%15 = load i8*, i8** %7
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
store i64 0, i64* %5
br label %23
23: ; preds = %58, %22
%24 = load i64, i64* %5
%25 = load i64, i64* %4
%26 = icmp ult i64 %24, %25
br i1 %26, label %27, label %61
27: ; preds = %23
%28 = load i8*, i8** @hex_image.hex
%29 = load i8*, i8** %6
%30 = load i64, i64* %5
%31 = getelementptr inbounds i8, i8* %29, i64 %30
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = sdiv i32 %33, 16
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %28, i64 %35
%37 = load i8, i8* %36
%38 = load i8*, i8** %7
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
store i8 %37, i8* %42
%43 = load i8*, i8** @hex_image.hex
%44 = load i8*, i8** %6
%45 = load i64, i64* %5
%46 = getelementptr inbounds i8, i8* %44, i64 %45
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = srem i32 %48, 16
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %43, i64 %50
%52 = load i8, i8* %51
%53 = load i8*, i8** %7
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 1
store i32 %55, i32* %8
%56 = sext i32 %54 to i64
%57 = getelementptr inbounds i8, i8* %53, i64 %56
store i8 %52, i8* %57
br label %58
58: ; preds = %27
%59 = load i64, i64* %5
%60 = add i64 %59, 1
store i64 %60, i64* %5
br label %23
61: ; preds = %23
%62 = load i8*, i8** %7
%63 = load i32, i32* %8
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 0, i8* %65
%66 = load i8*, i8** %7
ret i8* %66
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"malloc failed\0A\00"
@EXIT_FAILURE = global i32 0
define noalias i8* @hex_image(i8* nocapture %0, i64 %1) {
%3 = trunc i64 %1 to i32
%4 = shl i32 %3, 1
%5 = or i32 %4, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = icmp eq i64 %1, 0
br i1 %9, label %36, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %8, %15
%16 = phi i64 [ %30, %15 ], [ 0, %8 ]
%17 = phi i64 [ %32, %15 ], [ 0, %8 ]
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = lshr i8 %19, 4
%21 = zext i8 %20 to i64
%22 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = or i64 %16, 1
%25 = getelementptr inbounds i8, i8* %6, i64 %16
store i8 %23, i8* %25
%26 = and i8 %19, 15
%27 = zext i8 %26 to i64
%28 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = add nuw i64 %16, 2
%31 = getelementptr inbounds i8, i8* %6, i64 %24
store i8 %29, i8* %31
%32 = add nuw i64 %17, 1
%33 = icmp eq i64 %32, %1
br i1 %33, label %34, label %15
34: ; preds = %15
%35 = and i64 %30, 4294967294
br label %36
36: ; preds = %34, %8
%37 = phi i64 [ 0, %8 ], [ %35, %34 ]
%38 = getelementptr inbounds i8, i8* %6, i64 %37
store i8 0, i8* %38
ret i8* %6
}