#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;
unsigned long ULONG_MAX;
scalar_t__ errno;
unsigned long strtoul(char const *, char **, int /*<<< orphan*/);
int parse_uint16(const char *str, uint16_t *value) {
  char *endptr;
  unsigned long parsed_ulong;
  parsed_ulong = strtoul(str, &endptr, 0);
  if ((parsed_ulong == ULONG_MAX && errno == ERANGE) || *endptr != '\0' ||
      parsed_ulong > UINT16_MAX) {
    return -1;
  }
  *value = (uint16_t)parsed_ulong;
  return 0;
}


x86 assembly (clang, -O0):
<parse_uint16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e82(%rip),%rcx
cmp    (%rcx),%rax
jne    1161 <parse_uint16+0x51>
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
mov    0x2e90(%rip),%rcx
cmp    (%rcx),%rax
je     1185 <parse_uint16+0x75>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1185 <parse_uint16+0x75>
mov    -0x28(%rbp),%rax
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%rax
jbe    1191 <parse_uint16+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a3 <parse_uint16+0x93>
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):
<parse_uint16>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    0x2e95(%rip),%rcx
cmp    (%rcx),%rax
jne    114b <parse_uint16+0x3b>
mov    0x2ea1(%rip),%rcx
mov    (%rcx),%rdx
mov    0x2ea7(%rip),%rsi
mov    $0xffffffff,%ecx
cmp    (%rsi),%rdx
je     116b <parse_uint16+0x5b>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
mov    $0xffffffff,%ecx
jne    116b <parse_uint16+0x5b>
mov    0x2e6f(%rip),%rdx
cmp    (%rdx),%rax
ja     116b <parse_uint16+0x5b>
mov    %rax,(%rbx)
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@ULONG_MAX = global i64 0
@errno = global i64 0
@ERANGE = global i64 0
@UINT16_MAX = global i64 0
define i32 @parse_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
%8 = load i8*, i8** %4
%9 = call i64 @strtoul(i8* %8, i8** %6, i32 0)
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = load i64, i64* @ULONG_MAX
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i64, i64* @errno
%15 = load i64, i64* @ERANGE
%16 = icmp eq i64 %14, %15
br i1 %16, label %26, label %17
17: ; preds = %13, %2
%18 = load i8*, i8** %6
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %17
%23 = load i64, i64* %7
%24 = load i64, i64* @UINT16_MAX
%25 = icmp ugt i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22, %17, %13
store i32 -1, i32* %3
br label %30
27: ; preds = %22
%28 = load i64, i64* %7
%29 = load i64*, i64** %5
store i64 %28, i64* %29
store i32 0, i32* %3
br label %30
30: ; preds = %27, %26
%31 = load i32, i32* %3
ret i32 %31
}

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