#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 */
int EINVAL;
int ERANGE;
unsigned long LONG_MAX;
unsigned long UINT_MAX;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
unsigned long strtoul(char *, char **, int);
int parse_itemcount(char **args, unsigned int *count) {
  int rc = 0;
  unsigned long n;
  char *ptr = NULL;
  n = strtoul(args[0], &ptr, 10);
  if (ptr && *ptr != '\0') {
    fprintf(stderr, "Invalid error code string: %s", args[0]);
    rc = -EINVAL;
  } else if ((n == LONG_MAX) || (n > UINT_MAX)) {
    fprintf(stderr, "Item count %lu out of range", n);
    rc = -ERANGE;
  } else {
    *count = n;
  }
  return rc;
}


x86 assembly (clang, -O0):
<parse_itemcount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11a4 <parse_itemcount+0x84>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11a4 <parse_itemcount+0x84>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
lea    0xe76(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
xor    %eax,%eax
mov    0x2e26(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
jmp    120b <parse_itemcount+0xeb>
mov    -0x20(%rbp),%rax
mov    0x2e19(%rip),%rcx
cmp    (%rcx),%rax
je     11cc <parse_itemcount+0xac>
mov    -0x20(%rbp),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
jbe    11fa <parse_itemcount+0xda>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
xor    %eax,%eax
mov    0x2df0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
jmp    1206 <parse_itemcount+0xe6>
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    120b <parse_itemcount+0xeb>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_itemcount>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,(%rsp)
mov    (%rdi),%rdi
mov    %rsp,%rsi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    (%rsp),%rcx
test   %rcx,%rcx
je     1175 <parse_itemcount+0x55>
cmpb   $0x0,(%rcx)
je     1175 <parse_itemcount+0x55>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe9d(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2e4d(%rip),%rax
jmp    11b7 <parse_itemcount+0x97>
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
je     1194 <parse_itemcount+0x74>
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%rax
ja     1194 <parse_itemcount+0x74>
mov    %eax,(%r14)
xor    %ebx,%ebx
jmp    11b9 <parse_itemcount+0x99>
mov    0x2e35(%rip),%rcx
mov    (%rcx),%edi
lea    0xe7a(%rip),%rsi
xor    %ebx,%ebx
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2e29(%rip),%rax
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"Invalid error code string: %s\00"
@EINVAL = global i32 0
@LONG_MAX = global i64 0
@UINT_MAX = global i64 0
@.str.1 = [28 x i8] c"Item count %lu out of range\00"
@ERANGE = global i32 0
define i32 @parse_itemcount(i8** %0, i32* %1) {
%3 = alloca i8**
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
store i8** %0, i8*** %3
store i32* %1, i32** %4
store i32 0, i32* %5
store i8* null, i8** %7
%8 = load i8**, i8*** %3
%9 = getelementptr inbounds i8*, i8** %8, i64 0
%10 = load i8*, i8** %9
%11 = call i64 @strtoul(i8* %10, i8** %7, i32 10)
store i64 %11, i64* %6
%12 = load i8*, i8** %7
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %27
14: ; preds = %2
%15 = load i8*, i8** %7
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %3
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EINVAL
%26 = sub nsw i32 0, %25
store i32 %26, i32* %5
br label %46
27: ; preds = %14, %2
%28 = load i64, i64* %6
%29 = load i64, i64* @LONG_MAX
%30 = icmp eq i64 %28, %29
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = load i64, i64* %6
%33 = load i64, i64* @UINT_MAX
%34 = icmp ugt i64 %32, %33
br i1 %34, label %35, label %41
35: ; preds = %31, %27
%36 = load i32, i32* @stderr
%37 = load i64, i64* %6
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %37)
%39 = load i32, i32* @ERANGE
%40 = sub nsw i32 0, %39
store i32 %40, i32* %5
br label %45
41: ; preds = %31
%42 = load i64, i64* %6
%43 = trunc i64 %42 to i32
%44 = load i32*, i32** %4
store i32 %43, i32* %44
br label %45
45: ; preds = %41, %35
br label %46
46: ; preds = %45, %19
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"Invalid error code string: %s\00"
@EINVAL = global i32 0
@LONG_MAX = global i64 0
@UINT_MAX = global i64 0
@.str.1 = [28 x i8] c"Item count %lu out of range\00"
@ERANGE = global i32 0
define i32 @parse_itemcount(i8** nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = load i8*, i8** %0
%6 = call i64 @strtoul(i8* %5, i8** %3, i32 10)
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %18, label %9
9: ; preds = %2
%10 = load i8, i8* %7
%11 = icmp eq i8 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %0
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EINVAL
%17 = sub nsw i32 0, %16
br label %31
18: ; preds = %9, %2
%19 = load i64, i64* @LONG_MAX
%20 = icmp eq i64 %6, %19
%21 = load i64, i64* @UINT_MAX
%22 = icmp ugt i64 %6, %21
%23 = select i1 %20, i1 true, i1 %22
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %6)
%27 = load i32, i32* @ERANGE
%28 = sub nsw i32 0, %27
br label %31
29: ; preds = %18
%30 = trunc i64 %6 to i32
store i32 %30, i32* %1
br label %31
31: ; preds = %24, %29, %12
%32 = phi i32 [ %17, %12 ], [ %28, %24 ], [ 0, %29 ]
ret i32 %32
}