#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;
typedef int uint32_t;

/* Variables and functions */
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int strlen(char *);
char *pslr_hexdump(uint8_t *buf, uint32_t bufLen) {
  char *ret = malloc(4 * bufLen);
  uint32_t i;
  sprintf(ret, "%s", "");
  for (i = 0; i < bufLen; i++) {
    if (i % 16 == 0) {
      sprintf(ret + strlen(ret), "0x%04x | ", i);
    }
    sprintf(ret + strlen(ret), "%02x ", buf[i]);
    if (i % 8 == 7) {
      sprintf(ret + strlen(ret), " ");
    }
    if (i % 16 == 15) {
      sprintf(ret + strlen(ret), "\n");
    }
  }
  if (i % 16 != 15) {
    sprintf(ret + strlen(ret), "\n");
  }
  return ret;
}


x86 assembly (clang, -O0):
<pslr_hexdump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
shl    $0x2,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xea7(%rip),%rsi
lea    0xeb4(%rip),%rdx
mov    $0x0,%al
call   1050 <sprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1277 <pslr_hexdump+0x147>
mov    -0x1c(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11b9 <pslr_hexdump+0x89>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x1c(%rbp),%edx
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x7,%edx
jne    1228 <pslr_hexdump+0xf8>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
cltq
add    %rax,%rdi
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x1c(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0xf,%edx
jne    1264 <pslr_hexdump+0x134>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rdi
cltq
add    %rax,%rdi
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    1269 <pslr_hexdump+0x139>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116e <pslr_hexdump+0x3e>
mov    -0x1c(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0xf,%edx
je     12b3 <pslr_hexdump+0x183>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rdi
cltq
add    %rax,%rdi
lea    0xd67(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pslr_hexdump>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r15
lea    0x0(,%rbp,4),%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
test   %ebp,%ebp
jle    11f6 <pslr_hexdump+0xc6>
mov    %rbp,(%rsp)
mov    %ebp,%r12d
lea    0xea2(%rip),%r13
xor    %ebp,%ebp
jmp    1179 <pslr_hexdump+0x49>
nopl   0x0(%rax)
add    $0x1,%rbp
cmp    %rbp,%r12
je     11ec <pslr_hexdump+0xbc>
mov    %ebp,%r14d
and    $0xf,%r14d
jne    11a0 <pslr_hexdump+0x70>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
lea    0xe69(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
mov    (%r15,%rbp,4),%edx
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %ebp,%eax
not    %eax
test   $0x7,%al
jne    11d4 <pslr_hexdump+0xa4>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movw   $0x20,(%rbx,%rax,1)
cmp    $0xf,%r14d
jne    1170 <pslr_hexdump+0x40>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movw   $0xa,(%rbx,%rax,1)
jmp    1170 <pslr_hexdump+0x40>
mov    (%rsp),%rax
not    %eax
test   $0xf,%al
je     1206 <pslr_hexdump+0xd6>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movw   $0xa,(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [10 x i8] c"0x%04x | \00"
@.str.3 = [6 x i8] c"%02x \00"
@.str.4 = [2 x i8] c" \00"
@.str.5 = [2 x i8] c"\0A\00"
define i8* @pslr_hexdump(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = mul nsw i32 4, %7
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 (i8*, i8*, ...) @sprintf(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %6
br label %12
12: ; preds = %62, %2
%13 = load i32, i32* %6
%14 = load i32, i32* %4
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %65
16: ; preds = %12
%17 = load i32, i32* %6
%18 = srem i32 %17, 16
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %16
%21 = load i8*, i8** %5
%22 = load i8*, i8** %5
%23 = call i32 @strlen(i8* %22)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
%26 = load i32, i32* %6
%27 = call i32 (i8*, i8*, ...) @sprintf(i8* %25, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %20, %16
%29 = load i8*, i8** %5
%30 = load i8*, i8** %5
%31 = call i32 @strlen(i8* %30)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = load i32*, i32** %3
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
%38 = load i32, i32* %37
%39 = call i32 (i8*, i8*, ...) @sprintf(i8* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %6
%41 = srem i32 %40, 8
%42 = icmp eq i32 %41, 7
br i1 %42, label %43, label %50
43: ; preds = %28
%44 = load i8*, i8** %5
%45 = load i8*, i8** %5
%46 = call i32 @strlen(i8* %45)
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %44, i64 %47
%49 = call i32 (i8*, i8*, ...) @sprintf(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %43, %28
%51 = load i32, i32* %6
%52 = srem i32 %51, 16
%53 = icmp eq i32 %52, 15
br i1 %53, label %54, label %61
54: ; preds = %50
%55 = load i8*, i8** %5
%56 = load i8*, i8** %5
%57 = call i32 @strlen(i8* %56)
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
%60 = call i32 (i8*, i8*, ...) @sprintf(i8* %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %61
61: ; preds = %54, %50
br label %62
62: ; preds = %61
%63 = load i32, i32* %6
%64 = add nsw i32 %63, 1
store i32 %64, i32* %6
br label %12
65: ; preds = %12
%66 = load i32, i32* %6
%67 = srem i32 %66, 16
%68 = icmp ne i32 %67, 15
br i1 %68, label %69, label %76
69: ; preds = %65
%70 = load i8*, i8** %5
%71 = load i8*, i8** %5
%72 = call i32 @strlen(i8* %71)
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %70, i64 %73
%75 = call i32 (i8*, i8*, ...) @sprintf(i8* %74, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %76
76: ; preds = %69, %65
%77 = load i8*, i8** %5
ret i8* %77
}

LLVM IR (-O2):
@.str.2 = [10 x i8] c"0x%04x | \00"
@.str.3 = [6 x i8] c"%02x \00"
define i8* @pslr_hexdump(i32* nocapture %0, i32 %1) {
%3 = shl nsw i32 %1, 2
%4 = tail call i8* @malloc(i32 %3)
store i8 0, i8* %4
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %45
6: ; preds = %2
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %6, %39
%9 = phi i64 [ 0, %6 ], [ %40, %39 ]
%10 = trunc i64 %9 to i32
%11 = and i32 %10, 15
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = tail call i32 @strlen(i8* %4)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %4, i64 %15
%17 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %16, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %18
18: ; preds = %13, %8
%19 = tail call i32 @strlen(i8* %4)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %4, i64 %20
%22 = getelementptr inbounds i32, i32* %0, i64 %9
%23 = load i32, i32* %22
%24 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %21, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = and i32 %10, 7
%26 = icmp eq i32 %25, 7
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = tail call i32 @strlen(i8* %4)
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %4, i64 %29
%31 = bitcast i8* %30 to i16*
store i16 32, i16* %31
br label %32
32: ; preds = %27, %18
%33 = icmp eq i32 %11, 15
br i1 %33, label %34, label %39
34: ; preds = %32
%35 = tail call i32 @strlen(i8* %4)
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %4, i64 %36
%38 = bitcast i8* %37 to i16*
store i16 10, i16* %38
br label %39
39: ; preds = %32, %34
%40 = add nuw nsw i64 %9, 1
%41 = icmp eq i64 %40, %7
br i1 %41, label %42, label %8
42: ; preds = %39
%43 = and i32 %1, 15
%44 = icmp eq i32 %43, 15
br i1 %44, label %50, label %45
45: ; preds = %2, %42
%46 = tail call i32 @strlen(i8* %4)
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %4, i64 %47
%49 = bitcast i8* %48 to i16*
store i16 10, i16* %49
br label %50
50: ; preds = %45, %42
ret i8* %4
}