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

/* Variables and functions */
scalar_t__ ERANGE;
scalar_t__ UINT16_MAX;
scalar_t__ errno;
scalar_t__ strtoimax(char const *, char **, int);
int str_to_uint16(const char *str, uint16_t *res) {
  char *end;
  errno = 0;
  intmax_t val = strtoimax(str, &end, 10);
  if (errno == ERANGE || val < 0 || val > UINT16_MAX || end == str ||
      *end != '\0')
    return -1;
  *res = (uint16_t)val;
  return 0;
}


x86 assembly (clang, -O0):
<str_to_uint16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtoimax@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%rax
mov    0x2e93(%rip),%rcx
cmp    (%rcx),%rax
je     119b <str_to_uint16+0x8b>
cmpq   $0x0,-0x28(%rbp)
jl     119b <str_to_uint16+0x8b>
mov    -0x28(%rbp),%rax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
jg     119b <str_to_uint16+0x8b>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     119b <str_to_uint16+0x8b>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11a7 <str_to_uint16+0x97>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <str_to_uint16+0xa9>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_uint16>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eb2(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtoimax@plt>
mov    %rax,%rcx
mov    (%r15),%rdx
mov    0x2e9f(%rip),%rsi
mov    $0xffffffff,%eax
cmp    (%rsi),%rdx
je     1178 <str_to_uint16+0x68>
test   %rcx,%rcx
js     1178 <str_to_uint16+0x68>
mov    0x2e71(%rip),%rdx
cmp    (%rdx),%rcx
jg     1178 <str_to_uint16+0x68>
mov    0x8(%rsp),%rdx
cmp    %rbx,%rdx
je     1178 <str_to_uint16+0x68>
cmpb   $0x0,(%rdx)
jne    1178 <str_to_uint16+0x68>
mov    %rcx,(%r14)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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