#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;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, int const);
const char *to_hex(const uint8_t *text, size_t size) {
  static char hex[1024];
  int i;
  for (i = 0; i < size; i++) {
    sprintf(hex + 2 * i, "%02x", text[i]);
  }
  hex[2 * i] = 0;
  return hex;
}


x86 assembly (clang, -O0):
<to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    116c <to_hex+0x5c>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
cltq
lea    0x2efc(%rip),%rdi
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xea7(%rip),%rsi
call   1030 <sprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1127 <to_hex+0x17>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
movslq %eax,%rcx
lea    0x2ec4(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2eb9(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rsi,%rsi
je     1161 <to_hex+0x51>
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x2f15(%rip),%r13
xor    %ebx,%ebx
lea    0xecc(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15,%rbx,4),%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <sprintf@plt>
add    $0x1,%rbx
add    $0x2,%r13
cmp    %rbx,%r14
jne    1140 <to_hex+0x30>
add    %r14d,%r14d
jmp    1164 <to_hex+0x54>
xor    %r14d,%r14d
lea    0x2ed5(%rip),%rax
movb   $0x0,(%r14,%rax,1)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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