#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 */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
void hex_dump_to_buffer(char *output_buffer, unsigned char *buf,
                        size_t buf_size) {
  unsigned int i;
  char *p = output_buffer;
  memset(output_buffer, 0, 1 + (2 * buf_size));
  for (i = 0; i < buf_size; i++)
    p += sprintf(p, "%2.2x", buf[i]);
}


x86 assembly (clang, -O0):
<hex_dump_to_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1030 <memset@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    11a2 <hex_dump_to_buffer+0x82>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
lea    0xe85(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115c <hex_dump_to_buffer+0x3c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_dump_to_buffer>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r13
add    %edx,%edx
add    $0x1,%edx
xor    %esi,%esi
call   1030 <memset@plt>
test   %r15,%r15
je     116d <hex_dump_to_buffer+0x4d>
xor    %ebx,%ebx
lea    0xeb4(%rip),%r12
nopl   0x0(%rax)
movzbl (%r14,%rbx,1),%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1040 <sprintf@plt>
cltq
add    %rax,%r13
add    $0x1,%ebx
cmp    %r15,%rbx
jb     1150 <hex_dump_to_buffer+0x30>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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