#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 toupper(char const);
int parseAlphaHex(const char *buf, int digits) {
  int i;
  int r = 0;
  for (i = 0; i < digits; ++i) {
    int c = toupper(buf[i]);
    c -= 'A';
    if (c < 0) {
      return -1;
    }
    if (c > 15) {
      return -1;
    }
    r = (r << 4) + c;
  }
  return r;
}


x86 assembly (clang, -O0):
<parseAlphaHex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119c <parseAlphaHex+0x8c>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
sub    $0x41,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    116c <parseAlphaHex+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a2 <parseAlphaHex+0x92>
cmpl   $0xf,-0x20(%rbp)
jle    1182 <parseAlphaHex+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a2 <parseAlphaHex+0x92>
mov    -0x1c(%rbp),%eax
shl    $0x4,%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112d <parseAlphaHex+0x1d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parseAlphaHex>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    1155 <parseAlphaHex+0x45>
mov    %rdi,%r14
mov    %esi,%r15d
xor    %r12d,%r12d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsbl (%r14,%r12,1),%edi
call   1030 <toupper@plt>
lea    -0x51(%rax),%ecx
cmp    $0xfffffff0,%ecx
jb     1159 <parseAlphaHex+0x49>
shl    $0x4,%ebx
add    %eax,%ebx
add    $0xffffffbf,%ebx
add    $0x1,%r12
cmp    %r12,%r15
jne    1130 <parseAlphaHex+0x20>
jmp    115e <parseAlphaHex+0x4e>
xor    %ebx,%ebx
jmp    115e <parseAlphaHex+0x4e>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @parseAlphaHex(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %7
store i32 0, i32* %6
br label %9
9: ; preds = %34, %2
%10 = load i32, i32* %6
%11 = load i32, i32* %5
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %37
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = call i32 @toupper(i8 signext %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = sub nsw i32 %20, 65
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %13
store i32 -1, i32* %3
br label %39
25: ; preds = %13
%26 = load i32, i32* %8
%27 = icmp sgt i32 %26, 15
br i1 %27, label %28, label %29
28: ; preds = %25
store i32 -1, i32* %3
br label %39
29: ; preds = %25
%30 = load i32, i32* %7
%31 = shl i32 %30, 4
%32 = load i32, i32* %8
%33 = add nsw i32 %31, %32
store i32 %33, i32* %7
br label %34
34: ; preds = %29
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %6
br label %9
37: ; preds = %9
%38 = load i32, i32* %7
store i32 %38, i32* %3
br label %39
39: ; preds = %37, %28, %24
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
define i32 @parseAlphaHex(i8* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %20
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %14
%7 = phi i64 [ 0, %4 ], [ %18, %14 ]
%8 = phi i32 [ 0, %4 ], [ %17, %14 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %7
%10 = load i8, i8* %9
%11 = tail call i32 @toupper(i8 signext %10)
%12 = add i32 %11, -81
%13 = icmp ult i32 %12, -16
br i1 %13, label %20, label %14
14: ; preds = %6
%15 = shl i32 %8, 4
%16 = add i32 %15, -65
%17 = add i32 %16, %11
%18 = add nuw nsw i64 %7, 1
%19 = icmp eq i64 %18, %5
br i1 %19, label %20, label %6
20: ; preds = %6, %14, %2
%21 = phi i32 [ 0, %2 ], [ %17, %14 ], [ -1, %6 ]
ret i32 %21
}