#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 /*<<< orphan*/ ERANGE;
int HOST_NOT_FOUND;
int NO_RECOVERY;
int TRY_AGAIN;
int /*<<< orphan*/ 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) {
      errno = ERANGE;
      return -1;
    }
    buf += sizeof(char *) - i;
    buflen -= sizeof(char *) - i;
  }
  getaddrinfo(name, 0, &hint, &ai);
  switch (getaddrinfo(name, 0, &hint, &ai)) {
  case EAI_NONAME:
    *err = HOST_NOT_FOUND;
    return -1;
  case EAI_AGAIN:
    *err = TRY_AGAIN;
    return -1;
  default:
  case EAI_MEMORY:
  case EAI_SYSTEM:
  case EAI_FAIL:
    *err = NO_RECOVERY;
    return -1;
  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);
    errno = ERANGE;
    return -1;
  }
  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    $0xc0,%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     1278 <gethostbyname2_r+0x108>
mov    -0x30(%rbp),%rax
movslq -0x74(%rbp),%rdx
mov    $0x8,%ecx
sub    %rdx,%rcx
cmp    %rcx,%rax
jae    124d <gethostbyname2_r+0xdd>
mov    0x2d9a(%rip),%rax
mov    (%rax),%ecx
mov    0x2d69(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    164e <gethostbyname2_r+0x4de>
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
mov    %esi,-0xac(%rbp)
lea    -0x60(%rbp),%rdx
mov    %rdx,-0xa8(%rbp)
lea    -0x68(%rbp),%rcx
mov    %rcx,-0xa0(%rbp)
call   1080 <getaddrinfo@plt>
mov    -0xac(%rbp),%esi
mov    -0xa8(%rbp),%rdx
mov    -0xa0(%rbp),%rcx
mov    -0x10(%rbp),%rdi
call   1080 <getaddrinfo@plt>
mov    %eax,%ecx
mov    %ecx,-0x98(%rbp)
test   %eax,%eax
je     1382 <gethostbyname2_r+0x212>
jmp    12d1 <gethostbyname2_r+0x161>
mov    -0x98(%rbp),%eax
sub    $0x80,%eax
je     1367 <gethostbyname2_r+0x1f7>
jmp    12e7 <gethostbyname2_r+0x177>
mov    -0x98(%rbp),%eax
sub    $0x81,%eax
je     132c <gethostbyname2_r+0x1bc>
jmp    12fd <gethostbyname2_r+0x18d>
mov    -0x98(%rbp),%eax
add    $0xffffff7e,%eax
sub    $0x2,%eax
jb     1367 <gethostbyname2_r+0x1f7>
jmp    1316 <gethostbyname2_r+0x1a6>
mov    -0x98(%rbp),%eax
sub    $0x84,%eax
je     1347 <gethostbyname2_r+0x1d7>
jmp    1362 <gethostbyname2_r+0x1f2>
mov    0x2c95(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    164e <gethostbyname2_r+0x4de>
mov    0x2c6a(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    164e <gethostbyname2_r+0x4de>
jmp    1367 <gethostbyname2_r+0x1f7>
mov    0x2c42(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    164e <gethostbyname2_r+0x4de>
jmp    1387 <gethostbyname2_r+0x217>
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edx
mov    0x2c3e(%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     13d5 <gethostbyname2_r+0x265>
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0xb8(%rbp)
jmp    13e0 <gethostbyname2_r+0x270>
mov    -0x10(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0xb8(%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     143e <gethostbyname2_r+0x2ce>
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    1405 <gethostbyname2_r+0x295>
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    14a0 <gethostbyname2_r+0x330>
mov    -0x68(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    0x2b47(%rip),%rax
mov    (%rax),%ecx
mov    0x2b16(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    164e <gethostbyname2_r+0x4de>
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     157e <gethostbyname2_r+0x40e>
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    158e <gethostbyname2_r+0x41e>
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     161f <gethostbyname2_r+0x4af>
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    15ad <gethostbyname2_r+0x43d>
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    $0xc0,%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    %rcx,%r12
mov    %rdx,%r14
mov    %esi,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x40(%rsp)
movq   $0x0,0x50(%rsp)
mov    0x2e45(%rip),%rax
cmp    %esi,(%rax)
je     11a0 <gethostbyname2_r+0x40>
mov    0x2e4a(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,0x30(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x50(%rsp)
mov    %r12d,%ecx
and    $0x7,%ecx
je     11db <gethostbyname2_r+0x7b>
mov    $0x8,%eax
sub    %ecx,%eax
cmp    %r8,%rax
ja     12be <gethostbyname2_r+0x15e>
mov    %r9,0x10(%rsp)
add    %rax,%r12
sub    %rax,%r8
mov    %r8,0x18(%rsp)
jmp    11e5 <gethostbyname2_r+0x85>
mov    %r8,0x18(%rsp)
mov    %r9,0x10(%rsp)
lea    0x30(%rsp),%rbx
lea    0x8(%rsp),%r15
mov    %rdi,%r13
xor    %esi,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1070 <getaddrinfo@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     123d <gethostbyname2_r+0xdd>
mov    0x90(%rsp),%rcx
cmp    $0x84,%eax
je     12d0 <gethostbyname2_r+0x170>
cmp    $0x81,%eax
jne    12d9 <gethostbyname2_r+0x179>
mov    0x2d90(%rip),%rax
jmp    12e0 <gethostbyname2_r+0x180>
mov    %ebp,(%r14)
mov    0x2d91(%rip),%rax
cmp    (%rax),%ebp
mov    $0x10,%ecx
mov    $0x4,%eax
cmove  %ecx,%eax
mov    %eax,0x4(%r14)
mov    0x8(%rsp),%rcx
mov    0x8(%rcx),%rbp
test   %rbp,%rbp
cmove  %r13,%rbp
or     $0x8,%eax
mov    $0x1,%r15d
mov    $0x22,%ebx
mov    %rcx,0x20(%rsp)
nopl   (%rax)
add    %rax,%rbx
add    $0x1,%r15d
mov    0x18(%rcx),%rcx
test   %rcx,%rcx
jne    1280 <gethostbyname2_r+0x120>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %rax,0x28(%rsp)
mov    %rbp,%rdi
call   1040 <strlen@plt>
add    0x28(%rsp),%rax
add    %rbx,%rax
cmp    0x18(%rsp),%rax
jbe    12fc <gethostbyname2_r+0x19c>
mov    0x20(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%eax
mov    0x2cda(%rip),%rcx
jmp    12e2 <gethostbyname2_r+0x182>
mov    0x2ce1(%rip),%rax
jmp    12e0 <gethostbyname2_r+0x180>
mov    0x2cd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rcx)
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movslq %r15d,%rax
mov    %r12,0x8(%r14)
lea    0x18(%r12),%rcx
mov    %rcx,0x18(%r14)
lea    (%r12,%rax,8),%rbx
add    $0x18,%rbx
mov    %rbx,(%r12)
mov    %rbx,0x10(%r14)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    0x10(%r14),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbx,1),%r12
add    $0x1,%r12
mov    %rbp,%rdi
mov    %r13,%rsi
call   1050 <strcmp@plt>
mov    0x8(%r14),%rcx
test   %rax,%rax
je     1379 <gethostbyname2_r+0x219>
mov    %r12,0x8(%rcx)
mov    0x8(%r14),%rax
mov    0x8(%rax),%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    0x8(%r14),%rbx
mov    0x8(%rbx),%rdi
call   1040 <strlen@plt>
add    %rax,%r12
add    $0x1,%r12
jmp    1385 <gethostbyname2_r+0x225>
movq   $0x0,0x8(%rcx)
mov    0x8(%r14),%rbx
movq   $0x0,0x10(%rbx)
mov    0x8(%rsp),%rbp
xor    %r15d,%r15d
test   %rbp,%rbp
je     13d1 <gethostbyname2_r+0x271>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    0x18(%r14),%rax
mov    %r12,(%rax,%rbx,8)
movslq 0x4(%r14),%rdx
add    %rdx,%r12
mov    0x18(%r14),%rax
mov    (%rax,%rbx,8),%rdi
mov    0x10(%rbp),%rsi
call   1060 <memcpy@plt>
add    $0x1,%rbx
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
jne    13a0 <gethostbyname2_r+0x240>
mov    %ebx,%eax
jmp    13d3 <gethostbyname2_r+0x273>
xor    %eax,%eax
mov    0x18(%r14),%rcx
movq   $0x0,(%rcx,%rax,8)
mov    0x10(%rsp),%rax
mov    %r14,(%rax)
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
jmp    12ea <gethostbyname2_r+0x18a>

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

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