#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 /*<<< orphan*/ uint8_t;

/* Variables and functions */
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, int /*<<< orphan*/);
char *hexdump(uint8_t *data, int len) {
  char *buf, *ptr;
  ptr = buf = malloc(len * 3 + 1);
  while (len--) {
    ptr += sprintf(ptr, "%02x ", *data++);
  }
  return buf;
}


x86 assembly (clang, -O0):
<hexdump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
imul   $0x3,-0xc(%rbp),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     118d <hexdump+0x6d>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%edx
lea    0xe8d(%rip),%rsi
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1143 <hexdump+0x23>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexdump>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r12
lea    (%rbx,%rbx,2),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,%r14
test   %ebx,%ebx
je     117d <hexdump+0x5d>
mov    %ebx,%r13d
xor    %ebp,%ebp
lea    0xeb2(%rip),%r15
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r12,%rbp,4),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <sprintf@plt>
cltq
add    %rax,%rbx
add    $0x1,%rbp
cmp    %ebp,%r13d
jne    1160 <hexdump+0x40>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%02x \00"
define i8* @hexdump(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = mul nsw i32 %7, 3
%9 = add nsw i32 %8, 1
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %5
store i8* %10, i8** %6
br label %11
11: ; preds = %15, %2
%12 = load i32, i32* %4
%13 = add nsw i32 %12, -1
store i32 %13, i32* %4
%14 = icmp ne i32 %12, 0
br i1 %14, label %15, label %24
15: ; preds = %11
%16 = load i8*, i8** %6
%17 = load i32*, i32** %3
%18 = getelementptr inbounds i32, i32* %17, i32 1
store i32* %18, i32** %3
%19 = load i32, i32* %17
%20 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i8*, i8** %6
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8* %23, i8** %6
br label %11
24: ; preds = %11
%25 = load i8*, i8** %5
ret i8* %25
}

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