#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 *, ...);
void convert_bytes_to_hex(char *buf, uint8_t *value, int size) {
  sprintf(buf, "0x");
  buf += 2;
  for (int i = 0; i < size; i++) {
    buf += sprintf(buf, "%02x", value[i]);
  }
  *buf = 0;
}


x86 assembly (clang, -O0):
<convert_bytes_to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xed2(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118f <convert_bytes_to_hex+0x7f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe99(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1148 <convert_bytes_to_hex+0x38>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_bytes_to_hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r13
movb   $0x0,0x2(%rdi)
movw   $0x7830,(%rdi)
add    $0x2,%r13
test   %edx,%edx
jle    1164 <convert_bytes_to_hex+0x54>
mov    %rsi,%r14
mov    %edx,%r12d
lea    0xec9(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    (%r14,%rbx,4),%edx
mov    %r13,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <sprintf@plt>
cltq
add    %r13,%rax
add    $0x1,%rbx
mov    %rax,%r13
cmp    %rbx,%r12
jne    1140 <convert_bytes_to_hex+0x30>
jmp    1167 <convert_bytes_to_hex+0x57>
mov    %r13,%rax
movb   $0x0,(%rax)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
@.str.1 = [5 x i8] c"%02x\00"
define void @convert_bytes_to_hex(i8* nocapture writeonly %0, i32* nocapture %1, i32 %2) {
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %0, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 3, i1 false)
%4 = getelementptr inbounds i8, i8* %0, i64 2
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = zext i32 %2 to i64
br label %10
8: ; preds = %10, %3
%9 = phi i8* [ %4, %3 ], [ %17, %10 ]
store i8 0, i8* %9
ret void
10: ; preds = %6, %10
%11 = phi i64 [ 0, %6 ], [ %18, %10 ]
%12 = phi i8* [ %4, %6 ], [ %17, %10 ]
%13 = getelementptr inbounds i32, i32* %1, i64 %11
%14 = load i32, i32* %13
%15 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %12, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %12, i64 %16
%18 = add nuw nsw i64 %11, 1
%19 = icmp eq i64 %18, %7
br i1 %19, label %8, label %10
}