#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;
};

/* Variables and functions */
int ERANGE;
int /*<<< orphan*/ NO_RECOVERY;
int /*<<< orphan*/ free(struct hostent *);
int gethostbyname2_r(char const *, int, struct hostent *, void *, size_t,
                     struct hostent **, int /*<<< orphan*/ *);
int /*<<< orphan*/ h_errno;
struct hostent *malloc(size_t);
struct hostent *gethostbyname2(const char *name, 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 = gethostbyname2_r(name, af, h, (void *)(h + 1), size - sizeof *h, &res,
                           &h_errno);
  } while (err == ERANGE);
  return err ? 0 : h;
}


x86 assembly (clang, -O0):
<gethostbyname2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x3f,-0x20(%rbp)
mov    0x2ef2(%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,0x2ed1(%rip)
cmpq   $0x0,0x2ec9(%rip)
jne    119c <gethostbyname2+0x6c>
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5b(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    120e <gethostbyname2+0xde>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e96(%rip),%rdx
mov    0x2e8f(%rip),%rcx
add    $0x4,%rcx
mov    -0x20(%rbp),%r8
sub    $0x4,%r8
lea    -0x28(%rbp),%r9
mov    0x2e20(%rip),%rax
mov    %rax,(%rsp)
call   1050 <gethostbyname2_r@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%eax
je     1147 <gethostbyname2+0x17>
cmpl   $0x0,-0x2c(%rbp)
je     11fb <gethostbyname2+0xcb>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    1206 <gethostbyname2+0xd6>
mov    0x2e3e(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethostbyname2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %rdi,%r12
mov    $0x3f,%ebp
lea    0x10(%rsp),%r14
mov    0x2e8b(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2ed9(%rip),%rdi
call   1030 <free@plt>
lea    0x1(,%rbp,1),%rbx
add    %rbp,%rbx
mov    %rbx,%rdi
call   1040 <malloc@plt>
mov    %rax,0x2eba(%rip)
test   %rax,%rax
je     11ce <gethostbyname2+0x9e>
add    %rbp,%rbp
mov    %rax,%rcx
add    $0x4,%rcx
add    $0xfffffffffffffffd,%rbp
mov    0x2e48(%rip),%rdx
mov    %rdx,(%rsp)
mov    %r12,%rdi
mov    %r15d,%esi
mov    %rax,%rdx
mov    %rbp,%r8
mov    %r14,%r9
call   1050 <gethostbyname2_r@plt>
mov    %rbx,%rbp
cmp    0x0(%r13),%eax
je     1160 <gethostbyname2+0x30>
test   %eax,%eax
jne    11e0 <gethostbyname2+0xb0>
mov    0x2e74(%rip),%rax
jmp    11e2 <gethostbyname2+0xb2>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    0x2e0a(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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