#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__ isalpha(char);
scalar_t__ isdigit(char);
int /*<<< orphan*/ toupper(char);
int __digit(char digit, int base) {
  int val;
  if (isdigit(digit)) {
    val = digit - '0';
  } else if (isalpha(digit)) {
    toupper(digit);
    val = digit - 'A';
  } else {
    return -1;
  }
  return (val >= base) ? -1 : val;
}


x86 assembly (clang, -O0):
<__digit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %esi,-0xc(%rbp)
movsbl -0x5(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1163 <__digit+0x33>
movsbl -0x5(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x10(%rbp)
jmp    119f <__digit+0x6f>
movsbl -0x5(%rbp),%edi
call   1050 <isalpha@plt>
cmp    $0x0,%rax
je     118e <__digit+0x5e>
movsbl -0x5(%rbp),%edi
call   1040 <toupper@plt>
movsbl -0x5(%rbp),%eax
sub    $0x41,%eax
mov    %eax,-0x10(%rbp)
jmp    119a <__digit+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c4 <__digit+0x94>
jmp    119f <__digit+0x6f>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     11b8 <__digit+0x88>
mov    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11be <__digit+0x8e>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__digit>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
call   1030 <isdigit@plt>
mov    $0xffffffd0,%ecx
movsbl %bpl,%ebp
test   %rax,%rax
jne    1162 <__digit+0x32>
mov    %ebp,%edi
call   1050 <isalpha@plt>
test   %rax,%rax
je     1175 <__digit+0x45>
mov    %ebp,%edi
call   1040 <toupper@plt>
mov    $0xffffffbf,%ecx
add    %ebp,%ecx
cmp    %ebx,%ecx
mov    $0xffffffff,%eax
cmovl  %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    116e <__digit+0x3e>

LLVM IR (-O0):
define i32 @__digit(i8 signext %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
store i8 %0, i8* %4
store i32 %1, i32* %5
%7 = load i8, i8* %4
%8 = call i64 @isdigit(i8 signext %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i8, i8* %4
%12 = sext i8 %11 to i32
%13 = sub nsw i32 %12, 48
store i32 %13, i32* %6
br label %26
14: ; preds = %2
%15 = load i8, i8* %4
%16 = call i64 @isalpha(i8 signext %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = load i8, i8* %4
%20 = call i32 @toupper(i8 signext %19)
%21 = load i8, i8* %4
%22 = sext i8 %21 to i32
%23 = sub nsw i32 %22, 65
store i32 %23, i32* %6
br label %25
24: ; preds = %14
store i32 -1, i32* %3
br label %35
25: ; preds = %18
br label %26
26: ; preds = %25, %10
%27 = load i32, i32* %6
%28 = load i32, i32* %5
%29 = icmp sge i32 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
br label %33
31: ; preds = %26
%32 = load i32, i32* %6
br label %33
33: ; preds = %31, %30
%34 = phi i32 [ -1, %30 ], [ %32, %31 ]
store i32 %34, i32* %3
br label %35
35: ; preds = %33, %24
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
define i32 @__digit(i8 signext %0, i32 %1) {
%3 = tail call i64 @isdigit(i8 signext %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = tail call i64 @isalpha(i8 signext %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %5
%9 = tail call i32 @toupper(i8 signext %0)
br label %10
10: ; preds = %2, %8
%11 = phi i32 [ -65, %8 ], [ -48, %2 ]
%12 = sext i8 %0 to i32
%13 = add nsw i32 %11, %12
%14 = icmp slt i32 %13, %1
%15 = select i1 %14, i32 %13, i32 -1
br label %16
16: ; preds = %5, %10
%17 = phi i32 [ %15, %10 ], [ -1, %5 ]
ret i32 %17
}