#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*/ __ebt_print_error(char *, char const *, char const *);
struct servent *getservbyname(char const *, char const *);
int ntohs(int /*<<< orphan*/);
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;
  }
  __ebt_print_error("Problem with specified %s port '%s'",
                    protocol ? protocol : "", name);
  ;
  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   1040 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11b3 <parse_port+0x73>
cmpq   $0x0,-0x10(%rbp)
je     11ae <parse_port+0x6e>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <getservbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11ae <parse_port+0x6e>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    121d <parse_port+0xdd>
jmp    11da <parse_port+0x9a>
cmpl   $0x0,-0x2c(%rbp)
jge    11ca <parse_port+0x8a>
cmpl   $0xffff,-0x2c(%rbp)
jg     11d5 <parse_port+0x95>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <parse_port+0xdd>
jmp    11da <parse_port+0x9a>
cmpq   $0x0,-0x10(%rbp)
je     11f2 <parse_port+0xb2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1202 <parse_port+0xc2>
lea    0xe2a(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    1202 <parse_port+0xc2>
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdef(%rip),%rdi
call   1060 <__ebt_print_error@plt>
movl   $0x0,-0x4(%rbp)
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   1040 <strtol@plt>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     11a1 <parse_port+0x61>
test   %r14,%r14
je     1186 <parse_port+0x46>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <getservbyname@plt>
test   %rax,%rax
je     118d <parse_port+0x4d>
mov    (%rax),%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <ntohs@plt>
lea    0xe96(%rip),%r14
lea    0xe6c(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <__ebt_print_error@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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
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 %48
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 %48
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 @__ebt_print_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %45, i8* %46)
store i32 0, i32* %3
br label %48
48: ; preds = %44, %35, %23
%49 = load i32, i32* %3
ret i32 %49
}

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
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 %21, 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 %21
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 @__ebt_print_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %1)
br label %21
21: ; preds = %2, %18, %14
%22 = phi i32 [ %17, %14 ], [ 0, %18 ], [ %5, %2 ]
ret i32 %22
}