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

/* Variables and functions */
scalar_t__ ERANGE;
scalar_t__ ULONG_MAX;
scalar_t__ USHRT_MAX;
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
scalar_t__ strtoul(char *, char **, int);
int get_port(char *port) {
  u_long p;
  char *ep;
  errno = 0;
  p = strtoul(port, &ep, 10);
  if (*port == '\0' || *ep != '\0')
    err(1, "Port is non numerical.");
  if ((errno == ERANGE && p == ULONG_MAX) || (p > USHRT_MAX))
    err(1, "Port value is out of range.");
  return p;
}


x86 assembly (clang, -O0):
<get_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e9d(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1170 <get_port+0x50>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1181 <get_port+0x61>
mov    $0x1,%edi
lea    0xe84(%rip),%rsi
call   1030 <err@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    11af <get_port+0x8f>
mov    -0x10(%rbp),%rax
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
je     11c3 <get_port+0xa3>
mov    -0x10(%rbp),%rax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%rax
jle    11d4 <get_port+0xb4>
mov    $0x1,%edi
lea    0xe48(%rip),%rsi
call   1030 <err@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_port>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e9d(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    %rax,%r14
cmpb   $0x0,(%rbx)
je     115b <get_port+0x3b>
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     116c <get_port+0x4c>
lea    0xe9e(%rip),%rsi
mov    $0x1,%edi
call   1030 <err@plt>
mov    (%r15),%rax
mov    0x2e6a(%rip),%rcx
xor    (%rcx),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%rcx
xor    %r14,%rcx
or     %rax,%rcx
je     1197 <get_port+0x77>
mov    0x2e56(%rip),%rax
cmp    (%rax),%r14
jle    11a8 <get_port+0x88>
lea    0xe79(%rip),%rsi
mov    $0x1,%edi
call   1030 <err@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [23 x i8] c"Port is non numerical.\00"
@ERANGE = global i64 0
@ULONG_MAX = global i64 0
@USHRT_MAX = global i64 0
@.str.1 = [28 x i8] c"Port value is out of range.\00"
define i32 @get_port(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
store i64 0, i64* @errno
%5 = load i8*, i8** %2
%6 = call i64 @strtoul(i8* %5, i8** %4, i32 10)
store i64 %6, i64* %3
%7 = load i8*, i8** %2
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11, %1
%17 = call i32 @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = load i64, i64* @errno
%20 = load i64, i64* @ERANGE
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i64, i64* %3
%24 = load i64, i64* @ULONG_MAX
%25 = icmp eq i64 %23, %24
br i1 %25, label %30, label %26
26: ; preds = %22, %18
%27 = load i64, i64* %3
%28 = load i64, i64* @USHRT_MAX
%29 = icmp sgt i64 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26, %22
%31 = call i32 @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = load i64, i64* %3
%34 = trunc i64 %33 to i32
ret i32 %34
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [23 x i8] c"Port is non numerical.\00"
@ERANGE = global i64 0
@ULONG_MAX = global i64 0
@USHRT_MAX = global i64 0
@.str.1 = [28 x i8] c"Port value is out of range.\00"
define i32 @get_port(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call i64 @strtoul(i8* %0, i8** %2, i32 10)
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %7, %1
%12 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %7
%14 = load i64, i64* @errno
%15 = load i64, i64* @ERANGE
%16 = icmp eq i64 %14, %15
%17 = load i64, i64* @ULONG_MAX
%18 = icmp eq i64 %4, %17
%19 = select i1 %16, i1 %18, i1 false
%20 = load i64, i64* @USHRT_MAX
%21 = icmp sgt i64 %4, %20
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %13, %23
%26 = trunc i64 %4 to i32
ret i32 %26
}