#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct hostent {
  int h_addrtype;
  int h_length;
  char **h_aliases;
  char *h_name;
  void **h_addr_list;
};
struct addrinfo {
  int ai_family;
  char const *ai_canonname;
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int AF_INET;
int AF_INET6;
int /*<<< orphan*/ AI_CANONNAME;
#define EAI_AGAIN 132
#define EAI_FAIL 131
#define EAI_MEMORY 130
#define EAI_NONAME 129
#define EAI_SYSTEM 128
int ERANGE;
int HOST_NOT_FOUND;
int NO_RECOVERY;
int TRY_AGAIN;
int errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, int /*<<< orphan*/, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memcpy(void *, int /*<<< orphan*/ *, int);
scalar_t__ strcmp(char const *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int gethostbyname2_r(const char *name, int af, struct hostent *h, char *buf,
                     size_t buflen, struct hostent **res, int *err) {
  struct addrinfo hint = {.ai_family = af == AF_INET6 ? af : AF_INET,
                          .ai_flags = AI_CANONNAME};
  struct addrinfo *ai, *p;
  int i;
  size_t need;
  const char *canon;
  af = hint.ai_family;
  i = (uintptr_t)buf & sizeof(char *) - 1;
  if (i) {
    if (buflen < sizeof(char *) - i)
      return ERANGE;
    buf += sizeof(char *) - i;
    buflen -= sizeof(char *) - i;
  }
  switch (getaddrinfo(name, 0, &hint, &ai)) {
  case EAI_NONAME:
    *err = HOST_NOT_FOUND;
    return errno;
  case EAI_AGAIN:
    *err = TRY_AGAIN;
    return errno;
  default:
  case EAI_MEMORY:
  case EAI_SYSTEM:
  case EAI_FAIL:
    *err = NO_RECOVERY;
    return errno;
  case 0:
    break;
  }
  h->h_addrtype = af;
  h->h_length = af == AF_INET6 ? 16 : 4;
  canon = ai->ai_canonname ? ai->ai_canonname : name;
  need = 4 * sizeof(char *);
  for (i = 0, p = ai; p; i++, p = p->ai_next)
    need += sizeof(char *) + h->h_length;
  need += strlen(name) + 1;
  need += strlen(canon) + 1;
  if (need > buflen) {
    freeaddrinfo(ai);
    return ERANGE;
  }
  h->h_aliases = (void *)buf;
  buf += 3 * sizeof(char *);
  h->h_addr_list = (void *)buf;
  buf += (i + 1) * sizeof(char *);
  h->h_name = h->h_aliases[0] = buf;
  strcpy(h->h_name, canon);
  buf += strlen(h->h_name) + 1;
  if (strcmp(h->h_name, name)) {
    h->h_aliases[1] = buf;
    strcpy(h->h_aliases[1], name);
    buf += strlen(h->h_aliases[1]) + 1;
  } else
    h->h_aliases[1] = 0;
  h->h_aliases[2] = 0;
  for (i = 0, p = ai; p; i++, p = p->ai_next) {
    h->h_addr_list[i] = (void *)buf;
    buf += h->h_length;
    memcpy(h->h_addr_list[i], &((struct sockaddr_in *)p->ai_addr)->sin_addr,
           h->h_length);
  }
  h->h_addr_list[i] = 0;
  *res = h;
  freeaddrinfo(ai);
  return 0;
}


x86 assembly (clang, -O0):
<gethostbyname2_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
lea    -0x60(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%eax
jne    11d1 <gethostbyname2_r+0x61>
mov    -0x14(%rbp),%eax
mov    %eax,-0x94(%rbp)
jmp    11e0 <gethostbyname2_r+0x70>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x94(%rbp)
mov    -0x90(%rbp),%rax
mov    -0x94(%rbp),%ecx
mov    %ecx,(%rax)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x60(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rax
and    $0x7,%rax
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
je     126b <gethostbyname2_r+0xfb>
mov    -0x30(%rbp),%rax
movslq -0x74(%rbp),%rdx
mov    $0x8,%ecx
sub    %rdx,%rcx
cmp    %rcx,%rax
jae    1240 <gethostbyname2_r+0xd0>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1612 <gethostbyname2_r+0x4a2>
movslq -0x74(%rbp),%rcx
mov    $0x8,%eax
sub    %rcx,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
movslq -0x74(%rbp),%rax
mov    $0x8,%ecx
sub    %rax,%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
lea    -0x60(%rbp),%rdx
lea    -0x68(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,%ecx
mov    %ecx,-0x98(%rbp)
test   %eax,%eax
je     1353 <gethostbyname2_r+0x1e3>
jmp    1293 <gethostbyname2_r+0x123>
mov    -0x98(%rbp),%eax
sub    $0x80,%eax
je     1333 <gethostbyname2_r+0x1c3>
jmp    12a9 <gethostbyname2_r+0x139>
mov    -0x98(%rbp),%eax
sub    $0x81,%eax
je     12ee <gethostbyname2_r+0x17e>
jmp    12bf <gethostbyname2_r+0x14f>
mov    -0x98(%rbp),%eax
add    $0xffffff7e,%eax
sub    $0x2,%eax
jb     1333 <gethostbyname2_r+0x1c3>
jmp    12d8 <gethostbyname2_r+0x168>
mov    -0x98(%rbp),%eax
sub    $0x84,%eax
je     130e <gethostbyname2_r+0x19e>
jmp    132e <gethostbyname2_r+0x1be>
mov    0x2cd3(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2ca4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1612 <gethostbyname2_r+0x4a2>
mov    0x2ca3(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2c84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1612 <gethostbyname2_r+0x4a2>
jmp    1333 <gethostbyname2_r+0x1c3>
mov    0x2c76(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2c5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1612 <gethostbyname2_r+0x4a2>
jmp    1358 <gethostbyname2_r+0x1e8>
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edx
mov    0x2c6d(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%ecx
mov    $0x10,%eax
cmp    %esi,%edx
cmove  %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x68(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     13a6 <gethostbyname2_r+0x236>
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0xa0(%rbp)
jmp    13b1 <gethostbyname2_r+0x241>
mov    -0x10(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rax
mov    %rax,-0x88(%rbp)
movq   $0x20,-0x80(%rbp)
movl   $0x0,-0x74(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     140f <gethostbyname2_r+0x29f>
mov    -0x20(%rbp),%rax
movslq 0x4(%rax),%rax
add    $0x8,%rax
add    -0x80(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
mov    -0x70(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x70(%rbp)
jmp    13d6 <gethostbyname2_r+0x266>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
add    -0x80(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x88(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
add    -0x80(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    1464 <gethostbyname2_r+0x2f4>
mov    -0x68(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    0x2b76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1612 <gethostbyname2_r+0x4a2>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x28(%rbp),%rax
add    $0x18,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x74(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x88(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1542 <gethostbyname2_r+0x3d2>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rcx,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1552 <gethostbyname2_r+0x3e2>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
movq   $0x0,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
movq   $0x0,0x10(%rax)
movl   $0x0,-0x74(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     15e3 <gethostbyname2_r+0x473>
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
movslq -0x74(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
movslq -0x74(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x70(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
call   1070 <memcpy@plt>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
mov    -0x70(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x70(%rbp)
jmp    1571 <gethostbyname2_r+0x401>
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
movslq -0x74(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x20(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x68(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethostbyname2_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r8,%rbx
mov    %rcx,%r12
mov    %rdx,%r13
mov    %esi,%ebp
mov    %rdi,%r15
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x40(%rsp)
movq   $0x0,0x50(%rsp)
mov    0x2e3f(%rip),%r14
cmp    %esi,(%r14)
je     11a7 <gethostbyname2_r+0x47>
mov    0x2e43(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,0x30(%rsp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x50(%rsp)
mov    %r12d,%ecx
and    $0x7,%ecx
je     11dd <gethostbyname2_r+0x7d>
mov    $0x8,%eax
sub    %ecx,%eax
cmp    %rbx,%rax
ja     12ae <gethostbyname2_r+0x14e>
mov    %r9,0x8(%rsp)
add    %rax,%r12
sub    %rax,%rbx
jmp    11e2 <gethostbyname2_r+0x82>
mov    %r9,0x8(%rsp)
lea    0x30(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %r15,%rdi
xor    %esi,%esi
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     1224 <gethostbyname2_r+0xc4>
mov    0x90(%rsp),%rcx
cmp    $0x84,%eax
je     12b7 <gethostbyname2_r+0x157>
cmp    $0x81,%eax
jne    12c0 <gethostbyname2_r+0x160>
mov    0x2da9(%rip),%rax
jmp    12c7 <gethostbyname2_r+0x167>
mov    %ebp,0x0(%r13)
cmp    (%r14),%ebp
mov    $0x10,%ecx
mov    $0x4,%eax
cmove  %ecx,%eax
mov    %eax,0x4(%r13)
mov    0x10(%rsp),%rdx
mov    0x8(%rdx),%rcx
test   %rcx,%rcx
cmove  %r15,%rcx
mov    %rcx,0x18(%rsp)
or     $0x8,%eax
mov    $0x1,%r14d
mov    $0x22,%ebp
mov    %rdx,0x20(%rsp)
mov    %rdx,%rcx
nopw   0x0(%rax,%rax,1)
add    %rax,%rbp
add    $0x1,%r14d
mov    0x18(%rcx),%rcx
test   %rcx,%rcx
jne    1270 <gethostbyname2_r+0x110>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,0x28(%rsp)
mov    0x18(%rsp),%rdi
call   1040 <strlen@plt>
add    0x28(%rsp),%rax
add    %rbp,%rax
cmp    %rbx,%rax
jbe    12e7 <gethostbyname2_r+0x187>
mov    0x20(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    0x2d1b(%rip),%rax
jmp    12d2 <gethostbyname2_r+0x172>
mov    0x2cfa(%rip),%rax
jmp    12c7 <gethostbyname2_r+0x167>
mov    0x2ce9(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rcx)
mov    0x2cd6(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movslq %r14d,%rax
mov    %r12,0x8(%r13)
lea    0x18(%r12),%rcx
mov    %rcx,0x18(%r13)
lea    (%r12,%rax,8),%r14
add    $0x18,%r14
mov    %r14,(%r12)
mov    %r14,0x10(%r13)
mov    %r14,%rdi
mov    0x18(%rsp),%rsi
call   1030 <strcpy@plt>
mov    0x10(%r13),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
add    %rax,%r14
add    $0x1,%r14
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
mov    0x8(%r13),%rcx
test   %rax,%rax
je     1365 <gethostbyname2_r+0x205>
mov    %r14,0x8(%rcx)
mov    0x8(%r13),%rax
mov    0x8(%rax),%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    0x8(%r13),%rbx
mov    0x8(%rbx),%rdi
call   1040 <strlen@plt>
add    %rax,%r14
add    $0x1,%r14
jmp    1371 <gethostbyname2_r+0x211>
movq   $0x0,0x8(%rcx)
mov    0x8(%r13),%rbx
mov    0x8(%rsp),%r12
movq   $0x0,0x10(%rbx)
mov    0x10(%rsp),%rbx
xor    %r15d,%r15d
test   %rbx,%rbx
je     13c1 <gethostbyname2_r+0x261>
xor    %ebp,%ebp
nopl   (%rax)
mov    0x18(%r13),%rax
mov    %r14,(%rax,%rbp,8)
movslq 0x4(%r13),%rdx
add    %rdx,%r14
mov    0x18(%r13),%rax
mov    (%rax,%rbp,8),%rdi
mov    0x10(%rbx),%rsi
call   1060 <memcpy@plt>
add    $0x1,%rbp
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1390 <gethostbyname2_r+0x230>
mov    %ebp,%eax
jmp    13c3 <gethostbyname2_r+0x263>
xor    %eax,%eax
mov    0x18(%r13),%rcx
movq   $0x0,(%rcx,%rax,8)
mov    %r13,(%r12)
mov    0x10(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
jmp    12d5 <gethostbyname2_r+0x175>

LLVM IR (-O0):
%struct.hostent = type { i32, i32, i8**, i8*, i8** }
%struct.addrinfo = type { i32, i8*, i64, %struct.addrinfo*, i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@AI_CANONNAME = global i32 0
@ERANGE = global i32 0
@HOST_NOT_FOUND = global i32 0
@errno = global i32 0
@TRY_AGAIN = global i32 0
@NO_RECOVERY = global i32 0
define i32 @gethostbyname2_r(i8* %0, i32 %1, %struct.hostent* %2, i8* %3, i64 %4, %struct.hostent** %5, i32* %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca %struct.hostent*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca %struct.hostent**
%15 = alloca i32*
%16 = alloca %struct.addrinfo
%17 = alloca %struct.addrinfo*
%18 = alloca %struct.addrinfo*
%19 = alloca i32
%20 = alloca i64
%21 = alloca i8*
store i8* %0, i8** %9
store i32 %1, i32* %10
store %struct.hostent* %2, %struct.hostent** %11
store i8* %3, i8** %12
store i64 %4, i64* %13
store %struct.hostent** %5, %struct.hostent*** %14
store i32* %6, i32** %15
%22 = bitcast %struct.addrinfo* %16 to i8*
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 0
%24 = load i32, i32* %10
%25 = load i32, i32* @AF_INET6
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %7
%28 = load i32, i32* %10
br label %31
29: ; preds = %7
%30 = load i32, i32* @AF_INET
br label %31
31: ; preds = %29, %27
%32 = phi i32 [ %28, %27 ], [ %30, %29 ]
store i32 %32, i32* %23
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 4
%34 = load i32, i32* @AI_CANONNAME
store i32 %34, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 0
%36 = load i32, i32* %35
store i32 %36, i32* %10
%37 = load i8*, i8** %12
%38 = ptrtoint i8* %37 to i64
%39 = and i64 %38, 7
%40 = trunc i64 %39 to i32
store i32 %40, i32* %19
%41 = load i32, i32* %19
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %62
43: ; preds = %31
%44 = load i64, i64* %13
%45 = load i32, i32* %19
%46 = sext i32 %45 to i64
%47 = sub i64 8, %46
%48 = icmp ult i64 %44, %47
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = load i32, i32* @ERANGE
store i32 %50, i32* %8
br label %263
51: ; preds = %43
%52 = load i32, i32* %19
%53 = sext i32 %52 to i64
%54 = sub i64 8, %53
%55 = load i8*, i8** %12
%56 = getelementptr inbounds i8, i8* %55, i64 %54
store i8* %56, i8** %12
%57 = load i32, i32* %19
%58 = sext i32 %57 to i64
%59 = sub i64 8, %58
%60 = load i64, i64* %13
%61 = sub i64 %60, %59
store i64 %61, i64* %13
br label %62
62: ; preds = %51, %31
%63 = load i8*, i8** %9
%64 = call i32 @getaddrinfo(i8* %63, i32 0, %struct.addrinfo* %16, %struct.addrinfo** %17)
switch i32 %64, label %73 [
i32 129, label %65
i32 132, label %69
i32 130, label %74
i32 128, label %74
i32 131, label %74
i32 0, label %78
]
65: ; preds = %62
%66 = load i32, i32* @HOST_NOT_FOUND
%67 = load i32*, i32** %15
store i32 %66, i32* %67
%68 = load i32, i32* @errno
store i32 %68, i32* %8
br label %263
69: ; preds = %62
%70 = load i32, i32* @TRY_AGAIN
%71 = load i32*, i32** %15
store i32 %70, i32* %71
%72 = load i32, i32* @errno
store i32 %72, i32* %8
br label %263
73: ; preds = %62
br label %74
74: ; preds = %62, %62, %62, %73
%75 = load i32, i32* @NO_RECOVERY
%76 = load i32*, i32** %15
store i32 %75, i32* %76
%77 = load i32, i32* @errno
store i32 %77, i32* %8
br label %263
78: ; preds = %62
br label %79
79: ; preds = %78
%80 = load i32, i32* %10
%81 = load %struct.hostent*, %struct.hostent** %11
%82 = getelementptr inbounds %struct.hostent, %struct.hostent* %81, i32 0, i32 0
store i32 %80, i32* %82
%83 = load i32, i32* %10
%84 = load i32, i32* @AF_INET6
%85 = icmp eq i32 %83, %84
%86 = zext i1 %85 to i64
%87 = select i1 %85, i32 16, i32 4
%88 = load %struct.hostent*, %struct.hostent** %11
%89 = getelementptr inbounds %struct.hostent, %struct.hostent* %88, i32 0, i32 1
store i32 %87, i32* %89
%90 = load %struct.addrinfo*, %struct.addrinfo** %17
%91 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %90, i32 0, i32 1
%92 = load i8*, i8** %91
%93 = icmp ne i8* %92, null
br i1 %93, label %94, label %98
94: ; preds = %79
%95 = load %struct.addrinfo*, %struct.addrinfo** %17
%96 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %95, i32 0, i32 1
%97 = load i8*, i8** %96
br label %100
98: ; preds = %79
%99 = load i8*, i8** %9
br label %100
100: ; preds = %98, %94
%101 = phi i8* [ %97, %94 ], [ %99, %98 ]
store i8* %101, i8** %21
store i64 32, i64* %20
store i32 0, i32* %19
%102 = load %struct.addrinfo*, %struct.addrinfo** %17
store %struct.addrinfo* %102, %struct.addrinfo** %18
br label %103
103: ; preds = %114, %100
%104 = load %struct.addrinfo*, %struct.addrinfo** %18
%105 = icmp ne %struct.addrinfo* %104, null
br i1 %105, label %106, label %120
106: ; preds = %103
%107 = load %struct.hostent*, %struct.hostent** %11
%108 = getelementptr inbounds %struct.hostent, %struct.hostent* %107, i32 0, i32 1
%109 = load i32, i32* %108
%110 = sext i32 %109 to i64
%111 = add i64 8, %110
%112 = load i64, i64* %20
%113 = add i64 %112, %111
store i64 %113, i64* %20
br label %114
114: ; preds = %106
%115 = load i32, i32* %19
%116 = add nsw i32 %115, 1
store i32 %116, i32* %19
%117 = load %struct.addrinfo*, %struct.addrinfo** %18
%118 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %117, i32 0, i32 3
%119 = load %struct.addrinfo*, %struct.addrinfo** %118
store %struct.addrinfo* %119, %struct.addrinfo** %18
br label %103
120: ; preds = %103
%121 = load i8*, i8** %9
%122 = call i64 @strlen(i8* %121)
%123 = add nsw i64 %122, 1
%124 = load i64, i64* %20
%125 = add i64 %124, %123
store i64 %125, i64* %20
%126 = load i8*, i8** %21
%127 = call i64 @strlen(i8* %126)
%128 = add nsw i64 %127, 1
%129 = load i64, i64* %20
%130 = add i64 %129, %128
store i64 %130, i64* %20
%131 = load i64, i64* %20
%132 = load i64, i64* %13
%133 = icmp ugt i64 %131, %132
br i1 %133, label %134, label %138
134: ; preds = %120
%135 = load %struct.addrinfo*, %struct.addrinfo** %17
%136 = call i32 @freeaddrinfo(%struct.addrinfo* %135)
%137 = load i32, i32* @ERANGE
store i32 %137, i32* %8
br label %263
138: ; preds = %120
%139 = load i8*, i8** %12
%140 = bitcast i8* %139 to i8**
%141 = load %struct.hostent*, %struct.hostent** %11
%142 = getelementptr inbounds %struct.hostent, %struct.hostent* %141, i32 0, i32 2
store i8** %140, i8*** %142
%143 = load i8*, i8** %12
%144 = getelementptr inbounds i8, i8* %143, i64 24
store i8* %144, i8** %12
%145 = load i8*, i8** %12
%146 = bitcast i8* %145 to i8**
%147 = load %struct.hostent*, %struct.hostent** %11
%148 = getelementptr inbounds %struct.hostent, %struct.hostent* %147, i32 0, i32 4
store i8** %146, i8*** %148
%149 = load i32, i32* %19
%150 = add nsw i32 %149, 1
%151 = sext i32 %150 to i64
%152 = mul i64 %151, 8
%153 = load i8*, i8** %12
%154 = getelementptr inbounds i8, i8* %153, i64 %152
store i8* %154, i8** %12
%155 = load i8*, i8** %12
%156 = load %struct.hostent*, %struct.hostent** %11
%157 = getelementptr inbounds %struct.hostent, %struct.hostent* %156, i32 0, i32 2
%158 = load i8**, i8*** %157
%159 = getelementptr inbounds i8*, i8** %158, i64 0
store i8* %155, i8** %159
%160 = load %struct.hostent*, %struct.hostent** %11
%161 = getelementptr inbounds %struct.hostent, %struct.hostent* %160, i32 0, i32 3
store i8* %155, i8** %161
%162 = load %struct.hostent*, %struct.hostent** %11
%163 = getelementptr inbounds %struct.hostent, %struct.hostent* %162, i32 0, i32 3
%164 = load i8*, i8** %163
%165 = load i8*, i8** %21
%166 = call i32 @strcpy(i8* %164, i8* %165)
%167 = load %struct.hostent*, %struct.hostent** %11
%168 = getelementptr inbounds %struct.hostent, %struct.hostent* %167, i32 0, i32 3
%169 = load i8*, i8** %168
%170 = call i64 @strlen(i8* %169)
%171 = add nsw i64 %170, 1
%172 = load i8*, i8** %12
%173 = getelementptr inbounds i8, i8* %172, i64 %171
store i8* %173, i8** %12
%174 = load %struct.hostent*, %struct.hostent** %11
%175 = getelementptr inbounds %struct.hostent, %struct.hostent* %174, i32 0, i32 3
%176 = load i8*, i8** %175
%177 = load i8*, i8** %9
%178 = call i64 @strcmp(i8* %176, i8* %177)
%179 = icmp ne i64 %178, 0
br i1 %179, label %180, label %202
180: ; preds = %138
%181 = load i8*, i8** %12
%182 = load %struct.hostent*, %struct.hostent** %11
%183 = getelementptr inbounds %struct.hostent, %struct.hostent* %182, i32 0, i32 2
%184 = load i8**, i8*** %183
%185 = getelementptr inbounds i8*, i8** %184, i64 1
store i8* %181, i8** %185
%186 = load %struct.hostent*, %struct.hostent** %11
%187 = getelementptr inbounds %struct.hostent, %struct.hostent* %186, i32 0, i32 2
%188 = load i8**, i8*** %187
%189 = getelementptr inbounds i8*, i8** %188, i64 1
%190 = load i8*, i8** %189
%191 = load i8*, i8** %9
%192 = call i32 @strcpy(i8* %190, i8* %191)
%193 = load %struct.hostent*, %struct.hostent** %11
%194 = getelementptr inbounds %struct.hostent, %struct.hostent* %193, i32 0, i32 2
%195 = load i8**, i8*** %194
%196 = getelementptr inbounds i8*, i8** %195, i64 1
%197 = load i8*, i8** %196
%198 = call i64 @strlen(i8* %197)
%199 = add nsw i64 %198, 1
%200 = load i8*, i8** %12
%201 = getelementptr inbounds i8, i8* %200, i64 %199
store i8* %201, i8** %12
br label %207
202: ; preds = %138
%203 = load %struct.hostent*, %struct.hostent** %11
%204 = getelementptr inbounds %struct.hostent, %struct.hostent* %203, i32 0, i32 2
%205 = load i8**, i8*** %204
%206 = getelementptr inbounds i8*, i8** %205, i64 1
store i8* null, i8** %206
br label %207
207: ; preds = %202, %180
%208 = load %struct.hostent*, %struct.hostent** %11
%209 = getelementptr inbounds %struct.hostent, %struct.hostent* %208, i32 0, i32 2
%210 = load i8**, i8*** %209
%211 = getelementptr inbounds i8*, i8** %210, i64 2
store i8* null, i8** %211
store i32 0, i32* %19
%212 = load %struct.addrinfo*, %struct.addrinfo** %17
store %struct.addrinfo* %212, %struct.addrinfo** %18
br label %213
213: ; preds = %246, %207
%214 = load %struct.addrinfo*, %struct.addrinfo** %18
%215 = icmp ne %struct.addrinfo* %214, null
br i1 %215, label %216, label %252
216: ; preds = %213
%217 = load i8*, i8** %12
%218 = load %struct.hostent*, %struct.hostent** %11
%219 = getelementptr inbounds %struct.hostent, %struct.hostent* %218, i32 0, i32 4
%220 = load i8**, i8*** %219
%221 = load i32, i32* %19
%222 = sext i32 %221 to i64
%223 = getelementptr inbounds i8*, i8** %220, i64 %222
store i8* %217, i8** %223
%224 = load %struct.hostent*, %struct.hostent** %11
%225 = getelementptr inbounds %struct.hostent, %struct.hostent* %224, i32 0, i32 1
%226 = load i32, i32* %225
%227 = load i8*, i8** %12
%228 = sext i32 %226 to i64
%229 = getelementptr inbounds i8, i8* %227, i64 %228
store i8* %229, i8** %12
%230 = load %struct.hostent*, %struct.hostent** %11
%231 = getelementptr inbounds %struct.hostent, %struct.hostent* %230, i32 0, i32 4
%232 = load i8**, i8*** %231
%233 = load i32, i32* %19
%234 = sext i32 %233 to i64
%235 = getelementptr inbounds i8*, i8** %232, i64 %234
%236 = load i8*, i8** %235
%237 = load %struct.addrinfo*, %struct.addrinfo** %18
%238 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %237, i32 0, i32 2
%239 = load i64, i64* %238
%240 = inttoptr i64 %239 to %struct.sockaddr_in*
%241 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %240, i32 0, i32 0
%242 = load %struct.hostent*, %struct.hostent** %11
%243 = getelementptr inbounds %struct.hostent, %struct.hostent* %242, i32 0, i32 1
%244 = load i32, i32* %243
%245 = call i32 @memcpy(i8* %236, i32* %241, i32 %244)
br label %246
246: ; preds = %216
%247 = load i32, i32* %19
%248 = add nsw i32 %247, 1
store i32 %248, i32* %19
%249 = load %struct.addrinfo*, %struct.addrinfo** %18
%250 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %249, i32 0, i32 3
%251 = load %struct.addrinfo*, %struct.addrinfo** %250
store %struct.addrinfo* %251, %struct.addrinfo** %18
br label %213
252: ; preds = %213
%253 = load %struct.hostent*, %struct.hostent** %11
%254 = getelementptr inbounds %struct.hostent, %struct.hostent* %253, i32 0, i32 4
%255 = load i8**, i8*** %254
%256 = load i32, i32* %19
%257 = sext i32 %256 to i64
%258 = getelementptr inbounds i8*, i8** %255, i64 %257
store i8* null, i8** %258
%259 = load %struct.hostent*, %struct.hostent** %11
%260 = load %struct.hostent**, %struct.hostent*** %14
store %struct.hostent* %259, %struct.hostent** %260
%261 = load %struct.addrinfo*, %struct.addrinfo** %17
%262 = call i32 @freeaddrinfo(%struct.addrinfo* %261)
store i32 0, i32* %8
br label %263
263: ; preds = %252, %134, %74, %69, %65, %49
%264 = load i32, i32* %8
ret i32 %264
}

LLVM IR (-O2):
%struct.hostent = type { i32, i32, i8**, i8*, i8** }
%struct.addrinfo = type { i32, i8*, i64, %struct.addrinfo*, i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@AI_CANONNAME = global i32 0
@ERANGE = global i32 0
@HOST_NOT_FOUND = global i32 0
@errno = global i32 0
@TRY_AGAIN = global i32 0
@NO_RECOVERY = global i32 0
define i32 @gethostbyname2_r(i8* %0, i32 %1, %struct.hostent* %2, i8* %3, i64 %4, %struct.hostent** nocapture writeonly %5, i32* nocapture writeonly %6) {
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = bitcast %struct.addrinfo* %8 to i8*
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 0
%12 = load i32, i32* @AF_INET6
%13 = icmp eq i32 %12, %1
%14 = load i32, i32* @AF_INET
%15 = select i1 %13, i32 %1, i32 %14
store i32 %15, i32* %11
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 4
%17 = load i32, i32* @AI_CANONNAME
store i32 %17, i32* %16
%18 = bitcast %struct.addrinfo** %9 to i8*
%19 = ptrtoint i8* %3 to i64
%20 = trunc i64 %19 to i32
%21 = and i32 %20, 7
%22 = icmp eq i32 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %7
%24 = sub nuw nsw i32 8, %21
%25 = zext i32 %24 to i64
%26 = icmp ugt i64 %25, %4
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load i32, i32* @ERANGE
br label %145
29: ; preds = %23
%30 = getelementptr inbounds i8, i8* %3, i64 %25
%31 = sub i64 %4, %25
br label %32
32: ; preds = %29, %7
%33 = phi i64 [ %31, %29 ], [ %4, %7 ]
%34 = phi i8* [ %30, %29 ], [ %3, %7 ]
%35 = call i32 @getaddrinfo(i8* %0, i32 0, %struct.addrinfo* %8, %struct.addrinfo** %9)
switch i32 %35, label %42 [
i32 129, label %36
i32 132, label %39
i32 0, label %45
]
36: ; preds = %32
%37 = load i32, i32* @HOST_NOT_FOUND
store i32 %37, i32* %6
%38 = load i32, i32* @errno
br label %145
39: ; preds = %32
%40 = load i32, i32* @TRY_AGAIN
store i32 %40, i32* %6
%41 = load i32, i32* @errno
br label %145
42: ; preds = %32
%43 = load i32, i32* @NO_RECOVERY
store i32 %43, i32* %6
%44 = load i32, i32* @errno
br label %145
45: ; preds = %32
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 0
store i32 %15, i32* %46
%47 = load i32, i32* @AF_INET6
%48 = icmp eq i32 %15, %47
%49 = select i1 %48, i32 16, i32 4
%50 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 1
store i32 %49, i32* %50
%51 = load %struct.addrinfo*, %struct.addrinfo** %9
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i64 0, i32 1
%53 = load i8*, i8** %52
%54 = icmp eq i8* %53, null
%55 = select i1 %54, i8* %0, i8* %53
%56 = or i32 %49, 8
%57 = zext i32 %56 to i64
br label %58
58: ; preds = %45, %58
%59 = phi i64 [ 32, %45 ], [ %62, %58 ]
%60 = phi i32 [ 1, %45 ], [ %64, %58 ]
%61 = phi %struct.addrinfo* [ %51, %45 ], [ %65, %58 ]
%62 = add i64 %59, %57
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i64 0, i32 3
%64 = add i32 %60, 1
%65 = load %struct.addrinfo*, %struct.addrinfo** %63
%66 = icmp eq %struct.addrinfo* %65, null
br i1 %66, label %67, label %58
67: ; preds = %58
%68 = or i64 %62, 2
%69 = call i64 @strlen(i8* dereferenceable(1) %0)
%70 = call i64 @strlen(i8* dereferenceable(1) %55)
%71 = add i64 %68, %69
%72 = add i64 %71, %70
%73 = icmp ugt i64 %72, %33
br i1 %73, label %74, label %77
74: ; preds = %67
%75 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
%76 = load i32, i32* @ERANGE
br label %145
77: ; preds = %67
%78 = sext i32 %64 to i64
%79 = shl nsw i64 %78, 3
%80 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 2
%81 = bitcast i8*** %80 to i8**
store i8* %34, i8** %81
%82 = getelementptr inbounds i8, i8* %34, i64 24
%83 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 4
%84 = bitcast i8*** %83 to i8**
store i8* %82, i8** %84
%85 = getelementptr inbounds i8, i8* %82, i64 %79
%86 = bitcast i8* %34 to i8**
store i8* %85, i8** %86
%87 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 3
store i8* %85, i8** %87
%88 = call i32 @strcpy(i8* %85, i8* %55)
%89 = load i8*, i8** %87
%90 = call i64 @strlen(i8* dereferenceable(1) %89)
%91 = add nsw i64 %90, 1
%92 = getelementptr inbounds i8, i8* %85, i64 %91
%93 = call i64 @strcmp(i8* %89, i8* %0)
%94 = icmp eq i64 %93, 0
%95 = load i8**, i8*** %80
%96 = getelementptr inbounds i8*, i8** %95, i64 1
br i1 %94, label %108, label %97
97: ; preds = %77
store i8* %92, i8** %96
%98 = load i8**, i8*** %80
%99 = getelementptr inbounds i8*, i8** %98, i64 1
%100 = load i8*, i8** %99
%101 = call i32 @strcpy(i8* %100, i8* %0)
%102 = load i8**, i8*** %80
%103 = getelementptr inbounds i8*, i8** %102, i64 1
%104 = load i8*, i8** %103
%105 = call i64 @strlen(i8* dereferenceable(1) %104)
%106 = add nsw i64 %105, 1
%107 = getelementptr inbounds i8, i8* %92, i64 %106
br label %110
108: ; preds = %77
store i8* null, i8** %96
%109 = load i8**, i8*** %80
br label %110
110: ; preds = %108, %97
%111 = phi i8** [ %102, %97 ], [ %109, %108 ]
%112 = phi i8* [ %107, %97 ], [ %92, %108 ]
%113 = getelementptr inbounds i8*, i8** %111, i64 2
store i8* null, i8** %113
%114 = load %struct.addrinfo*, %struct.addrinfo** %9
%115 = icmp eq %struct.addrinfo* %114, null
br i1 %115, label %139, label %116
116: ; preds = %110, %116
%117 = phi i64 [ %133, %116 ], [ 0, %110 ]
%118 = phi %struct.addrinfo* [ %135, %116 ], [ %114, %110 ]
%119 = phi i8* [ %124, %116 ], [ %112, %110 ]
%120 = load i8**, i8*** %83
%121 = getelementptr inbounds i8*, i8** %120, i64 %117
store i8* %119, i8** %121
%122 = load i32, i32* %50
%123 = sext i32 %122 to i64
%124 = getelementptr inbounds i8, i8* %119, i64 %123
%125 = load i8**, i8*** %83
%126 = getelementptr inbounds i8*, i8** %125, i64 %117
%127 = load i8*, i8** %126
%128 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %118, i64 0, i32 2
%129 = load i64, i64* %128
%130 = inttoptr i64 %129 to %struct.sockaddr_in*
%131 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %130, i64 0, i32 0
%132 = call i32 @memcpy(i8* %127, i32* %131, i32 %122)
%133 = add nuw i64 %117, 1
%134 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %118, i64 0, i32 3
%135 = load %struct.addrinfo*, %struct.addrinfo** %134
%136 = icmp eq %struct.addrinfo* %135, null
br i1 %136, label %137, label %116
137: ; preds = %116
%138 = and i64 %133, 4294967295
br label %139
139: ; preds = %137, %110
%140 = phi i64 [ 0, %110 ], [ %138, %137 ]
%141 = load i8**, i8*** %83
%142 = getelementptr inbounds i8*, i8** %141, i64 %140
store i8* null, i8** %142
store %struct.hostent* %2, %struct.hostent** %5
%143 = load %struct.addrinfo*, %struct.addrinfo** %9
%144 = call i32 @freeaddrinfo(%struct.addrinfo* %143)
br label %145
145: ; preds = %139, %74, %42, %39, %36, %27
%146 = phi i32 [ %28, %27 ], [ %44, %42 ], [ %76, %74 ], [ 0, %139 ], [ %41, %39 ], [ %38, %36 ]
ret i32 %146
}