#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 protoent {
  char *p_name;
  int p_proto;
  char **p_aliases;
};

/* Variables and functions */
int /*<<< orphan*/ endprotoent();
struct protoent *getprotoent();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ setprotoent(int);
int _getprotoent(char const *progname) {
  struct protoent *pe;
  unsigned int i;
  char *const *p;
  char const *sep;
  printf("%s: Testing getprotoent()\n", progname);
  setprotoent(1);
  for (i = 0; (pe = getprotoent()) != NULL; i++) {
    printf("%s\t%d", pe->p_name, pe->p_proto);
    if (pe->p_aliases != NULL) {
      sep = "\t";
      for (p = pe->p_aliases; *p != NULL; p++) {
        printf("%s%s", sep, *p);
        sep = ", ";
      }
    }
    putchar('\n');
  }
  endprotoent();
  printf("%u protocols listed\n", i);
  return 0;
}


x86 assembly (clang, -O0):
<_getprotoent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <setprotoent@plt>
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1060 <getprotoent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1236 <_getprotoent+0xe6>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     121e <_getprotoent+0xce>
lea    0xe5b(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     1219 <_getprotoent+0xc9>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe24(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    11d6 <_getprotoent+0x86>
jmp    121e <_getprotoent+0xce>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117f <_getprotoent+0x2f>
mov    $0x0,%al
call   1040 <endprotoent@plt>
mov    -0x14(%rbp),%esi
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_getprotoent>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rsi
lea    0xe9b(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <setprotoent@plt>
xor    %eax,%eax
call   1060 <getprotoent@plt>
test   %rax,%rax
je     122c <_getprotoent+0xdc>
mov    %rax,%rbp
lea    0xe89(%rip),%r14
lea    0xe8a(%rip),%r15
lea    0xe81(%rip),%r12
lea    0xe81(%rip),%r13
xor    %ebx,%ebx
jmp    11cc <_getprotoent+0x7c>
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%ebx
xor    %eax,%eax
call   1060 <getprotoent@plt>
mov    %rax,%rbp
test   %rax,%rax
je     122c <_getprotoent+0xdc>
mov    0x0(%rbp),%rsi
mov    0x8(%rbp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rbp),%rbp
test   %rbp,%rbp
je     11b0 <_getprotoent+0x60>
mov    0x0(%rbp),%rdx
test   %rdx,%rdx
je     11b0 <_getprotoent+0x60>
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbp),%rdx
test   %rdx,%rdx
je     11b0 <_getprotoent+0x60>
add    $0x10,%rbp
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%rbp),%rdx
add    $0x8,%rbp
test   %rdx,%rdx
jne    1210 <_getprotoent+0xc0>
jmp    11b0 <_getprotoent+0x60>
xor    %eax,%eax
call   1040 <endprotoent@plt>
lea    0xdf1(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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