#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 */
int /*<<< orphan*/ LLONG_MAX;
int /*<<< orphan*/ LLONG_MIN;
long long SHRT_MAX;
long long SHRT_MIN;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
long long strtonum(char *, int /*<<< orphan*/, int /*<<< orphan*/,
                   char const **);
short stringtoshort(char *string) {
  const char *err;
  long long ll;
  short answer;
  errno = 0;
  ll = strtonum(string, LLONG_MIN, LLONG_MAX, &err);
  if (err != NULL) {
    errno = -2;
    fprintf(stderr, "strtonum error: %s\n", err);
    return 0;
  }
  if (ll > SHRT_MAX) {
    errno = 1;
    fprintf(stderr, "stringtoint: number too big for int! (Number was %lld)\n",
            ll);
    return 0;
  } else if (ll < SHRT_MIN) {
    errno = -1;
    fprintf(stderr, "stringtoint: number too big for int! (Number was %lld)\n",
            ll);
    return 0;
  }
  answer = (short)ll;
  errno = 0;
  return answer;
}


x86 assembly (clang, -O0):
<stringtoshort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
mov    0x2e9b(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
call   1040 <strtonum@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119a <stringtoshort+0x7a>
mov    0x2e5a(%rip),%rax
movl   $0xfffffffe,(%rax)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movw   $0x0,-0x2(%rbp)
jmp    124a <stringtoshort+0x12a>
mov    -0x20(%rbp),%rax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%rax
jle    11e1 <stringtoshort+0xc1>
mov    0x2e13(%rip),%rax
movl   $0x1,(%rax)
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movw   $0x0,-0x2(%rbp)
jmp    124a <stringtoshort+0x12a>
mov    -0x20(%rbp),%rax
mov    0x2dd4(%rip),%rcx
cmp    (%rcx),%rax
jge    1228 <stringtoshort+0x108>
mov    0x2dcc(%rip),%rax
movl   $0xffffffff,(%rax)
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movw   $0x0,-0x2(%rbp)
jmp    124a <stringtoshort+0x12a>
jmp    122d <stringtoshort+0x10d>
mov    -0x20(%rbp),%rax
mov    %ax,-0x22(%rbp)
mov    0x2d8c(%rip),%rax
movl   $0x0,(%rax)
mov    -0x22(%rbp),%ax
mov    %ax,-0x2(%rbp)
movswl -0x2(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stringtoshort>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e9c(%rip),%rbx
movl   $0x0,(%rbx)
mov    0x2e9f(%rip),%rax
mov    (%rax),%esi
mov    0x2ea6(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
call   1040 <strtonum@plt>
mov    0x8(%rsp),%rdx
test   %rdx,%rdx
je     1172 <stringtoshort+0x52>
movl   $0xfffffffe,(%rbx)
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
xor    %ebx,%ebx
jmp    11ad <stringtoshort+0x8d>
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
jle    1186 <stringtoshort+0x66>
movl   $0x1,(%rbx)
jmp    1198 <stringtoshort+0x78>
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%rax
jge    11bc <stringtoshort+0x9c>
movl   $0xffffffff,(%rbx)
mov    0x2e31(%rip),%rcx
mov    (%rcx),%edi
lea    0xe6c(%rip),%rsi
xor    %ebx,%ebx
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
movl   $0x0,(%rbx)
mov    %eax,%ebx
jmp    11b4 <stringtoshort+0x94>

LLVM IR (-O0):
@errno = global i32 0
@LLONG_MIN = global i32 0
@LLONG_MAX = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"strtonum error: %s\0A\00"
@SHRT_MAX = global i64 0
@.str.1 = [56 x i8] c"stringtoint: number too big for int! (Number was %lld)\0A\00"
@SHRT_MIN = global i64 0
define signext i16 @stringtoshort(i8* %0) {
%2 = alloca i16
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i16
store i8* %0, i8** %3
store i32 0, i32* @errno
%7 = load i8*, i8** %3
%8 = load i32, i32* @LLONG_MIN
%9 = load i32, i32* @LLONG_MAX
%10 = call i64 @strtonum(i8* %7, i32 %8, i32 %9, i8** %4)
store i64 %10, i64* %5
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
store i32 -2, i32* @errno
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
store i16 0, i16* %2
br label %38
17: ; preds = %1
%18 = load i64, i64* %5
%19 = load i64, i64* @SHRT_MAX
%20 = icmp sgt i64 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %17
store i32 1, i32* @errno
%22 = load i32, i32* @stderr
%23 = load i64, i64* %5
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64 %23)
store i16 0, i16* %2
br label %38
25: ; preds = %17
%26 = load i64, i64* %5
%27 = load i64, i64* @SHRT_MIN
%28 = icmp slt i64 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %25
store i32 -1, i32* @errno
%30 = load i32, i32* @stderr
%31 = load i64, i64* %5
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64 %31)
store i16 0, i16* %2
br label %38
33: ; preds = %25
br label %34
34: ; preds = %33
%35 = load i64, i64* %5
%36 = trunc i64 %35 to i16
store i16 %36, i16* %6
store i32 0, i32* @errno
%37 = load i16, i16* %6
store i16 %37, i16* %2
br label %38
38: ; preds = %34, %29, %21, %13
%39 = load i16, i16* %2
ret i16 %39
}

LLVM IR (-O2):
@errno = global i32 0
@LLONG_MIN = global i32 0
@LLONG_MAX = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"strtonum error: %s\0A\00"
@SHRT_MAX = global i64 0
@.str.1 = [56 x i8] c"stringtoint: number too big for int! (Number was %lld)\0A\00"
@SHRT_MIN = global i64 0
define signext i16 @stringtoshort(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i32 0, i32* @errno
%4 = load i32, i32* @LLONG_MIN
%5 = load i32, i32* @LLONG_MAX
%6 = call i64 @strtonum(i8* %0, i32 %4, i32 %5, i8** %2)
%7 = load i8*, i8** %2
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
store i32 -2, i32* @errno
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %26
12: ; preds = %1
%13 = load i64, i64* @SHRT_MAX
%14 = icmp sgt i64 %6, %13
br i1 %14, label %15, label %18
15: ; preds = %12
store i32 1, i32* @errno
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64 %6)
br label %26
18: ; preds = %12
%19 = load i64, i64* @SHRT_MIN
%20 = icmp slt i64 %6, %19
br i1 %20, label %21, label %24
21: ; preds = %18
store i32 -1, i32* @errno
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64 %6)
br label %26
24: ; preds = %18
%25 = trunc i64 %6 to i16
store i32 0, i32* @errno
br label %26
26: ; preds = %24, %21, %15, %9
%27 = phi i16 [ 0, %9 ], [ 0, %15 ], [ 0, %21 ], [ %25, %24 ]
ret i16 %27
}