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

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *to_hex(uint8_t *buf, int len) {
  static char str[64];
  int i, o;
  for (i = 0, o = 0; i < len; i++, o += 3) {
    snprintf(str + o, sizeof(str) - o, "%02x ", buf[i]);
  }
  return str;
}


x86 assembly (clang, -O0):
<to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1183 <to_hex+0x73>
movslq -0x14(%rbp),%rax
lea    0x2efc(%rip),%rdi
add    %rax,%rdi
movslq -0x14(%rbp),%rcx
mov    $0x40,%eax
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xe99(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x14(%rbp)
jmp    112d <to_hex+0x1d>
lea    0x2eb6(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    1161 <to_hex+0x51>
mov    %rdi,%r14
mov    %esi,%r12d
lea    0x2f14(%rip),%r13
mov    $0x40,%ebp
xor    %ebx,%ebx
lea    0xec6(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r14,%rbx,4),%ecx
mov    %r13,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
call   1030 <snprintf@plt>
add    $0x1,%rbx
add    $0x3,%r13
add    $0xfffffffd,%ebp
cmp    %rbx,%r12
jne    1140 <to_hex+0x30>
lea    0x2ed8(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@to_hex.str = internal global [64 x i8] zeroinitializer
@.str = [6 x i8] c"%02x \00"
define i8* @to_hex(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %7
7: ; preds = %25, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %30
11: ; preds = %7
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @to_hex.str, i64 0, i64 0), i64 %13
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = sub i64 64, %16
%18 = trunc i64 %17 to i32
%19 = load i32*, i32** %3
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = load i32, i32* %22
%24 = call i32 @snprintf(i8* %14, i32 %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %11
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 3
store i32 %29, i32* %6
br label %7
30: ; preds = %7
ret i8* getelementptr inbounds ([64 x i8], [64 x i8]* @to_hex.str, i64 0, i64 0)
}

LLVM IR (-O2):
@to_hex.str = internal global [64 x i8] zeroinitializer
@.str = [6 x i8] c"%02x \00"
define i8* @to_hex(i32* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %18
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %6
%7 = phi i64 [ 0, %4 ], [ %15, %6 ]
%8 = phi i64 [ 0, %4 ], [ %16, %6 ]
%9 = getelementptr inbounds [64 x i8], [64 x i8]* @to_hex.str, i64 0, i64 %8
%10 = trunc i64 %8 to i32
%11 = sub nsw i32 64, %10
%12 = getelementptr inbounds i32, i32* %0, i64 %7
%13 = load i32, i32* %12
%14 = tail call i32 @snprintf(i8* %9, i32 %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = add nuw nsw i64 %7, 1
%16 = add nuw nsw i64 %8, 3
%17 = icmp eq i64 %15, %5
br i1 %17, label %18, label %6
18: ; preds = %6, %2
ret i8* getelementptr inbounds ([64 x i8], [64 x i8]* @to_hex.str, i64 0, i64 0)
}