#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;

/* Variables and functions */
scalar_t__ ERANGE;
unsigned long UINT16_MAX;
scalar_t__ errno;
unsigned long strtoul(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int str_uint16(const char *str, uint16_t *val) {
  unsigned long tmp;
  errno = 0;
  tmp = strtoul(str, NULL, 0);
  if (tmp > UINT16_MAX || str[0] == '-') {
    errno = ERANGE;
    return -1;
  }
  if (errno != 0) {
    return -1;
  }
  *val = (uint16_t)tmp;
  return 0;
}


x86 assembly (clang, -O0):
<str_uint16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e84(%rip),%rcx
cmp    (%rcx),%rax
ja     1165 <str_uint16+0x55>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    1185 <str_uint16+0x75>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rcx
mov    0x2e62(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <str_uint16+0xa4>
mov    0x2e4c(%rip),%rax
cmpq   $0x0,(%rax)
je     11a2 <str_uint16+0x92>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <str_uint16+0xa4>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_uint16>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eb6(%rip),%r15
movq   $0x0,(%r15)
xor    %esi,%esi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    0x2e97(%rip),%rcx
cmp    (%rcx),%rax
ja     1143 <str_uint16+0x33>
cmpb   $0x2d,(%rbx)
jne    115d <str_uint16+0x4d>
mov    0x2e9e(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
mov    $0xffffffff,%ecx
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    $0xffffffff,%ecx
cmpq   $0x0,(%r15)
jne    1155 <str_uint16+0x45>
mov    %rax,(%r14)
xor    %ecx,%ecx
jmp    1155 <str_uint16+0x45>

LLVM IR (-O0):
@errno = global i64 0
@UINT16_MAX = global i64 0
@ERANGE = global i64 0
define i32 @str_uint16(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
store i64 0, i64* @errno
%7 = load i8*, i8** %4
%8 = call i64 @strtoul(i8* %7, i32* null, i32 0)
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = load i64, i64* @UINT16_MAX
%11 = icmp ugt i64 %9, %10
br i1 %11, label %18, label %12
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 45
br i1 %17, label %18, label %20
18: ; preds = %12, %2
%19 = load i64, i64* @ERANGE
store i64 %19, i64* @errno
store i32 -1, i32* %3
br label %27
20: ; preds = %12
%21 = load i64, i64* @errno
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 -1, i32* %3
br label %27
24: ; preds = %20
%25 = load i64, i64* %6
%26 = load i64*, i64** %5
store i64 %25, i64* %26
store i32 0, i32* %3
br label %27
27: ; preds = %24, %23, %18
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@errno = global i64 0
@UINT16_MAX = global i64 0
@ERANGE = global i64 0
define i32 @str_uint16(i8* nocapture %0, i64* nocapture writeonly %1) {
store i64 0, i64* @errno
%3 = tail call i64 @strtoul(i8* nocapture %0, i32* null, i32 0)
%4 = load i64, i64* @UINT16_MAX
%5 = icmp ugt i64 %3, %4
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 45
br i1 %8, label %9, label %11
9: ; preds = %6, %2
%10 = load i64, i64* @ERANGE
store i64 %10, i64* @errno
br label %15
11: ; preds = %6
%12 = load i64, i64* @errno
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11
store i64 %3, i64* %1
br label %15
15: ; preds = %11, %14, %9
%16 = phi i32 [ -1, %9 ], [ 0, %14 ], [ -1, %11 ]
ret i32 %16
}