#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;
  *res = 0;
  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    -0x38(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x60(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rax
and    $0x7,%rax
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
je     1276 <gethostbyname2_r+0x106>
mov    -0x30(%rbp),%rax
movslq -0x74(%rbp),%rdx
mov    $0x8,%ecx
sub    %rdx,%rcx
cmp    %rcx,%rax
jae    124b <gethostbyname2_r+0xdb>
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    161d <gethostbyname2_r+0x4ad>
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     135e <gethostbyname2_r+0x1ee>
jmp    129e <gethostbyname2_r+0x12e>
mov    -0x98(%rbp),%eax
sub    $0x80,%eax
je     133e <gethostbyname2_r+0x1ce>
jmp    12b4 <gethostbyname2_r+0x144>
mov    -0x98(%rbp),%eax
sub    $0x81,%eax
je     12f9 <gethostbyname2_r+0x189>
jmp    12ca <gethostbyname2_r+0x15a>
mov    -0x98(%rbp),%eax
add    $0xffffff7e,%eax
sub    $0x2,%eax
jb     133e <gethostbyname2_r+0x1ce>
jmp    12e3 <gethostbyname2_r+0x173>
mov    -0x98(%rbp),%eax
sub    $0x84,%eax
je     1319 <gethostbyname2_r+0x1a9>
jmp    1339 <gethostbyname2_r+0x1c9>
mov    0x2cc8(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2c99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    161d <gethostbyname2_r+0x4ad>
mov    0x2c98(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2c79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    161d <gethostbyname2_r+0x4ad>
jmp    133e <gethostbyname2_r+0x1ce>
mov    0x2c6b(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2c54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    161d <gethostbyname2_r+0x4ad>
jmp    1363 <gethostbyname2_r+0x1f3>
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edx
mov    0x2c62(%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     13b1 <gethostbyname2_r+0x241>
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0xa0(%rbp)
jmp    13bc <gethostbyname2_r+0x24c>
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     141a <gethostbyname2_r+0x2aa>
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    13e1 <gethostbyname2_r+0x271>
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    146f <gethostbyname2_r+0x2ff>
mov    -0x68(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    0x2b6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    161d <gethostbyname2_r+0x4ad>
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     154d <gethostbyname2_r+0x3dd>
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    155d <gethostbyname2_r+0x3ed>
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     15ee <gethostbyname2_r+0x47e>
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    157c <gethostbyname2_r+0x40c>
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)
movq   $0x0,(%r9)
mov    %r12d,%ecx
and    $0x7,%ecx
je     11e4 <gethostbyname2_r+0x84>
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    11e9 <gethostbyname2_r+0x89>
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     122b <gethostbyname2_r+0xcb>
mov    0x90(%rsp),%rcx
cmp    $0x84,%eax
je     12b7 <gethostbyname2_r+0x157>
cmp    $0x81,%eax
jne    12c0 <gethostbyname2_r+0x160>
mov    0x2da2(%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
xchg   %ax,%ax
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 = load %struct.hostent**, %struct.hostent*** %14
store %struct.hostent* null, %struct.hostent** %35
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 0
%37 = load i32, i32* %36
store i32 %37, i32* %10
%38 = load i8*, i8** %12
%39 = ptrtoint i8* %38 to i64
%40 = and i64 %39, 7
%41 = trunc i64 %40 to i32
store i32 %41, i32* %19
%42 = load i32, i32* %19
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %63
44: ; preds = %31
%45 = load i64, i64* %13
%46 = load i32, i32* %19
%47 = sext i32 %46 to i64
%48 = sub i64 8, %47
%49 = icmp ult i64 %45, %48
br i1 %49, label %50, label %52
50: ; preds = %44
%51 = load i32, i32* @ERANGE
store i32 %51, i32* %8
br label %264
52: ; preds = %44
%53 = load i32, i32* %19
%54 = sext i32 %53 to i64
%55 = sub i64 8, %54
%56 = load i8*, i8** %12
%57 = getelementptr inbounds i8, i8* %56, i64 %55
store i8* %57, i8** %12
%58 = load i32, i32* %19
%59 = sext i32 %58 to i64
%60 = sub i64 8, %59
%61 = load i64, i64* %13
%62 = sub i64 %61, %60
store i64 %62, i64* %13
br label %63
63: ; preds = %52, %31
%64 = load i8*, i8** %9
%65 = call i32 @getaddrinfo(i8* %64, i32 0, %struct.addrinfo* %16, %struct.addrinfo** %17)
switch i32 %65, label %74 [
i32 129, label %66
i32 132, label %70
i32 130, label %75
i32 128, label %75
i32 131, label %75
i32 0, label %79
]
66: ; preds = %63
%67 = load i32, i32* @HOST_NOT_FOUND
%68 = load i32*, i32** %15
store i32 %67, i32* %68
%69 = load i32, i32* @errno
store i32 %69, i32* %8
br label %264
70: ; preds = %63
%71 = load i32, i32* @TRY_AGAIN
%72 = load i32*, i32** %15
store i32 %71, i32* %72
%73 = load i32, i32* @errno
store i32 %73, i32* %8
br label %264
74: ; preds = %63
br label %75
75: ; preds = %63, %63, %63, %74
%76 = load i32, i32* @NO_RECOVERY
%77 = load i32*, i32** %15
store i32 %76, i32* %77
%78 = load i32, i32* @errno
store i32 %78, i32* %8
br label %264
79: ; preds = %63
br label %80
80: ; preds = %79
%81 = load i32, i32* %10
%82 = load %struct.hostent*, %struct.hostent** %11
%83 = getelementptr inbounds %struct.hostent, %struct.hostent* %82, i32 0, i32 0
store i32 %81, i32* %83
%84 = load i32, i32* %10
%85 = load i32, i32* @AF_INET6
%86 = icmp eq i32 %84, %85
%87 = zext i1 %86 to i64
%88 = select i1 %86, i32 16, i32 4
%89 = load %struct.hostent*, %struct.hostent** %11
%90 = getelementptr inbounds %struct.hostent, %struct.hostent* %89, i32 0, i32 1
store i32 %88, i32* %90
%91 = load %struct.addrinfo*, %struct.addrinfo** %17
%92 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %91, i32 0, i32 1
%93 = load i8*, i8** %92
%94 = icmp ne i8* %93, null
br i1 %94, label %95, label %99
95: ; preds = %80
%96 = load %struct.addrinfo*, %struct.addrinfo** %17
%97 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %96, i32 0, i32 1
%98 = load i8*, i8** %97
br label %101
99: ; preds = %80
%100 = load i8*, i8** %9
br label %101
101: ; preds = %99, %95
%102 = phi i8* [ %98, %95 ], [ %100, %99 ]
store i8* %102, i8** %21
store i64 32, i64* %20
store i32 0, i32* %19
%103 = load %struct.addrinfo*, %struct.addrinfo** %17
store %struct.addrinfo* %103, %struct.addrinfo** %18
br label %104
104: ; preds = %115, %101
%105 = load %struct.addrinfo*, %struct.addrinfo** %18
%106 = icmp ne %struct.addrinfo* %105, null
br i1 %106, label %107, label %121
107: ; preds = %104
%108 = load %struct.hostent*, %struct.hostent** %11
%109 = getelementptr inbounds %struct.hostent, %struct.hostent* %108, i32 0, i32 1
%110 = load i32, i32* %109
%111 = sext i32 %110 to i64
%112 = add i64 8, %111
%113 = load i64, i64* %20
%114 = add i64 %113, %112
store i64 %114, i64* %20
br label %115
115: ; preds = %107
%116 = load i32, i32* %19
%117 = add nsw i32 %116, 1
store i32 %117, i32* %19
%118 = load %struct.addrinfo*, %struct.addrinfo** %18
%119 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %118, i32 0, i32 3
%120 = load %struct.addrinfo*, %struct.addrinfo** %119
store %struct.addrinfo* %120, %struct.addrinfo** %18
br label %104
121: ; preds = %104
%122 = load i8*, i8** %9
%123 = call i64 @strlen(i8* %122)
%124 = add nsw i64 %123, 1
%125 = load i64, i64* %20
%126 = add i64 %125, %124
store i64 %126, i64* %20
%127 = load i8*, i8** %21
%128 = call i64 @strlen(i8* %127)
%129 = add nsw i64 %128, 1
%130 = load i64, i64* %20
%131 = add i64 %130, %129
store i64 %131, i64* %20
%132 = load i64, i64* %20
%133 = load i64, i64* %13
%134 = icmp ugt i64 %132, %133
br i1 %134, label %135, label %139
135: ; preds = %121
%136 = load %struct.addrinfo*, %struct.addrinfo** %17
%137 = call i32 @freeaddrinfo(%struct.addrinfo* %136)
%138 = load i32, i32* @ERANGE
store i32 %138, i32* %8
br label %264
139: ; preds = %121
%140 = load i8*, i8** %12
%141 = bitcast i8* %140 to i8**
%142 = load %struct.hostent*, %struct.hostent** %11
%143 = getelementptr inbounds %struct.hostent, %struct.hostent* %142, i32 0, i32 2
store i8** %141, i8*** %143
%144 = load i8*, i8** %12
%145 = getelementptr inbounds i8, i8* %144, i64 24
store i8* %145, i8** %12
%146 = load i8*, i8** %12
%147 = bitcast i8* %146 to i8**
%148 = load %struct.hostent*, %struct.hostent** %11
%149 = getelementptr inbounds %struct.hostent, %struct.hostent* %148, i32 0, i32 4
store i8** %147, i8*** %149
%150 = load i32, i32* %19
%151 = add nsw i32 %150, 1
%152 = sext i32 %151 to i64
%153 = mul i64 %152, 8
%154 = load i8*, i8** %12
%155 = getelementptr inbounds i8, i8* %154, i64 %153
store i8* %155, i8** %12
%156 = load i8*, i8** %12
%157 = load %struct.hostent*, %struct.hostent** %11
%158 = getelementptr inbounds %struct.hostent, %struct.hostent* %157, i32 0, i32 2
%159 = load i8**, i8*** %158
%160 = getelementptr inbounds i8*, i8** %159, i64 0
store i8* %156, i8** %160
%161 = load %struct.hostent*, %struct.hostent** %11
%162 = getelementptr inbounds %struct.hostent, %struct.hostent* %161, i32 0, i32 3
store i8* %156, i8** %162
%163 = load %struct.hostent*, %struct.hostent** %11
%164 = getelementptr inbounds %struct.hostent, %struct.hostent* %163, i32 0, i32 3
%165 = load i8*, i8** %164
%166 = load i8*, i8** %21
%167 = call i32 @strcpy(i8* %165, i8* %166)
%168 = load %struct.hostent*, %struct.hostent** %11
%169 = getelementptr inbounds %struct.hostent, %struct.hostent* %168, i32 0, i32 3
%170 = load i8*, i8** %169
%171 = call i64 @strlen(i8* %170)
%172 = add nsw i64 %171, 1
%173 = load i8*, i8** %12
%174 = getelementptr inbounds i8, i8* %173, i64 %172
store i8* %174, i8** %12
%175 = load %struct.hostent*, %struct.hostent** %11
%176 = getelementptr inbounds %struct.hostent, %struct.hostent* %175, i32 0, i32 3
%177 = load i8*, i8** %176
%178 = load i8*, i8** %9
%179 = call i64 @strcmp(i8* %177, i8* %178)
%180 = icmp ne i64 %179, 0
br i1 %180, label %181, label %203
181: ; preds = %139
%182 = load i8*, i8** %12
%183 = load %struct.hostent*, %struct.hostent** %11
%184 = getelementptr inbounds %struct.hostent, %struct.hostent* %183, i32 0, i32 2
%185 = load i8**, i8*** %184
%186 = getelementptr inbounds i8*, i8** %185, i64 1
store i8* %182, i8** %186
%187 = load %struct.hostent*, %struct.hostent** %11
%188 = getelementptr inbounds %struct.hostent, %struct.hostent* %187, i32 0, i32 2
%189 = load i8**, i8*** %188
%190 = getelementptr inbounds i8*, i8** %189, i64 1
%191 = load i8*, i8** %190
%192 = load i8*, i8** %9
%193 = call i32 @strcpy(i8* %191, i8* %192)
%194 = load %struct.hostent*, %struct.hostent** %11
%195 = getelementptr inbounds %struct.hostent, %struct.hostent* %194, i32 0, i32 2
%196 = load i8**, i8*** %195
%197 = getelementptr inbounds i8*, i8** %196, i64 1
%198 = load i8*, i8** %197
%199 = call i64 @strlen(i8* %198)
%200 = add nsw i64 %199, 1
%201 = load i8*, i8** %12
%202 = getelementptr inbounds i8, i8* %201, i64 %200
store i8* %202, i8** %12
br label %208
203: ; preds = %139
%204 = load %struct.hostent*, %struct.hostent** %11
%205 = getelementptr inbounds %struct.hostent, %struct.hostent* %204, i32 0, i32 2
%206 = load i8**, i8*** %205
%207 = getelementptr inbounds i8*, i8** %206, i64 1
store i8* null, i8** %207
br label %208
208: ; preds = %203, %181
%209 = load %struct.hostent*, %struct.hostent** %11
%210 = getelementptr inbounds %struct.hostent, %struct.hostent* %209, i32 0, i32 2
%211 = load i8**, i8*** %210
%212 = getelementptr inbounds i8*, i8** %211, i64 2
store i8* null, i8** %212
store i32 0, i32* %19
%213 = load %struct.addrinfo*, %struct.addrinfo** %17
store %struct.addrinfo* %213, %struct.addrinfo** %18
br label %214
214: ; preds = %247, %208
%215 = load %struct.addrinfo*, %struct.addrinfo** %18
%216 = icmp ne %struct.addrinfo* %215, null
br i1 %216, label %217, label %253
217: ; preds = %214
%218 = load i8*, i8** %12
%219 = load %struct.hostent*, %struct.hostent** %11
%220 = getelementptr inbounds %struct.hostent, %struct.hostent* %219, i32 0, i32 4
%221 = load i8**, i8*** %220
%222 = load i32, i32* %19
%223 = sext i32 %222 to i64
%224 = getelementptr inbounds i8*, i8** %221, i64 %223
store i8* %218, i8** %224
%225 = load %struct.hostent*, %struct.hostent** %11
%226 = getelementptr inbounds %struct.hostent, %struct.hostent* %225, i32 0, i32 1
%227 = load i32, i32* %226
%228 = load i8*, i8** %12
%229 = sext i32 %227 to i64
%230 = getelementptr inbounds i8, i8* %228, i64 %229
store i8* %230, i8** %12
%231 = load %struct.hostent*, %struct.hostent** %11
%232 = getelementptr inbounds %struct.hostent, %struct.hostent* %231, i32 0, i32 4
%233 = load i8**, i8*** %232
%234 = load i32, i32* %19
%235 = sext i32 %234 to i64
%236 = getelementptr inbounds i8*, i8** %233, i64 %235
%237 = load i8*, i8** %236
%238 = load %struct.addrinfo*, %struct.addrinfo** %18
%239 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %238, i32 0, i32 2
%240 = load i64, i64* %239
%241 = inttoptr i64 %240 to %struct.sockaddr_in*
%242 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %241, i32 0, i32 0
%243 = load %struct.hostent*, %struct.hostent** %11
%244 = getelementptr inbounds %struct.hostent, %struct.hostent* %243, i32 0, i32 1
%245 = load i32, i32* %244
%246 = call i32 @memcpy(i8* %237, i32* %242, i32 %245)
br label %247
247: ; preds = %217
%248 = load i32, i32* %19
%249 = add nsw i32 %248, 1
store i32 %249, i32* %19
%250 = load %struct.addrinfo*, %struct.addrinfo** %18
%251 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %250, i32 0, i32 3
%252 = load %struct.addrinfo*, %struct.addrinfo** %251
store %struct.addrinfo* %252, %struct.addrinfo** %18
br label %214
253: ; preds = %214
%254 = load %struct.hostent*, %struct.hostent** %11
%255 = getelementptr inbounds %struct.hostent, %struct.hostent* %254, i32 0, i32 4
%256 = load i8**, i8*** %255
%257 = load i32, i32* %19
%258 = sext i32 %257 to i64
%259 = getelementptr inbounds i8*, i8** %256, i64 %258
store i8* null, i8** %259
%260 = load %struct.hostent*, %struct.hostent** %11
%261 = load %struct.hostent**, %struct.hostent*** %14
store %struct.hostent* %260, %struct.hostent** %261
%262 = load %struct.addrinfo*, %struct.addrinfo** %17
%263 = call i32 @freeaddrinfo(%struct.addrinfo* %262)
store i32 0, i32* %8
br label %264
264: ; preds = %253, %135, %75, %70, %66, %50
%265 = load i32, i32* %8
ret i32 %265
}

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*
store %struct.hostent* null, %struct.hostent** %5
%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
}