#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__ EINVAL;
scalar_t__ ERANGE;
unsigned long USHRT_MAX;
int /*<<< orphan*/ abort();
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
unsigned long strtoul(char *, char **, int);
short parse_args(int argc, char **argv) {
  if (argc < 2) {
    return 8080;
  } else {
    errno = 0;
    char *endptr = NULL;
    unsigned long ulPort = strtoul(argv[1], &endptr, 10);
    if (0 == errno) {
      if ('\0' != endptr[0])
        errno = EINVAL;
      else if (ulPort > USHRT_MAX)
        errno = ERANGE;
    }
    if (0 != errno) {
      fprintf(stderr, "Failed to parse port number \"%s\": %s\n", argv[1],
              strerror(errno));
      abort();
    }
    return ulPort;
  }
}


x86 assembly (clang, -O0):
<parse_args>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1164 <parse_args+0x24>
movw   $0x1f90,-0x2(%rbp)
jmp    1260 <parse_args+0x120>
mov    0x2e5d(%rip),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
jne    1202 <parse_args+0xc2>
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
je     11d0 <parse_args+0x90>
mov    0x2e02(%rip),%rax
mov    (%rax),%rcx
mov    0x2e00(%rip),%rax
mov    %rcx,(%rax)
jmp    11fd <parse_args+0xbd>
mov    -0x20(%rbp),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
jbe    11f8 <parse_args+0xb8>
mov    0x2df5(%rip),%rax
mov    (%rax),%rcx
mov    0x2dd3(%rip),%rax
mov    %rcx,(%rax)
jmp    11fd <parse_args+0xbd>
jmp    1202 <parse_args+0xc2>
xor    %eax,%eax
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%rax
je     1258 <parse_args+0x118>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xdb4(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rax
mov    %ax,-0x2(%rbp)
movswl -0x2(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_args>:
push   %rbp
push   %rbx
push   %rax
mov    $0x1f90,%ax
cmp    $0x2,%edi
jl     11b1 <parse_args+0x71>
mov    %rsi,%rbx
mov    0x2e72(%rip),%rbp
movq   $0x0,0x0(%rbp)
movq   $0x0,(%rsp)
mov    0x8(%rsi),%rdi
mov    %rsp,%rsi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    0x0(%rbp),%rdi
test   %rdi,%rdi
jne    11b8 <parse_args+0x78>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1192 <parse_args+0x52>
mov    0x2e30(%rip),%rcx
jmp    11a5 <parse_args+0x65>
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
jbe    11b1 <parse_args+0x71>
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%rdi
mov    %rdi,0x0(%rbp)
test   %rdi,%rdi
jne    11b8 <parse_args+0x78>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e11(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%rbx),%rbx
call   1060 <strerror@plt>
lea    0xe2f(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINVAL = global i64 0
@USHRT_MAX = global i64 0
@ERANGE = global i64 0
@stderr = global i32 0
@.str = [38 x i8] c"Failed to parse port number \22%s\22: %s\0A\00"
define signext i16 @parse_args(i32 %0, i8** %1) {
%3 = alloca i16
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %11
10: ; preds = %2
store i16 8080, i16* %3
br label %49
11: ; preds = %2
store i64 0, i64* @errno
store i8* null, i8** %6
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @strtoul(i8* %14, i8** %6, i32 10)
store i64 %15, i64* %7
%16 = load i64, i64* @errno
%17 = icmp eq i64 0, %16
br i1 %17, label %18, label %34
18: ; preds = %11
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 0, %22
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = load i64, i64* @EINVAL
store i64 %25, i64* @errno
br label %33
26: ; preds = %18
%27 = load i64, i64* %7
%28 = load i64, i64* @USHRT_MAX
%29 = icmp ugt i64 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i64, i64* @ERANGE
store i64 %31, i64* @errno
br label %32
32: ; preds = %30, %26
br label %33
33: ; preds = %32, %24
br label %34
34: ; preds = %33, %11
%35 = load i64, i64* @errno
%36 = icmp ne i64 0, %35
br i1 %36, label %37, label %46
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = load i64, i64* @errno
%43 = call i8* @strerror(i64 %42)
%44 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %41, i8* %43)
%45 = call i32 (...) @abort()
unreachable
46: ; preds = %34
%47 = load i64, i64* %7
%48 = trunc i64 %47 to i16
store i16 %48, i16* %3
br label %49
49: ; preds = %46, %10
%50 = load i16, i16* %3
ret i16 %50
}

LLVM IR (-O2):
@errno = global i64 0
@EINVAL = global i64 0
@USHRT_MAX = global i64 0
@ERANGE = global i64 0
@stderr = global i32 0
@.str = [38 x i8] c"Failed to parse port number \22%s\22: %s\0A\00"
define signext i16 @parse_args(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = icmp slt i32 %0, 2
br i1 %4, label %32, label %5
5: ; preds = %2
store i64 0, i64* @errno
%6 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = call i64 @strtoul(i8* %8, i8** %3, i32 10)
%10 = load i64, i64* @errno
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %5
%13 = load i8*, i8** %3
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i64, i64* @USHRT_MAX
%18 = icmp ugt i64 %9, %17
br i1 %18, label %19, label %30
19: ; preds = %16, %12
%20 = phi i64* [ @EINVAL, %12 ], [ @ERANGE, %16 ]
%21 = load i64, i64* %20
store i64 %21, i64* @errno
%22 = icmp eq i64 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %5, %19
%24 = phi i64 [ %21, %19 ], [ %10, %5 ]
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %7
%27 = tail call i8* @strerror(i64 %24)
%28 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %27)
%29 = tail call i32 (...) @abort()
unreachable
30: ; preds = %16, %19
%31 = trunc i64 %9 to i16
br label %32
32: ; preds = %2, %30
%33 = phi i16 [ %31, %30 ], [ 8080, %2 ]
ret i16 %33
}