#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 const *);
long strtol(char const *, int /*<<< orphan*/ *, int);
int parse_serial(const char *str) {
  long val;
  if ((strlen(str) > 2) && (str[0] == '0') &&
      (((str[1] == 'x')) || ((str[1] == 'X')))) {
    val = strtol(str, NULL, 16);
  } else {
    val = strtol(str, NULL, 10);
  }
  return (int)val;
}


x86 assembly (clang, -O0):
<parse_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x2,%eax
jle    118b <parse_serial+0x6b>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    118b <parse_serial+0x6b>
mov    -0x8(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x78,%eax
je     1170 <parse_serial+0x50>
mov    -0x8(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x58,%eax
jne    118b <parse_serial+0x6b>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %rax,-0x10(%rbp)
jmp    11a1 <parse_serial+0x81>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_serial>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmp    $0x3,%eax
jl     1148 <parse_serial+0x28>
cmpb   $0x30,(%rbx)
jne    1148 <parse_serial+0x28>
mov    0x1(%rbx),%al
or     $0x20,%al
cmp    $0x78,%al
jne    1148 <parse_serial+0x28>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
jmp    1152 <parse_serial+0x32>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
pop    %rbx
ret

LLVM IR (-O0):
define i32 @parse_serial(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @strlen(i8* %4)
%6 = icmp sgt i32 %5, 2
br i1 %6, label %7, label %28
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 48
br i1 %12, label %13, label %28
13: ; preds = %7
%14 = load i8*, i8** %2
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 120
br i1 %18, label %25, label %19
19: ; preds = %13
%20 = load i8*, i8** %2
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 88
br i1 %24, label %25, label %28
25: ; preds = %19, %13
%26 = load i8*, i8** %2
%27 = call i64 @strtol(i8* %26, i32* null, i32 16)
store i64 %27, i64* %3
br label %31
28: ; preds = %19, %7, %1
%29 = load i8*, i8** %2
%30 = call i64 @strtol(i8* %29, i32* null, i32 10)
store i64 %30, i64* %3
br label %31
31: ; preds = %28, %25
%32 = load i64, i64* %3
%33 = trunc i64 %32 to i32
ret i32 %33
}

LLVM IR (-O2):
define i32 @parse_serial(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 2
br i1 %3, label %4, label %12
4: ; preds = %1
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 48
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %0, i64 1
%9 = load i8, i8* %8
switch i8 %9, label %12 [
i8 120, label %10
i8 88, label %10
]
10: ; preds = %7, %7
%11 = tail call i64 @strtol(i8* nocapture %0, i32* null, i32 16)
br label %14
12: ; preds = %7, %4, %1
%13 = tail call i64 @strtol(i8* nocapture %0, i32* null, i32 10)
br label %14
14: ; preds = %12, %10
%15 = phi i64 [ %11, %10 ], [ %13, %12 ]
%16 = trunc i64 %15 to i32
ret i32 %16
}