#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(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
char *bytes2str(size_t s, unsigned char *p) {
  char *ret = malloc(2 * s + 1);
  char *tmp = ret;
  while (s) {
    sprintf(tmp, "%02x", (*p & 0x0ff));
    tmp += 2;
    p++;
    s--;
  }
  return ret;
}


x86 assembly (clang, -O0):
<bytes2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a2 <bytes2str+0x82>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xff,%eax
lea    0xe8f(%rip),%rsi
movzbl %al,%edx
call   1040 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    114f <bytes2str+0x2f>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bytes2str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r13
add    %edi,%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,%r15
test   %r13,%r13
je     116d <bytes2str+0x4d>
xor    %ebp,%ebp
lea    0xeb4(%rip),%r12
mov    %r15,%rbx
nop
movzbl (%r14,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <sprintf@plt>
add    $0x2,%rbx
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1150 <bytes2str+0x30>
mov    %r15,%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* @bytes2str(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i64 %0, i64* %3
store i8* %1, i8** %4
%7 = load i64, i64* %3
%8 = mul i64 2, %7
%9 = add i64 %8, 1
%10 = trunc i64 %9 to i32
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
store i8* %12, i8** %6
br label %13
13: ; preds = %16, %2
%14 = load i64, i64* %3
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %13
%17 = load i8*, i8** %6
%18 = load i8*, i8** %4
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = and i32 %20, 255
%22 = trunc i32 %21 to i8
%23 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %22)
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 2
store i8* %25, i8** %6
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %4
%28 = load i64, i64* %3
%29 = add i64 %28, -1
store i64 %29, i64* %3
br label %13
30: ; preds = %13
%31 = load i8*, i8** %5
ret i8* %31
}

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