#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_name;
  int s_port;
  char *s_proto;
  char **s_aliases;
};

/* Variables and functions */
int /*<<< orphan*/ endservent();
struct servent *getservent();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ setservent(int);
int _getservent(char const *progname) {
  struct servent *se;
  unsigned int i;
  char *const *p;
  printf("%s: Testing getservent()\n", progname);
  setservent(1);
  for (i = 0; (se = getservent()) != NULL; i++) {
    printf("%s\t%d/%s", se->s_name, se->s_port, se->s_proto);
    if (se->s_aliases != NULL)
      for (p = se->s_aliases; *p != NULL; p++)
        printf(" %s", *p);
    putchar('\n');
  }
  endservent();
  printf("%u services listed\n", i);
  return 0;
}


x86 assembly (clang, -O0):
<_getservent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1060 <setservent@plt>
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getservent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1224 <_getservent+0xd4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     120c <_getservent+0xbc>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     1207 <_getservent+0xb7>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    11d3 <_getservent+0x83>
jmp    120c <_getservent+0xbc>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117f <_getservent+0x2f>
mov    $0x0,%al
call   1070 <endservent@plt>
mov    -0x14(%rbp),%esi
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_getservent>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rsi
lea    0xe9f(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1060 <setservent@plt>
xor    %eax,%eax
call   1030 <getservent@plt>
test   %rax,%rax
je     1209 <_getservent+0xb9>
mov    %rax,%rbp
lea    0xe8b(%rip),%r14
lea    0xe8d(%rip),%rbx
xor    %r15d,%r15d
jmp    11bd <_getservent+0x6d>
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1040 <putchar@plt>
add    $0x1,%r15d
xor    %eax,%eax
call   1030 <getservent@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1209 <_getservent+0xb9>
mov    0x0(%rbp),%rsi
mov    0x8(%rbp),%edx
mov    0x10(%rbp),%rcx
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     11a0 <_getservent+0x50>
mov    0x0(%rbp),%rsi
test   %rsi,%rsi
je     11a0 <_getservent+0x50>
add    $0x8,%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%rbp),%rsi
add    $0x8,%rbp
test   %rsi,%rsi
jne    11f0 <_getservent+0xa0>
jmp    11a0 <_getservent+0x50>
xor    %eax,%eax
call   1070 <endservent@plt>
lea    0xe10(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.servent = type { i8*, i32, i8*, i8** }
@.str = [26 x i8] c"%s: Testing getservent()\0A\00"
@.str.1 = [9 x i8] c"%s\09%d/%s\00"
@.str.2 = [4 x i8] c" %s\00"
@.str.3 = [20 x i8] c"%u services listed\0A\00"
define i32 @_getservent(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.servent*
%4 = alloca i32
%5 = alloca i8**
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = call i32 @setservent(i32 1)
store i32 0, i32* %4
br label %9
9: ; preds = %45, %1
%10 = call %struct.servent* (...) @getservent()
store %struct.servent* %10, %struct.servent** %3
%11 = icmp ne %struct.servent* %10, null
br i1 %11, label %12, label %48
12: ; preds = %9
%13 = load %struct.servent*, %struct.servent** %3
%14 = getelementptr inbounds %struct.servent, %struct.servent* %13, i32 0, i32 0
%15 = load i8*, i8** %14
%16 = load %struct.servent*, %struct.servent** %3
%17 = getelementptr inbounds %struct.servent, %struct.servent* %16, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load %struct.servent*, %struct.servent** %3
%20 = getelementptr inbounds %struct.servent, %struct.servent* %19, i32 0, i32 2
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %18, i8* %21)
%23 = load %struct.servent*, %struct.servent** %3
%24 = getelementptr inbounds %struct.servent, %struct.servent* %23, i32 0, i32 3
%25 = load i8**, i8*** %24
%26 = icmp ne i8** %25, null
br i1 %26, label %27, label %43
27: ; preds = %12
%28 = load %struct.servent*, %struct.servent** %3
%29 = getelementptr inbounds %struct.servent, %struct.servent* %28, i32 0, i32 3
%30 = load i8**, i8*** %29
store i8** %30, i8*** %5
br label %31
31: ; preds = %39, %27
%32 = load i8**, i8*** %5
%33 = load i8*, i8** %32
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %42
35: ; preds = %31
%36 = load i8**, i8*** %5
%37 = load i8*, i8** %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %35
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i32 1
store i8** %41, i8*** %5
br label %31
42: ; preds = %31
br label %43
43: ; preds = %42, %12
%44 = call i32 @putchar(i8 signext 10)
br label %45
45: ; preds = %43
%46 = load i32, i32* %4
%47 = add i32 %46, 1
store i32 %47, i32* %4
br label %9
48: ; preds = %9
%49 = call i32 (...) @endservent()
%50 = load i32, i32* %4
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %50)
ret i32 0
}

LLVM IR (-O2):
%struct.servent = type { i8*, i32, i8*, i8** }
@.str = [26 x i8] c"%s: Testing getservent()\0A\00"
@.str.1 = [9 x i8] c"%s\09%d/%s\00"
@.str.2 = [4 x i8] c" %s\00"
@.str.3 = [20 x i8] c"%u services listed\0A\00"
define i32 @_getservent(i8* %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = tail call i32 @setservent(i32 1)
%4 = tail call %struct.servent* (...) @getservent()
%5 = icmp eq %struct.servent* %4, null
br i1 %5, label %34, label %6
6: ; preds = %1, %29
%7 = phi %struct.servent* [ %32, %29 ], [ %4, %1 ]
%8 = phi i32 [ %31, %29 ], [ 0, %1 ]
%9 = getelementptr inbounds %struct.servent, %struct.servent* %7, i64 0, i32 0
%10 = load i8*, i8** %9
%11 = getelementptr inbounds %struct.servent, %struct.servent* %7, i64 0, i32 1
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.servent, %struct.servent* %7, i64 0, i32 2
%14 = load i8*, i8** %13
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %10, i32 %12, i8* %14)
%16 = getelementptr inbounds %struct.servent, %struct.servent* %7, i64 0, i32 3
%17 = load i8**, i8*** %16
%18 = icmp eq i8** %17, null
br i1 %18, label %29, label %19
19: ; preds = %6
%20 = load i8*, i8** %17
%21 = icmp eq i8* %20, null
br i1 %21, label %29, label %22
22: ; preds = %19, %22
%23 = phi i8* [ %27, %22 ], [ %20, %19 ]
%24 = phi i8** [ %26, %22 ], [ %17, %19 ]
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%26 = getelementptr inbounds i8*, i8** %24, i64 1
%27 = load i8*, i8** %26
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %22
29: ; preds = %22, %19, %6
%30 = tail call i32 @putchar(i8 signext 10)
%31 = add i32 %8, 1
%32 = tail call %struct.servent* (...) @getservent()
%33 = icmp eq %struct.servent* %32, null
br i1 %33, label %34, label %6
34: ; preds = %29, %1
%35 = phi i32 [ 0, %1 ], [ %31, %29 ]
%36 = tail call i32 (...) @endservent()
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %35)
ret i32 0
}