#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;
long LONG_MAX;
long LONG_MIN;
scalar_t__ errno;
long strtol(char const *, char **, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *, char const *, char const *);
int /*<<< orphan*/ warnx(char *, char const *, char const *);
int getnum(const char *com, const char *str, int *val) {
  long v;
  char *ep;
  errno = 0;
  v = strtol(str, &ep, 0);
  if (*ep) {
    warnx("Bad %s argument: %s", com, str);
    return 1;
  }
  if ((v == LONG_MIN || v == LONG_MAX) && errno == ERANGE) {
    warn("Invalid %s argument: %s", com, str);
    return 1;
  }
  *val = (int)v;
  return 0;
}


x86 assembly (clang, -O0):
<getnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e7d(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     1192 <getnum+0x62>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe7f(%rip),%rdi
call   1040 <warnx@plt>
movl   $0x1,-0x4(%rbp)
jmp    1207 <getnum+0xd7>
mov    -0x28(%rbp),%rax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%rax
je     11ba <getnum+0x8a>
mov    -0x28(%rbp),%rax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%rax
jne    11f4 <getnum+0xc4>
mov    0x2e07(%rip),%rax
mov    (%rax),%rax
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%rax
jne    11f4 <getnum+0xc4>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe31(%rip),%rdi
call   1030 <warn@plt>
movl   $0x1,-0x4(%rbp)
jmp    1207 <getnum+0xd7>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getnum>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e80(%rip),%r12
movq   $0x0,(%r12)
mov    %rsp,%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     117f <getnum+0x4f>
lea    0xe93(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <warnx@plt>
mov    $0x1,%eax
jmp    11c5 <getnum+0x95>
mov    0x2e62(%rip),%rcx
cmp    (%rcx),%rax
je     1197 <getnum+0x67>
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
jne    11c0 <getnum+0x90>
mov    (%r12),%rcx
mov    0x2e3e(%rip),%rdx
cmp    (%rdx),%rcx
jne    11c0 <getnum+0x90>
lea    0xe66(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <warn@plt>
mov    $0x1,%eax
jmp    11c5 <getnum+0x95>
mov    %eax,(%r15)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [20 x i8] c"Bad %s argument: %s\00"
@LONG_MIN = global i64 0
@LONG_MAX = global i64 0
@ERANGE = global i64 0
@.str.1 = [24 x i8] c"Invalid %s argument: %s\00"
define i32 @getnum(i8* %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i64 0, i64* @errno
%10 = load i8*, i8** %6
%11 = call i64 @strtol(i8* %10, i8** %9, i32 0)
store i64 %11, i64* %8
%12 = load i8*, i8** %9
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = call i32 @warnx(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %17)
store i32 1, i32* %4
br label %39
19: ; preds = %3
%20 = load i64, i64* %8
%21 = load i64, i64* @LONG_MIN
%22 = icmp eq i64 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* %8
%25 = load i64, i64* @LONG_MAX
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %35
27: ; preds = %23, %19
%28 = load i64, i64* @errno
%29 = load i64, i64* @ERANGE
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i8*, i8** %5
%33 = load i8*, i8** %6
%34 = call i32 @warn(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* %33)
store i32 1, i32* %4
br label %39
35: ; preds = %27, %23
%36 = load i64, i64* %8
%37 = trunc i64 %36 to i32
%38 = load i32*, i32** %7
store i32 %37, i32* %38
store i32 0, i32* %4
br label %39
39: ; preds = %35, %31, %15
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [20 x i8] c"Bad %s argument: %s\00"
@LONG_MIN = global i64 0
@LONG_MAX = global i64 0
@ERANGE = global i64 0
@.str.1 = [24 x i8] c"Invalid %s argument: %s\00"
define i32 @getnum(i8* %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i64 0, i64* @errno
%6 = call i64 @strtol(i8* %1, i8** %4, i32 0)
%7 = load i8*, i8** %4
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = tail call i32 @warnx(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
br label %26
12: ; preds = %3
%13 = load i64, i64* @LONG_MIN
%14 = icmp eq i64 %6, %13
%15 = load i64, i64* @LONG_MAX
%16 = icmp eq i64 %6, %15
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %18, label %24
18: ; preds = %12
%19 = load i64, i64* @errno
%20 = load i64, i64* @ERANGE
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @warn(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
br label %26
24: ; preds = %12, %18
%25 = trunc i64 %6 to i32
store i32 %25, i32* %2
br label %26
26: ; preds = %24, %22, %10
%27 = phi i32 [ 1, %10 ], [ 1, %22 ], [ 0, %24 ]
ret i32 %27
}