#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 {
  scalar_t__ *s_aliases;
  scalar_t__ s_name;
  scalar_t__ s_proto;
};

/* Variables and functions */
int /*<<< orphan*/ endservent();
int /*<<< orphan*/ getservent_r(struct servent *, char *, size_t,
                                struct servent **);
scalar_t__ strcmp(scalar_t__, char const *);
extern int getservbyname_r(const char *name, const char *proto,
                           struct servent *res, char *buf, size_t buflen,
                           struct servent **res_sig) {
  while (!getservent_r(res, buf, buflen, res_sig)) {
    int i;
    if (proto && strcmp(res->s_proto, proto))
      continue;
    if (!strcmp(res->s_name, name))
      goto found;
    for (i = 0; res->s_aliases[i]; ++i)
      if (!strcmp(res->s_aliases[i], name))
        goto found;
  }
  *res_sig = 0;
found:
  endservent();
  return *res_sig ? 0 : -1;
}


x86 assembly (clang, -O0):
<getservbyname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%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    117a <getservbyname_r+0x4a>
jmp    1221 <getservbyname_r+0xf1>
cmpq   $0x0,-0x10(%rbp)
je     11a5 <getservbyname_r+0x75>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11a5 <getservbyname_r+0x75>
jmp    1150 <getservbyname_r+0x20>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11c5 <getservbyname_r+0x95>
jmp    122c <getservbyname_r+0xfc>
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x34(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     121c <getservbyname_r+0xec>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1209 <getservbyname_r+0xd9>
jmp    122c <getservbyname_r+0xfc>
jmp    120e <getservbyname_r+0xde>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11cc <getservbyname_r+0x9c>
jmp    1150 <getservbyname_r+0x20>
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    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getservbyname_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r12
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %rdi,%rbp
mov    %rdx,%rdi
mov    %rcx,%rsi
mov    %r8,(%rsp)
mov    %r8,%rdx
mov    %r9,%rcx
call   1030 <getservent_r@plt>
test   %eax,%eax
jne    11cf <getservbyname_r+0x9f>
test   %r13,%r13
je     1179 <getservbyname_r+0x49>
mov    0x10(%rbx),%rdi
mov    %r13,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    11c0 <getservbyname_r+0x90>
mov    0x8(%rbx),%rdi
mov    %rbp,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11d7 <getservbyname_r+0xa7>
mov    (%rbx),%rax
mov    (%rax),%rdi
test   %rdi,%rdi
je     11c0 <getservbyname_r+0x90>
mov    $0x8,%r14d
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11d7 <getservbyname_r+0xa7>
mov    (%rbx),%rax
mov    (%rax,%r14,1),%rdi
add    $0x8,%r14
test   %rdi,%rdi
jne    11a0 <getservbyname_r+0x70>
nopl   (%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    (%rsp),%rdx
mov    %r12,%rcx
jmp    115a <getservbyname_r+0x2a>
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 { i64*, i64, i64 }
define i32 @getservbyname_r(i8* %0, i8* %1, %struct.servent* %2, i8* %3, i64 %4, %struct.servent** %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.servent*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca %struct.servent**
%13 = alloca i32
store i8* %0, i8** %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 %14
14: ; preds = %67, %32, %6
%15 = load %struct.servent*, %struct.servent** %9
%16 = load i8*, i8** %10
%17 = load i64, i64* %11
%18 = load %struct.servent**, %struct.servent*** %12
%19 = call i32 @getservent_r(%struct.servent* %15, i8* %16, i64 %17, %struct.servent** %18)
%20 = icmp ne i32 %19, 0
%21 = xor i1 %20, true
br i1 %21, label %22, label %68
22: ; preds = %14
%23 = load i8*, i8** %8
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = load %struct.servent*, %struct.servent** %9
%27 = getelementptr inbounds %struct.servent, %struct.servent* %26, i32 0, i32 2
%28 = load i64, i64* %27
%29 = load i8*, i8** %8
%30 = call i64 @strcmp(i64 %28, i8* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %25
br label %14
33: ; preds = %25, %22
%34 = load %struct.servent*, %struct.servent** %9
%35 = getelementptr inbounds %struct.servent, %struct.servent* %34, i32 0, i32 1
%36 = load i64, i64* %35
%37 = load i8*, i8** %7
%38 = call i64 @strcmp(i64 %36, i8* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %33
br label %70
41: ; preds = %33
store i32 0, i32* %13
br label %42
42: ; preds = %64, %41
%43 = load %struct.servent*, %struct.servent** %9
%44 = getelementptr inbounds %struct.servent, %struct.servent* %43, i32 0, i32 0
%45 = load i64*, i64** %44
%46 = load i32, i32* %13
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i64, i64* %45, i64 %47
%49 = load i64, i64* %48
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %67
51: ; preds = %42
%52 = load %struct.servent*, %struct.servent** %9
%53 = getelementptr inbounds %struct.servent, %struct.servent* %52, i32 0, i32 0
%54 = load i64*, i64** %53
%55 = load i32, i32* %13
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i64, i64* %54, i64 %56
%58 = load i64, i64* %57
%59 = load i8*, i8** %7
%60 = call i64 @strcmp(i64 %58, i8* %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %63, label %62
62: ; preds = %51
br label %70
63: ; preds = %51
br label %64
64: ; preds = %63
%65 = load i32, i32* %13
%66 = add nsw i32 %65, 1
store i32 %66, i32* %13
br label %42
67: ; preds = %42
br label %14
68: ; preds = %14
%69 = load %struct.servent**, %struct.servent*** %12
store %struct.servent* null, %struct.servent** %69
br label %70
70: ; preds = %68, %62, %40
%71 = call i32 (...) @endservent()
%72 = load %struct.servent**, %struct.servent*** %12
%73 = load %struct.servent*, %struct.servent** %72
%74 = icmp ne %struct.servent* %73, null
%75 = zext i1 %74 to i64
%76 = select i1 %74, i32 0, i32 -1
ret i32 %76
}

LLVM IR (-O2):
%struct.servent = type { i64*, i64, i64 }
define i32 @getservbyname_r(i8* %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 %41
9: ; preds = %6
%10 = icmp eq i8* %1, null
%11 = getelementptr inbounds %struct.servent, %struct.servent* %2, i64 0, i32 2
%12 = getelementptr inbounds %struct.servent, %struct.servent* %2, i64 0, i32 1
%13 = getelementptr inbounds %struct.servent, %struct.servent* %2, i64 0, i32 0
br label %14
14: ; preds = %9, %38
br i1 %10, label %19, label %15
15: ; preds = %14
%16 = load i64, i64* %11
%17 = tail call i64 @strcmp(i64 %16, i8* %1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %38
19: ; preds = %15, %14
%20 = load i64, i64* %12
%21 = tail call i64 @strcmp(i64 %20, i8* %0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %42, label %23
23: ; preds = %19
%24 = load i64*, i64** %13
%25 = load i64, i64* %24
%26 = icmp eq i64 %25, 0
br i1 %26, label %38, label %32
27: ; preds = %32
%28 = load i64*, i64** %13
%29 = getelementptr inbounds i64, i64* %28, i64 %37
%30 = load i64, i64* %29
%31 = icmp eq i64 %30, 0
br i1 %31, label %38, label %32
32: ; preds = %23, %27
%33 = phi i64 [ %37, %27 ], [ 0, %23 ]
%34 = phi i64 [ %30, %27 ], [ %25, %23 ]
%35 = tail call i64 @strcmp(i64 %34, i8* %0)
%36 = icmp eq i64 %35, 0
%37 = add nuw i64 %33, 1
br i1 %36, label %42, label %27
38: ; preds = %27, %23, %15
%39 = tail call i32 @getservent_r(%struct.servent* %2, i8* %3, i64 %4, %struct.servent** %5)
%40 = icmp eq i32 %39, 0
br i1 %40, label %14, label %41
41: ; preds = %38, %6
store %struct.servent* null, %struct.servent** %5
br label %42
42: ; preds = %19, %32, %41
%43 = tail call i32 (...) @endservent()
%44 = load %struct.servent*, %struct.servent** %5
%45 = icmp eq %struct.servent* %44, null
%46 = sext i1 %45 to i32
ret i32 %46
}