#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(unsigned char *, char *, unsigned char);
int char_get_from_hex(unsigned char *hex, int hex_len, unsigned char *out_buf,
                      int out_buf_len) {
  int i;
  if ((hex_len * 2 + 1) > out_buf_len) {
    return -1;
  }
  for (i = 0; i < hex_len; i++) {
    sprintf(&out_buf[2 * i], "%02X", hex[i]);
  }
  out_buf[hex_len * 2 + 1] = 0;
  return 0;
}


x86 assembly (clang, -O0):
<char_get_from_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cmp    -0x24(%rbp),%eax
jle    1144 <char_get_from_hex+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a7 <char_get_from_hex+0x97>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118c <char_get_from_hex+0x7c>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
lea    0xe8b(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1030 <sprintf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    114b <char_get_from_hex+0x3b>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<char_get_from_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    (%rsi,%rsi,1),%eax
movslq %eax,%r13
or     $0x1,%r13
mov    $0xffffffff,%eax
cmp    %ecx,%r13d
jg     1178 <char_get_from_hex+0x68>
mov    %rdx,(%rsp)
test   %esi,%esi
jle    116d <char_get_from_hex+0x5d>
mov    %rdi,%r12
mov    %esi,%ebp
xor    %r14d,%r14d
lea    0xeba(%rip),%r15
mov    (%rsp),%rbx
nopw   0x0(%rax,%rax,1)
movzbl (%r12,%r14,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <sprintf@plt>
add    $0x1,%r14
add    $0x2,%rbx
cmp    %r14,%rbp
jne    1150 <char_get_from_hex+0x40>
mov    (%rsp),%rax
movb   $0x0,(%rax,%r13,1)
xor    %eax,%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"%02X\00"
define i32 @char_get_from_hex(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i32, i32* %7
%12 = mul nsw i32 %11, 2
%13 = add nsw i32 %12, 1
%14 = load i32, i32* %9
%15 = icmp sgt i32 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 -1, i32* %5
br label %44
17: ; preds = %4
store i32 0, i32* %10
br label %18
18: ; preds = %34, %17
%19 = load i32, i32* %10
%20 = load i32, i32* %7
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %37
22: ; preds = %18
%23 = load i8*, i8** %8
%24 = load i32, i32* %10
%25 = mul nsw i32 2, %24
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = load i8*, i8** %6
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %32)
br label %34
34: ; preds = %22
%35 = load i32, i32* %10
%36 = add nsw i32 %35, 1
store i32 %36, i32* %10
br label %18
37: ; preds = %18
%38 = load i8*, i8** %8
%39 = load i32, i32* %7
%40 = mul nsw i32 %39, 2
%41 = add nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %38, i64 %42
store i8 0, i8* %43
store i32 0, i32* %5
br label %44
44: ; preds = %37, %16
%45 = load i32, i32* %5
ret i32 %45
}

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