#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 */
char *malloc(unsigned int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ strcat(char *, char *);
char *bytes2hex(const char *bytes, unsigned int bytesize) {
  unsigned int i;
  char byte[3];
  char *hex;
  unsigned int size;
  size = sizeof(unsigned char) * 2 * bytesize + 1;
  hex = malloc(size);
  hex[0] = '\0';
  for (i = 0; i < bytesize; i++) {
    sprintf(byte, "%02x", (unsigned char)bytes[i]);
    strcat(hex, byte);
  }
  hex[size - 1] = '\0';
  return hex;
}


x86 assembly (clang, -O0):
<bytes2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%rax
add    $0x1,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    11a9 <bytes2hex+0x79>
lea    -0x13(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
lea    0xe7b(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x13(%rbp),%rsi
call   1040 <strcat@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1167 <bytes2hex+0x37>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bytes2hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
lea    (%rbx,%rbx,1),%eax
mov    %eax,(%rsp)
lea    (%rbx,%rbx,1),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,%r12
movb   $0x0,(%rax)
test   %ebx,%ebx
je     1194 <bytes2hex+0x64>
mov    %ebx,%ebp
xor    %r13d,%r13d
lea    0xe99(%rip),%r15
lea    0x5(%rsp),%rbx
nopl   0x0(%rax)
movzbl (%r14,%r13,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <sprintf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1040 <strcat@plt>
add    $0x1,%r13
cmp    %r13,%rbp
jne    1170 <bytes2hex+0x40>
mov    (%rsp),%eax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
define i8* @bytes2hex(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [3 x i8]
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = zext i32 %9 to i64
%11 = mul i64 2, %10
%12 = add i64 %11, 1
%13 = trunc i64 %12 to i32
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = call i8* @malloc(i32 %14)
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 0, i8* %17
store i32 0, i32* %5
br label %18
18: ; preds = %33, %2
%19 = load i32, i32* %5
%20 = load i32, i32* %4
%21 = icmp ult i32 %19, %20
br i1 %21, label %22, label %36
22: ; preds = %18
%23 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%24 = load i8*, i8** %3
%25 = load i32, i32* %5
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %28)
%30 = load i8*, i8** %7
%31 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%32 = call i32 @strcat(i8* %30, i8* %31)
br label %33
33: ; preds = %22
%34 = load i32, i32* %5
%35 = add i32 %34, 1
store i32 %35, i32* %5
br label %18
36: ; preds = %18
%37 = load i8*, i8** %7
%38 = load i32, i32* %8
%39 = sub i32 %38, 1
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %7
ret i8* %42
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i8* @bytes2hex(i8* nocapture %0, i32 %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%5 = shl i32 %1, 1
%6 = or i32 %5, 1
%7 = tail call i8* @malloc(i32 %6)
store i8 0, i8* %7
%8 = icmp eq i32 %1, 0
br i1 %8, label %19, label %9
9: ; preds = %2
%10 = zext i32 %1 to i64
br label %11
11: ; preds = %9, %11
%12 = phi i64 [ 0, %9 ], [ %17, %11 ]
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %14)
%16 = call i32 @strcat(i8* %7, i8* %4)
%17 = add nuw nsw i64 %12, 1
%18 = icmp eq i64 %17, %10
br i1 %18, label %19, label %11
19: ; preds = %11, %2
%20 = zext i32 %5 to i64
%21 = getelementptr inbounds i8, i8* %7, i64 %20
store i8 0, i8* %21
ret i8* %7
}