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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
void print_hex(FILE *stream, const char *prefix, uint8_t *buf, size_t len) {
  if (prefix != NULL) {
    fputs(prefix, stream);
  }
  for (size_t i = 0; i < len; i++) {
    fprintf(stream, "%02x", buf[i]);
  }
  fputs("\n", stream);
}


x86 assembly (clang, -O0):
<print_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1150 <print_hex+0x30>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <fputs@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1192 <print_hex+0x72>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe84(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1158 <print_hex+0x38>
mov    -0x8(%rbp),%rsi
lea    0xe68(%rip),%rdi
call   1030 <fputs@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rdi,%r13
test   %rsi,%rsi
je     1152 <print_hex+0x22>
mov    %rsi,%rdi
mov    %r13,%rsi
call   1030 <fputs@plt>
test   %r14,%r14
je     1178 <print_hex+0x48>
xor    %ebx,%ebx
lea    0xea0(%rip),%r12
mov    (%r15,%rbx,4),%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1160 <print_hex+0x30>
mov    $0xa,%edi
mov    %r13,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <fputc@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
@.str.1 = [2 x i8] c"\0A\00"
define void @print_hex(i32* %0, i8* %1, i32* %2, i64 %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i64 %3, i64* %8
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %4
%13 = load i8*, i8** %6
%14 = load i32*, i32** %5
%15 = call i32 @fputs(i8* %13, i32* %14)
br label %16
16: ; preds = %12, %4
store i64 0, i64* %9
br label %17
17: ; preds = %28, %16
%18 = load i64, i64* %9
%19 = load i64, i64* %8
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = load i32*, i32** %5
%23 = load i32*, i32** %7
%24 = load i64, i64* %9
%25 = getelementptr inbounds i32, i32* %23, i64 %24
%26 = load i32, i32* %25
%27 = call i32 @fprintf(i32* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %21
%29 = load i64, i64* %9
%30 = add i64 %29, 1
store i64 %30, i64* %9
br label %17
31: ; preds = %17
%32 = load i32*, i32** %5
%33 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32* %32)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define void @print_hex(i32* nocapture %0, i8* %1, i32* nocapture %2, i64 %3) {
%5 = icmp eq i8* %1, null
br i1 %5, label %8, label %6
6: ; preds = %4
%7 = tail call i32 @fputs(i8* %1, i32* %0)
br label %8
8: ; preds = %6, %4
%9 = icmp eq i64 %3, 0
br i1 %9, label %10, label %12
10: ; preds = %12, %8
%11 = tail call i32 @fputc(i32 10, i32* %0)
ret void
12: ; preds = %8, %12
%13 = phi i64 [ %17, %12 ], [ 0, %8 ]
%14 = getelementptr inbounds i32, i32* %2, i64 %13
%15 = load i32, i32* %14
%16 = tail call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = add nuw i64 %13, 1
%18 = icmp eq i64 %17, %3
br i1 %18, label %10, label %12
}