#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*/ uintmax_t;

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ ERANGE;
int /*<<< orphan*/ UINTMAX_MAX;
scalar_t__ errno;
int /*<<< orphan*/ strtoumax(char const *, char **, int /*<<< orphan*/);
int getnum(const char *str, uintmax_t *num) {
  char *ep;
  errno = 0;
  *num = strtoumax(str, &ep, 0);
  if (str[0] == '\0' || *ep != '\0') {
    errno = EINVAL;
    return -1;
  }
  if (errno == ERANGE && *num == UINTMAX_MAX)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<getnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoumax@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1165 <getnum+0x55>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1185 <getnum+0x75>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rcx
mov    0x2e5a(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <getnum+0xb7>
mov    0x2e44(%rip),%rax
mov    (%rax),%rax
mov    0x2e4a(%rip),%rcx
cmp    (%rcx),%rax
jne    11c0 <getnum+0xb0>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%eax
jne    11c0 <getnum+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <getnum+0xb7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getnum>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eaa(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <strtoumax@plt>
mov    %eax,(%r14)
cmpb   $0x0,(%rbx)
je     114b <getnum+0x3b>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
je     115f <getnum+0x4f>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
mov    $0xffffffff,%eax
jmp    1182 <getnum+0x72>
mov    (%r15),%rcx
mov    0x2e77(%rip),%rdx
cmp    (%rdx),%rcx
sete   %cl
mov    0x2e7a(%rip),%rdx
cmp    (%rdx),%eax
sete   %al
and    %cl,%al
movzbl %al,%eax
neg    %eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EINVAL = global i64 0
@ERANGE = global i64 0
@UINTMAX_MAX = global i32 0
define i32 @getnum(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i64 0, i64* @errno
%7 = load i8*, i8** %4
%8 = call i32 @strtoumax(i8* %7, i8** %6, i32 0)
%9 = load i32*, i32** %5
store i32 %8, i32* %9
%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** %6
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15, %2
%21 = load i64, i64* @EINVAL
store i64 %21, i64* @errno
store i32 -1, i32* %3
br label %33
22: ; preds = %15
%23 = load i64, i64* @errno
%24 = load i64, i64* @ERANGE
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i32*, i32** %5
%28 = load i32, i32* %27
%29 = load i32, i32* @UINTMAX_MAX
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 -1, i32* %3
br label %33
32: ; preds = %26, %22
store i32 0, i32* %3
br label %33
33: ; preds = %32, %31, %20
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@errno = global i64 0
@EINVAL = global i64 0
@ERANGE = global i64 0
@UINTMAX_MAX = global i32 0
define i32 @getnum(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i64 0, i64* @errno
%5 = call i32 @strtoumax(i8* %0, i8** %3, i32 0)
store i32 %5, i32* %1
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %12, 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 %14, label %12
12: ; preds = %8, %2
%13 = load i64, i64* @EINVAL
store i64 %13, i64* @errno
br label %22
14: ; preds = %8
%15 = load i64, i64* @errno
%16 = load i64, i64* @ERANGE
%17 = icmp eq i64 %15, %16
%18 = load i32, i32* @UINTMAX_MAX
%19 = icmp eq i32 %5, %18
%20 = select i1 %17, i1 %19, i1 false
%21 = sext i1 %20 to i32
br label %22
22: ; preds = %14, %12
%23 = phi i32 [ -1, %12 ], [ %21, %14 ]
ret i32 %23
}