#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*/ bzero(char *, size_t);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *bintobuf(char *outbuf, const uint8_t *buf, size_t len) {
  unsigned int i = 0;
  bzero(outbuf, len * 2);
  for (i = 0; i < len; i++) {
    uint8_t v = buf[i];
    sprintf(&outbuf[i * 2], "%02x ", v);
  }
  return outbuf;
}


x86 assembly (clang, -O0):
<bintobuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
shl    $0x1,%rsi
call   1030 <bzero@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    1199 <bintobuf+0x79>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
add    %rax,%rdi
mov    -0x20(%rbp),%edx
lea    0xe7a(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1153 <bintobuf+0x33>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bintobuf>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
lea    (%rdx,%rdx,1),%rsi
call   1030 <bzero@plt>
test   %r14,%r14
je     116a <bintobuf+0x4a>
xor    %ebx,%ebx
lea    0xeb7(%rip),%r12
nopl   0x0(%rax)
mov    (%r15,%rbx,4),%edx
lea    (%rbx,%rbx,1),%edi
add    %r13,%rdi
mov    %r12,%rsi
call   1040 <sprintf@plt>
add    $0x1,%ebx
cmp    %r14,%rbx
jb     1150 <bintobuf+0x30>
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%02x \00"
define i8* @bintobuf(i8* returned writeonly %0, i32* nocapture %1, i64 %2) {
%4 = shl i64 %2, 1
%5 = tail call i32 @bzero(i8* %0, i64 %4)
%6 = icmp eq i64 %2, 0
br i1 %6, label %18, label %7
7: ; preds = %3, %7
%8 = phi i64 [ %16, %7 ], [ 0, %3 ]
%9 = getelementptr inbounds i32, i32* %1, i64 %8
%10 = load i32, i32* %9
%11 = shl nuw nsw i64 %8, 1
%12 = and i64 %11, 4294967294
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = tail call i32 @sprintf(i8* dereferenceable(1) %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %10)
%15 = add nuw nsw i64 %8, 1
%16 = and i64 %15, 4294967295
%17 = icmp ult i64 %16, %2
br i1 %17, label %7, label %18
18: ; preds = %7, %3
ret i8* %0
}