#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*/ printf(char *, char *);
int sprintf(char *, char *, unsigned char);
void print_buffer(unsigned char *buf, int size) {
  char str_buf[512 * 3] = {0};
  char *pstr_buf = str_buf;
  int i, n;
  for (i = 0; i < size; i++) {
    n = sprintf(pstr_buf, "%02x ", buf[i]);
    pstr_buf += n;
  }
  printf("%s\n", str_buf);
}


x86 assembly (clang, -O0):
<print_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x620,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x610(%rbp),%rdi
xor    %esi,%esi
mov    $0x600,%edx
call   1040 <memset@plt>
lea    -0x610(%rbp),%rax
mov    %rax,-0x618(%rbp)
movl   $0x0,-0x61c(%rbp)
mov    -0x61c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11d2 <print_buffer+0xa2>
mov    -0x618(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x61c(%rbp),%rcx
lea    0xe6b(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    %eax,-0x620(%rbp)
mov    -0x620(%rbp),%ecx
mov    -0x618(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x618(%rbp)
mov    -0x61c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x61c(%rbp)
jmp    116d <print_buffer+0x3d>
lea    -0x610(%rbp),%rsi
lea    0xe26(%rip),%rdi
call   1030 <printf@plt>
add    $0x620,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x600,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    %rsp,%rbx
mov    $0x600,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
test   %ebp,%ebp
jle    118e <print_buffer+0x5e>
mov    %ebp,%r12d
xor    %ebp,%ebp
lea    0xe9a(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
movzbl (%r14,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <sprintf@plt>
cltq
add    %rax,%rbx
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1170 <print_buffer+0x40>
lea    0xe71(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
add    $0x600,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%02x \00"
@.str.1 = [4 x i8] c"%s\0A\00"
define void @print_buffer(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1536 x i8]
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = bitcast [1536 x i8]* %5 to i8*
%10 = getelementptr inbounds [1536 x i8], [1536 x i8]* %5, i64 0, i64 0
store i8* %10, i8** %6
store i32 0, i32* %7
br label %11
11: ; preds = %27, %2
%12 = load i32, i32* %7
%13 = load i32, i32* %4
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %30
15: ; preds = %11
%16 = load i8*, i8** %6
%17 = load i8*, i8** %3
%18 = load i32, i32* %7
%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)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = load i8*, i8** %6
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8* %26, i8** %6
br label %27
27: ; preds = %15
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
br label %11
30: ; preds = %11
%31 = getelementptr inbounds [1536 x i8], [1536 x i8]* %5, i64 0, i64 0
%32 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %31)
ret void
}

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