#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 */

/* Variables and functions */
scalar_t__ ERANGE;
long LONG_MAX;
long LONG_MIN;
scalar_t__ errno;
long strtol(char const *, char **, int /*<<< orphan*/);
int a2port(const char *s) {
  long port;
  char *endp;
  errno = 0;
  port = strtol(s, &endp, 0);
  if (s == endp || *endp != '\0' ||
      (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
      port <= 0 || port > 65535)
    return 0;
  return port;
}


x86 assembly (clang, -O0):
<a2port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ea5(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11b6 <a2port+0xa6>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11b6 <a2port+0xa6>
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
mov    0x2e74(%rip),%rcx
cmp    (%rcx),%rax
jne    119d <a2port+0x8d>
mov    -0x18(%rbp),%rax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%rax
je     11b6 <a2port+0xa6>
mov    -0x18(%rbp),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
je     11b6 <a2port+0xa6>
cmpq   $0x0,-0x18(%rbp)
jle    11b6 <a2port+0xa6>
cmpq   $0xffff,-0x18(%rbp)
jle    11c2 <a2port+0xb2>
movl   $0x0,-0x4(%rbp)
jmp    11c9 <a2port+0xb9>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<a2port>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ea6(%rip),%r14
movq   $0x0,(%r14)
lea    0x8(%rsp),%rsi
xor    %ebp,%ebp
xor    %edx,%edx
call   1030 <strtol@plt>
mov    0x8(%rsp),%rcx
cmp    %rbx,%rcx
je     1146 <a2port+0x36>
cmpb   $0x0,(%rcx)
je     1151 <a2port+0x41>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%r14),%rcx
mov    0x2e85(%rip),%rdx
cmp    (%rdx),%rcx
jne    118a <a2port+0x7a>
mov    0x2e81(%rip),%rcx
cmp    (%rcx),%rax
je     1146 <a2port+0x36>
lea    -0x10000(%rax),%rcx
cmp    $0xffffffffffff0001,%rcx
jb     1146 <a2port+0x36>
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
jne    119d <a2port+0x8d>
jmp    1146 <a2port+0x36>
mov    %rax,%rcx
add    $0xffffffffffff0000,%rcx
cmp    $0xffffffffffff0001,%rcx
jb     1146 <a2port+0x36>
mov    %eax,%ebp
jmp    1146 <a2port+0x36>

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

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MIN = global i64 0
@LONG_MAX = global i64 0
define i32 @a2port(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call i64 @strtol(i8* %0, i8** %2, i32 0)
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, %0
br i1 %6, label %28, label %7
7: ; preds = %1
%8 = load i8, i8* %5
%9 = icmp eq i8 %8, 0
br i1 %9, label %10, label %28
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @ERANGE
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %23
14: ; preds = %10
%15 = load i64, i64* @LONG_MIN
%16 = icmp eq i64 %4, %15
br i1 %16, label %28, label %17
17: ; preds = %14
%18 = load i64, i64* @LONG_MAX
%19 = icmp eq i64 %4, %18
%20 = add i64 %4, -65536
%21 = icmp ult i64 %20, -65535
%22 = or i1 %21, %19
br i1 %22, label %28, label %26
23: ; preds = %10
%24 = add i64 %4, -65536
%25 = icmp ult i64 %24, -65535
br i1 %25, label %28, label %26
26: ; preds = %23, %17
%27 = trunc i64 %4 to i32
br label %28
28: ; preds = %1, %7, %14, %17, %23, %26
%29 = phi i32 [ %27, %26 ], [ 0, %23 ], [ 0, %17 ], [ 0, %14 ], [ 0, %7 ], [ 0, %1 ]
ret i32 %29
}