#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*/ *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char const *, int);
unsigned long strtoul(char *, char **, int /*<<< orphan*/);
int str2int(const char *str) {
  int a;
  unsigned long value;
  char *endptr, buf[11];
  strncpy(buf, str, 10);
  buf[10] = '\0';
  a = 2;
  while ((buf[a] != '\0') && (strchr("0123456789abcdefABCDEF", buf[a]) != NULL))
    a++;
  buf[a] = '\0';
  if (a < 3)
    return -1;
  value = strtoul(buf, &endptr, 0);
  if (*endptr == '\0')
    if (value <= 0xffff)
      return value;
  return -1;
}


x86 assembly (clang, -O0):
<str2int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x33(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x29(%rbp)
movl   $0x2,-0x14(%rbp)
movslq -0x14(%rbp),%rax
movsbl -0x33(%rbp,%rax,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x34(%rbp)
je     118f <str2int+0x5f>
movslq -0x14(%rbp),%rax
lea    0xe85(%rip),%rdi
movsbl -0x33(%rbp,%rax,1),%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x34(%rbp)
mov    -0x34(%rbp),%al
test   $0x1,%al
jne    119f <str2int+0x6f>
jmp    11ad <str2int+0x7d>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1159 <str2int+0x29>
movslq -0x14(%rbp),%rax
movb   $0x0,-0x33(%rbp,%rax,1)
cmpl   $0x3,-0x14(%rbp)
jge    11cc <str2int+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <str2int+0xe5>
lea    -0x33(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    120e <str2int+0xde>
cmpq   $0xffff,-0x20(%rbp)
ja     1209 <str2int+0xd9>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1215 <str2int+0xe5>
jmp    120e <str2int+0xde>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2int>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rsi
lea    0xd(%rsp),%rdi
mov    $0xa,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x17(%rsp)
mov    0xf(%rsp),%al
mov    $0xffffffff,%r14d
test   %al,%al
je     11ca <str2int+0x9a>
mov    $0x3,%ebp
lea    0xe97(%rip),%rbx
nopl   0x0(%rax)
movsbl %al,%esi
mov    %rbx,%rdi
call   1040 <strchr@plt>
test   %rax,%rax
je     118d <str2int+0x5d>
movzbl 0xd(%rsp,%rbp,1),%eax
add    $0x1,%rbp
test   %al,%al
jne    1170 <str2int+0x40>
add    $0xffffffff,%ebp
mov    %ebp,%eax
movb   $0x0,0xd(%rsp,%rax,1)
cmp    $0x3,%ebp
jb     11ca <str2int+0x9a>
lea    0xd(%rsp),%rdi
lea    0x18(%rsp),%rsi
xor    %ebx,%ebx
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    0x18(%rsp),%rcx
cmp    $0x10000,%rax
mov    $0xffffffff,%edx
cmovb  %eax,%edx
cmp    (%rcx),%bl
sbb    %r14d,%r14d
or     %edx,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [23 x i8] c"0123456789abcdefABCDEF\00"
define i32 @str2int(i8* %0) {
%2 = alloca i8*
%3 = alloca [11 x i8]
%4 = bitcast i8** %2 to i8*
%5 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%6 = call i32 @strncpy(i8* %5, i8* %0, i32 10)
%7 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 10
store i8 0, i8* %7
%8 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 2
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 0
br i1 %10, label %39, label %11
11: ; preds = %1, %17
%12 = phi i64 [ %18, %17 ], [ 2, %1 ]
%13 = phi i8 [ %21, %17 ], [ %9, %1 ]
%14 = phi i32 [ %19, %17 ], [ 2, %1 ]
%15 = call i32* @strchr(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8 signext %13)
%16 = icmp eq i32* %15, null
br i1 %16, label %23, label %17
17: ; preds = %11
%18 = add nuw i64 %12, 1
%19 = add nuw nsw i32 %14, 1
%20 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 %18
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %25, label %11
23: ; preds = %11
%24 = trunc i64 %12 to i32
br label %25
25: ; preds = %17, %23
%26 = phi i32 [ %24, %23 ], [ %19, %17 ]
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 %27
store i8 0, i8* %28
%29 = icmp ult i32 %26, 3
br i1 %29, label %39, label %30
30: ; preds = %25
%31 = call i64 @strtoul(i8* %5, i8** %2, i32 0)
%32 = load i8*, i8** %2
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 0
%35 = icmp ult i64 %31, 65536
%36 = select i1 %34, i1 %35, i1 false
%37 = trunc i64 %31 to i32
%38 = select i1 %36, i32 %37, i32 -1
br label %39
39: ; preds = %1, %30, %25
%40 = phi i32 [ -1, %25 ], [ %38, %30 ], [ -1, %1 ]
ret i32 %40
}