#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 */
scalar_t__ lib_memcmp(char *, char *, int);
int lib_strlen(char *);
scalar_t__ lib_xtoi(char *);
scalar_t__ lib_xtoq(char *);
long getaddr(char *str) {
  int longaddr = 0;
  long newaddr;
  longaddr = lib_strlen(str);
  if (lib_memcmp(str, "0x", 2) == 0)
    longaddr -= 2;
  longaddr = (longaddr > 8) ? 1 : 0;
  if (longaddr)
    newaddr = (long)lib_xtoq(str);
  else
    newaddr = (long)lib_xtoi(str);
  return newaddr;
}


x86 assembly (clang, -O0):
<getaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <lib_strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe96(%rip),%rsi
mov    $0x2,%edx
call   1040 <lib_memcmp@plt>
cmp    $0x0,%rax
jne    1187 <getaddr+0x47>
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x8,%edx
cmovg  %ecx,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11b6 <getaddr+0x76>
mov    -0x8(%rbp),%rdi
call   1030 <lib_xtoq@plt>
mov    %rax,-0x18(%rbp)
jmp    11c3 <getaddr+0x83>
mov    -0x8(%rbp),%rdi
call   1050 <lib_xtoi@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getaddr>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1060 <lib_strlen@plt>
mov    %eax,%r14d
lea    0xeaa(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1040 <lib_memcmp@plt>
lea    -0x2(%r14),%ecx
test   %rax,%rax
cmovne %r14d,%ecx
mov    %rbx,%rdi
add    $0x8,%rsp
cmp    $0x9,%ecx
jl     1182 <getaddr+0x42>
pop    %rbx
pop    %r14
jmp    1030 <lib_xtoq@plt>
pop    %rbx
pop    %r14
jmp    1050 <lib_xtoi@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"0x\00"
define i64 @getaddr(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
store i8* %0, i8** %2
store i32 0, i32* %3
%5 = load i8*, i8** %2
%6 = call i32 @lib_strlen(i8* %5)
store i32 %6, i32* %3
%7 = load i8*, i8** %2
%8 = call i64 @lib_memcmp(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* %3
%12 = sub nsw i32 %11, 2
store i32 %12, i32* %3
br label %13
13: ; preds = %10, %1
%14 = load i32, i32* %3
%15 = icmp sgt i32 %14, 8
%16 = zext i1 %15 to i64
%17 = select i1 %15, i32 1, i32 0
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i8*, i8** %2
%22 = call i64 @lib_xtoq(i8* %21)
store i64 %22, i64* %4
br label %26
23: ; preds = %13
%24 = load i8*, i8** %2
%25 = call i64 @lib_xtoi(i8* %24)
store i64 %25, i64* %4
br label %26
26: ; preds = %23, %20
%27 = load i64, i64* %4
ret i64 %27
}

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
define i64 @getaddr(i8* %0) {
%2 = tail call i32 @lib_strlen(i8* %0)
%3 = tail call i64 @lib_memcmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%4 = icmp eq i64 %3, 0
%5 = add nsw i32 %2, -2
%6 = select i1 %4, i32 %5, i32 %2
%7 = icmp sgt i32 %6, 8
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i64 @lib_xtoq(i8* %0)
br label %12
10: ; preds = %1
%11 = tail call i64 @lib_xtoi(i8* %0)
br label %12
12: ; preds = %10, %8
%13 = phi i64 [ %9, %8 ], [ %11, %10 ]
ret i64 %13
}