#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 char wchar_t;

/* Variables and functions */
char uppercase(char);
int wcstol(char *, char **, int);
int x2dec(char *, int);
int extract_entity_number(wchar_t *s) {
  int number;
  wchar_t *tmp = s;
  if ((s[0] == '&') && (s[1] == '#')) {
    if (uppercase(s[2]) == 'X') {
      tmp += 3;
      number = x2dec(tmp, 16);
    } else {
      tmp += 2;
      number = wcstol(tmp, (wchar_t **)NULL, 10);
    }
    return (number);
  } else {
    return (-1);
  }
}


x86 assembly (clang, -O0):
<extract_entity_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x26,%eax
jne    11cc <extract_entity_number+0x9c>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x23,%eax
jne    11cc <extract_entity_number+0x9c>
mov    -0x10(%rbp),%rax
movsbl 0x2(%rax),%edi
call   1050 <uppercase@plt>
movsbl %al,%eax
cmp    $0x58,%eax
jne    11a0 <extract_entity_number+0x70>
mov    -0x20(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x10,%esi
call   1040 <x2dec@plt>
mov    %eax,-0x14(%rbp)
jmp    11c1 <extract_entity_number+0x91>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1030 <wcstol@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d3 <extract_entity_number+0xa3>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_entity_number>:
push   %rbx
cmpb   $0x26,(%rdi)
jne    115e <extract_entity_number+0x2e>
mov    %rdi,%rbx
cmpb   $0x23,0x1(%rdi)
jne    115e <extract_entity_number+0x2e>
movsbl 0x2(%rbx),%edi
call   1050 <uppercase@plt>
cmp    $0x58,%al
jne    1165 <extract_entity_number+0x35>
add    $0x3,%rbx
mov    %rbx,%rdi
mov    $0x10,%esi
pop    %rbx
jmp    1040 <x2dec@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret
add    $0x2,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
pop    %rbx
jmp    1030 <wcstol@plt>

LLVM IR (-O0):
define i32 @extract_entity_number(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
store i8* %6, i8** %5
%7 = load i8*, i8** %3
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 38
br i1 %11, label %12, label %37
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 35
br i1 %17, label %18, label %37
18: ; preds = %12
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i64 2
%21 = load i8, i8* %20
%22 = call signext i8 @uppercase(i8 signext %21)
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 88
br i1 %24, label %25, label %30
25: ; preds = %18
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i64 3
store i8* %27, i8** %5
%28 = load i8*, i8** %5
%29 = call i32 @x2dec(i8* %28, i32 16)
store i32 %29, i32* %4
br label %35
30: ; preds = %18
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i64 2
store i8* %32, i8** %5
%33 = load i8*, i8** %5
%34 = call i32 @wcstol(i8* %33, i8** null, i32 10)
store i32 %34, i32* %4
br label %35
35: ; preds = %30, %25
%36 = load i32, i32* %4
store i32 %36, i32* %2
br label %38
37: ; preds = %12, %1
store i32 -1, i32* %2
br label %38
38: ; preds = %37, %35
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
define i32 @extract_entity_number(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 38
br i1 %3, label %4, label %18
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 35
br i1 %7, label %8, label %18
8: ; preds = %4
%9 = getelementptr inbounds i8, i8* %0, i64 2
%10 = load i8, i8* %9
%11 = tail call signext i8 @uppercase(i8 signext %10)
%12 = icmp eq i8 %11, 88
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = getelementptr inbounds i8, i8* %0, i64 3
%15 = tail call i32 @x2dec(i8* %14, i32 16)
br label %18
16: ; preds = %8
%17 = tail call i32 @wcstol(i8* %9, i8** null, i32 10)
br label %18
18: ; preds = %1, %4, %13, %16
%19 = phi i32 [ %15, %13 ], [ %17, %16 ], [ -1, %4 ], [ -1, %1 ]
ret i32 %19
}