#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__ uint64_t;
typedef scalar_t__ uint16_t;

/* Variables and functions */
int ERANGE;
scalar_t__ UINT16_MAX;
int dspd_strtou64(char const *, scalar_t__ *, int);
int dspd_strtou16(const char *str, uint16_t *n, int base) {
  uint64_t tmp;
  int ret;
  ret = dspd_strtou64(str, &tmp, base);
  if (ret == 0) {
    if (tmp > UINT16_MAX)
      ret = -ERANGE;
    else
      *n = tmp;
  }
  return ret;
}


x86 assembly (clang, -O0):
<dspd_strtou16>:
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_strtou64@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1177 <dspd_strtou16+0x67>
mov    -0x20(%rbp),%rax
mov    0x2e8d(%rip),%rcx
cmp    (%rcx),%rax
jle    1167 <dspd_strtou16+0x57>
xor    %eax,%eax
mov    0x2e8b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    1172 <dspd_strtou16+0x62>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    1177 <dspd_strtou16+0x67>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dspd_strtou16>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    0x8(%rsp),%rsi
call   1030 <dspd_strtou64@plt>
test   %eax,%eax
jne    1149 <dspd_strtou16+0x39>
mov    0x8(%rsp),%rax
mov    0x2ea6(%rip),%rcx
cmp    (%rcx),%rax
jle    1144 <dspd_strtou16+0x34>
mov    0x2eaa(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    1149 <dspd_strtou16+0x39>
mov    %rax,(%rbx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@UINT16_MAX = global i64 0
@ERANGE = global i32 0
define i32 @dspd_strtou16(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_strtou64(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 %25
14: ; preds = %3
%15 = load i64, i64* %7
%16 = load i64, i64* @UINT16_MAX
%17 = icmp sgt i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* @ERANGE
%20 = sub nsw i32 0, %19
store i32 %20, i32* %8
br label %24
21: ; preds = %14
%22 = load i64, i64* %7
%23 = load i64*, i64** %5
store i64 %22, i64* %23
br label %24
24: ; preds = %21, %18
br label %25
25: ; preds = %24, %3
%26 = load i32, i32* %8
ret i32 %26
}

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