#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 */
struct servent {
  scalar_t__ s_port;
};

/* Variables and functions */
int atoi(char const *);
struct servent *getservbyname(char const *, int /*<<< orphan*/ *);
scalar_t__ re_match(char const *, char *);
int validate_port(const char *str) {
  if (!str)
    return 0;
  int port;
  struct servent *s;
  if (re_match(str, "^[0-9]+$")) {
    port = atoi(str);
    if (port < 1 || port > 65535)
      return 0;
    else
      return 1;
  } else {
    s = getservbyname(str, NULL);
    if (!s)
      return 0;
    if (s->s_port)
      return 1;
    else
      return 0;
  }
}


x86 assembly (clang, -O0):
<validate_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1153 <validate_port+0x23>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <validate_port+0xc1>
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1030 <re_match@plt>
cmp    $0x0,%rax
je     11a8 <validate_port+0x78>
mov    -0x10(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jl     1190 <validate_port+0x60>
cmpl   $0xffff,-0x14(%rbp)
jle    119c <validate_port+0x6c>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <validate_port+0xc1>
movl   $0x1,-0x4(%rbp)
jmp    11f1 <validate_port+0xc1>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <getservbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d0 <validate_port+0xa0>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <validate_port+0xc1>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11ea <validate_port+0xba>
movl   $0x1,-0x4(%rbp)
jmp    11f1 <validate_port+0xc1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_port>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1163 <validate_port+0x33>
mov    %rdi,%rbx
lea    0xebd(%rip),%rsi
call   1030 <re_match@plt>
test   %rax,%rax
je     1168 <validate_port+0x38>
mov    %rbx,%rdi
call   1050 <atoi@plt>
add    $0xffffffff,%eax
cmp    $0xffff,%eax
setb   %r14b
jmp    1182 <validate_port+0x52>
xor    %r14d,%r14d
jmp    1182 <validate_port+0x52>
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <getservbyname@plt>
test   %rax,%rax
je     1182 <validate_port+0x52>
cmpq   $0x0,(%rax)
setne  %r14b
movzbl %r14b,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.servent = type { i64 }
@.str = [9 x i8] c"^[0-9]+$\00"
define i32 @validate_port(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.servent*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i32 0, i32* %2
br label %36
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @re_match(i8* %10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = call i32 @atoi(i8* %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 65535
br i1 %20, label %21, label %22
21: ; preds = %18, %13
store i32 0, i32* %2
br label %36
22: ; preds = %18
store i32 1, i32* %2
br label %36
23: ; preds = %9
%24 = load i8*, i8** %3
%25 = call %struct.servent* @getservbyname(i8* %24, i32* null)
store %struct.servent* %25, %struct.servent** %5
%26 = load %struct.servent*, %struct.servent** %5
%27 = icmp ne %struct.servent* %26, null
br i1 %27, label %29, label %28
28: ; preds = %23
store i32 0, i32* %2
br label %36
29: ; preds = %23
%30 = load %struct.servent*, %struct.servent** %5
%31 = getelementptr inbounds %struct.servent, %struct.servent* %30, i32 0, i32 0
%32 = load i64, i64* %31
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 1, i32* %2
br label %36
35: ; preds = %29
store i32 0, i32* %2
br label %36
36: ; preds = %35, %34, %28, %22, %21, %8
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.servent = type { i64 }
@.str = [9 x i8] c"^[0-9]+$\00"
define i32 @validate_port(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %17, label %3
3: ; preds = %1
%4 = tail call i64 @re_match(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i64 %4, 0
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = tail call i32 @atoi(i8* %0)
%8 = add i32 %7, -1
%9 = icmp ult i32 %8, 65535
br label %17
10: ; preds = %3
%11 = tail call %struct.servent* @getservbyname(i8* %0, i32* null)
%12 = icmp eq %struct.servent* %11, null
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.servent, %struct.servent* %11, i64 0, i32 0
%15 = load i64, i64* %14
%16 = icmp ne i64 %15, 0
br label %17
17: ; preds = %6, %10, %13, %1
%18 = phi i1 [ false, %1 ], [ %9, %6 ], [ false, %10 ], [ %16, %13 ]
%19 = zext i1 %18 to i32
ret i32 %19
}