#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(unsigned char);
scalar_t__ isdigit(unsigned char);
char toupper(char);
int s_ch2val(char c, int r) {
  int out;
  if (isdigit((unsigned char)c))
    out = c - '0';
  else if (r > 10 && isalpha((unsigned char)c))
    out = toupper(c) - 'A' + 10;
  else
    return -1;
  return (out >= r) ? -1 : out;
}


x86 assembly (clang, -O0):
<s_ch2val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %esi,-0xc(%rbp)
movzbl -0x5(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1163 <s_ch2val+0x33>
movsbl -0x5(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x10(%rbp)
jmp    11ab <s_ch2val+0x7b>
cmpl   $0xa,-0xc(%rbp)
jle    119a <s_ch2val+0x6a>
movzbl -0x5(%rbp),%edi
call   1050 <isalpha@plt>
cmp    $0x0,%rax
je     119a <s_ch2val+0x6a>
movsbl -0x5(%rbp),%edi
call   1040 <toupper@plt>
movsbl %al,%eax
sub    $0x41,%eax
add    $0xa,%eax
mov    %eax,-0x10(%rbp)
jmp    11a6 <s_ch2val+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <s_ch2val+0xa0>
jmp    11ab <s_ch2val+0x7b>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     11c4 <s_ch2val+0x94>
mov    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11ca <s_ch2val+0x9a>
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):
<s_ch2val>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
movzbl %r15b,%ebp
mov    %ebp,%edi
call   1030 <isdigit@plt>
mov    $0xffffffd0,%ecx
test   %rax,%rax
je     1170 <s_ch2val+0x40>
movsbl %r15b,%eax
add    %ecx,%eax
cmp    %r14d,%eax
mov    $0xffffffff,%ebx
cmovl  %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebx
cmp    $0xb,%r14d
jl     1163 <s_ch2val+0x33>
mov    %ebp,%edi
call   1050 <isalpha@plt>
test   %rax,%rax
je     1163 <s_ch2val+0x33>
movsbl %r15b,%edi
call   1040 <toupper@plt>
mov    %eax,%r15d
mov    $0xffffffc9,%ecx
jmp    1152 <s_ch2val+0x22>

LLVM IR (-O0):
define i32 @s_ch2val(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 zeroext %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 %29
14: ; preds = %2
%15 = load i32, i32* %5
%16 = icmp sgt i32 %15, 10
br i1 %16, label %17, label %27
17: ; preds = %14
%18 = load i8, i8* %4
%19 = call i64 @isalpha(i8 zeroext %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = load i8, i8* %4
%23 = call signext i8 @toupper(i8 signext %22)
%24 = sext i8 %23 to i32
%25 = sub nsw i32 %24, 65
%26 = add nsw i32 %25, 10
store i32 %26, i32* %6
br label %28
27: ; preds = %17, %14
store i32 -1, i32* %3
br label %38
28: ; preds = %21
br label %29
29: ; preds = %28, %10
%30 = load i32, i32* %6
%31 = load i32, i32* %5
%32 = icmp sge i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
br label %36
34: ; preds = %29
%35 = load i32, i32* %6
br label %36
36: ; preds = %34, %33
%37 = phi i32 [ -1, %33 ], [ %35, %34 ]
store i32 %37, i32* %3
br label %38
38: ; preds = %36, %27
%39 = load i32, i32* %3
ret i32 %39
}

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