#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 int uint32_t;
typedef int uint16_t;

/* Variables and functions */
scalar_t__ errno;
int strtoul(char *, char **, int /*<<< orphan*/);
int str2u16(char *arg, uint16_t *val) {
  char *err = NULL;
  uint32_t t;
  errno = 0;
  t = strtoul(arg, &err, 0);
  if (errno || (err == arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
    return -1;
  }
  *val = t & 0xFFFF;
  return 0;
}


x86 assembly (clang, -O0):
<str2u16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2eb1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e91(%rip),%rax
cmpq   $0x0,(%rax)
jne    118f <str2u16+0x7f>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     118f <str2u16+0x7f>
cmpq   $0x0,-0x20(%rbp)
je     1182 <str2u16+0x72>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    118f <str2u16+0x7f>
cmpl   $0x10000,-0x24(%rbp)
jl     119b <str2u16+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <str2u16+0xa1>
mov    -0x24(%rbp),%ecx
and    $0xffff,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2u16>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
mov    0x2eb1(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmpq   $0x0,(%r15)
je     1159 <str2u16+0x49>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x8(%rsp),%rdx
cmp    %rbx,%rdx
je     114f <str2u16+0x3f>
test   %rdx,%rdx
je     116d <str2u16+0x5d>
cmpb   $0x0,(%rdx)
jne    114f <str2u16+0x3f>
cmp    $0xffff,%ecx
jg     114f <str2u16+0x3f>
movzwl %cx,%eax
mov    %eax,(%r14)
xor    %eax,%eax
jmp    114f <str2u16+0x3f>

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

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