#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 /*<<< orphan*/ alltrim(char *);
char *strchr(char *, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ toupper(char);
int base2int(int base, char *string) {
  static char *t = "0123456789ABCDEF";
  char *p;
  int l, i, value = 0;
  if (!string)
    return -1;
  alltrim(string);
  l = strlen(string) - 1;
  for (i = 1; l >= 0; i *= base, l--) {
    if ((p = strchr(t, toupper(string[l]))) == NULL)
      return -1;
    value += (p - t) * i;
  }
  return (value);
}


x86 assembly (clang, -O0):
<base2int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <base2int+0x2d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <base2int+0xd5>
mov    -0x10(%rbp),%rdi
call   1060 <alltrim@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     120f <base2int+0xcf>
mov    0x2ea3(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1050 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d7 <base2int+0x97>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <base2int+0xd5>
mov    -0x18(%rbp),%rcx
mov    0x2e5e(%rip),%rax
sub    %rax,%rcx
movslq -0x20(%rbp),%rax
imul   %rax,%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%eax
imul   -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    118c <base2int+0x4c>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base2int>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     11ba <base2int+0x7a>
mov    %rsi,%r12
mov    %edi,%r14d
mov    %rsi,%rdi
call   1060 <alltrim@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11c2 <base2int+0x82>
mov    %eax,%ebx
add    $0x1,%rbx
mov    $0x1,%ebp
xor    %r13d,%r13d
lea    0xe81(%rip),%r15
nop
lea    -0x2(%rbx),%eax
movsbl (%r12,%rax,1),%edi
call   1030 <toupper@plt>
mov    $0x11,%edx
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <memchr@plt>
test   %rax,%rax
je     11ba <base2int+0x7a>
sub    %r15d,%eax
imul   %ebp,%eax
add    %eax,%r13d
imul   %r14d,%ebp
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1180 <base2int+0x40>
jmp    11c5 <base2int+0x85>
mov    $0xffffffff,%r13d
jmp    11c5 <base2int+0x85>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@base2int.t = internal global i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i32 0, i32 0)
@.str = [17 x i8] c"0123456789ABCDEF\00"
define i32 @base2int(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %9
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 -1, i32* %3
br label %54
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = call i32 @alltrim(i8* %14)
%16 = load i8*, i8** %5
%17 = call i32 @strlen(i8* %16)
%18 = sub nsw i32 %17, 1
store i32 %18, i32* %7
store i32 1, i32* %8
br label %19
19: ; preds = %46, %13
%20 = load i32, i32* %7
%21 = icmp sge i32 %20, 0
br i1 %21, label %22, label %52
22: ; preds = %19
%23 = load i8*, i8** @base2int.t
%24 = load i8*, i8** %5
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = call i32 @toupper(i8 signext %28)
%30 = call i8* @strchr(i8* %23, i32 %29)
store i8* %30, i8** %6
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %22
store i32 -1, i32* %3
br label %54
33: ; preds = %22
%34 = load i8*, i8** %6
%35 = load i8*, i8** @base2int.t
%36 = ptrtoint i8* %34 to i64
%37 = ptrtoint i8* %35 to i64
%38 = sub i64 %36, %37
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = mul nsw i64 %38, %40
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = add nsw i64 %43, %41
%45 = trunc i64 %44 to i32
store i32 %45, i32* %9
br label %46
46: ; preds = %33
%47 = load i32, i32* %4
%48 = load i32, i32* %8
%49 = mul nsw i32 %48, %47
store i32 %49, i32* %8
%50 = load i32, i32* %7
%51 = add nsw i32 %50, -1
store i32 %51, i32* %7
br label %19
52: ; preds = %19
%53 = load i32, i32* %9
store i32 %53, i32* %3
br label %54
54: ; preds = %52, %32, %12
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789ABCDEF\00"
define i32 @base2int(i32 %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %29, label %4
4: ; preds = %2
%5 = tail call i32 @alltrim(i8* %1)
%6 = tail call i32 @strlen(i8* %1)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %29
8: ; preds = %4
%9 = zext i32 %6 to i64
br label %10
10: ; preds = %8, %21
%11 = phi i64 [ %9, %8 ], [ %14, %21 ]
%12 = phi i32 [ 0, %8 ], [ %26, %21 ]
%13 = phi i32 [ 1, %8 ], [ %27, %21 ]
%14 = add nsw i64 %11, -1
%15 = and i64 %14, 4294967295
%16 = getelementptr inbounds i8, i8* %1, i64 %15
%17 = load i8, i8* %16
%18 = tail call i32 @toupper(i8 signext %17)
%19 = tail call i8* @memchr(i8* dereferenceable(17) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %18, i64 17)
%20 = icmp eq i8* %19, null
br i1 %20, label %29, label %21
21: ; preds = %10
%22 = ptrtoint i8* %19 to i64
%23 = trunc i64 %22 to i32
%24 = sub i32 %23, ptrtoint ([17 x i8]* @.str to i32)
%25 = mul i32 %24, %13
%26 = add i32 %25, %12
%27 = mul nsw i32 %13, %0
%28 = icmp ugt i64 %11, 1
br i1 %28, label %10, label %29
29: ; preds = %10, %21, %4, %2
%30 = phi i32 [ -1, %2 ], [ 0, %4 ], [ -1, %10 ], [ %26, %21 ]
ret i32 %30
}