#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 void *uint64_t;
typedef scalar_t__ uint32_t;

/* Variables and functions */
void *strtol(char const *, char **, int);
scalar_t__ strtoll(char const *, char **, int);
int strto32(const char *str, uint32_t *result) {
  const char *parse = str;
  int base = 10;
  if (str[0] == '0' && str[1] == 'x') {
    base = 16;
    parse += 2;
  } else if (*str == '0') {
    base = 8;
    parse += 1;
  }
  uint32_t ret;
  char *endptr = NULL;
  if (sizeof(int) == sizeof(uint32_t)) {
    if (sizeof(long) > sizeof(uint32_t)) {
      uint64_t ret_long;
      ret_long = strtol(parse, &endptr, base);
      ret = (uint32_t)ret_long;
    } else
      ret = (uint32_t)strtol(parse, &endptr, base);
  }
  if (sizeof(long) == sizeof(uint32_t)) {
    ret = (uint32_t)strtol(parse, &endptr, base);
  } else if (sizeof(long long) == sizeof(uint32_t)) {
    ret = (uint32_t)strtoll(parse, &endptr, base);
  } else {
    long ret_long;
    ret_long = strtol(parse, &endptr, base);
    ret = (uint32_t)ret_long;
  }
  if (str == endptr || endptr == NULL)
    return -1;
  int len = endptr - str;
  if (len < 0 || len > 128)
    return -1;
  *result = ret;
  return len;
}


x86 assembly (clang, -O0):
<strto32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0xa,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    1168 <strto32+0x58>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x78,%eax
jne    1168 <strto32+0x58>
movl   $0x10,-0x24(%rbp)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    1190 <strto32+0x80>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    118b <strto32+0x7b>
movl   $0x8,-0x24(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1190 <strto32+0x80>
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    -0x38(%rbp),%rsi
call   1030 <strtol@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     11c5 <strto32+0xb5>
cmpq   $0x0,-0x38(%rbp)
jne    11d1 <strto32+0xc1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <strto32+0x103>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jl     11f6 <strto32+0xe6>
cmpl   $0x80,-0x3c(%rbp)
jle    1202 <strto32+0xf2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <strto32+0x103>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strto32>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%al
mov    $0xa,%edx
cmp    $0x30,%al
jne    113c <strto32+0x2c>
mov    $0x8,%edx
cmpb   $0x78,0x1(%rbx)
jne    113c <strto32+0x2c>
mov    $0x10,%edx
mov    $0x2,%edi
jmp    1144 <strto32+0x34>
xor    %edi,%edi
cmp    $0x30,%al
sete   %dil
add    %rbx,%rdi
movq   $0x0,(%rsp)
mov    %rsp,%rsi
call   1030 <strtol@plt>
mov    %rax,%rcx
mov    (%rsp),%rdx
mov    $0xffffffff,%eax
cmp    %rbx,%rdx
je     117c <strto32+0x6c>
test   %rdx,%rdx
je     117c <strto32+0x6c>
sub    %ebx,%edx
cmp    $0x80,%edx
ja     117c <strto32+0x6c>
mov    %rcx,(%r14)
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @strto32(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
%11 = load i8*, i8** %4
store i8* %11, i8** %6
store i32 10, i32* %7
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 48
br i1 %16, label %17, label %26
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 120
br i1 %22, label %23, label %26
23: ; preds = %17
store i32 16, i32* %7
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 2
store i8* %25, i8** %6
br label %35
26: ; preds = %17, %2
%27 = load i8*, i8** %4
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 48
br i1 %30, label %31, label %34
31: ; preds = %26
store i32 8, i32* %7
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i64 1
store i8* %33, i8** %6
br label %34
34: ; preds = %31, %26
br label %35
35: ; preds = %34, %23
store i8* null, i8** %9
%36 = load i8*, i8** %6
%37 = load i32, i32* %7
%38 = call i8* @strtol(i8* %36, i8** %9, i32 %37)
%39 = ptrtoint i8* %38 to i64
store i64 %39, i64* %8
%40 = load i8*, i8** %4
%41 = load i8*, i8** %9
%42 = icmp eq i8* %40, %41
br i1 %42, label %46, label %43
43: ; preds = %35
%44 = load i8*, i8** %9
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %47
46: ; preds = %43, %35
store i32 -1, i32* %3
br label %64
47: ; preds = %43
%48 = load i8*, i8** %9
%49 = load i8*, i8** %4
%50 = ptrtoint i8* %48 to i64
%51 = ptrtoint i8* %49 to i64
%52 = sub i64 %50, %51
%53 = trunc i64 %52 to i32
store i32 %53, i32* %10
%54 = load i32, i32* %10
%55 = icmp slt i32 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %47
%57 = load i32, i32* %10
%58 = icmp sgt i32 %57, 128
br i1 %58, label %59, label %60
59: ; preds = %56, %47
store i32 -1, i32* %3
br label %64
60: ; preds = %56
%61 = load i64, i64* %8
%62 = load i64*, i64** %5
store i64 %61, i64* %62
%63 = load i32, i32* %10
store i32 %63, i32* %3
br label %64
64: ; preds = %60, %59, %46
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
define i32 @strto32(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca i8*
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 48
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = getelementptr inbounds i8, i8* %0, i64 1
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 120
br i1 %9, label %13, label %10
10: ; preds = %6, %2
%11 = phi i32 [ 8, %6 ], [ 10, %2 ]
%12 = zext i1 %5 to i64
br label %13
13: ; preds = %6, %10
%14 = phi i64 [ %12, %10 ], [ 2, %6 ]
%15 = phi i32 [ %11, %10 ], [ 16, %6 ]
%16 = getelementptr i8, i8* %0, i64 %14
%17 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%18 = call i8* @strtol(i8* %16, i8** %3, i32 %15)
%19 = ptrtoint i8* %18 to i64
%20 = load i8*, i8** %3
%21 = icmp eq i8* %20, %0
%22 = icmp eq i8* %20, null
%23 = or i1 %21, %22
br i1 %23, label %31, label %24
24: ; preds = %13
%25 = ptrtoint i8* %20 to i64
%26 = ptrtoint i8* %0 to i64
%27 = sub i64 %25, %26
%28 = trunc i64 %27 to i32
%29 = icmp ugt i32 %28, 128
br i1 %29, label %31, label %30
30: ; preds = %24
store i64 %19, i64* %1
br label %31
31: ; preds = %30, %24, %13
%32 = phi i32 [ -1, %13 ], [ %28, %30 ], [ -1, %24 ]
ret i32 %32
}