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

/* Variables and functions */
int /*<<< orphan*/ endservent();
struct servent *getservent();
int /*<<< orphan*/ setservent(int);
scalar_t__ strcmp(char const *, char *);
struct servent *getservbyname(const char *name, const char *proto) {
  struct servent *se;
  char **aliases;
  if ((name == NULL) || (proto == NULL)) {
    return NULL;
  }
  setservent(1);
  while ((se = getservent()) != NULL) {
    if (strcmp(proto, se->s_proto) != 0) {
      continue;
    }
    if (strcmp(name, se->s_name) == 0) {
      break;
    }
    for (aliases = se->s_aliases; *aliases != NULL; ++aliases) {
      if (strcmp(name, *aliases) == 0) {
        break;
      }
    }
    if (*aliases != NULL) {
      break;
    }
  }
  endservent();
  return se;
}


x86 assembly (clang, -O0):
<getservbyname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1166 <getservbyname+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1173 <getservbyname+0x33>
movq   $0x0,-0x8(%rbp)
jmp    1247 <getservbyname+0x107>
mov    $0x1,%edi
call   1040 <setservent@plt>
mov    $0x0,%al
call   1030 <getservent@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1238 <getservbyname+0xf8>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11b1 <getservbyname+0x71>
jmp    117d <getservbyname+0x3d>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11d1 <getservbyname+0x91>
jmp    1238 <getservbyname+0xf8>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
je     1220 <getservbyname+0xe0>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    120a <getservbyname+0xca>
jmp    1220 <getservbyname+0xe0>
jmp    120f <getservbyname+0xcf>
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
jmp    11dd <getservbyname+0x9d>
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
je     1233 <getservbyname+0xf3>
jmp    1238 <getservbyname+0xf8>
jmp    117d <getservbyname+0x3d>
mov    $0x0,%al
call   1060 <endservent@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getservbyname>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %r12d,%r12d
test   %rdi,%rdi
je     1205 <getservbyname+0xc5>
mov    %rsi,%r14
test   %rsi,%rsi
je     1205 <getservbyname+0xc5>
mov    %rdi,%r15
mov    $0x1,%edi
call   1040 <setservent@plt>
xor    %r12d,%r12d
xor    %eax,%eax
call   1030 <getservent@plt>
test   %rax,%rax
je     11fe <getservbyname+0xbe>
mov    %rax,%r12
jmp    119f <getservbyname+0x5f>
cmpq   $0x0,-0x8(%rbx)
jne    11fe <getservbyname+0xbe>
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <getservent@plt>
mov    %rax,%r12
test   %rax,%rax
je     11fb <getservbyname+0xbb>
mov    (%r12),%rsi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1190 <getservbyname+0x50>
mov    0x8(%r12),%rsi
mov    %r15,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11fe <getservbyname+0xbe>
mov    0x10(%r12),%rbx
mov    (%rbx),%rsi
test   %rsi,%rsi
je     1190 <getservbyname+0x50>
add    $0x8,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1185 <getservbyname+0x45>
mov    (%rbx),%rsi
add    $0x8,%rbx
test   %rsi,%rsi
jne    11e0 <getservbyname+0xa0>
jmp    1190 <getservbyname+0x50>
xor    %r12d,%r12d
xor    %eax,%eax
call   1060 <endservent@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.servent = type { i8*, i8*, i8** }
define %struct.servent* @getservbyname(i8* %0, i8* %1) {
%3 = alloca %struct.servent*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.servent*
%7 = alloca i8**
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %10, %2
store %struct.servent* null, %struct.servent** %3
br label %63
14: ; preds = %10
%15 = call i32 @setservent(i32 1)
br label %16
16: ; preds = %59, %26, %14
%17 = call %struct.servent* (...) @getservent()
store %struct.servent* %17, %struct.servent** %6
%18 = icmp ne %struct.servent* %17, null
br i1 %18, label %19, label %60
19: ; preds = %16
%20 = load i8*, i8** %5
%21 = load %struct.servent*, %struct.servent** %6
%22 = getelementptr inbounds %struct.servent, %struct.servent* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i64 @strcmp(i8* %20, i8* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %19
br label %16
27: ; preds = %19
%28 = load i8*, i8** %4
%29 = load %struct.servent*, %struct.servent** %6
%30 = getelementptr inbounds %struct.servent, %struct.servent* %29, i32 0, i32 1
%31 = load i8*, i8** %30
%32 = call i64 @strcmp(i8* %28, i8* %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %27
br label %60
35: ; preds = %27
%36 = load %struct.servent*, %struct.servent** %6
%37 = getelementptr inbounds %struct.servent, %struct.servent* %36, i32 0, i32 2
%38 = load i8**, i8*** %37
store i8** %38, i8*** %7
br label %39
39: ; preds = %51, %35
%40 = load i8**, i8*** %7
%41 = load i8*, i8** %40
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %54
43: ; preds = %39
%44 = load i8*, i8** %4
%45 = load i8**, i8*** %7
%46 = load i8*, i8** %45
%47 = call i64 @strcmp(i8* %44, i8* %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %43
br label %54
50: ; preds = %43
br label %51
51: ; preds = %50
%52 = load i8**, i8*** %7
%53 = getelementptr inbounds i8*, i8** %52, i32 1
store i8** %53, i8*** %7
br label %39
54: ; preds = %49, %39
%55 = load i8**, i8*** %7
%56 = load i8*, i8** %55
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %59
58: ; preds = %54
br label %60
59: ; preds = %54
br label %16
60: ; preds = %58, %34, %16
%61 = call i32 (...) @endservent()
%62 = load %struct.servent*, %struct.servent** %6
store %struct.servent* %62, %struct.servent** %3
br label %63
63: ; preds = %60, %13
%64 = load %struct.servent*, %struct.servent** %3
ret %struct.servent* %64
}

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