#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 atoi(char const *);
int strlen(char const *);
int strtoul(char const *, int /*<<< orphan*/ *, int);
unsigned int to_int(const char *value) {
  const char *vp = value;
  int retval = 0;
  if (vp == NULL) {
    return 0;
  }
  if (strlen(value) >= 3) {
    if (*vp == '0' && *(vp + 1) == 'x') {
      retval = strtoul(vp + 2, NULL, 16);
      return retval;
    }
  }
  retval = atoi(value);
  return retval;
}


x86 assembly (clang, -O0):
<to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1162 <to_int+0x32>
movl   $0x0,-0x4(%rbp)
jmp    11d0 <to_int+0xa0>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x3,%eax
jl     11be <to_int+0x8e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    11b9 <to_int+0x89>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x78,%eax
jne    11b9 <to_int+0x89>
mov    -0x18(%rbp),%rdi
add    $0x2,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1040 <strtoul@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d0 <to_int+0xa0>
jmp    11be <to_int+0x8e>
mov    -0x10(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_int>:
push   %rbx
test   %rdi,%rdi
je     116b <to_int+0x3b>
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmp    $0x3,%eax
jl     1162 <to_int+0x32>
cmpb   $0x30,(%rbx)
jne    1162 <to_int+0x32>
cmpb   $0x78,0x1(%rbx)
jne    1162 <to_int+0x32>
add    $0x2,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
pop    %rbx
jmp    1040 <strtoul@plt>
mov    %rbx,%rdi
pop    %rbx
jmp    1050 <atoi@plt>
xor    %eax,%eax
pop    %rbx
ret

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

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