#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 */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
long strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
bool parse_port(uint16_t *result, char *input, char *program_name) {
  long long_port = strtol(input, NULL, 0);
  if (long_port <= 0 || long_port > 0xFFFF) {
    fprintf(stderr, "%s: port number out of range\n", program_name);
    return false;
  }
  *result = (uint16_t)long_port;
  return true;
}


x86 assembly (clang, -O0):
<parse_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <strtol@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jle    1160 <parse_port+0x40>
cmpq   $0xffff,-0x28(%rbp)
jle    1185 <parse_port+0x65>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe8c(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1197 <parse_port+0x77>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_port>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
mov    %rsi,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <strtol@plt>
lea    -0x10000(%rax),%rcx
cmp    $0xffffffffffff0000,%rcx
ja     1163 <parse_port+0x43>
mov    0x2e90(%rip),%rax
mov    (%rax),%edi
lea    0xea7(%rip),%rsi
mov    %r14,%rdx
call   1030 <fprintf@plt>
jmp    116b <parse_port+0x4b>
mov    %rax,(%r15)
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"%s: port number out of range\0A\00"
define i32 @parse_port(i64* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
store i64* %0, i64** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = load i8*, i8** %6
%10 = call i64 @strtol(i8* %9, i32* null, i32 0)
store i64 %10, i64* %8
%11 = load i64, i64* %8
%12 = icmp sle i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load i64, i64* %8
%15 = icmp sgt i64 %14, 65535
br i1 %15, label %16, label %20
16: ; preds = %13, %3
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %7
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 0, i32* %4
br label %23
20: ; preds = %13
%21 = load i64, i64* %8
%22 = load i64*, i64** %5
store i64 %21, i64* %22
store i32 1, i32* %4
br label %23
23: ; preds = %20, %16
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"%s: port number out of range\0A\00"
define i32 @parse_port(i64* nocapture writeonly %0, i8* nocapture %1, i8* %2) {
%4 = tail call i64 @strtol(i8* nocapture %1, i32* null, i32 0)
%5 = add i64 %4, -65536
%6 = icmp ult i64 %5, -65535
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %11
10: ; preds = %3
store i64 %4, i64* %0
br label %11
11: ; preds = %10, %7
%12 = phi i32 [ 0, %7 ], [ 1, %10 ]
ret i32 %12
}