#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 *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *bintobuf(char *outbuf, const uint8_t *buf, size_t len) {
  unsigned int i = 0;
  bzero(outbuf, 1024);
  for (i = 0; i < ((len) < (1024) ? (len) : (1024)); 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    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x400,%esi
call   1030 <bzero@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rax,-0x28(%rbp)
cmpq   $0x400,-0x18(%rbp)
jae    1172 <bintobuf+0x52>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1180 <bintobuf+0x60>
mov    $0x400,%eax
mov    %rax,-0x30(%rbp)
jmp    1180 <bintobuf+0x60>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
cmp    %rcx,%rax
jae    11ca <bintobuf+0xaa>
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    0xe49(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1150 <bintobuf+0x30>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bintobuf>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x400,%esi
call   1030 <bzero@plt>
cmp    $0x400,%r15
mov    $0x400,%r13d
cmovb  %r15,%r13
test   %r13,%r13
je     1179 <bintobuf+0x59>
add    %r13,%r13
xor    %ebx,%ebx
lea    0xea2(%rip),%r15
xchg   %ax,%ax
mov    (%r14,%rbx,2),%edx
lea    (%r12,%rbx,1),%rdi
mov    %r15,%rsi
call   1040 <sprintf@plt>
add    $0x2,%rbx
cmp    %rbx,%r13
jne    1160 <bintobuf+0x40>
mov    %r12,%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 = call i32 @bzero(i8* %9, i32 1024)
store i32 0, i32* %7
br label %11
11: ; preds = %35, %3
%12 = load i32, i32* %7
%13 = zext i32 %12 to i64
%14 = load i64, i64* %6
%15 = icmp ult i64 %14, 1024
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = load i64, i64* %6
br label %19
18: ; preds = %11
br label %19
19: ; preds = %18, %16
%20 = phi i64 [ %17, %16 ], [ 1024, %18 ]
%21 = icmp ult i64 %13, %20
br i1 %21, label %22, label %38
22: ; preds = %19
%23 = load i32*, i32** %5
%24 = load i32, i32* %7
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
store i32 %27, i32* %8
%28 = load i8*, i8** %4
%29 = load i32, i32* %7
%30 = mul i32 %29, 2
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = load i32, i32* %8
%34 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %22
%36 = load i32, i32* %7
%37 = add i32 %36, 1
store i32 %37, i32* %7
br label %11
38: ; preds = %19
%39 = load i8*, i8** %4
ret i8* %39
}

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