#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 in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int ERANGE;
int /*<<< orphan*/ INADDR_LOOPBACK;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, int);
int gethostbyaddr_r(char *, int, int /*<<< orphan*/, struct hostent *, char *,
                    size_t, struct hostent **, int *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
char *malloc(size_t);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ stderr;
int main(void) {
  struct in_addr localhost;
  struct hostent hostbuf, *hp;
  char *buf;
  int res, herr;
  size_t buflen = 1024;
  localhost.s_addr = htonl(INADDR_LOOPBACK);
  buf = malloc(buflen);
  while ((res = gethostbyaddr_r((char *)&localhost, sizeof localhost, AF_INET,
                                &hostbuf, buf, buflen, &hp, &herr)) == ERANGE)
    buf = (char *)realloc(buf, buflen *= 2);
  if (res || hp == NULL) {
    fprintf(stderr, "errno = %d, herr = %d, res = %d\n", errno, herr, res);
    return -1;
  } else
    return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x400,-0x30(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
call   1030 <htonl@plt>
mov    %eax,-0x8(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
lea    -0x8(%rbp),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%r8
mov    -0x30(%rbp),%r9
mov    $0x4,%esi
lea    -0x10(%rbp),%rcx
lea    -0x18(%rbp),%r10
lea    -0x28(%rbp),%rax
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1070 <gethostbyaddr_r@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%eax
jne    11e9 <main+0x99>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
shl    $0x1,%rsi
mov    %rsi,-0x30(%rbp)
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    1185 <main+0x35>
cmpl   $0x0,-0x24(%rbp)
jne    11fe <main+0xae>
cmpq   $0x0,-0x18(%rbp)
jne    122f <main+0xdf>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%r8d
lea    0xde2(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <main+0xe6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
call   1030 <htonl@plt>
mov    %eax,(%rsp)
mov    $0x400,%ebx
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rax
lea    0x10(%rsp),%r15
mov    %rsp,%rdi
lea    0x8(%rsp),%rcx
mov    $0x400,%r9d
mov    $0x4,%esi
mov    %r14,%r8
push   %rax
push   %r15
call   1070 <gethostbyaddr_r@plt>
add    $0x10,%rsp
mov    0x2e23(%rip),%r13
cmp    0x0(%r13),%eax
jne    1212 <main+0xc2>
mov    %rsp,%rbp
lea    0x8(%rsp),%r12
nopl   0x0(%rax,%rax,1)
add    %rbx,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
mov    %rax,%r14
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    $0x4,%esi
mov    %r12,%rcx
mov    %r14,%r8
mov    %rbx,%r9
lea    0x4(%rsp),%rax
push   %rax
push   %r15
call   1070 <gethostbyaddr_r@plt>
add    $0x10,%rsp
cmp    0x0(%r13),%eax
je     11d0 <main+0x80>
test   %eax,%eax
jne    1220 <main+0xd0>
xor    %ecx,%ecx
cmpq   $0x0,0x10(%rsp)
jne    124a <main+0xfa>
mov    0x2da1(%rip),%rcx
mov    (%rcx),%edi
mov    0x2d90(%rip),%rcx
mov    (%rcx),%edx
mov    0x4(%rsp),%ecx
lea    0xdc3(%rip),%rsi
mov    %eax,%r8d
call   1040 <fprintf@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32 }
@INADDR_LOOPBACK = global i32 0
@AF_INET = global i32 0
@ERANGE = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"errno = %d, herr = %d, res = %d\0A\00"
@errno = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.in_addr
%3 = alloca %struct.hostent
%4 = alloca %struct.hostent*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i32 0, i32* %1
store i64 1024, i64* %8
%9 = load i32, i32* @INADDR_LOOPBACK
%10 = call i32 @htonl(i32 %9)
%11 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %2, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i64, i64* %8
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %5
br label %14
14: ; preds = %22, %0
%15 = bitcast %struct.in_addr* %2 to i8*
%16 = load i32, i32* @AF_INET
%17 = load i8*, i8** %5
%18 = load i64, i64* %8
%19 = call i32 @gethostbyaddr_r(i8* %15, i32 4, i32 %16, %struct.hostent* %3, i8* %17, i64 %18, %struct.hostent** %4, i32* %7)
store i32 %19, i32* %6
%20 = load i32, i32* @ERANGE
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %28
22: ; preds = %14
%23 = load i8*, i8** %5
%24 = load i64, i64* %8
%25 = mul i64 %24, 2
store i64 %25, i64* %8
%26 = call i64 @realloc(i8* %23, i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %5
br label %14
28: ; preds = %14
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load %struct.hostent*, %struct.hostent** %4
%33 = icmp eq %struct.hostent* %32, null
br i1 %33, label %34, label %40
34: ; preds = %31, %28
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = load i32, i32* %7
%38 = load i32, i32* %6
%39 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %36, i32 %37, i32 %38)
store i32 -1, i32* %1
br label %41
40: ; preds = %31
store i32 0, i32* %1
br label %41
41: ; preds = %40, %34
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32 }
@INADDR_LOOPBACK = global i32 0
@AF_INET = global i32 0
@ERANGE = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"errno = %d, herr = %d, res = %d\0A\00"
@errno = global i32 0
define i32 @main() {
%1 = alloca %struct.in_addr
%2 = alloca %struct.hostent
%3 = alloca %struct.hostent*
%4 = alloca i32
%5 = bitcast %struct.in_addr* %1 to i8*
%6 = bitcast %struct.hostent* %2 to i8*
%7 = bitcast %struct.hostent** %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = load i32, i32* @INADDR_LOOPBACK
%10 = tail call i32 @htonl(i32 %9)
%11 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %1, i64 0, i32 0
store i32 %10, i32* %11
%12 = tail call dereferenceable_or_null(1024) i8* @malloc(i64 1024)
%13 = load i32, i32* @AF_INET
%14 = call i32 @gethostbyaddr_r(i8* %5, i32 4, i32 %13, %struct.hostent* %2, i8* %12, i64 1024, %struct.hostent** %3, i32* %4)
%15 = load i32, i32* @ERANGE
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %27
17: ; preds = %0, %17
%18 = phi i64 [ %20, %17 ], [ 1024, %0 ]
%19 = phi i8* [ %22, %17 ], [ %12, %0 ]
%20 = shl i64 %18, 1
%21 = call i64 @realloc(i8* %19, i64 %20)
%22 = inttoptr i64 %21 to i8*
%23 = load i32, i32* @AF_INET
%24 = call i32 @gethostbyaddr_r(i8* %5, i32 4, i32 %23, %struct.hostent* %2, i8* %22, i64 %20, %struct.hostent** %3, i32* %4)
%25 = load i32, i32* @ERANGE
%26 = icmp eq i32 %24, %25
br i1 %26, label %17, label %27
27: ; preds = %17, %0
%28 = phi i32 [ %14, %0 ], [ %24, %17 ]
%29 = icmp ne i32 %28, 0
%30 = load %struct.hostent*, %struct.hostent** %3
%31 = icmp eq %struct.hostent* %30, null
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %33, label %38
33: ; preds = %27
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = load i32, i32* %4
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %35, i32 %36, i32 %28)
br label %38
38: ; preds = %27, %33
%39 = phi i32 [ -1, %33 ], [ 0, %27 ]
ret i32 %39
}