#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 */
typedef scalar_t__ uint32_t;

/* Variables and functions */
int EINVAL;
scalar_t__ ERANGE;
unsigned long ULONG_MAX;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
unsigned long strtoul(char *, char **, int);
int sb_token_to_long(char *tok, uint32_t *rid) {
  char *endptr;
  unsigned long id;
  if (tok[0] != '0' || tok[1] != 'x') {
    fprintf(stderr, "ERR: Invalid hexadecimal number!\n");
    return -EINVAL;
  }
  tok += 2;
  errno = 0;
  id = strtoul(tok, &endptr, 16);
  if ((errno == ERANGE && id == ULONG_MAX) || (errno != 0 && id == 0)) {
    fprintf(stderr, "ERR: Value can't be decoded!\n");
    return -EINVAL;
  }
  if (id > 0xffffffff) {
    fprintf(stderr, "ERR: Value too big!\n");
    return -EINVAL;
  }
  if (endptr == tok) {
    fprintf(stderr, "ERR: Deformed value!\n");
    return -EINVAL;
  }
  *rid = (uint32_t)id;
  return 0;
}


x86 assembly (clang, -O0):
<sb_token_to_long>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    1151 <sb_token_to_long+0x31>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x78,%eax
je     1179 <sb_token_to_long+0x59>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
lea    0xe9f(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
mov    0x2e59(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    129a <sb_token_to_long+0x17a>
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
mov    0x2e44(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1040 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%rax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%rax
jne    11d7 <sb_token_to_long+0xb7>
mov    -0x28(%rbp),%rax
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%rax
je     11f3 <sb_token_to_long+0xd3>
mov    0x2df2(%rip),%rax
cmpq   $0x0,(%rax)
je     121b <sb_token_to_long+0xfb>
cmpq   $0x0,-0x28(%rbp)
jne    121b <sb_token_to_long+0xfb>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
mov    0x2db7(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    129a <sb_token_to_long+0x17a>
mov    $0xffffffff,%eax
cmp    %rax,-0x28(%rbp)
jbe    1252 <sb_token_to_long+0x132>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
mov    0x2d80(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    129a <sb_token_to_long+0x17a>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1288 <sb_token_to_long+0x168>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
mov    0x2d4a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    129a <sb_token_to_long+0x17a>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sb_token_to_long>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmpb   $0x30,(%rdi)
jne    1195 <sb_token_to_long+0x75>
mov    %rdi,%rbx
cmpb   $0x78,0x1(%rdi)
jne    1195 <sb_token_to_long+0x75>
mov    %rsi,%r14
add    $0x2,%rbx
mov    0x2e8b(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x10,%edx
call   1040 <strtoul@plt>
mov    (%r15),%rcx
mov    0x2e80(%rip),%rdx
cmp    (%rdx),%rcx
jne    1179 <sb_token_to_long+0x59>
mov    0x2e44(%rip),%rdx
cmp    (%rdx),%rax
je     1183 <sb_token_to_long+0x63>
test   %rcx,%rcx
je     11bf <sb_token_to_long+0x9f>
test   %rax,%rax
jne    11bf <sb_token_to_long+0x9f>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe8f(%rip),%rsi
jmp    11a5 <sb_token_to_long+0x85>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e17(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rax,%rcx
shr    $0x20,%rcx
je     11da <sb_token_to_long+0xba>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
jmp    11a5 <sb_token_to_long+0x85>
cmp    %rbx,0x8(%rsp)
je     11e8 <sb_token_to_long+0xc8>
mov    %rax,(%r14)
xor    %eax,%eax
jmp    11b5 <sb_token_to_long+0x95>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
jmp    11a5 <sb_token_to_long+0x85>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [34 x i8] c"ERR: Invalid hexadecimal number!\0A\00"
@EINVAL = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@ULONG_MAX = global i64 0
@.str.1 = [30 x i8] c"ERR: Value can't be decoded!\0A\00"
@.str.2 = [21 x i8] c"ERR: Value too big!\0A\00"
@.str.3 = [22 x i8] c"ERR: Deformed value!\0A\00"
define i32 @sb_token_to_long(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
%8 = load i8*, i8** %4
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 48
br i1 %12, label %19, label %13
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 120
br i1 %18, label %19, label %24
19: ; preds = %13, %2
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EINVAL
%23 = sub nsw i32 0, %22
store i32 %23, i32* %3
br label %67
24: ; preds = %13
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i64 2
store i8* %26, i8** %4
store i64 0, i64* @errno
%27 = load i8*, i8** %4
%28 = call i64 @strtoul(i8* %27, i8** %6, i32 16)
store i64 %28, i64* %7
%29 = load i64, i64* @errno
%30 = load i64, i64* @ERANGE
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %24
%33 = load i64, i64* %7
%34 = load i64, i64* @ULONG_MAX
%35 = icmp eq i64 %33, %34
br i1 %35, label %42, label %36
36: ; preds = %32, %24
%37 = load i64, i64* @errno
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = load i64, i64* %7
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %39, %32
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%45 = load i32, i32* @EINVAL
%46 = sub nsw i32 0, %45
store i32 %46, i32* %3
br label %67
47: ; preds = %39, %36
%48 = load i64, i64* %7
%49 = icmp ugt i64 %48, 4294967295
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%53 = load i32, i32* @EINVAL
%54 = sub nsw i32 0, %53
store i32 %54, i32* %3
br label %67
55: ; preds = %47
%56 = load i8*, i8** %6
%57 = load i8*, i8** %4
%58 = icmp eq i8* %56, %57
br i1 %58, label %59, label %64
59: ; preds = %55
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%62 = load i32, i32* @EINVAL
%63 = sub nsw i32 0, %62
store i32 %63, i32* %3
br label %67
64: ; preds = %55
%65 = load i64, i64* %7
%66 = load i64*, i64** %5
store i64 %65, i64* %66
store i32 0, i32* %3
br label %67
67: ; preds = %64, %59, %50, %42, %19
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [34 x i8] c"ERR: Invalid hexadecimal number!\0A\00"
@EINVAL = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@ULONG_MAX = global i64 0
@.str.1 = [30 x i8] c"ERR: Value can't be decoded!\0A\00"
@.str.2 = [21 x i8] c"ERR: Value too big!\0A\00"
@.str.3 = [22 x i8] c"ERR: Deformed value!\0A\00"
define i32 @sb_token_to_long(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 48
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = getelementptr inbounds i8, i8* %0, i64 1
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 120
br i1 %10, label %16, label %11
11: ; preds = %7, %2
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
br label %50
16: ; preds = %7
%17 = getelementptr inbounds i8, i8* %0, i64 2
store i64 0, i64* @errno
%18 = call i64 @strtoul(i8* %17, i8** %3, i32 16)
%19 = load i64, i64* @errno
%20 = load i64, i64* @ERANGE
%21 = icmp eq i64 %19, %20
%22 = load i64, i64* @ULONG_MAX
%23 = icmp eq i64 %18, %22
%24 = select i1 %21, i1 %23, i1 false
br i1 %24, label %29, label %25
25: ; preds = %16
%26 = icmp ne i64 %19, 0
%27 = icmp eq i64 %18, 0
%28 = select i1 %26, i1 %27, i1 false
br i1 %28, label %29, label %34
29: ; preds = %16, %25
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @EINVAL
%33 = sub nsw i32 0, %32
br label %50
34: ; preds = %25
%35 = icmp ugt i64 %18, 4294967295
br i1 %35, label %36, label %41
36: ; preds = %34
%37 = load i32, i32* @stderr
%38 = tail call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* @EINVAL
%40 = sub nsw i32 0, %39
br label %50
41: ; preds = %34
%42 = load i8*, i8** %3
%43 = icmp eq i8* %42, %17
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = tail call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* @EINVAL
%48 = sub nsw i32 0, %47
br label %50
49: ; preds = %41
store i64 %18, i64* %1
br label %50
50: ; preds = %49, %44, %36, %29, %11
%51 = phi i32 [ %15, %11 ], [ %33, %29 ], [ %40, %36 ], [ %48, %44 ], [ 0, %49 ]
ret i32 %51
}