#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 /*<<< orphan*/ strncmp(char *, char *, int);
unsigned long strtoul(char *, char **, int);
int eg_enc_strtoul(unsigned long *result, char *str) {
  char *endptr;
  unsigned long num;
  if (!strncmp(str, "0x", 2)) {
    num = strtoul(str, &endptr, 16);
  } else {
    num = strtoul(str, &endptr, 10);
  }
  if (*endptr != '\0') {
    return -1;
  }
  *result = num;
  return 0;
}


x86 assembly (clang, -O0):
<eg_enc_strtoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xec5(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1169 <eg_enc_strtoul+0x49>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1040 <strtoul@plt>
mov    %rax,-0x28(%rbp)
jmp    117f <eg_enc_strtoul+0x5f>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     119b <eg_enc_strtoul+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ad <eg_enc_strtoul+0x8d>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eg_enc_strtoul>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xecf(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
mov    $0x10,%eax
mov    $0xa,%edx
cmove  %eax,%edx
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <strtoul@plt>
mov    (%rsp),%rdx
mov    $0xffffffff,%ecx
cmpb   $0x0,(%rdx)
jne    116b <eg_enc_strtoul+0x4b>
mov    %rax,(%r14)
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
define i32 @eg_enc_strtoul(i64* nocapture writeonly %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i32 @strncmp(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%6 = icmp eq i32 %5, 0
%7 = select i1 %6, i32 16, i32 10
%8 = call i64 @strtoul(i8* %1, i8** %3, i32 %7)
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i64 %8, i64* %0
br label %13
13: ; preds = %2, %12
%14 = phi i32 [ 0, %12 ], [ -1, %2 ]
ret i32 %14
}