#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;
int SHRT_MAX;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
scalar_t__ errno;
int strtol(char *, int /*<<< orphan*/ *, int);
int parse_nid(char *tok, short *nid) {
  int n;
  assert(tok != NULL);
  assert(nid != NULL);
  n = strtol(tok, NULL, 10);
  if (n < 0 || errno == ERANGE) {
    return -1;
  }
  if (n > SHRT_MAX) {
    return -1;
  }
  *nid = n;
  return 0;
}


x86 assembly (clang, -O0):
<parse_nid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     118d <parse_nid+0x6d>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
mov    0x2e64(%rip),%rcx
cmp    (%rcx),%rax
jne    1199 <parse_nid+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <parse_nid+0xab>
mov    -0x1c(%rbp),%eax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%eax
jle    11b7 <parse_nid+0x97>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <parse_nid+0xab>
mov    -0x1c(%rbp),%eax
mov    %ax,%cx
mov    -0x18(%rbp),%rax
mov    %cx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_nid>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1030 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1030 <assert@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
js     1187 <parse_nid+0x67>
mov    0x2e71(%rip),%rdx
mov    (%rdx),%rdx
mov    0x2e77(%rip),%rsi
cmp    (%rsi),%rdx
je     1187 <parse_nid+0x67>
mov    0x2e53(%rip),%rdx
cmp    (%rdx),%ecx
jg     1187 <parse_nid+0x67>
mov    %cx,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@SHRT_MAX = global i32 0
define i32 @parse_nid(i8* %0, i16* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16*
%6 = alloca i32
store i8* %0, i8** %4
store i16* %1, i16** %5
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i16*, i16** %5
%12 = icmp ne i16* %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8*, i8** %4
%16 = call i32 @strtol(i8* %15, i32* null, i32 10)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %2
%20 = load i64, i64* @errno
%21 = load i64, i64* @ERANGE
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19, %2
store i32 -1, i32* %3
br label %33
24: ; preds = %19
%25 = load i32, i32* %6
%26 = load i32, i32* @SHRT_MAX
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %3
br label %33
29: ; preds = %24
%30 = load i32, i32* %6
%31 = trunc i32 %30 to i16
%32 = load i16*, i16** %5
store i16 %31, i16* %32
store i32 0, i32* %3
br label %33
33: ; preds = %29, %28, %23
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@SHRT_MAX = global i32 0
define i32 @parse_nid(i8* %0, i16* writeonly %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp ne i16* %1, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i32 @strtol(i8* nocapture %0, i32* null, i32 10)
%10 = icmp slt i32 %9, 0
br i1 %10, label %20, label %11
11: ; preds = %2
%12 = load i64, i64* @errno
%13 = load i64, i64* @ERANGE
%14 = icmp eq i64 %12, %13
%15 = load i32, i32* @SHRT_MAX
%16 = icmp sgt i32 %9, %15
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %20, label %18
18: ; preds = %11
%19 = trunc i32 %9 to i16
store i16 %19, i16* %1
br label %20
20: ; preds = %2, %11, %18
%21 = phi i32 [ 0, %18 ], [ -1, %11 ], [ -1, %2 ]
ret i32 %21
}