#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 */
long strtol(char const *, char **, int);
long int parse_port(const char *string) {
  char *endptr;
  long int port = strtol(string, &endptr, 10);
  if (endptr == string || *endptr != '\0') {
    return -1;
  } else if (port < 0 || port > 1 << 16) {
    return -1;
  } else {
    return port;
  }
}


x86 assembly (clang, -O0):
<parse_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1150 <parse_port+0x40>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     115d <parse_port+0x4d>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    118b <parse_port+0x7b>
cmpq   $0x0,-0x20(%rbp)
jl     1176 <parse_port+0x66>
cmpq   $0x10000,-0x20(%rbp)
jle    1183 <parse_port+0x73>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    118b <parse_port+0x7b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_port>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,%rcx
mov    0x8(%rsp),%rdx
mov    $0xffffffffffffffff,%rax
cmp    %rbx,%rdx
je     1152 <parse_port+0x42>
cmpb   $0x0,(%rdx)
jne    1152 <parse_port+0x42>
cmp    $0x10001,%rcx
mov    $0xffffffffffffffff,%rax
cmovb  %rcx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i64 @parse_port(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @strtol(i8* %6, i8** %4, i32 10)
store i64 %7, i64* %5
%8 = load i8*, i8** %4
%9 = load i8*, i8** %3
%10 = icmp eq i8* %8, %9
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 %17
16: ; preds = %11, %1
store i64 -1, i64* %2
br label %26
17: ; preds = %11
%18 = load i64, i64* %5
%19 = icmp slt i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load i64, i64* %5
%22 = icmp sgt i64 %21, 65536
br i1 %22, label %23, label %24
23: ; preds = %20, %17
store i64 -1, i64* %2
br label %26
24: ; preds = %20
%25 = load i64, i64* %5
store i64 %25, i64* %2
br label %26
26: ; preds = %24, %23, %16
%27 = load i64, i64* %2
ret i64 %27
}

LLVM IR (-O2):
define i64 @parse_port(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = call i64 @strtol(i8* %0, i8** %2, i32 10)
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, %0
br i1 %6, label %13, label %7
7: ; preds = %1
%8 = load i8, i8* %5
%9 = icmp eq i8 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = icmp ugt i64 %4, 65536
%12 = select i1 %11, i64 -1, i64 %4
br label %13
13: ; preds = %10, %1, %7
%14 = phi i64 [ -1, %7 ], [ -1, %1 ], [ %12, %10 ]
ret i64 %14
}