#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 hostent {
  char *h_name;
  int h_addrtype;
  int h_length;
  char **h_aliases;
};

/* Variables and functions */
int /*<<< orphan*/ endhostent();
struct hostent *gethostent();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ sethostent(int);
int _gethostent(char const *progname) {
  struct hostent *he;
  unsigned int i;
  char *const *p;
  char const *sep;
  printf("%s: Testing gethostent()\n", progname);
  sethostent(1);
  for (i = 0; (he = gethostent()) != NULL; i++) {
    printf("%s\t%d %d", he->h_name, he->h_addrtype, he->h_length);
    if (he->h_aliases != NULL) {
      sep = "\t";
      for (p = he->h_aliases; *p != NULL; p++) {
        printf("%s%s", sep, *p);
        sep = ", ";
      }
    }
    putchar('\n');
  }
  endhostent();
  printf("%u hosts listed\n", i);
  return (i > 0) ? 0 : 1;
}


x86 assembly (clang, -O0):
<_gethostent>:
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   1060 <sethostent@plt>
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1040 <gethostent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     123d <_gethostent+0xed>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%ecx
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     1225 <_gethostent+0xd5>
lea    0xe56(%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     1220 <_gethostent+0xd0>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe1f(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    11dd <_gethostent+0x8d>
jmp    1225 <_gethostent+0xd5>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117f <_gethostent+0x2f>
mov    $0x0,%al
call   1070 <endhostent@plt>
mov    -0x14(%rbp),%esi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmova  %ecx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_gethostent>:
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   1060 <sethostent@plt>
xor    %eax,%eax
call   1040 <gethostent@plt>
test   %rax,%rax
je     122c <_gethostent+0xdc>
mov    %rax,%rbp
lea    0xe88(%rip),%r14
lea    0xe8c(%rip),%r15
lea    0xe83(%rip),%r12
lea    0xe83(%rip),%r13
xor    %ebx,%ebx
jmp    11cc <_gethostent+0x7c>
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%ebx
xor    %eax,%eax
call   1040 <gethostent@plt>
mov    %rax,%rbp
test   %rax,%rax
je     122c <_gethostent+0xdc>
mov    0x0(%rbp),%rsi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%ecx
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rbp),%rbp
test   %rbp,%rbp
je     11b0 <_gethostent+0x60>
mov    0x0(%rbp),%rdx
test   %rdx,%rdx
je     11b0 <_gethostent+0x60>
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbp),%rdx
test   %rdx,%rdx
je     11b0 <_gethostent+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 <_gethostent+0xc0>
jmp    11b0 <_gethostent+0x60>
xor    %eax,%eax
call   1070 <endhostent@plt>
lea    0xdf3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
test   %ebx,%ebx
sete   %al
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i8*, i32, i32, i8** }
@.str = [26 x i8] c"%s: Testing gethostent()\0A\00"
@.str.1 = [9 x i8] c"%s\09%d %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 = [17 x i8] c"%u hosts listed\0A\00"
define i32 @_gethostent(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.hostent*
%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 ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = call i32 @sethostent(i32 1)
store i32 0, i32* %4
br label %10
10: ; preds = %47, %1
%11 = call %struct.hostent* (...) @gethostent()
store %struct.hostent* %11, %struct.hostent** %3
%12 = icmp ne %struct.hostent* %11, null
br i1 %12, label %13, label %50
13: ; preds = %10
%14 = load %struct.hostent*, %struct.hostent** %3
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i32 0, i32 0
%16 = load i8*, i8** %15
%17 = load %struct.hostent*, %struct.hostent** %3
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load %struct.hostent*, %struct.hostent** %3
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 2
%22 = load i32, i32* %21
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %16, i32 %19, i32 %22)
%24 = load %struct.hostent*, %struct.hostent** %3
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i32 0, i32 3
%26 = load i8**, i8*** %25
%27 = icmp ne i8** %26, null
br i1 %27, label %28, label %45
28: ; preds = %13
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %6
%29 = load %struct.hostent*, %struct.hostent** %3
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 3
%31 = load i8**, i8*** %30
store i8** %31, i8*** %5
br label %32
32: ; preds = %41, %28
%33 = load i8**, i8*** %5
%34 = load i8*, i8** %33
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %44
36: ; preds = %32
%37 = load i8*, i8** %6
%38 = load i8**, i8*** %5
%39 = load i8*, i8** %38
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %37, i8* %39)
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %6
br label %41
41: ; preds = %36
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i32 1
store i8** %43, i8*** %5
br label %32
44: ; preds = %32
br label %45
45: ; preds = %44, %13
%46 = call i32 @putchar(i8 signext 10)
br label %47
47: ; preds = %45
%48 = load i32, i32* %4
%49 = add i32 %48, 1
store i32 %49, i32* %4
br label %10
50: ; preds = %10
%51 = call i32 (...) @endhostent()
%52 = load i32, i32* %4
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %4
%55 = icmp ugt i32 %54, 0
%56 = zext i1 %55 to i64
%57 = select i1 %55, i32 0, i32 1
ret i32 %57
}

LLVM IR (-O2):
%struct.hostent = type { i8*, i32, i32, i8** }
@.str = [26 x i8] c"%s: Testing gethostent()\0A\00"
@.str.1 = [9 x i8] c"%s\09%d %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 = [17 x i8] c"%u hosts listed\0A\00"
define i32 @_gethostent(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 @sethostent(i32 1)
%4 = tail call %struct.hostent* (...) @gethostent()
%5 = icmp eq %struct.hostent* %4, null
br i1 %5, label %39, label %6
6: ; preds = %1, %34
%7 = phi %struct.hostent* [ %37, %34 ], [ %4, %1 ]
%8 = phi i32 [ %36, %34 ], [ 0, %1 ]
%9 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%10 = load i8*, i8** %9
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 1
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 2
%14 = load i32, i32* %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, i32 %14)
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 3
%17 = load i8**, i8*** %16
%18 = icmp eq i8** %17, null
br i1 %18, label %34, label %19
19: ; preds = %6
%20 = load i8*, i8** %17
%21 = icmp eq i8* %20, null
br i1 %21, label %34, label %22
22: ; preds = %19
%23 = 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* %20)
%24 = getelementptr inbounds i8*, i8** %17, i64 1
%25 = load i8*, i8** %24
%26 = icmp eq i8* %25, null
br i1 %26, label %34, label %27
27: ; preds = %22, %27
%28 = phi i8* [ %32, %27 ], [ %25, %22 ]
%29 = phi i8** [ %31, %27 ], [ %24, %22 ]
%30 = 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* %28)
%31 = getelementptr inbounds i8*, i8** %29, i64 1
%32 = load i8*, i8** %31
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %27
34: ; preds = %27, %22, %19, %6
%35 = tail call i32 @putchar(i8 signext 10)
%36 = add i32 %8, 1
%37 = tail call %struct.hostent* (...) @gethostent()
%38 = icmp eq %struct.hostent* %37, null
br i1 %38, label %39, label %6
39: ; preds = %34, %1
%40 = phi i32 [ 0, %1 ], [ %36, %34 ]
%41 = tail call i32 (...) @endhostent()
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%43 = icmp eq i32 %40, 0
%44 = zext i1 %43 to i32
ret i32 %44
}