#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 int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ u_int32_t;

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ strtoul(char *, char **, int /*<<< orphan*/);
int str2u32(char *arg, u_int32_t *val) {
  char *err = NULL;
  uint32_t t;
  errno = 0;
  t = strtoul(arg, &err, 0);
  if (errno || (err == arg) || ((err != NULL) && *err)) {
    return -1;
  }
  *val = t;
  return 0;
}


x86 assembly (clang, -O0):
<str2u32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2eb1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e91(%rip),%rax
cmpq   $0x0,(%rax)
jne    1182 <str2u32+0x72>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1182 <str2u32+0x72>
cmpq   $0x0,-0x20(%rbp)
je     118e <str2u32+0x7e>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     118e <str2u32+0x7e>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <str2u32+0x8e>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2u32>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
mov    0x2eb1(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmpq   $0x0,(%r15)
je     1159 <str2u32+0x49>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x8(%rsp),%rdx
cmp    %rbx,%rdx
je     114f <str2u32+0x3f>
test   %rdx,%rdx
je     116d <str2u32+0x5d>
cmpb   $0x0,(%rdx)
jne    114f <str2u32+0x3f>
mov    %ecx,(%r14)
xor    %eax,%eax
jmp    114f <str2u32+0x3f>

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

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