#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ endservent();
struct servent *getservbyname(char *, char *);
struct servent *getservbyport(int /*<<< orphan*/, char *);
struct servent *getservent();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ output_servent(char *, struct servent *);
int /*<<< orphan*/ setservent(int /*<<< orphan*/);
void test_services(void) {
  struct servent *sptr;
  sptr = getservbyname("domain", "tcp");
  output_servent("getservbyname (\"domain\", \"tcp\")", sptr);
  sptr = getservbyname("domain", "udp");
  output_servent("getservbyname (\"domain\", \"udp\")", sptr);
  sptr = getservbyname("domain", NULL);
  output_servent("getservbyname (\"domain\", NULL)", sptr);
  sptr = getservbyname("not-existant", NULL);
  output_servent("getservbyname (\"not-existant\", NULL)", sptr);
  sptr = getservbyname("", "");
  output_servent("getservbyname (\"\", \"\")", sptr);
  sptr = getservbyname("", "tcp");
  output_servent("getservbyname (\"\", \"tcp\")", sptr);
  sptr = getservbyport(htons(53), "tcp");
  output_servent("getservbyport (htons(53), \"tcp\")", sptr);
  sptr = getservbyport(htons(53), NULL);
  output_servent("getservbyport (htons(53), NULL)", sptr);
  sptr = getservbyport(htons(1), "udp");
  output_servent("getservbyport (htons(1), \"udp\")", sptr);
  setservent(0);
  do {
    sptr = getservent();
    output_servent("getservent ()", sptr);
  } while (sptr != NULL);
  endservent();
}


