#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*/ socklen_t;

/* Variables and functions */
int ERANGE;
int /*<<< orphan*/ NO_RECOVERY;
int /*<<< orphan*/ free(struct hostent *);
int gethostbyaddr_r(void const *, int /*<<< orphan*/, int, struct hostent *,
                    void *, size_t, struct hostent **, int /*<<< orphan*/ *);
int /*<<< orphan*/ h_errno;
struct hostent *malloc(size_t);
struct hostent *gethostbyaddr(const void *a, socklen_t l, int af) {
  static struct hostent *h;
  size_t size = 63;
  struct hostent *res;
  int err;
  do {
    free(h);
    h = malloc(size += size + 1);
    if (!h) {
      h_errno = NO_RECOVERY;
      return 0;
    }
    err = gethostbyaddr_r(a, l, af, h, (void *)(h + 1), size - sizeof *h, &res,
                          &h_errno);
  } while (err == ERANGE);
  return err ? 0 : h;
}


x86 assembly (clang, -O0):
<gethostbyaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movq   $0x3f,-0x20(%rbp)
mov    0x2eef(%rip),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
add    -0x20(%rbp),%rdi
mov    %rdi,-0x20(%rbp)
call   1040 <malloc@plt>
mov    %rax,0x2ece(%rip)
cmpq   $0x0,0x2ec6(%rip)
jne    119f <gethostbyaddr+0x6f>
mov    0x2e49(%rip),%rax
mov    (%rax),%ecx
mov    0x2e58(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1219 <gethostbyaddr+0xe9>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    0x2e90(%rip),%rcx
mov    0x2e89(%rip),%r8
add    $0x4,%r8
mov    -0x20(%rbp),%r9
sub    $0x4,%r9
lea    -0x28(%rbp),%r10
mov    0x2e1a(%rip),%rax
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1050 <gethostbyaddr_r@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%eax
je     114a <gethostbyaddr+0x1a>
cmpl   $0x0,-0x2c(%rbp)
je     1206 <gethostbyaddr+0xd6>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    1211 <gethostbyaddr+0xe1>
mov    0x2e33(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethostbyaddr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%r12
mov    $0x3f,%ebp
mov    0x2e90(%rip),%r13
mov    0x2ee9(%rip),%rdi
call   1030 <free@plt>
lea    0x1(,%rbp,1),%rbx
add    %rbp,%rbx
mov    %rbx,%rdi
call   1040 <malloc@plt>
mov    %rax,0x2eca(%rip)
test   %rax,%rax
je     11c3 <gethostbyaddr+0x93>
add    %rbp,%rbp
mov    %rax,%r8
add    $0x4,%r8
add    $0xfffffffffffffffd,%rbp
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
mov    %rax,%rcx
mov    %rbp,%r9
push   0x2e4a(%rip)
lea    0x8(%rsp),%rax
push   %rax
call   1050 <gethostbyaddr_r@plt>
add    $0x10,%rsp
mov    %rbx,%rbp
cmp    0x0(%r13),%eax
je     1150 <gethostbyaddr+0x20>
test   %eax,%eax
jne    11d5 <gethostbyaddr+0xa5>
mov    0x2e7f(%rip),%rax
jmp    11d7 <gethostbyaddr+0xa7>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    0x2e15(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32 }
@gethostbyaddr.h = internal global %struct.hostent* null
@NO_RECOVERY = global i32 0
@h_errno = global i32 0
@ERANGE = global i32 0
define %struct.hostent* @gethostbyaddr(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.hostent*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca %struct.hostent*
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i64 63, i64* %8
br label %11
11: ; preds = %34, %3
%12 = load %struct.hostent*, %struct.hostent** @gethostbyaddr.h
%13 = call i32 @free(%struct.hostent* %12)
%14 = load i64, i64* %8
%15 = add i64 %14, 1
%16 = load i64, i64* %8
%17 = add i64 %16, %15
store i64 %17, i64* %8
%18 = call %struct.hostent* @malloc(i64 %17)
store %struct.hostent* %18, %struct.hostent** @gethostbyaddr.h
%19 = load %struct.hostent*, %struct.hostent** @gethostbyaddr.h
%20 = icmp ne %struct.hostent* %19, null
br i1 %20, label %23, label %21
21: ; preds = %11
%22 = load i32, i32* @NO_RECOVERY
store i32 %22, i32* @h_errno
store %struct.hostent* null, %struct.hostent** %4
br label %46
23: ; preds = %11
%24 = load i8*, i8** %5
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = load %struct.hostent*, %struct.hostent** @gethostbyaddr.h
%28 = load %struct.hostent*, %struct.hostent** @gethostbyaddr.h
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i64 1
%30 = bitcast %struct.hostent* %29 to i8*
%31 = load i64, i64* %8
%32 = sub i64 %31, 4
%33 = call i32 @gethostbyaddr_r(i8* %24, i32 %25, i32 %26, %struct.hostent* %27, i8* %30, i64 %32, %struct.hostent** %9, i32* @h_errno)
store i32 %33, i32* %10
br label %34
34: ; preds = %23
%35 = load i32, i32* %10
%36 = load i32, i32* @ERANGE
%37 = icmp eq i32 %35, %36
br i1 %37, label %11, label %38
38: ; preds = %34
%39 = load i32, i32* %10
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %38
br label %44
42: ; preds = %38
%43 = load %struct.hostent*, %struct.hostent** @gethostbyaddr.h
br label %44
44: ; preds = %42, %41
%45 = phi %struct.hostent* [ null, %41 ], [ %43, %42 ]
store %struct.hostent* %45, %struct.hostent** %4
br label %46
46: ; preds = %44, %21
%47 = load %struct.hostent*, %struct.hostent** %4
ret %struct.hostent* %47
}

LLVM IR (-O2):
%struct.hostent = type { i32 }
@gethostbyaddr.h = internal unnamed_addr global %struct.hostent* null
@NO_RECOVERY = global i32 0
@h_errno = global i32 0
@ERANGE = global i32 0
define %struct.hostent* @gethostbyaddr(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.hostent*
%5 = bitcast %struct.hostent** %4 to i8*
br label %6
6: ; preds = %16, %3
%7 = phi i64 [ 63, %3 ], [ %11, %16 ]
%8 = load %struct.hostent*, %struct.hostent** @gethostbyaddr.h
%9 = call i32 @free(%struct.hostent* %8)
%10 = shl i64 %7, 1
%11 = or i64 %10, 1
%12 = call %struct.hostent* @malloc(i64 %11)
store %struct.hostent* %12, %struct.hostent** @gethostbyaddr.h
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = load i32, i32* @NO_RECOVERY
store i32 %15, i32* @h_errno
br label %27
16: ; preds = %6
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 1
%18 = bitcast %struct.hostent* %17 to i8*
%19 = add i64 %10, -3
%20 = call i32 @gethostbyaddr_r(i8* %0, i32 %1, i32 %2, %struct.hostent* %12, i8* %18, i64 %19, %struct.hostent** %4, i32* @h_errno)
%21 = load i32, i32* @ERANGE
%22 = icmp eq i32 %20, %21
br i1 %22, label %6, label %23
23: ; preds = %16
%24 = icmp eq i32 %20, 0
%25 = load %struct.hostent*, %struct.hostent** @gethostbyaddr.h
%26 = select i1 %24, %struct.hostent* %25, %struct.hostent* null
br label %27
27: ; preds = %23, %14
%28 = phi %struct.hostent* [ %26, %23 ], [ null, %14 ]
ret %struct.hostent* %28
}