#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 */
unsigned long UINT_MAX;
scalar_t__ errno;
unsigned long strtoul(char const *, char **, int /*<<< orphan*/);
int parse_num(unsigned *num, const char *str) {
  unsigned long val;
  char *end;
  errno = 0;
  val = strtoul(str, &end, 0);
  if (errno || *end || val > UINT_MAX)
    return -1;
  *num = val;
  return 0;
}


x86 assembly (clang, -O0):
<parse_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e90(%rip),%rax
cmpq   $0x0,(%rax)
jne    1176 <parse_num+0x66>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1176 <parse_num+0x66>
mov    -0x20(%rbp),%rax
mov    0x2e7b(%rip),%rcx
cmp    (%rcx),%rax
jbe    1182 <parse_num+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <parse_num+0x85>
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_num>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %rdi,%r14
mov    0x2eb7(%rip),%rbx
movq   $0x0,(%rbx)
mov    %rsp,%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmpq   $0x0,(%rbx)
jne    115d <parse_num+0x4d>
mov    (%rsp),%rdx
cmpb   $0x0,(%rdx)
jne    115d <parse_num+0x4d>
mov    0x2e95(%rip),%rdx
cmp    (%rdx),%rcx
ja     115d <parse_num+0x4d>
mov    %ecx,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@errno = global i64 0
@UINT_MAX = global i64 0
define i32 @parse_num(i32* nocapture writeonly %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i64 0, i64* @errno
%5 = call i64 @strtoul(i8* %1, 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 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
%12 = load i64, i64* @UINT_MAX
%13 = icmp ugt i64 %5, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %17, label %15
15: ; preds = %8
%16 = trunc i64 %5 to i32
store i32 %16, i32* %0
br label %17
17: ; preds = %2, %8, %15
%18 = phi i32 [ 0, %15 ], [ -1, %8 ], [ -1, %2 ]
ret i32 %18
}