x86 assembly (clang, -O0):
<test_services>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe81(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1050 <getservbyname@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe71(%rip),%rdi
call   1070 <output_servent@plt>
lea    0xe5a(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1050 <getservbyname@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe6e(%rip),%rdi
call   1070 <output_servent@plt>
lea    0xe33(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <getservbyname@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe6a(%rip),%rdi
call   1070 <output_servent@plt>
lea    0xe7d(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <getservbyname@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe72(%rip),%rdi
call   1070 <output_servent@plt>
lea    0xea1(%rip),%rsi
mov    %rsi,%rdi
call   1050 <getservbyname@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe74(%rip),%rdi
call   1070 <output_servent@plt>
lea    0xe7e(%rip),%rdi
lea    0xdc8(%rip),%rsi
call   1050 <getservbyname@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe64(%rip),%rdi
call   1070 <output_servent@plt>
mov    $0x35,%edi
call   1040 <htons@plt>
mov    %eax,%edi
lea    0xd9c(%rip),%rsi
call   1090 <getservbyport@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe52(%rip),%rdi
call   1070 <output_servent@plt>
mov    $0x35,%edi
call   1040 <htons@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1090 <getservbyport@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe4a(%rip),%rdi
call   1070 <output_servent@plt>
mov    $0x1,%edi
call   1040 <htons@plt>
mov    %eax,%edi
lea    0xd6b(%rip),%rsi
call   1090 <getservbyport@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe3e(%rip),%rdi
call   1070 <output_servent@plt>
xor    %edi,%edi
call   1060 <setservent@plt>
mov    $0x0,%al
call   1030 <getservent@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe3c(%rip),%rdi
call   1070 <output_servent@plt>
cmpq   $0x0,-0x8(%rbp)
jne    12e0 <test_services+0x170>
mov    $0x0,%al
call   1080 <endservent@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_services>:
push   %r15
push   %r14
push   %rbx
lea    0xe84(%rip),%rbx
lea    0xe84(%rip),%r15
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <getservbyname@plt>
lea    0xe76(%rip),%rdi
mov    %rax,%rsi
call   1070 <output_servent@plt>
lea    0xe87(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <getservbyname@plt>
lea    0xe79(%rip),%rdi
mov    %rax,%rsi
call   1070 <output_servent@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <getservbyname@plt>
lea    0xe80(%rip),%rdi
mov    %rax,%rsi
call   1070 <output_servent@plt>
lea    0xe90(%rip),%rdi
xor    %esi,%esi
call   1050 <getservbyname@plt>
lea    0xe8f(%rip),%rdi
mov    %rax,%rsi
call   1070 <output_servent@plt>
lea    0xebb(%rip),%rbx
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1050 <getservbyname@plt>
lea    0xe93(%rip),%rdi
mov    %rax,%rsi
call   1070 <output_servent@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <getservbyname@plt>
lea    0xe90(%rip),%rdi
mov    %rax,%rsi
call   1070 <output_servent@plt>
mov    $0x35,%edi
call   1040 <htons@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   1090 <getservbyport@plt>
lea    0xe87(%rip),%rdi
mov    %rax,%rsi
call   1070 <output_servent@plt>
mov    $0x35,%edi
call   1040 <htons@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1090 <getservbyport@plt>
lea    0xe86(%rip),%rdi
mov    %rax,%rsi
call   1070 <output_servent@plt>
mov    $0x1,%edi
call   1040 <htons@plt>
mov    %eax,%edi
mov    %r14,%rsi
call   1090 <getservbyport@plt>
lea    0xe83(%rip),%rdi
mov    %rax,%rsi
call   1070 <output_servent@plt>
xor    %edi,%edi
call   1060 <setservent@plt>
lea    0xe8d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1030 <getservent@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1070 <output_servent@plt>
test   %rbx,%rbx
jne    12b0 <test_services+0x140>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1080 <endservent@plt>

LLVM IR (-O0):
%struct.servent = type { i32 }
@.str = [7 x i8] c"domain\00"
@.str.1 = [4 x i8] c"tcp\00"
@.str.2 = [32 x i8] c"getservbyname (\22domain\22, \22tcp\22)\00"
@.str.3 = [4 x i8] c"udp\00"
@.str.4 = [32 x i8] c"getservbyname (\22domain\22, \22udp\22)\00"
@.str.5 = [31 x i8] c"getservbyname (\22domain\22, NULL)\00"
@.str.6 = [13 x i8] c"not-existant\00"
@.str.7 = [37 x i8] c"getservbyname (\22not-existant\22, NULL)\00"
@.str.8 = [1 x i8] zeroinitializer
@.str.9 = [23 x i8] c"getservbyname (\22\22, \22\22)\00"
@.str.10 = [26 x i8] c"getservbyname (\22\22, \22tcp\22)\00"
@.str.11 = [33 x i8] c"getservbyport (htons(53), \22tcp\22)\00"
@.str.12 = [32 x i8] c"getservbyport (htons(53), NULL)\00"
@.str.13 = [32 x i8] c"getservbyport (htons(1), \22udp\22)\00"
@.str.14 = [14 x i8] c"getservent ()\00"
define void @test_services() {
%1 = alloca %struct.servent*
%2 = call %struct.servent* @getservbyname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store %struct.servent* %2, %struct.servent** %1
%3 = load %struct.servent*, %struct.servent** %1
%4 = call i32 @output_servent(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), %struct.servent* %3)
%5 = call %struct.servent* @getservbyname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store %struct.servent* %5, %struct.servent** %1
%6 = load %struct.servent*, %struct.servent** %1
%7 = call i32 @output_servent(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), %struct.servent* %6)
%8 = call %struct.servent* @getservbyname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* null)
store %struct.servent* %8, %struct.servent** %1
%9 = load %struct.servent*, %struct.servent** %1
%10 = call i32 @output_servent(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), %struct.servent* %9)
%11 = call %struct.servent* @getservbyname(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* null)
store %struct.servent* %11, %struct.servent** %1
%12 = load %struct.servent*, %struct.servent** %1
%13 = call i32 @output_servent(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), %struct.servent* %12)
%14 = call %struct.servent* @getservbyname(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
store %struct.servent* %14, %struct.servent** %1
%15 = load %struct.servent*, %struct.servent** %1
%16 = call i32 @output_servent(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0), %struct.servent* %15)
%17 = call %struct.servent* @getservbyname(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store %struct.servent* %17, %struct.servent** %1
%18 = load %struct.servent*, %struct.servent** %1
%19 = call i32 @output_servent(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0), %struct.servent* %18)
%20 = call i32 @htons(i32 53)
%21 = call %struct.servent* @getservbyport(i32 %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store %struct.servent* %21, %struct.servent** %1
%22 = load %struct.servent*, %struct.servent** %1
%23 = call i32 @output_servent(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.11, i64 0, i64 0), %struct.servent* %22)
%24 = call i32 @htons(i32 53)
%25 = call %struct.servent* @getservbyport(i32 %24, i8* null)
store %struct.servent* %25, %struct.servent** %1
%26 = load %struct.servent*, %struct.servent** %1
%27 = call i32 @output_servent(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.12, i64 0, i64 0), %struct.servent* %26)
%28 = call i32 @htons(i32 1)
%29 = call %struct.servent* @getservbyport(i32 %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store %struct.servent* %29, %struct.servent** %1
%30 = load %struct.servent*, %struct.servent** %1
%31 = call i32 @output_servent(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.13, i64 0, i64 0), %struct.servent* %30)
%32 = call i32 @setservent(i32 0)
br label %33
33: ; preds = %37, %0
%34 = call %struct.servent* (...) @getservent()
store %struct.servent* %34, %struct.servent** %1
%35 = load %struct.servent*, %struct.servent** %1
%36 = call i32 @output_servent(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.14, i64 0, i64 0), %struct.servent* %35)
br label %37
37: ; preds = %33
%38 = load %struct.servent*, %struct.servent** %1
%39 = icmp ne %struct.servent* %38, null
br i1 %39, label %33, label %40
40: ; preds = %37
%41 = call i32 (...) @endservent()
ret void
}

