#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__ EINVAL;
scalar_t__ ERANGE;
unsigned int ULONG_MAX;
scalar_t__ errno;
unsigned int strtoul(char const *, int /*<<< orphan*/ *, int);
int m_str_to_uint(const char *str, unsigned int *val) {
  errno = 0;
  *val = strtoul(str, NULL, 10);
  if ((*val == 0 && errno == EINVAL) ||
      (*val == ULONG_MAX && errno == ERANGE)) {
    return -1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<m_str_to_uint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1030 <strtoul@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    116f <m_str_to_uint+0x5f>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%rax
je     119e <m_str_to_uint+0x8e>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%eax
jne    11aa <m_str_to_uint+0x9a>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
jne    11aa <m_str_to_uint+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <m_str_to_uint+0xa1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<m_str_to_uint>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2eba(%rip),%r14
movq   $0x0,(%r14)
xor    %esi,%esi
mov    $0xa,%edx
call   1030 <strtoul@plt>
mov    %eax,(%rbx)
test   %eax,%eax
jne    114b <m_str_to_uint+0x3b>
mov    (%r14),%rdx
mov    0x2e8f(%rip),%rsi
mov    $0xffffffff,%ecx
cmp    (%rsi),%rdx
je     116c <m_str_to_uint+0x5c>
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%eax
jne    116a <m_str_to_uint+0x5a>
mov    (%r14),%rax
mov    0x2e88(%rip),%rdx
mov    $0xffffffff,%ecx
cmp    (%rdx),%rax
je     116c <m_str_to_uint+0x5c>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i64 0
@EINVAL = global i64 0
@ULONG_MAX = global i32 0
@ERANGE = global i64 0
define i32 @m_str_to_uint(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i64 0, i64* @errno
%6 = load i8*, i8** %4
%7 = call i32 @strtoul(i8* %6, i32* null, i32 10)
%8 = load i32*, i32** %5
store i32 %7, i32* %8
%9 = load i32*, i32** %5
%10 = load i32, i32* %9
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINVAL
%15 = icmp eq i64 %13, %14
br i1 %15, label %25, label %16
16: ; preds = %12, %2
%17 = load i32*, i32** %5
%18 = load i32, i32* %17
%19 = load i32, i32* @ULONG_MAX
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i64, i64* @errno
%23 = load i64, i64* @ERANGE
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21, %12
store i32 -1, i32* %3
br label %27
26: ; preds = %21, %16
store i32 0, i32* %3
br label %27
27: ; preds = %26, %25
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@errno = global i64 0
@EINVAL = global i64 0
@ULONG_MAX = global i32 0
@ERANGE = global i64 0
define i32 @m_str_to_uint(i8* nocapture %0, i32* nocapture %1) {
store i64 0, i64* @errno
%3 = tail call i32 @strtoul(i8* nocapture %0, i32* null, i32 10)
store i32 %3, i32* %1
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i64, i64* @errno
%7 = load i64, i64* @EINVAL
%8 = icmp eq i64 %6, %7
br i1 %8, label %17, label %9
9: ; preds = %5, %2
%10 = load i32, i32* @ULONG_MAX
%11 = icmp eq i32 %3, %10
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @ERANGE
%15 = icmp eq i64 %13, %14
br i1 %15, label %17, label %16
16: ; preds = %12, %9
br label %17
17: ; preds = %5, %12, %16
%18 = phi i32 [ 0, %16 ], [ -1, %12 ], [ -1, %5 ]
ret i32 %18
}