#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 {
  int dummy;
};
typedef int /*<<< orphan*/ h_buf;

/* Variables and functions */
int errno;
int gethostent_r(struct hostent *, char *, int, struct hostent **, int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ printhostent(struct hostent *);
int main(int argc, char **argv) {
  struct hostent he_buf;
  struct hostent *he_res;
  char h_buf[1024];
  int ch_errno;
  int ret;
  do {
    ret = gethostent_r(&he_buf, h_buf, sizeof(h_buf), &he_res, &ch_errno);
    printf("ret: %d, h_errno: %d\n", ret, ch_errno);
    if (ret != 0) {
      errno = ret;
      ret = -1;
    }
    if (ret == -1) {
      perror("gethostent_r");
      break;
    }
    if (he_res) {
      printhostent(he_res);
    }
  } while (he_res);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x420(%rbp),%rsi
lea    -0x18(%rbp),%rdi
mov    $0x400,%edx
lea    -0x20(%rbp),%rcx
lea    -0x424(%rbp),%r8
call   1030 <gethostent_r@plt>
mov    %eax,-0x428(%rbp)
mov    -0x428(%rbp),%esi
mov    -0x424(%rbp),%edx
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x428(%rbp)
je     11bd <main+0x7d>
mov    -0x428(%rbp),%ecx
mov    0x2e2f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x428(%rbp)
ff ff ff
cmpl   $0xffffffff,-0x428(%rbp)
jne    11db <main+0x9b>
lea    0xe45(%rip),%rdi
call   1060 <perror@plt>
jmp    11ff <main+0xbf>
cmpq   $0x0,-0x20(%rbp)
je     11ef <main+0xaf>
mov    -0x20(%rbp),%rdi
call   1050 <printhostent@plt>
jmp    11f4 <main+0xb4>
cmpq   $0x0,-0x20(%rbp)
jne    1159 <main+0x19>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
lea    0x18(%rsp),%r14
lea    0x20(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
lea    0xe94(%rip),%rbx
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x400,%edx
mov    %r12,%rcx
mov    %r13,%r8
call   1030 <gethostent_r@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edx
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
test   %ebp,%ebp
jne    11c5 <main+0x85>
mov    0x10(%rsp),%rdi
test   %rdi,%rdi
je     11b1 <main+0x71>
call   1050 <printhostent@plt>
cmpq   $0x0,0x10(%rsp)
jne    1170 <main+0x30>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e14(%rip),%rax
mov    %ebp,(%rax)
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
jmp    11b1 <main+0x71>

LLVM IR (-O0):
%struct.hostent = type { i32 }
@.str = [22 x i8] c"ret: %d, h_errno: %d\0A\00"
@errno = global i32 0
@.str.1 = [13 x i8] c"gethostent_r\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.hostent
%7 = alloca %struct.hostent*
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
br label %11
11: ; preds = %33, %2
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%13 = call i32 @gethostent_r(%struct.hostent* %6, i8* %12, i32 1024, %struct.hostent** %7, i32* %9)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = load i32, i32* %9
%16 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15)
%17 = load i32, i32* %10
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = load i32, i32* %10
store i32 %20, i32* @errno
store i32 -1, i32* %10
br label %21
21: ; preds = %19, %11
%22 = load i32, i32* %10
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %36
26: ; preds = %21
%27 = load %struct.hostent*, %struct.hostent** %7
%28 = icmp ne %struct.hostent* %27, null
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load %struct.hostent*, %struct.hostent** %7
%31 = call i32 @printhostent(%struct.hostent* %30)
br label %32
32: ; preds = %29, %26
br label %33
33: ; preds = %32
%34 = load %struct.hostent*, %struct.hostent** %7
%35 = icmp ne %struct.hostent* %34, null
br i1 %35, label %11, label %36
36: ; preds = %33, %24
ret i32 0
}

LLVM IR (-O2):
%struct.hostent = type { i32 }
@.str = [22 x i8] c"ret: %d, h_errno: %d\0A\00"
@errno = global i32 0
@.str.1 = [13 x i8] c"gethostent_r\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.hostent
%4 = alloca %struct.hostent*
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = bitcast %struct.hostent* %3 to i8*
%8 = bitcast %struct.hostent** %4 to i8*
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%10 = bitcast i32* %6 to i8*
br label %11
11: ; preds = %21, %2
%12 = call i32 @gethostent_r(%struct.hostent* %3, i8* %9, i32 1024, %struct.hostent** %4, i32* %6)
%13 = load i32, i32* %6
%14 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
%15 = icmp eq i32 %12, 0
br i1 %15, label %18, label %16
16: ; preds = %11
store i32 %12, i32* @errno
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %25
18: ; preds = %11
%19 = load %struct.hostent*, %struct.hostent** %4
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = call i32 @printhostent(%struct.hostent* %19)
%23 = load %struct.hostent*, %struct.hostent** %4
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %11
25: ; preds = %18, %21, %16
ret i32 0
}