LLVM IR (-O2):
%struct.servent = type { i32 }
@.str = [7 x i8] c"domain\00"
@.str.1 = [4 x i8] c"tcp\00"
@.str.2 = [32 x i8] c"getservbyname (\22domain\22, \22tcp\22)\00"
@.str.3 = [4 x i8] c"udp\00"
@.str.4 = [32 x i8] c"getservbyname (\22domain\22, \22udp\22)\00"
@.str.5 = [31 x i8] c"getservbyname (\22domain\22, NULL)\00"
@.str.6 = [13 x i8] c"not-existant\00"
@.str.7 = [37 x i8] c"getservbyname (\22not-existant\22, NULL)\00"
@.str.8 = [1 x i8] zeroinitializer
@.str.9 = [23 x i8] c"getservbyname (\22\22, \22\22)\00"
@.str.10 = [26 x i8] c"getservbyname (\22\22, \22tcp\22)\00"
@.str.11 = [33 x i8] c"getservbyport (htons(53), \22tcp\22)\00"
@.str.12 = [32 x i8] c"getservbyport (htons(53), NULL)\00"
@.str.13 = [32 x i8] c"getservbyport (htons(1), \22udp\22)\00"
@.str.14 = [14 x i8] c"getservent ()\00"
define void @test_services() {
%1 = tail call %struct.servent* @getservbyname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i32 @output_servent(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), %struct.servent* %1)
%3 = tail call %struct.servent* @getservbyname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%4 = tail call i32 @output_servent(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), %struct.servent* %3)
%5 = tail call %struct.servent* @getservbyname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* null)
%6 = tail call i32 @output_servent(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), %struct.servent* %5)
%7 = tail call %struct.servent* @getservbyname(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* null)
%8 = tail call i32 @output_servent(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), %struct.servent* %7)
%9 = tail call %struct.servent* @getservbyname(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
%10 = tail call i32 @output_servent(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0), %struct.servent* %9)
%11 = tail call %struct.servent* @getservbyname(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @output_servent(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0), %struct.servent* %11)
%13 = tail call i32 @htons(i32 53)
%14 = tail call %struct.servent* @getservbyport(i32 %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @output_servent(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.11, i64 0, i64 0), %struct.servent* %14)
%16 = tail call i32 @htons(i32 53)
%17 = tail call %struct.servent* @getservbyport(i32 %16, i8* null)
%18 = tail call i32 @output_servent(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.12, i64 0, i64 0), %struct.servent* %17)
%19 = tail call i32 @htons(i32 1)
%20 = tail call %struct.servent* @getservbyport(i32 %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%21 = tail call i32 @output_servent(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.13, i64 0, i64 0), %struct.servent* %20)
%22 = tail call i32 @setservent(i32 0)
br label %23
23: ; preds = %23, %0
%24 = tail call %struct.servent* (...) @getservent()
%25 = tail call i32 @output_servent(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.14, i64 0, i64 0), %struct.servent* %24)
%26 = icmp eq %struct.servent* %24, null
br i1 %26, label %27, label %23
27: ; preds = %23
%28 = tail call i32 (...) @endservent()
ret void
}