#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*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
const char *buf2str(uint8_t *buf, int len) {
  static char str[2049];
  int i;
  if (len <= 0 || len > 1024)
    return NULL;
  memset(str, 0, 2049);
  for (i = 0; i < len; i++)
    sprintf(str + i + i, "%2.2x", buf[i]);
  str[len * 2] = '\0';
  return (const char *)str;
}


x86 assembly (clang, -O0):
<buf2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1146 <buf2str+0x26>
cmpl   $0x400,-0x14(%rbp)
jle    1153 <buf2str+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11d2 <buf2str+0xb2>
lea    0x2ee6(%rip),%rdi
xor    %esi,%esi
mov    $0x801,%edx
call   1030 <memset@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b3 <buf2str+0x93>
movslq -0x18(%rbp),%rax
lea    0x2ebc(%rip),%rdi
add    %rax,%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe60(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    116d <buf2str+0x4d>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
movslq %eax,%rcx
lea    0x2e7d(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e72(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf2str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
lea    -0x401(%r14),%eax
cmp    $0xfffffc00,%eax
jae    1140 <buf2str+0x20>
xor    %eax,%eax
jmp    119b <buf2str+0x7b>
mov    %rdi,%r15
lea    0x2ef6(%rip),%r13
xor    %ebx,%ebx
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x801,%edx
call   1030 <memset@plt>
mov    %r14d,%ebp
lea    0xe9b(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15,%rbx,4),%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1040 <sprintf@plt>
add    $0x1,%rbx
add    $0x2,%r13
cmp    %rbx,%rbp
jne    1170 <buf2str+0x50>
add    %r14d,%r14d
lea    0x2eaa(%rip),%rax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@buf2str.str = internal global [2049 x i8] zeroinitializer
@.str = [6 x i8] c"%2.2x\00"
define i8* @buf2str(i32* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp sle i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i32, i32* %5
%11 = icmp sgt i32 %10, 1024
br i1 %11, label %12, label %13
12: ; preds = %9, %2
store i8* null, i8** %3
br label %40
13: ; preds = %9
%14 = call i32 @memset(i8* getelementptr inbounds ([2049 x i8], [2049 x i8]* @buf2str.str, i64 0, i64 0), i32 0, i32 2049)
store i32 0, i32* %6
br label %15
15: ; preds = %32, %13
%16 = load i32, i32* %6
%17 = load i32, i32* %5
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %35
19: ; preds = %15
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* getelementptr inbounds ([2049 x i8], [2049 x i8]* @buf2str.str, i64 0, i64 0), i64 %21
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i32*, i32** %4
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = load i32, i32* %29
%31 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %19
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
br label %15
35: ; preds = %15
%36 = load i32, i32* %5
%37 = mul nsw i32 %36, 2
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [2049 x i8], [2049 x i8]* @buf2str.str, i64 0, i64 %38
store i8 0, i8* %39
store i8* getelementptr inbounds ([2049 x i8], [2049 x i8]* @buf2str.str, i64 0, i64 0), i8** %3
br label %40
40: ; preds = %35, %12
%41 = load i8*, i8** %3
ret i8* %41
}

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