#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 */
typedef scalar_t__ int64_t;
typedef scalar_t__ int16_t;

/* Variables and functions */
int ERANGE;
scalar_t__ INT16_MAX;
scalar_t__ INT16_MIN;
int dspd_strtoi64(char const *, scalar_t__ *, int);
int dspd_strtoi16(const char *str, int16_t *n, int base) {
  int64_t tmp;
  int ret;
  ret = dspd_strtoi64(str, &tmp, base);
  if (ret == 0) {
    if (tmp > INT16_MAX || tmp < INT16_MIN)
      ret = -ERANGE;
    else
      *n = tmp;
  }
  return ret;
}


x86 assembly (clang, -O0):
<dspd_strtoi16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rsi
call   1030 <dspd_strtoi64@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    118b <dspd_strtoi16+0x7b>
mov    -0x20(%rbp),%rax
mov    0x2e8d(%rip),%rcx
cmp    (%rcx),%rax
jg     1168 <dspd_strtoi16+0x58>
mov    -0x20(%rbp),%rax
mov    0x2e71(%rip),%rcx
cmp    (%rcx),%rax
jge    117b <dspd_strtoi16+0x6b>
xor    %eax,%eax
mov    0x2e77(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    1186 <dspd_strtoi16+0x76>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    118b <dspd_strtoi16+0x7b>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dspd_strtoi16>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    0x8(%rsp),%rsi
call   1030 <dspd_strtoi64@plt>
test   %eax,%eax
jne    1155 <dspd_strtoi16+0x45>
mov    0x8(%rsp),%rax
mov    0x2ea6(%rip),%rcx
cmp    (%rcx),%rax
jg     114a <dspd_strtoi16+0x3a>
mov    0x2e92(%rip),%rcx
cmp    (%rcx),%rax
jl     114a <dspd_strtoi16+0x3a>
mov    %rax,(%rbx)
xor    %eax,%eax
jmp    1155 <dspd_strtoi16+0x45>
mov    0x2e97(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@INT16_MAX = global i64 0
@INT16_MIN = global i64 0
@ERANGE = global i32 0
define i32 @dspd_strtoi16(i8* %0, i64* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* %6
%11 = call i32 @dspd_strtoi64(i8* %9, i64* %7, i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %3
%15 = load i64, i64* %7
%16 = load i64, i64* @INT16_MAX
%17 = icmp sgt i64 %15, %16
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = load i64, i64* %7
%20 = load i64, i64* @INT16_MIN
%21 = icmp slt i64 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18, %14
%23 = load i32, i32* @ERANGE
%24 = sub nsw i32 0, %23
store i32 %24, i32* %8
br label %28
25: ; preds = %18
%26 = load i64, i64* %7
%27 = load i64*, i64** %5
store i64 %26, i64* %27
br label %28
28: ; preds = %25, %22
br label %29
29: ; preds = %28, %3
%30 = load i32, i32* %8
ret i32 %30
}

LLVM IR (-O2):
@INT16_MAX = global i64 0
@INT16_MIN = global i64 0
@ERANGE = global i32 0
define i32 @dspd_strtoi16(i8* %0, i64* nocapture writeonly %1, i32 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = call i32 @dspd_strtoi64(i8* %0, i64* %4, i32 %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %19
8: ; preds = %3
%9 = load i64, i64* %4
%10 = load i64, i64* @INT16_MAX
%11 = icmp sgt i64 %9, %10
%12 = load i64, i64* @INT16_MIN
%13 = icmp slt i64 %9, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = load i32, i32* @ERANGE
%17 = sub nsw i32 0, %16
br label %19
18: ; preds = %8
store i64 %9, i64* %1
br label %19
19: ; preds = %15, %18, %3
%20 = phi i32 [ %17, %15 ], [ 0, %18 ], [ %6, %3 ]
ret i32 %20
}