#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*/ sprintf(char *, char *, ...);
int strlen(char *);
void sprint_mem(char *out, const char *buf, size_t len) {
  size_t i;
  *out = '\0';
  for (i = 0; i < len; i++) {
    if (i > 0) {
      sprintf(out + strlen(out), " ");
    }
    sprintf(out + strlen(out), "%02x", buf[i]);
  }
}


x86 assembly (clang, -O0):
<sprint_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11c9 <sprint_mem+0xa9>
cmpq   $0x0,-0x20(%rbp)
jbe    1184 <sprint_mem+0x64>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rdi
cltq
add    %rax,%rdi
lea    0xe83(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1143 <sprint_mem+0x23>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sprint_mem>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
movb   $0x0,(%rdi)
test   %rdx,%rdx
je     11a8 <sprint_mem+0x88>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r13
call   1030 <strlen@plt>
movslq %eax,%rdi
add    %r13,%rdi
movsbl (%r14),%edx
lea    0xeb0(%rip),%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
cmp    $0x1,%r15
je     11a8 <sprint_mem+0x88>
mov    $0x1,%ebx
lea    0xe97(%rip),%r12
nopl   0x0(%rax)
mov    %r13,%rdi
call   1030 <strlen@plt>
cltq
movw   $0x20,0x0(%r13,%rax,1)
mov    %r13,%rdi
call   1030 <strlen@plt>
movslq %eax,%rdi
add    %r13,%rdi
movsbl (%r14,%rbx,1),%edx
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1170 <sprint_mem+0x50>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str.1 = [5 x i8] c"%02x\00"
define void @sprint_mem(i8* %0, i8* nocapture %1, i64 %2) {
store i8 0, i8* %0
%4 = icmp eq i64 %2, 0
br i1 %4, label %28, label %5
5: ; preds = %3
%6 = tail call i32 @strlen(i8* %0)
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %1
%10 = sext i8 %9 to i32
%11 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp eq i64 %2, 1
br i1 %12, label %28, label %13
13: ; preds = %5, %13
%14 = phi i64 [ %26, %13 ], [ 1, %5 ]
%15 = tail call i32 @strlen(i8* %0)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = bitcast i8* %17 to i16*
store i16 32, i16* %18
%19 = tail call i32 @strlen(i8* %0)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = getelementptr inbounds i8, i8* %1, i64 %14
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %21, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = add nuw i64 %14, 1
%27 = icmp eq i64 %26, %2
br i1 %27, label %28, label %13
28: ; preds = %13, %5, %3
ret void
}