#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ sscanf(unsigned char *, char *, unsigned char *);
int strlen(char *);
unsigned char *hex_to_byte(char *hex_str, int *len) {
  unsigned char *res = (char *)malloc(strlen(hex_str) / 2);
  unsigned char *ptr = hex_str;
  *len = strlen(hex_str) / 2;
  int i = 0;
  for (i = 0; i < *len; i++) {
    sscanf(ptr, "%2hhX", &res[i]);
    ptr += 2;
  }
  return res;
}


x86 assembly (clang, -O0):
<hex_to_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    11cf <hex_to_byte+0x9f>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
movslq -0x24(%rbp),%rax
add    %rax,%rdx
lea    0xe50(%rip),%rsi
call   1050 <sscanf@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    118b <hex_to_byte+0x5b>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_byte>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r13
call   1030 <strlen@plt>
mov    %eax,%edi
shr    $0x1f,%edi
add    %eax,%edi
sar    %edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %r13,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
mov    %ecx,(%r14)
cmp    $0x2,%eax
jl     119f <hex_to_byte+0x6f>
xor    %ebx,%ebx
lea    0xe89(%rip),%r12
nopw   0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%rdx
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <sscanf@plt>
add    $0x2,%r13
add    $0x1,%rbx
movslq (%r14),%rax
cmp    %rax,%rbx
jl     1180 <hex_to_byte+0x50>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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