#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*/);
scalar_t__ 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 = (char *)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 i64 @malloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %5
br label %15
15: ; preds = %23, %0
%16 = bitcast %struct.in_addr* %2 to i8*
%17 = load i32, i32* @AF_INET
%18 = load i8*, i8** %5
%19 = load i64, i64* %8
%20 = call i32 @gethostbyaddr_r(i8* %16, i32 4, i32 %17, %struct.hostent* %3, i8* %18, i64 %19, %struct.hostent** %4, i32* %7)
store i32 %20, i32* %6
%21 = load i32, i32* @ERANGE
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %29
23: ; preds = %15
%24 = load i8*, i8** %5
%25 = load i64, i64* %8
%26 = mul i64 %25, 2
store i64 %26, i64* %8
%27 = call i64 @realloc(i8* %24, i64 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %5
br label %15
29: ; preds = %15
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load %struct.hostent*, %struct.hostent** %4
%34 = icmp eq %struct.hostent* %33, null
br i1 %34, label %35, label %41
35: ; preds = %32, %29
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = load i32, i32* %7
%39 = load i32, i32* %6
%40 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %37, i32 %38, i32 %39)
store i32 -1, i32* %1
br label %42
41: ; preds = %32
store i32 0, i32* %1
br label %42
42: ; preds = %41, %35
%43 = load i32, i32* %1
ret i32 %43
}

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 i64 @malloc(i64 1024)
%13 = inttoptr i64 %12 to i8*
%14 = load i32, i32* @AF_INET
%15 = call i32 @gethostbyaddr_r(i8* %5, i32 4, i32 %14, %struct.hostent* %2, i8* %13, i64 1024, %struct.hostent** %3, i32* %4)
%16 = load i32, i32* @ERANGE
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %28
18: ; preds = %0, %18
%19 = phi i8* [ %23, %18 ], [ %13, %0 ]
%20 = phi i64 [ %21, %18 ], [ 1024, %0 ]
%21 = shl i64 %20, 1
%22 = call i64 @realloc(i8* %19, i64 %21)
%23 = inttoptr i64 %22 to i8*
%24 = load i32, i32* @AF_INET
%25 = call i32 @gethostbyaddr_r(i8* %5, i32 4, i32 %24, %struct.hostent* %2, i8* %23, i64 %21, %struct.hostent** %3, i32* %4)
%26 = load i32, i32* @ERANGE
%27 = icmp eq i32 %25, %26
br i1 %27, label %18, label %28
28: ; preds = %18, %0
%29 = phi i32 [ %15, %0 ], [ %25, %18 ]
%30 = icmp ne i32 %29, 0
%31 = load %struct.hostent*, %struct.hostent** %3
%32 = icmp eq %struct.hostent* %31, null
%33 = select i1 %30, i1 true, i1 %32
br i1 %33, label %34, label %39
34: ; preds = %28
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = load i32, i32* %4
%38 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %36, i32 %37, i32 %29)
br label %39
39: ; preds = %28, %34
%40 = phi i32 [ -1, %34 ], [ 0, %28 ]
ret i32 %40
}