#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;
typedef int uint32_t;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, int);
int32_t sprint_buff(uint8_t *hex_buff, uint32_t hex_buff_len, uint8_t *buff,
                    uint32_t buff_len) {
  uint32_t i;
  char *p = (char *)hex_buff;
  uint32_t total = buff_len * 3;
  if (hex_buff_len < total)
    return -1;
  for (i = 0; i < buff_len; ++i) {
    p += sprintf(p, "%02X ", buff[i]);
  }
  *(p - 1) = '\0';
  return (int32_t)(p - (char *)hex_buff);
}


x86 assembly (clang, -O0):
<sprint_buff>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
imul   $0x3,-0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    114d <sprint_buff+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11af <sprint_buff+0x9f>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1199 <sprint_buff+0x89>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe8a(%rip),%rsi
call   1030 <sprintf@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1154 <sprint_buff+0x44>
mov    -0x30(%rbp),%rax
movb   $0x0,-0x1(%rax)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sprint_buff>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    (%rcx,%rcx,2),%eax
mov    $0xffffffff,%ebx
cmp    %esi,%eax
jg     1169 <sprint_buff+0x59>
mov    %rdi,%r14
test   %ecx,%ecx
jle    115f <sprint_buff+0x4f>
mov    %rdx,%r15
mov    %ecx,%r13d
xor    %ebp,%ebp
lea    0xec3(%rip),%r12
mov    %r14,%rbx
mov    (%r15,%rbp,4),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <sprintf@plt>
cltq
add    %rax,%rbx
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1140 <sprint_buff+0x30>
jmp    1162 <sprint_buff+0x52>
mov    %r14,%rbx
movb   $0x0,-0x1(%rbx)
sub    %r14d,%ebx
mov    %ebx,%eax
add    $0x8,%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 i32 @sprint_buff(i32* %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%13 = load i32*, i32** %6
%14 = bitcast i32* %13 to i8*
store i8* %14, i8** %11
%15 = load i32, i32* %9
%16 = mul nsw i32 %15, 3
store i32 %16, i32* %12
%17 = load i32, i32* %7
%18 = load i32, i32* %12
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %4
store i32 -1, i32* %5
br label %50
21: ; preds = %4
store i32 0, i32* %10
br label %22
22: ; preds = %37, %21
%23 = load i32, i32* %10
%24 = load i32, i32* %9
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %40
26: ; preds = %22
%27 = load i8*, i8** %11
%28 = load i32*, i32** %8
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i32, i32* %31
%33 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = load i8*, i8** %11
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** %11
br label %37
37: ; preds = %26
%38 = load i32, i32* %10
%39 = add nsw i32 %38, 1
store i32 %39, i32* %10
br label %22
40: ; preds = %22
%41 = load i8*, i8** %11
%42 = getelementptr inbounds i8, i8* %41, i64 -1
store i8 0, i8* %42
%43 = load i8*, i8** %11
%44 = load i32*, i32** %6
%45 = bitcast i32* %44 to i8*
%46 = ptrtoint i8* %43 to i64
%47 = ptrtoint i8* %45 to i64
%48 = sub i64 %46, %47
%49 = trunc i64 %48 to i32
store i32 %49, i32* %5
br label %50
50: ; preds = %40, %20
%51 = load i32, i32* %5
ret i32 %51
}

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