#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*/ EINVAL;
int /*<<< orphan*/ ERANGE;
long SHRT_MAX;
long SHRT_MIN;
int /*<<< orphan*/ errno;
long strtol(char const *, char **, int);
short mjst2s(const char *str, char **endptr, int base) {
  long v = strtol(str, endptr, base);
  if (*endptr == str)
    errno = EINVAL;
  if (v < SHRT_MIN) {
    errno = ERANGE;
    v = SHRT_MIN;
  } else if (v > SHRT_MAX) {
    errno = ERANGE;
    v = SHRT_MAX;
  }
  return ((short)v);
}


x86 assembly (clang, -O0):
<mjst2s>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
cmp    -0x8(%rbp),%rax
jne    115a <mjst2s+0x4a>
mov    0x2e71(%rip),%rax
mov    (%rax),%ecx
mov    0x2e80(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
jge    1193 <mjst2s+0x83>
mov    0x2e73(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5a(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11cc <mjst2s+0xbc>
mov    -0x20(%rbp),%rax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
jle    11c7 <mjst2s+0xb7>
mov    0x2e3a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e21(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11cc <mjst2s+0xbc>
mov    -0x20(%rbp),%rax
cwtl
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mjst2s>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strtol@plt>
cmp    %rbx,(%r14)
jne    1136 <mjst2s+0x26>
mov    0x2e95(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2ea4(%rip),%rdx
mov    %ecx,(%rdx)
mov    0x2e93(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jl     1154 <mjst2s+0x44>
mov    0x2e7c(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jle    1169 <mjst2s+0x59>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    0x2e74(%rip),%rdx
mov    %eax,(%rdx)
mov    %rcx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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