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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
struct servent *getservbyname(char const *, char const *);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int strtol(char const *, char **, int);
uint16_t parse_port(const char *protocol, const char *name) {
  struct servent *service;
  char *end;
  int port;
  port = strtol(name, &end, 10);
  if (*end != '\0') {
    if (protocol && (service = getservbyname(name, protocol)) != NULL)
      return ntohs(service->s_port);
  } else if (port >= 0 || port <= 0xFFFF) {
    return port;
  }
  {
    printf("Problem with specified %s port '%s'", protocol ? protocol : "",
           name);
    printf(".\n");
    exit(-1);
  };
  return 0;
}


x86 assembly (clang, -O0):
<parse_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c3 <parse_port+0x73>
cmpq   $0x0,-0x10(%rbp)
je     11be <parse_port+0x6e>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11be <parse_port+0x6e>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    1240 <parse_port+0xf0>
jmp    11ea <parse_port+0x9a>
cmpl   $0x0,-0x2c(%rbp)
jge    11da <parse_port+0x8a>
cmpl   $0xffff,-0x2c(%rbp)
jg     11e5 <parse_port+0x95>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <parse_port+0xf0>
jmp    11ea <parse_port+0x9a>
cmpq   $0x0,-0x10(%rbp)
je     1202 <parse_port+0xb2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1212 <parse_port+0xc2>
lea    0xe1d(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    1212 <parse_port+0xc2>
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_port>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     11a6 <parse_port+0x46>
test   %r14,%r14
je     11ae <parse_port+0x4e>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <getservbyname@plt>
test   %rax,%rax
je     11b5 <parse_port+0x55>
mov    (%rax),%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <ntohs@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6e(%rip),%r14
lea    0xe44(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.servent = type { i32 }
@.str = [36 x i8] c"Problem with specified %s port '%s'\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [3 x i8] c".\0A\00"
define i32 @parse_port(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.servent*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = call i32 @strtol(i8* %9, i8** %7, i32 10)
store i32 %10, i32* %8
%11 = load i8*, i8** %7
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %29
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %28
18: ; preds = %15
%19 = load i8*, i8** %5
%20 = load i8*, i8** %4
%21 = call %struct.servent* @getservbyname(i8* %19, i8* %20)
store %struct.servent* %21, %struct.servent** %6
%22 = icmp ne %struct.servent* %21, null
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load %struct.servent*, %struct.servent** %6
%25 = getelementptr inbounds %struct.servent, %struct.servent* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @ntohs(i32 %26)
store i32 %27, i32* %3
br label %50
28: ; preds = %18, %15
br label %38
29: ; preds = %2
%30 = load i32, i32* %8
%31 = icmp sge i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load i32, i32* %8
%34 = icmp sle i32 %33, 65535
br i1 %34, label %35, label %37
35: ; preds = %32, %29
%36 = load i32, i32* %8
store i32 %36, i32* %3
br label %50
37: ; preds = %32
br label %38
38: ; preds = %37, %28
%39 = load i8*, i8** %4
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = load i8*, i8** %4
br label %44
43: ; preds = %38
br label %44
44: ; preds = %43, %41
%45 = phi i8* [ %42, %41 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %43 ]
%46 = load i8*, i8** %5
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %45, i8* %46)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @exit(i32 -1)
unreachable
50: ; preds = %35, %23
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.servent = type { i32 }
@.str = [36 x i8] c"Problem with specified %s port '%s'\00"
@.str.1 = [1 x i8] zeroinitializer
@str = [2 x i8] c".\00"
define i32 @parse_port(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i32 @strtol(i8* %1, i8** %3, i32 10)
%6 = load i8*, i8** %3
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %2
%10 = icmp eq i8* %0, null
br i1 %10, label %18, label %11
11: ; preds = %9
%12 = tail call %struct.servent* @getservbyname(i8* %1, i8* %0)
%13 = icmp eq %struct.servent* %12, null
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.servent, %struct.servent* %12, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i32 @ntohs(i32 %16)
br label %23
18: ; preds = %9, %11
%19 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %9 ], [ %0, %11 ]
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %1)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %2, %14
%24 = phi i32 [ %17, %14 ], [ %5, %2 ]
ret i32 %24
}