#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 strlen(char *);
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int);
int base_operand(char *operand) {
  int last = strlen(operand) - 1;
  int base;
  switch (operand[last]) {
  case 'h':
  case 'H':
    base = 16;
    break;
  case 'd':
  case 'D':
    base = 10;
    break;
  default:
    return -1;
    break;
  }
  return (unsigned int)strtol(operand, NULL, base);
}


x86 assembly (clang, -O0):
<base_operand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
add    $0xffffffbc,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x24,%eax
ja     1185 <base_operand+0x65>
mov    -0x20(%rbp),%rax
lea    0xe9c(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x10,-0x18(%rbp)
jmp    1191 <base_operand+0x71>
movl   $0xa,-0x18(%rbp)
jmp    1191 <base_operand+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a4 <base_operand+0x84>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <strtol@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base_operand>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
cltq
movsbl -0x1(%rax,%rbx,1),%ecx
add    $0xffffffbc,%ecx
rol    $0x1e,%ecx
mov    $0xffffffff,%eax
cmp    $0x9,%ecx
ja     1161 <base_operand+0x41>
mov    $0x303,%edx
bt     %ecx,%edx
jae    1161 <base_operand+0x41>
movslq %ecx,%rax
lea    0xeac(%rip),%rcx
mov    (%rcx,%rax,4),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <strtol@plt>
pop    %rbx
ret

LLVM IR (-O0):
define i32 @base_operand(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
%8 = sub nsw i32 %7, 1
store i32 %8, i32* %4
%9 = load i8*, i8** %3
%10 = load i32, i32* %4
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
switch i32 %14, label %17 [
i32 104, label %15
i32 72, label %15
i32 100, label %16
i32 68, label %16
]
15: ; preds = %1, %1
store i32 16, i32* %5
br label %18
16: ; preds = %1, %1
store i32 10, i32* %5
br label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %23
18: ; preds = %16, %15
%19 = load i8*, i8** %3
%20 = load i32, i32* %5
%21 = call i64 @strtol(i8* %19, i32* null, i32 %20)
%22 = trunc i64 %21 to i32
store i32 %22, i32* %2
br label %23
23: ; preds = %18, %17
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@switch.table.base_operand = [10 x i32] [i32 10, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 10, i32 16]
define i32 @base_operand(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = load i8, i8* %5
%7 = sext i8 %6 to i32
%8 = add nsw i32 %7, -68
%9 = tail call i32 @llvm.fshl.i32(i32 %8, i32 %8, i32 30)
%10 = icmp ult i32 %9, 10
br i1 %10, label %11, label %22
11: ; preds = %1
%12 = trunc i32 %9 to i16
%13 = lshr i16 771, %12
%14 = and i16 %13, 1
%15 = icmp eq i16 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %11
%17 = sext i32 %9 to i64
%18 = getelementptr inbounds [10 x i32], [10 x i32]* @switch.table.base_operand, i64 0, i64 %17
%19 = load i32, i32* %18
%20 = tail call i64 @strtol(i8* nocapture %0, i32* null, i32 %19)
%21 = trunc i64 %20 to i32
br label %22
22: ; preds = %11, %1, %16
%23 = phi i32 [ %21, %16 ], [ -1, %1 ], [ -1, %11 ]
ret i32 %23
}