#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 /*<<< orphan*/ endservent();
struct servent *getservent();
int htons(unsigned short);
int /*<<< orphan*/ setservent(int);
scalar_t__ strcmp(char const *, int /*<<< orphan*/);
struct servent *getservbyport(int port, const char *proto) {
  struct servent *se;
  if (proto == NULL) {
    return NULL;
  }
  setservent(1);
  port = htons((unsigned short)port);
  while ((se = getservent()) != NULL) {
    if (strcmp(proto, se->s_proto) != 0) {
      continue;
    }
    if (se->s_port == port) {
      break;
    }
  }
  endservent();
  return se;
}


x86 assembly (clang, -O0):
<getservbyport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1177 <getservbyport+0x27>
movq   $0x0,-0x8(%rbp)
jmp    11eb <getservbyport+0x9b>
mov    $0x1,%edi
call   1050 <setservent@plt>
mov    -0xc(%rbp),%eax
movzwl %ax,%edi
call   1040 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getservent@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11dc <getservbyport+0x8c>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11c3 <getservbyport+0x73>
jmp    118f <getservbyport+0x3f>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    -0xc(%rbp),%eax
jne    11d7 <getservbyport+0x87>
jmp    11dc <getservbyport+0x8c>
jmp    118f <getservbyport+0x3f>
mov    $0x0,%al
call   1070 <endservent@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getservbyport>:
push   %rbp
push   %r14
push   %rbx
test   %rsi,%rsi
je     11b5 <getservbyport+0x65>
mov    %rsi,%r14
mov    %edi,%ebx
mov    $0x1,%edi
call   1050 <setservent@plt>
movzwl %bx,%edi
call   1040 <htons@plt>
mov    %eax,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getservent@plt>
test   %rax,%rax
je     11bb <getservbyport+0x6b>
mov    %rax,%rbx
jmp    119f <getservbyport+0x4f>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1030 <getservent@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11b9 <getservbyport+0x69>
mov    0x4(%rbx),%esi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1190 <getservbyport+0x40>
cmp    %ebp,(%rbx)
jne    1190 <getservbyport+0x40>
jmp    11bb <getservbyport+0x6b>
xor    %ebx,%ebx
jmp    11c2 <getservbyport+0x72>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <endservent@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.servent = type { i32, i32 }
define %struct.servent* @getservbyport(i32 %0, i8* %1) {
%3 = alloca %struct.servent*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.servent*
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %2
store %struct.servent* null, %struct.servent** %3
br label %37
10: ; preds = %2
%11 = call i32 @setservent(i32 1)
%12 = load i32, i32* %4
%13 = trunc i32 %12 to i16
%14 = call i32 @htons(i16 zeroext %13)
store i32 %14, i32* %4
br label %15
15: ; preds = %33, %25, %10
%16 = call %struct.servent* (...) @getservent()
store %struct.servent* %16, %struct.servent** %6
%17 = icmp ne %struct.servent* %16, null
br i1 %17, label %18, label %34
18: ; preds = %15
%19 = load i8*, i8** %5
%20 = load %struct.servent*, %struct.servent** %6
%21 = getelementptr inbounds %struct.servent, %struct.servent* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = call i64 @strcmp(i8* %19, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %18
br label %15
26: ; preds = %18
%27 = load %struct.servent*, %struct.servent** %6
%28 = getelementptr inbounds %struct.servent, %struct.servent* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load i32, i32* %4
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %26
br label %34
33: ; preds = %26
br label %15
34: ; preds = %32, %15
%35 = call i32 (...) @endservent()
%36 = load %struct.servent*, %struct.servent** %6
store %struct.servent* %36, %struct.servent** %3
br label %37
37: ; preds = %34, %9
%38 = load %struct.servent*, %struct.servent** %3
ret %struct.servent* %38
}

LLVM IR (-O2):
%struct.servent = type { i32, i32 }
define %struct.servent* @getservbyport(i32 %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %26, label %4
4: ; preds = %2
%5 = tail call i32 @setservent(i32 1)
%6 = trunc i32 %0 to i16
%7 = tail call i32 @htons(i16 zeroext %6)
%8 = tail call %struct.servent* (...) @getservent()
%9 = icmp eq %struct.servent* %8, null
br i1 %9, label %23, label %10
10: ; preds = %4, %16
%11 = phi %struct.servent* [ %17, %16 ], [ %8, %4 ]
%12 = getelementptr inbounds %struct.servent, %struct.servent* %11, i64 0, i32 1
%13 = load i32, i32* %12
%14 = tail call i64 @strcmp(i8* %1, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %10, %19
%17 = tail call %struct.servent* (...) @getservent()
%18 = icmp eq %struct.servent* %17, null
br i1 %18, label %23, label %10
19: ; preds = %10
%20 = getelementptr inbounds %struct.servent, %struct.servent* %11, i64 0, i32 0
%21 = load i32, i32* %20
%22 = icmp eq i32 %21, %7
br i1 %22, label %23, label %16
23: ; preds = %16, %19, %4
%24 = phi %struct.servent* [ null, %4 ], [ %11, %19 ], [ null, %16 ]
%25 = tail call i32 (...) @endservent()
br label %26
26: ; preds = %2, %23
%27 = phi %struct.servent* [ %24, %23 ], [ null, %2 ]
ret %struct.servent* %27
}