#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__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
long strtol(char *, char **, int);
unsigned short scanshort(char *s) {
  char *end;
  errno = 0;
  long n = strtol(s, &end, 10);
  if (errno) {
    perror("noq: strtol");
    exit(1);
  }
  if (*end) {
    fprintf(stderr, "noq: bad number: %s\n", s);
    exit(1);
  }
  if (n != (long)(unsigned short)n) {
    fprintf(stderr, "noq: overflow: %s\n", s);
    exit(1);
  }
  return n;
}


x86 assembly (clang, -O0):
<scanshort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e61(%rip),%rax
cmpq   $0x0,(%rax)
je     1197 <scanshort+0x57>
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11c7 <scanshort+0x87>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe54(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
mov    -0x18(%rbp),%rcx
movzwl %cx,%ecx
cmp    %rcx,%rax
je     11fe <scanshort+0xbe>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe32(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
movzwl %ax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scanshort>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e8a(%rip),%rbx
movq   $0x0,(%rbx)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
cmpq   $0x0,(%rbx)
jne    1181 <scanshort+0x41>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    1197 <scanshort+0x57>
cmp    $0x10000,%rax
jae    11a9 <scanshort+0x69>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
jmp    11b9 <scanshort+0x79>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
mov    %r14,%rdx
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@.str = [12 x i8] c"noq: strtol\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"noq: bad number: %s\0A\00"
@.str.2 = [19 x i8] c"noq: overflow: %s\0A\00"
define zeroext i16 @scanshort(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* @errno
%5 = load i8*, i8** %2
%6 = call i64 @strtol(i8* %5, i8** %3, i32 10)
store i64 %6, i64* %4
%7 = load i64, i64* @errno
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %2
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = load i64, i64* %4
%23 = load i64, i64* %4
%24 = trunc i64 %23 to i16
%25 = zext i16 %24 to i64
%26 = icmp ne i64 %22, %25
br i1 %26, label %27, label %32
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %2
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
%33 = load i64, i64* %4
%34 = trunc i64 %33 to i16
ret i16 %34
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [12 x i8] c"noq: strtol\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"noq: bad number: %s\0A\00"
@.str.2 = [19 x i8] c"noq: overflow: %s\0A\00"
define zeroext i16 @scanshort(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call i64 @strtol(i8* %0, i8** %2, i32 10)
%5 = load i64, i64* @errno
%6 = icmp eq i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = icmp ult i64 %4, 65536
br i1 %19, label %24, label %20
20: ; preds = %18
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %18
%25 = trunc i64 %4 to i16
ret i16 %25
}