#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*/ sscanf(char *, char *, unsigned int *);
size_t strlen(char *);
size_t hexstr2char(char *src, char *dest, size_t destsize) {
  size_t i = 0;
  size_t size = strlen(src);
  char *ptr = dest;
  unsigned int ch;
  if ((size / 2) > destsize)
    size = destsize;
  while (i < size) {
    sscanf(src, "%2x", &ch);
    *dest++ = ch;
    src += 2;
    i += 2;
  }
  return size / 2;
}


x86 assembly (clang, -O0):
<hexstr2char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
shr    $0x1,%rax
cmp    -0x18(%rbp),%rax
jbe    116b <hexstr2char+0x4b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1170 <hexstr2char+0x50>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11c5 <hexstr2char+0xa5>
mov    -0x8(%rbp),%rdi
lea    0xe77(%rip),%rsi
lea    -0x34(%rbp),%rdx
call   1040 <sscanf@plt>
mov    -0x34(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    1170 <hexstr2char+0x50>
mov    -0x28(%rbp),%rax
shr    $0x1,%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexstr2char>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%r15
shr    %rax
cmp    %r12,%rax
cmova  %r12,%r15
test   %r15,%r15
je     1183 <hexstr2char+0x63>
xor    %ebp,%ebp
lea    0xeac(%rip),%r12
lea    0x4(%rsp),%r13
nopl   0x0(%rax)
lea    (%r14,%rbp,1),%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <sscanf@plt>
movzbl 0x4(%rsp),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0x2,%rbp
cmp    %r15,%rbp
jb     1160 <hexstr2char+0x40>
shr    %r15
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"%2x\00"
define i64 @hexstr2char(i8* nocapture %0, i8* nocapture writeonly %1, i64 %2) {
%4 = alloca i32
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = bitcast i32* %4 to i8*
%7 = lshr i64 %5, 1
%8 = icmp ugt i64 %7, %2
%9 = select i1 %8, i64 %2, i64 %5
%10 = icmp eq i64 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %3, %11
%12 = phi i8* [ %19, %11 ], [ %0, %3 ]
%13 = phi i64 [ %20, %11 ], [ 0, %3 ]
%14 = phi i8* [ %18, %11 ], [ %1, %3 ]
%15 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %4)
%16 = load i32, i32* %4
%17 = trunc i32 %16 to i8
%18 = getelementptr inbounds i8, i8* %14, i64 1
store i8 %17, i8* %14
%19 = getelementptr inbounds i8, i8* %12, i64 2
%20 = add i64 %13, 2
%21 = icmp ult i64 %20, %9
br i1 %21, label %11, label %22
22: ; preds = %11, %3
%23 = lshr i64 %9, 1
ret i64 %23
}