#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*/ sprintf(char *, char *, int);
int bytes_to_hex(char *from, int from_length, char *to) {
  char *pHex;
  char *pByte;
  int value;
  pHex = to;
  for (pByte = from; from_length > 0; from_length--) {
    value = *pByte++ & 0xFF;
    (void)sprintf(pHex, "%02x", value);
    pHex += 2;
  }
  return (1);
}


x86 assembly (clang, -O0):
<bytes_to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1184 <bytes_to_hex+0x74>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movsbl (%rax),%eax
and    $0xff,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%edx
lea    0xe9b(%rip),%rsi
call   1030 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1133 <bytes_to_hex+0x23>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bytes_to_hex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    114e <bytes_to_hex+0x3e>
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
add    $0x1,%ebp
lea    0xed3(%rip),%r15
nopl   (%rax)
movzbl (%rbx),%edx
add    $0x1,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <sprintf@plt>
add    $0x2,%r14
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     1130 <bytes_to_hex+0x20>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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