#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 /*<<< orphan*/ output;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
void buffer_hex_dump(char *output, unsigned char *buffer, int len) {
  char hex[14];
  output[0] = '\0';
  for (int i = 0; i < len; ++i) {
    sprintf(hex, "%02x:", buffer[i]);
    strncat(output, hex, sizeof(output));
  }
  output[strlen(output) - 1] = '\0';
}


x86 assembly (clang, -O0):
<buffer_hex_dump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1199 <buffer_hex_dump+0x69>
lea    -0x22(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
lea    0xe90(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x22(%rbp),%rsi
mov    $0x8,%edx
call   1040 <strncat@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1151 <buffer_hex_dump+0x21>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_hex_dump>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r12
movb   $0x0,(%rdi)
test   %edx,%edx
jle    1189 <buffer_hex_dump+0x59>
mov    %rsi,%r14
mov    %edx,%r13d
xor    %ebp,%ebp
lea    0xea9(%rip),%r15
lea    0xa(%rsp),%rbx
nopl   0x0(%rax)
movzbl (%r14,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <sprintf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1040 <strncat@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1160 <buffer_hex_dump+0x30>
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r12,1)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%02x:\00"
define void @buffer_hex_dump(i8* %0, i8* nocapture %1, i32 %2) {
%4 = alloca [14 x i8]
%5 = getelementptr inbounds [14 x i8], [14 x i8]* %4, i64 0, i64 0
store i8 0, i8* %0
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = zext i32 %2 to i64
br label %14
9: ; preds = %14, %3
%10 = call i32 @strlen(i8* %0)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 0, i8* %13
ret void
14: ; preds = %7, %14
%15 = phi i64 [ 0, %7 ], [ %20, %14 ]
%16 = getelementptr inbounds i8, i8* %1, i64 %15
%17 = load i8, i8* %16
%18 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %17)
%19 = call i32 @strncat(i8* %0, i8* %5, i32 8)
%20 = add nuw nsw i64 %15, 1
%21 = icmp eq i64 %20, %8
br i1 %21, label %9, label %14
}