#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__ ERANGE;
unsigned long const LONG_MAX;
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *, char const *, char const *);
unsigned long strtoul(char const *, char **, int);
unsigned long parse_ulong(const char *str, const char *message,
                          const unsigned long max) {
  char *endptr;
  errno = 0;
  const unsigned long value = strtoul(str, &endptr, 10);
  if (str[0] == '\0' || *endptr != '\0')
    errx(3, "%s '%s' (not a number)", message, str);
  else if (errno == ERANGE || value == LONG_MAX || value > max)
    errx(3, "%s '%s' (out of range)", message, str);
  return value;
}


x86 assembly (clang, -O0):
<parse_ulong>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e95(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1178 <parse_ulong+0x58>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1196 <parse_ulong+0x76>
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x3,%edi
lea    0xe74(%rip),%rsi
call   1030 <errx@plt>
jmp    11f0 <parse_ulong+0xd0>
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%rax
je     11d2 <parse_ulong+0xb2>
mov    -0x28(%rbp),%rax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
je     11d2 <parse_ulong+0xb2>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    11eb <parse_ulong+0xcb>
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x3,%edi
lea    0xe31(%rip),%rsi
call   1030 <errx@plt>
jmp    11f0 <parse_ulong+0xd0>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_ulong>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e9b(%rip),%r13
movq   $0x0,0x0(%r13)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    %rax,%r15
lea    0xea2(%rip),%rsi
cmpb   $0x0,(%rbx)
je     116d <parse_ulong+0x4d>
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     118e <parse_ulong+0x6e>
mov    $0x3,%edi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1030 <errx@plt>
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x0(%r13),%rax
mov    0x2e4f(%rip),%rcx
lea    0xe77(%rip),%rsi
cmp    (%rcx),%rax
je     116d <parse_ulong+0x4d>
test   %r15,%r15
je     116d <parse_ulong+0x4d>
cmp    %r12,%r15
jbe    117d <parse_ulong+0x5d>
jmp    116d <parse_ulong+0x4d>

LLVM IR (-O0):
@errno = global i64 0
@.str = [23 x i8] c"%s '%s' (not a number)\00"
@ERANGE = global i64 0
@LONG_MAX = constant i64 0
@.str.1 = [23 x i8] c"%s '%s' (out of range)\00"
define i64 @parse_ulong(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* @errno
%9 = load i8*, i8** %4
%10 = call i64 @strtoul(i8* %9, i8** %7, i32 10)
store i64 %10, i64* %8
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %3
%17 = load i8*, i8** %7
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16, %3
%22 = load i8*, i8** %5
%23 = load i8*, i8** %4
%24 = call i32 @errx(i32 3, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %23)
br label %42
25: ; preds = %16
%26 = load i64, i64* @errno
%27 = load i64, i64* @ERANGE
%28 = icmp eq i64 %26, %27
br i1 %28, label %37, label %29
29: ; preds = %25
%30 = load i64, i64* %8
%31 = load i64, i64* @LONG_MAX
%32 = icmp eq i64 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i64, i64* %8
%35 = load i64, i64* %6
%36 = icmp ugt i64 %34, %35
br i1 %36, label %37, label %41
37: ; preds = %33, %29, %25
%38 = load i8*, i8** %5
%39 = load i8*, i8** %4
%40 = call i32 @errx(i32 3, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %38, i8* %39)
br label %41
41: ; preds = %37, %33
br label %42
42: ; preds = %41, %21
%43 = load i64, i64* %8
ret i64 %43
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [23 x i8] c"%s '%s' (not a number)\00"
@ERANGE = global i64 0
@LONG_MAX = constant i64 0
@.str.1 = [23 x i8] c"%s '%s' (out of range)\00"
define i64 @parse_ulong(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i64 0, i64* @errno
%6 = call i64 @strtoul(i8* %0, i8** %4, i32 10)
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %21, label %9
9: ; preds = %3
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %9
%14 = load i64, i64* @errno
%15 = load i64, i64* @ERANGE
%16 = icmp eq i64 %14, %15
%17 = icmp eq i64 %6, 0
%18 = select i1 %16, i1 true, i1 %17
%19 = icmp ugt i64 %6, %2
%20 = select i1 %18, i1 true, i1 %19
br i1 %20, label %21, label %24
21: ; preds = %13, %3, %9
%22 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), %9 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), %13 ]
%23 = tail call i32 @errx(i32 3, i8* %22, i8* %1, i8* %0)
br label %24
24: ; preds = %21, %13
ret i64 %6
}