#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*/ memset(unsigned char *, int /*<<< orphan*/, size_t);
unsigned char strtol(char *, int /*<<< orphan*/ *, int);
int hex_scan(char *str, unsigned char *buf, size_t n) {
  size_t i;
  char tmp[] = {'0', '0', '\0'};
  memset(buf, 0, n);
  for (i = 0; i < n; i++) {
    tmp[0] = str[2 * i];
    tmp[1] = str[2 * i + 1];
    if (tmp[0] == '\0' || tmp[1] == '\0')
      return 0;
    buf[i] = strtol(tmp, NULL, 16);
  }
  return n;
}


x86 assembly (clang, -O0):
<hex_scan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0xec5(%rip),%ax
mov    %ax,-0x2b(%rbp)
mov    0xebd(%rip),%al
mov    %al,-0x29(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11e8 <hex_scan+0xc8>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
shl    $0x1,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x2b(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
shl    $0x1,%rcx
mov    0x1(%rax,%rcx,1),%al
mov    %al,-0x2a(%rbp)
movsbl -0x2b(%rbp),%eax
cmp    $0x0,%eax
je     11ac <hex_scan+0x8c>
movsbl -0x2a(%rbp),%eax
cmp    $0x0,%eax
jne    11b8 <hex_scan+0x98>
movl   $0x0,-0x4(%rbp)
jmp    11ef <hex_scan+0xcf>
lea    -0x2b(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    115f <hex_scan+0x3f>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_scan>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
movb   $0x0,0x6(%rsp)
movw   $0x3030,0x4(%rsp)
mov    %rsi,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
test   %r15,%r15
je     1195 <hex_scan+0x75>
xor    %r12d,%r12d
lea    0x4(%rsp),%r13
xor    %ebp,%ebp
nopl   0x0(%rax)
movzbl (%rbx,%rbp,2),%ecx
mov    %cl,0x4(%rsp)
movzbl 0x1(%rbx,%rbp,2),%eax
mov    %al,0x5(%rsp)
test   %cl,%cl
je     1198 <hex_scan+0x78>
test   %al,%al
je     1198 <hex_scan+0x78>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1160 <hex_scan+0x40>
mov    %r15d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.hex_scan.tmp = [3 x i8] c"00\00"
define i32 @hex_scan(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca [3 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = bitcast [3 x i8]* %9 to i8*
%11 = load i8*, i8** %6
%12 = load i64, i64* %7
%13 = call i32 @memset(i8* %11, i32 0, i64 %12)
store i64 0, i64* %8
br label %14
14: ; preds = %48, %3
%15 = load i64, i64* %8
%16 = load i64, i64* %7
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %51
18: ; preds = %14
%19 = load i8*, i8** %5
%20 = load i64, i64* %8
%21 = mul i64 2, %20
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
store i8 %23, i8* %24
%25 = load i8*, i8** %5
%26 = load i64, i64* %8
%27 = mul i64 2, %26
%28 = add i64 %27, 1
%29 = getelementptr inbounds i8, i8* %25, i64 %28
%30 = load i8, i8* %29
%31 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 1
store i8 %30, i8* %31
%32 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %18
%37 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 1
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %36, %18
store i32 0, i32* %4
br label %54
42: ; preds = %36
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%44 = call zeroext i8 @strtol(i8* %43, i32* null, i32 16)
%45 = load i8*, i8** %6
%46 = load i64, i64* %8
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8 %44, i8* %47
br label %48
48: ; preds = %42
%49 = load i64, i64* %8
%50 = add i64 %49, 1
store i64 %50, i64* %8
br label %14
51: ; preds = %14
%52 = load i64, i64* %7
%53 = trunc i64 %52 to i32
store i32 %53, i32* %4
br label %54
54: ; preds = %51, %41
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@__const.hex_scan.tmp = [3 x i8] c"00\00"
define i32 @hex_scan(i8* nocapture %0, i8* %1, i64 %2) {
%4 = alloca [3 x i8]
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @memset(i8* %1, i32 0, i64 %2)
%7 = icmp eq i64 %2, 0
br i1 %7, label %26, label %8
8: ; preds = %3
%9 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
br label %10
10: ; preds = %8, %21
%11 = phi i64 [ 0, %8 ], [ %24, %21 ]
%12 = shl i64 %11, 1
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
store i8 %14, i8* %5
%15 = or i64 %12, 1
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
store i8 %17, i8* %9
%18 = icmp eq i8 %14, 0
%19 = icmp eq i8 %17, 0
%20 = select i1 %18, i1 true, i1 %19
br i1 %20, label %28, label %21
21: ; preds = %10
%22 = call zeroext i8 @strtol(i8* nocapture %5, i32* null, i32 16)
%23 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 %22, i8* %23
%24 = add nuw i64 %11, 1
%25 = icmp eq i64 %24, %2
br i1 %25, label %26, label %10
26: ; preds = %21, %3
%27 = trunc i64 %2 to i32
br label %28
28: ; preds = %10, %26
%29 = phi i32 [ %27, %26 ], [ 0, %10 ]
ret i32 %29
}