#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 */
unsigned short error(char *, ...);
scalar_t__ isdigit(char);
char toupper(char);
unsigned short get_regnum(char *s) {
  unsigned short regnum;
  *s = toupper(*s);
  if (*s != 'R')
    return error("expecting register at '%s'", s);
  ++s;
  if (!isdigit(*s))
    return error("expecting register at '%s'", s);
  regnum = (*s - '0');
  ++s;
  if (isdigit(*s)) {
    regnum = regnum * 10 + (*s - '0');
    if (regnum > 15)
      return error("register number out of range");
    ++s;
  }
  return regnum;
}


x86 assembly (clang, -O0):
<get_regnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <toupper@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x52,%eax
je     117b <get_regnum+0x4b>
mov    -0x10(%rbp),%rsi
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
mov    %ax,-0x2(%rbp)
jmp    1237 <get_regnum+0x107>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
jne    11b8 <get_regnum+0x88>
mov    -0x10(%rbp),%rsi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
mov    %ax,-0x2(%rbp)
jmp    1237 <get_regnum+0x107>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
sub    $0x30,%eax
mov    %ax,-0x12(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     122f <get_regnum+0xff>
movzwl -0x12(%rbp),%eax
imul   $0xa,%eax,%eax
mov    -0x10(%rbp),%rcx
movsbl (%rcx),%ecx
sub    $0x30,%ecx
add    %ecx,%eax
mov    %ax,-0x12(%rbp)
movzwl -0x12(%rbp),%eax
cmp    $0xf,%eax
jle    1223 <get_regnum+0xf3>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
mov    %ax,-0x2(%rbp)
jmp    1237 <get_regnum+0x107>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x12(%rbp),%ax
mov    %ax,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_regnum>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
movsbl (%rdi),%edi
call   1040 <toupper@plt>
mov    %al,(%rbx)
cmp    $0x52,%al
jne    119e <get_regnum+0x6e>
movsbl 0x1(%rbx),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     119a <get_regnum+0x6a>
movsbl 0x1(%rbx),%ebp
add    $0xffffffd0,%ebp
movsbl 0x2(%rbx),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     1191 <get_regnum+0x61>
movswl %bp,%eax
lea    (%rax,%rax,4),%eax
movsbl 0x2(%rbx),%ecx
lea    (%rcx,%rax,2),%ebp
add    $0xffffffd0,%ebp
cmp    $0x10,%bp
jb     1191 <get_regnum+0x61>
lea    0xe97(%rip),%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <error@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
add    $0x1,%rbx
lea    0xe5b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <error@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"expecting register at '%s'\00"
@.str.1 = [29 x i8] c"register number out of range\00"
define zeroext i16 @get_regnum(i8* %0) {
%2 = alloca i16
%3 = alloca i8*
%4 = alloca i16
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i8, i8* %5
%7 = call signext i8 @toupper(i8 signext %6)
%8 = load i8*, i8** %3
store i8 %7, i8* %8
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 82
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call zeroext i16 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %14)
store i16 %15, i16* %2
br label %58
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %3
%19 = load i8*, i8** %3
%20 = load i8, i8* %19
%21 = call i64 @isdigit(i8 signext %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %16
%24 = load i8*, i8** %3
%25 = call zeroext i16 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %24)
store i16 %25, i16* %2
br label %58
26: ; preds = %16
%27 = load i8*, i8** %3
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = sub nsw i32 %29, 48
%31 = trunc i32 %30 to i16
store i16 %31, i16* %4
%32 = load i8*, i8** %3
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %3
%34 = load i8*, i8** %3
%35 = load i8, i8* %34
%36 = call i64 @isdigit(i8 signext %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %56
38: ; preds = %26
%39 = load i16, i16* %4
%40 = zext i16 %39 to i32
%41 = mul nsw i32 %40, 10
%42 = load i8*, i8** %3
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = sub nsw i32 %44, 48
%46 = add nsw i32 %41, %45
%47 = trunc i32 %46 to i16
store i16 %47, i16* %4
%48 = load i16, i16* %4
%49 = zext i16 %48 to i32
%50 = icmp sgt i32 %49, 15
br i1 %50, label %51, label %53
51: ; preds = %38
%52 = call zeroext i16 (i8*, ...) @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i16 %52, i16* %2
br label %58
53: ; preds = %38
%54 = load i8*, i8** %3
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %3
br label %56
56: ; preds = %53, %26
%57 = load i16, i16* %4
store i16 %57, i16* %2
br label %58
58: ; preds = %56, %51, %23, %13
%59 = load i16, i16* %2
ret i16 %59
}

LLVM IR (-O2):
@.str = [27 x i8] c"expecting register at '%s'\00"
@.str.1 = [29 x i8] c"register number out of range\00"
define zeroext i16 @get_regnum(i8* %0) {
%2 = load i8, i8* %0
%3 = tail call signext i8 @toupper(i8 signext %2)
store i8 %3, i8* %0
%4 = icmp eq i8 %3, 82
br i1 %4, label %7, label %5
5: ; preds = %1
%6 = tail call zeroext i16 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %31
7: ; preds = %1
%8 = getelementptr inbounds i8, i8* %0, i64 1
%9 = load i8, i8* %8
%10 = tail call i64 @isdigit(i8 signext %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call zeroext i16 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %31
14: ; preds = %7
%15 = load i8, i8* %8
%16 = sext i8 %15 to i16
%17 = add nsw i16 %16, -48
%18 = getelementptr inbounds i8, i8* %0, i64 2
%19 = load i8, i8* %18
%20 = tail call i64 @isdigit(i8 signext %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %31, label %22
22: ; preds = %14
%23 = mul nsw i16 %17, 10
%24 = load i8, i8* %18
%25 = sext i8 %24 to i16
%26 = add nsw i16 %23, -48
%27 = add nsw i16 %26, %25
%28 = icmp ugt i16 %27, 15
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = tail call zeroext i16 (i8*, ...) @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %14, %22, %29, %12, %5
%32 = phi i16 [ %6, %5 ], [ %30, %29 ], [ %13, %12 ], [ %17, %14 ], [ %27, %22 ]
ret i16 %32
}