#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 sprintf(char *, char *, unsigned char);
int binkey_sprintf(char *str, const void *key, size_t key_len) {
  int pos = 0;
  for (size_t i = 0; i < key_len; i++) {
    unsigned char b = ((unsigned char *)key)[i];
    int rc = sprintf(&str[pos], "%hhX", b);
    if (rc < 0)
      return rc;
    pos += rc;
  }
  return pos;
}


x86 assembly (clang, -O0):
<binkey_sprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    119c <binkey_sprintf+0x8c>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x31(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
lea    0xe9f(%rip),%rsi
movzbl -0x31(%rbp),%edx
call   1030 <sprintf@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1182 <binkey_sprintf+0x72>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a2 <binkey_sprintf+0x92>
mov    -0x38(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1133 <binkey_sprintf+0x23>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binkey_sprintf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1164 <binkey_sprintf+0x54>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %ebx,%ebx
lea    0xece(%rip),%r13
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl (%r15,%rbp,1),%edx
movslq %ebx,%rdi
add    %r12,%rdi
mov    %r13,%rsi
call   1030 <sprintf@plt>
test   %eax,%eax
js     1168 <binkey_sprintf+0x58>
add    %eax,%ebx
add    $0x1,%rbp
cmp    %rbp,%r14
jne    1140 <binkey_sprintf+0x30>
jmp    116a <binkey_sprintf+0x5a>
xor    %ebx,%ebx
jmp    116a <binkey_sprintf+0x5a>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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