#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 scalar_t__ u_long;

/* Variables and functions */
scalar_t__ ERANGE;
scalar_t__ ULONG_MAX;
scalar_t__ errno;
scalar_t__ strtoul(char *, char **, int /*<<< orphan*/);
int atoul(char *s, u_long *ulvalp) {
  u_long ulval;
  char *ep;
  errno = 0;
  ulval = strtoul(s, &ep, 0);
  if (s[0] == '\0' || *ep != '\0')
    return (-1);
  if (errno == ERANGE && ulval == ULONG_MAX)
    return (-1);
  *ulvalp = ulval;
  return (0);
}


x86 assembly (clang, -O0):
<atoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1161 <atoul+0x51>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     116d <atoul+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <atoul+0xa9>
mov    0x2e64(%rip),%rax
mov    (%rax),%rax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%rax
jne    11a7 <atoul+0x97>
mov    -0x20(%rbp),%rax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%rax
jne    11a7 <atoul+0x97>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <atoul+0xa9>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<atoul>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eb2(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmpb   $0x0,(%rbx)
je     1170 <atoul+0x60>
mov    0x8(%rsp),%rdx
cmpb   $0x0,(%rdx)
jne    1170 <atoul+0x60>
mov    (%r15),%rdx
mov    0x2e8e(%rip),%rsi
cmp    (%rsi),%rdx
jne    116b <atoul+0x5b>
mov    0x2e62(%rip),%rdx
cmp    (%rdx),%rcx
je     1170 <atoul+0x60>
mov    %rcx,(%r14)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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