#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__ LONG_MAX;
scalar_t__ UINT_MAX;
long strtol(char const *, char **, int /*<<< orphan*/);
unsigned strtounum(const char *str) {
  unsigned ret = 0;
  long val;
  char *ep = NULL;
  val = strtol(str, &ep, 0);
  if (val <= 0 ||
      val > (long)(((long long)UINT_MAX) < ((long long)LONG_MAX)
                       ? ((long long)UINT_MAX)
                       : ((long long)LONG_MAX)) ||
      ep == NULL || *ep != '\0')
    goto fail;
  ret = val;
fail:
  return ret;
}


x86 assembly (clang, -O0):
<strtounum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtol@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    11b8 <strtounum+0xa8>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e90(%rip),%rax
mov    (%rax),%rax
mov    0x2e76(%rip),%rcx
cmp    (%rcx),%rax
jge    117e <strtounum+0x6e>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    118c <strtounum+0x7c>
mov    0x2e53(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
cmp    %rcx,%rax
jg     11b8 <strtounum+0xa8>
cmpq   $0x0,-0x20(%rbp)
je     11b8 <strtounum+0xa8>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11bd <strtounum+0xad>
jmp    11c4 <strtounum+0xb4>
mov    -0x18(%rbp),%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtounum>:
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
xor    %ebx,%ebx
xor    %edx,%edx
call   1030 <strtol@plt>
test   %rax,%rax
jle    1165 <strtounum+0x55>
mov    0x2eb0(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e96(%rip),%rdx
mov    (%rdx),%rdx
cmp    %rdx,%rcx
cmovl  %rcx,%rdx
cmp    %rdx,%rax
jg     1165 <strtounum+0x55>
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
je     1165 <strtounum+0x55>
xor    %edx,%edx
cmpb   $0x0,(%rcx)
cmovne %edx,%eax
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@UINT_MAX = global i64 0
@LONG_MAX = global i64 0
define i32 @strtounum(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
store i8* null, i8** %5
%6 = load i8*, i8** %2
%7 = call i64 @strtol(i8* %6, i8** %5, i32 0)
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp sle i64 %8, 0
br i1 %9, label %30, label %10
10: ; preds = %1
%11 = load i64, i64* %4
%12 = load i64, i64* @UINT_MAX
%13 = load i64, i64* @LONG_MAX
%14 = icmp slt i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = load i64, i64* @UINT_MAX
br label %19
17: ; preds = %10
%18 = load i64, i64* @LONG_MAX
br label %19
19: ; preds = %17, %15
%20 = phi i64 [ %16, %15 ], [ %18, %17 ]
%21 = icmp sgt i64 %11, %20
br i1 %21, label %30, label %22
22: ; preds = %19
%23 = load i8*, i8** %5
%24 = icmp eq i8* %23, null
br i1 %24, label %30, label %25
25: ; preds = %22
%26 = load i8*, i8** %5
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25, %22, %19, %1
br label %34
31: ; preds = %25
%32 = load i64, i64* %4
%33 = trunc i64 %32 to i32
store i32 %33, i32* %3
br label %34
34: ; preds = %31, %30
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@UINT_MAX = global i64 0
@LONG_MAX = global i64 0
define i32 @strtounum(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%4 = call i64 @strtol(i8* %0, i8** %2, i32 0)
%5 = icmp slt i64 %4, 1
br i1 %5, label %20, label %6
6: ; preds = %1
%7 = load i64, i64* @UINT_MAX
%8 = load i64, i64* @LONG_MAX
%9 = icmp slt i64 %7, %8
%10 = select i1 %9, i64 %7, i64 %8
%11 = icmp sgt i64 %4, %10
%12 = load i8*, i8** %2
%13 = icmp eq i8* %12, null
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %20, label %15
15: ; preds = %6
%16 = load i8, i8* %12
%17 = icmp eq i8 %16, 0
%18 = trunc i64 %4 to i32
%19 = select i1 %17, i32 %18, i32 0
br label %20
20: ; preds = %15, %1, %6
%21 = phi i32 [ 0, %1 ], [ 0, %6 ], [ %19, %15 ]
ret i32 %21
}