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

/* Variables and functions */
int IPPROTO_TCP;
struct servent *getservbyname(char const *, char *);
void *ntohs(int);
int pcap_nametoproto(int /*<<< orphan*/);
int pcap_nametoport(const char *name, int *port, int *proto) {
  struct servent *sp;
  char *other;
  sp = getservbyname(name, (char *)0);
  if (sp != NULL) {
    (sp->s_port) = ntohs(sp->s_port);
    *port = sp->s_port;
    *proto = pcap_nametoproto(sp->s_proto);
    if (*proto == IPPROTO_TCP)
      other = "udp";
    else
      other = "tcp";
    sp = getservbyname(name, other);
    if (sp != 0) {
      (sp->s_port) = ntohs(sp->s_port);
      *proto = -1;
    }
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<pcap_nametoport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <getservbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1208 <pcap_nametoport+0xd8>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <pcap_nametoproto@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%eax
jne    11b8 <pcap_nametoport+0x88>
lea    0xe51(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11c3 <pcap_nametoport+0x93>
lea    0xe45(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <getservbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11fc <pcap_nametoport+0xcc>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
movl   $0xffffffff,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    120f <pcap_nametoport+0xdf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pcap_nametoport>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
xor    %r13d,%r13d
xor    %esi,%esi
call   1030 <getservbyname@plt>
test   %rax,%rax
je     11ad <pcap_nametoport+0x7d>
mov    %rax,%rbx
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,(%rbx)
mov    %eax,(%r12)
mov    0x4(%rbx),%edi
call   1040 <pcap_nametoproto@plt>
mov    %eax,(%r14)
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%eax
lea    0xe84(%rip),%rax
lea    0xe81(%rip),%rsi
cmove  %rax,%rsi
mov    %r15,%rdi
call   1030 <getservbyname@plt>
mov    $0x1,%r13d
test   %rax,%rax
je     11ad <pcap_nametoport+0x7d>
mov    %rax,%rbx
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,(%rbx)
movl   $0xffffffff,(%r14)
mov    %r13d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.servent = type { i32, i32 }
@IPPROTO_TCP = global i32 0
@.str = [4 x i8] c"udp\00"
@.str.1 = [4 x i8] c"tcp\00"
define i32 @pcap_nametoport(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.servent*
%9 = alloca i8*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%10 = load i8*, i8** %5
%11 = call %struct.servent* @getservbyname(i8* %10, i8* null)
store %struct.servent* %11, %struct.servent** %8
%12 = load %struct.servent*, %struct.servent** %8
%13 = icmp ne %struct.servent* %12, null
br i1 %13, label %14, label %53
14: ; preds = %3
%15 = load %struct.servent*, %struct.servent** %8
%16 = getelementptr inbounds %struct.servent, %struct.servent* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i8* @ntohs(i32 %17)
%19 = ptrtoint i8* %18 to i32
%20 = load %struct.servent*, %struct.servent** %8
%21 = getelementptr inbounds %struct.servent, %struct.servent* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = load %struct.servent*, %struct.servent** %8
%23 = getelementptr inbounds %struct.servent, %struct.servent* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32*, i32** %6
store i32 %24, i32* %25
%26 = load %struct.servent*, %struct.servent** %8
%27 = getelementptr inbounds %struct.servent, %struct.servent* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 @pcap_nametoproto(i32 %28)
%30 = load i32*, i32** %7
store i32 %29, i32* %30
%31 = load i32*, i32** %7
%32 = load i32, i32* %31
%33 = load i32, i32* @IPPROTO_TCP
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %14
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %9
br label %37
36: ; preds = %14
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %9
br label %37
37: ; preds = %36, %35
%38 = load i8*, i8** %5
%39 = load i8*, i8** %9
%40 = call %struct.servent* @getservbyname(i8* %38, i8* %39)
store %struct.servent* %40, %struct.servent** %8
%41 = load %struct.servent*, %struct.servent** %8
%42 = icmp ne %struct.servent* %41, null
br i1 %42, label %43, label %52
43: ; preds = %37
%44 = load %struct.servent*, %struct.servent** %8
%45 = getelementptr inbounds %struct.servent, %struct.servent* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i8* @ntohs(i32 %46)
%48 = ptrtoint i8* %47 to i32
%49 = load %struct.servent*, %struct.servent** %8
%50 = getelementptr inbounds %struct.servent, %struct.servent* %49, i32 0, i32 0
store i32 %48, i32* %50
%51 = load i32*, i32** %7
store i32 -1, i32* %51
br label %52
52: ; preds = %43, %37
store i32 1, i32* %4
br label %54
53: ; preds = %3
store i32 0, i32* %4
br label %54
54: ; preds = %53, %52
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
%struct.servent = type { i32, i32 }
@IPPROTO_TCP = global i32 0
@.str = [4 x i8] c"udp\00"
@.str.1 = [4 x i8] c"tcp\00"
define i32 @pcap_nametoport(i8* %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = tail call %struct.servent* @getservbyname(i8* %0, i8* null)
%5 = icmp eq %struct.servent* %4, null
br i1 %5, label %26, label %6
6: ; preds = %3
%7 = getelementptr inbounds %struct.servent, %struct.servent* %4, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i8* @ntohs(i32 %8)
%10 = ptrtoint i8* %9 to i64
%11 = trunc i64 %10 to i32
store i32 %11, i32* %7
store i32 %11, i32* %1
%12 = getelementptr inbounds %struct.servent, %struct.servent* %4, i64 0, i32 1
%13 = load i32, i32* %12
%14 = tail call i32 @pcap_nametoproto(i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* @IPPROTO_TCP
%16 = icmp eq i32 %14, %15
%17 = select i1 %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
%18 = tail call %struct.servent* @getservbyname(i8* %0, i8* %17)
%19 = icmp eq %struct.servent* %18, null
br i1 %19, label %26, label %20
20: ; preds = %6
%21 = getelementptr inbounds %struct.servent, %struct.servent* %18, i64 0, i32 0
%22 = load i32, i32* %21
%23 = tail call i8* @ntohs(i32 %22)
%24 = ptrtoint i8* %23 to i64
%25 = trunc i64 %24 to i32
store i32 %25, i32* %21
store i32 -1, i32* %2
br label %26
26: ; preds = %3, %6, %20
%27 = phi i32 [ 1, %20 ], [ 1, %6 ], [ 0, %3 ]
ret i32 %27
}