#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*/ printf(char *, ...);
void printhex(void *buf, int len) {
  int i;
  char *s = buf;
  while (len >= 0) {
    for (i = 0; i < 16 && len >= 0; s++, i++, len--) {
      int ch = (*s) & 0xff;
      printf("%3d ", ch);
    }
    printf("\n");
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<printhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11b7 <printhex+0xa7>
movl   $0x0,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0x10,-0x10(%rbp)
mov    %al,-0x1d(%rbp)
jge    1151 <printhex+0x41>
cmpl   $0x0,-0xc(%rbp)
setge  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1161 <printhex+0x51>
jmp    11a4 <printhex+0x94>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
and    $0xff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1138 <printhex+0x28>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1127 <printhex+0x17>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printhex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %esi,%esi
js     1190 <printhex+0x70>
mov    %esi,%r14d
mov    %rdi,%r12
lea    0xec6(%rip),%r15
nopw   0x0(%rax,%rax,1)
cmp    $0xf,%r14d
mov    $0xf,%r13d
cmovb  %r14d,%r13d
add    $0x1,%r13
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl (%r12,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %ebx,%r13d
jne    1160 <printhex+0x40>
mov    $0xa,%edi
call   1030 <putchar@plt>
sub    %ebx,%r14d
lea    0x1(%r14),%eax
add    %rbx,%r12
test   %eax,%eax
jg     1140 <printhex+0x20>
mov    $0xa,%edi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%3d \00"
@.str.1 = [2 x i8] c"\0A\00"
define void @printhex(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
store i8* %8, i8** %6
br label %9
9: ; preds = %35, %2
%10 = load i32, i32* %4
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %37
12: ; preds = %9
store i32 0, i32* %5
br label %13
13: ; preds = %28, %12
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 16
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* %4
%18 = icmp sge i32 %17, 0
br label %19
19: ; preds = %16, %13
%20 = phi i1 [ false, %13 ], [ %18, %16 ]
br i1 %20, label %21, label %35
21: ; preds = %19
%22 = load i8*, i8** %6
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = and i32 %24, 255
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %21
%29 = load i8*, i8** %6
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %6
%31 = load i32, i32* %5
%32 = add nsw i32 %31, 1
store i32 %32, i32* %5
%33 = load i32, i32* %4
%34 = add nsw i32 %33, -1
store i32 %34, i32* %4
br label %13
35: ; preds = %19
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %9
37: ; preds = %9
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%3d \00"
define void @printhex(i8* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, -1
br i1 %3, label %4, label %22
4: ; preds = %2, %19
%5 = phi i32 [ %17, %19 ], [ %1, %2 ]
%6 = phi i8* [ %15, %19 ], [ %0, %2 ]
%7 = call i32 @llvm.umin.i32(i32 %5, i32 15)
br label %8
8: ; preds = %4, %8
%9 = phi i32 [ %17, %8 ], [ %5, %4 ]
%10 = phi i8* [ %15, %8 ], [ %6, %4 ]
%11 = phi i32 [ %16, %8 ], [ 0, %4 ]
%12 = load i8, i8* %10
%13 = zext i8 %12 to i32
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds i8, i8* %10, i64 1
%16 = add nuw nsw i32 %11, 1
%17 = add nsw i32 %9, -1
%18 = icmp eq i32 %11, %7
br i1 %18, label %19, label %8
19: ; preds = %8
%20 = tail call i32 @putchar(i32 10)
%21 = icmp sgt i32 %9, 0
br i1 %21, label %4, label %22
22: ; preds = %19, %2
%23 = tail call i32 @putchar(i32 10)
ret void
}