#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 */
char *strchr(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ toupper(int);
int scanhex(char **cmdp, int n, unsigned char *dest) {
  static const char hexdigits[] = "0123456789ABCDEF";
  int c, v;
  char *p, *e;
  p = *cmdp;
  while (n--) {
    c = *p++;
    e = strchr(hexdigits, toupper(c));
    if (!e)
      return 0;
    v = (int)(e - hexdigits) << 4;
    c = *p++;
    e = strchr(hexdigits, toupper(c));
    if (!e)
      return 0;
    *dest++ = v | (int)(e - hexdigits);
  }
  *cmdp = p;
  return 1;
}


x86 assembly (clang, -O0):
<scanhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     121d <scanhex+0xfd>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movsbl (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <toupper@plt>
mov    %eax,%esi
lea    0xe88(%rip),%rdi
call   1040 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1198 <scanhex+0x78>
movl   $0x0,-0x4(%rbp)
jmp    122f <scanhex+0x10f>
mov    -0x38(%rbp),%rax
lea    0xe5d(%rip),%rcx
sub    %rcx,%rax
shl    $0x4,%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movsbl (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <toupper@plt>
mov    %eax,%esi
lea    0xe2e(%rip),%rdi
call   1040 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f2 <scanhex+0xd2>
movl   $0x0,-0x4(%rbp)
jmp    122f <scanhex+0x10f>
mov    -0x28(%rbp),%eax
mov    -0x38(%rbp),%rcx
lea    0xe00(%rip),%rdx
sub    %rdx,%rcx
or     %ecx,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    113e <scanhex+0x1e>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scanhex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x10(%rsp)
mov    (%rdi),%rbp
test   %esi,%esi
je     11b7 <scanhex+0x97>
mov    %rdi,0x8(%rsp)
mov    %esi,%r14d
lea    0xeb7(%rip),%r13
xor    %r15d,%r15d
xor    %r12d,%r12d
nop
movsbl 0x0(%rbp,%r12,2),%edi
call   1030 <toupper@plt>
mov    $0x11,%edx
mov    %r13,%rdi
mov    %eax,%esi
call   1040 <memchr@plt>
test   %rax,%rax
je     11c1 <scanhex+0xa1>
mov    %rax,%rbx
movsbl 0x1(%rbp,%r12,2),%edi
call   1030 <toupper@plt>
mov    $0x11,%edx
mov    %r13,%rdi
mov    %eax,%esi
call   1040 <memchr@plt>
test   %rax,%rax
je     11c1 <scanhex+0xa1>
shl    $0x4,%bl
sub    %r13b,%al
or     %bl,%al
mov    0x10(%rsp),%rcx
mov    %al,(%rcx,%r12,1)
add    $0x1,%r12
add    $0xfffffffffffffffe,%r15
cmp    %r12d,%r14d
jne    1150 <scanhex+0x30>
sub    %r15,%rbp
mov    0x8(%rsp),%rdi
mov    %rbp,(%rdi)
mov    $0x1,%eax
jmp    11c3 <scanhex+0xa3>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@scanhex.hexdigits = internal constant [17 x i8] c"0123456789ABCDEF\00"
define i32 @scanhex(i8** %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%12 = load i8**, i8*** %5
%13 = load i8*, i8** %12
store i8* %13, i8** %10
br label %14
14: ; preds = %45, %3
%15 = load i32, i32* %6
%16 = add nsw i32 %15, -1
store i32 %16, i32* %6
%17 = icmp ne i32 %15, 0
br i1 %17, label %18, label %55
18: ; preds = %14
%19 = load i8*, i8** %10
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %10
%21 = load i8, i8* %19
%22 = sext i8 %21 to i32
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = call i32 @toupper(i32 %23)
%25 = call i8* @strchr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @scanhex.hexdigits, i64 0, i64 0), i32 %24)
store i8* %25, i8** %11
%26 = load i8*, i8** %11
%27 = icmp ne i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %18
store i32 0, i32* %4
br label %58
29: ; preds = %18
%30 = load i8*, i8** %11
%31 = ptrtoint i8* %30 to i64
%32 = sub i64 %31, ptrtoint ([17 x i8]* @scanhex.hexdigits to i64)
%33 = trunc i64 %32 to i32
%34 = shl i32 %33, 4
store i32 %34, i32* %9
%35 = load i8*, i8** %10
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %10
%37 = load i8, i8* %35
%38 = sext i8 %37 to i32
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = call i32 @toupper(i32 %39)
%41 = call i8* @strchr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @scanhex.hexdigits, i64 0, i64 0), i32 %40)
store i8* %41, i8** %11
%42 = load i8*, i8** %11
%43 = icmp ne i8* %42, null
br i1 %43, label %45, label %44
44: ; preds = %29
store i32 0, i32* %4
br label %58
45: ; preds = %29
%46 = load i32, i32* %9
%47 = load i8*, i8** %11
%48 = ptrtoint i8* %47 to i64
%49 = sub i64 %48, ptrtoint ([17 x i8]* @scanhex.hexdigits to i64)
%50 = trunc i64 %49 to i32
%51 = or i32 %46, %50
%52 = trunc i32 %51 to i8
%53 = load i8*, i8** %7
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %7
store i8 %52, i8* %53
br label %14
55: ; preds = %14
%56 = load i8*, i8** %10
%57 = load i8**, i8*** %5
store i8* %56, i8** %57
store i32 1, i32* %4
br label %58
58: ; preds = %55, %44, %28
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@scanhex.hexdigits = internal constant [17 x i8] c"0123456789ABCDEF\00"
define i32 @scanhex(i8** nocapture %0, i32 %1, i8* nocapture writeonly %2) {
%4 = load i8*, i8** %0
%5 = icmp eq i32 %1, 0
br i1 %5, label %34, label %6
6: ; preds = %3, %23
%7 = phi i32 [ %10, %23 ], [ %1, %3 ]
%8 = phi i8* [ %24, %23 ], [ %4, %3 ]
%9 = phi i8* [ %32, %23 ], [ %2, %3 ]
%10 = add nsw i32 %7, -1
%11 = load i8, i8* %8
%12 = sext i8 %11 to i32
%13 = tail call i32 @toupper(i32 %12)
%14 = tail call i8* @memchr(i8* dereferenceable(17) getelementptr inbounds ([17 x i8], [17 x i8]* @scanhex.hexdigits, i64 0, i64 0), i32 %13, i64 17)
%15 = icmp eq i8* %14, null
br i1 %15, label %36, label %16
16: ; preds = %6
%17 = getelementptr inbounds i8, i8* %8, i64 1
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = tail call i32 @toupper(i32 %19)
%21 = tail call i8* @memchr(i8* dereferenceable(17) getelementptr inbounds ([17 x i8], [17 x i8]* @scanhex.hexdigits, i64 0, i64 0), i32 %20, i64 17)
%22 = icmp eq i8* %21, null
br i1 %22, label %36, label %23
23: ; preds = %16
%24 = getelementptr inbounds i8, i8* %8, i64 2
%25 = ptrtoint i8* %14 to i64
%26 = trunc i64 %25 to i8
%27 = shl i8 %26, 4
%28 = ptrtoint i8* %21 to i64
%29 = trunc i64 %28 to i8
%30 = sub i8 %29, ptrtoint ([17 x i8]* @scanhex.hexdigits to i8)
%31 = or i8 %30, %27
%32 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %31, i8* %9
%33 = icmp eq i32 %10, 0
br i1 %33, label %34, label %6
34: ; preds = %23, %3
%35 = phi i8* [ %4, %3 ], [ %24, %23 ]
store i8* %35, i8** %0
br label %36
36: ; preds = %16, %6, %34
%37 = phi i32 [ 1, %34 ], [ 0, %6 ], [ 0, %16 ]
ret i32 %37
}