#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();
int /*<<< orphan*/ getservent_r(struct servent *, char *, size_t,
                                struct servent **);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char const *);
extern int getservbyport_r(int port, const char *proto, struct servent *res,
                           char *buf, size_t buflen, struct servent **res_sig) {
  while (!getservent_r(res, buf, buflen, res_sig))
    if (port == res->s_port && !strcmp(res->s_proto, proto))
      goto ok;
  *res_sig = 0;
ok:
  endservent();
  return *res_sig ? 0 : -1;
}


x86 assembly (clang, -O0):
<getservbyport_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1030 <getservent_r@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1179 <getservbyport_r+0x49>
jmp    11ab <getservbyport_r+0x7b>
mov    -0x4(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jne    11a6 <getservbyport_r+0x76>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11a6 <getservbyport_r+0x76>
jmp    11b6 <getservbyport_r+0x86>
jmp    114f <getservbyport_r+0x1f>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1050 <endservent@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getservbyport_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r12
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %edi,%ebp
mov    %rdx,%rdi
mov    %rcx,%rsi
mov    %r8,%rdx
mov    %r9,%rcx
jmp    116c <getservbyport_r+0x3c>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r12,%rcx
call   1030 <getservent_r@plt>
test   %eax,%eax
jne    118a <getservbyport_r+0x5a>
cmp    %ebp,(%rbx)
jne    1160 <getservbyport_r+0x30>
mov    0x4(%rbx),%edi
mov    %r13,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
jne    1160 <getservbyport_r+0x30>
jmp    1192 <getservbyport_r+0x62>
movq   $0x0,(%r12)
xor    %eax,%eax
call   1050 <endservent@plt>
cmpq   $0x1,(%r12)
sbb    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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