#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 */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *malloc(size_t);
int /*<<< orphan*/ sprintf(char *, char *, int const);
int /*<<< orphan*/ stderr;
char *hexlify(const uint8_t *buf, size_t len) {
  char *hex_chars = malloc(len * 2 + 1);
  if (!(hex_chars)) {
    fprintf(stderr, "%s failed\n", "hex_chars");
    abort();
  };
  char *p = hex_chars;
  size_t i;
  for (i = 0; i < len; i++) {
    p += sprintf(p, "%02x", buf[i]);
  }
  *p = '\0';
  return hex_chars;
}


x86 assembly (clang, -O0):
<hexlify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x1,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1193 <hexlify+0x53>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
lea    0xe84(%rip),%rdx
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11ed <hexlify+0xad>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe4e(%rip),%rsi
call   1060 <sprintf@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11a3 <hexlify+0x63>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexlify>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
lea    (%rsi,%rsi,1),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11b2 <hexlify+0x72>
mov    %rax,%r15
mov    %rax,%rbx
test   %r12,%r12
je     119d <hexlify+0x5d>
xor    %ebp,%ebp
lea    0xe9e(%rip),%r13
mov    %r15,%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r14,%rbp,4),%edx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <sprintf@plt>
cltq
add    %rax,%rbx
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1180 <hexlify+0x40>
movb   $0x0,(%rbx)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
lea    0xe42(%rip),%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [11 x i8] c"%s failed\0A\00"
@.str.1 = [10 x i8] c"hex_chars\00"
@.str.2 = [5 x i8] c"%02x\00"
define i8* @hexlify(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i32* %0, i32** %3
store i64 %1, i64* %4
%8 = load i64, i64* %4
%9 = mul i64 %8, 2
%10 = add i64 %9, 1
%11 = call i8* @malloc(i64 %10)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %2
%19 = load i8*, i8** %5
store i8* %19, i8** %6
store i64 0, i64* %7
br label %20
20: ; preds = %34, %18
%21 = load i64, i64* %7
%22 = load i64, i64* %4
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %37
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = load i32*, i32** %3
%27 = load i64, i64* %7
%28 = getelementptr inbounds i32, i32* %26, i64 %27
%29 = load i32, i32* %28
%30 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = load i8*, i8** %6
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8* %33, i8** %6
br label %34
34: ; preds = %24
%35 = load i64, i64* %7
%36 = add i64 %35, 1
store i64 %36, i64* %7
br label %20
37: ; preds = %20
%38 = load i8*, i8** %6
store i8 0, i8* %38
%39 = load i8*, i8** %5
ret i8* %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [11 x i8] c"%s failed\0A\00"
@.str.1 = [10 x i8] c"hex_chars\00"
@.str.2 = [5 x i8] c"%02x\00"
define noalias i8* @hexlify(i32* nocapture %0, i64 %1) {
%3 = shl i64 %1, 1
%4 = or i64 %3, 1
%5 = tail call i8* @malloc(i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = icmp eq i64 %1, 0
br i1 %8, label %23, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 (...) @abort()
unreachable
13: ; preds = %7, %13
%14 = phi i64 [ %21, %13 ], [ 0, %7 ]
%15 = phi i8* [ %20, %13 ], [ %5, %7 ]
%16 = getelementptr inbounds i32, i32* %0, i64 %14
%17 = load i32, i32* %16
%18 = tail call i32 @sprintf(i8* dereferenceable(1) %15, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %15, i64 %19
%21 = add nuw i64 %14, 1
%22 = icmp eq i64 %21, %1
br i1 %22, label %23, label %13
23: ; preds = %13, %7
%24 = phi i8* [ %5, %7 ], [ %20, %13 ]
store i8 0, i8* %24
ret i8* %5
}