#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 dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  int h_addrtype;
  int h_length;
  char **h_addr_list;
  char **h_aliases;
  char *h_name;
};
typedef scalar_t__ int32_t;
typedef enum nss_status { ____Placeholder_nss_status } nss_status;

/* Variables and functions */
int AF_INET;
int EINVAL;
int NO_RECOVERY;
int NSS_STATUS_SUCCESS;
int NSS_STATUS_UNAVAIL;
scalar_t__ dnsq(struct sockaddr_in **, char const *, struct in_addr *,
                unsigned int *, char *);
scalar_t__ get_dnss_for_domain(struct sockaddr_in ***, char const *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
enum nss_status _nss_dnspq_gethostbyname3_r(const char *name, int af,
                                            struct hostent *host, char *buf,
                                            size_t buflen, int *errnop,
                                            int *h_errnop, int32_t *ttlp,
                                            char **canonp) {
  unsigned int ttl;
  char sid;
  struct sockaddr_in **dnsservers = NULL;
  size_t nlen = 0;
  if (af == AF_INET && (nlen = strlen(name)) > 0 &&
      buflen >= nlen + 1 + 2 * sizeof(void *) + sizeof(struct in_addr) +
                    sizeof(void *) &&
      get_dnss_for_domain(&dnsservers, name) &&
      dnsq(dnsservers, name, (struct in_addr *)buf, &ttl, &sid) == 0) {
    host->h_addrtype = af;
    host->h_length = sizeof(struct in_addr);
    host->h_addr_list = (char **)buf + sizeof(struct in_addr);
    host->h_addr_list[0] = buf;
    host->h_addr_list[1] = NULL;
    host->h_aliases = (char **)&host->h_addr_list[2];
    host->h_aliases[0] = NULL;
    host->h_name = (char *)&host->h_aliases[1];
    memcpy(host->h_name, name, nlen + 1);
    if (ttlp != NULL)
      *ttlp = (int32_t)ttl;
    if (canonp != NULL)
      *canonp = host->h_name;
    *errnop = 0;
    *h_errnop = 0;
    return NSS_STATUS_SUCCESS;
  }
  *errnop = EINVAL;
  *h_errnop = NO_RECOVERY;
  return NSS_STATUS_UNAVAIL;
}


x86 assembly (clang, -O0):
<_nss_dnspq_gethostbyname3_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
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)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%eax
jne    12e8 <_nss_dnspq_gethostbyname3_r+0x1a8>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
jbe    12e8 <_nss_dnspq_gethostbyname3_r+0x1a8>
mov    -0x30(%rbp),%rax
mov    -0x50(%rbp),%rcx
add    $0x1,%rcx
add    $0x10,%rcx
add    $0x4,%rcx
add    $0x8,%rcx
cmp    %rcx,%rax
jb     12e8 <_nss_dnspq_gethostbyname3_r+0x1a8>
mov    -0x10(%rbp),%rsi
lea    -0x48(%rbp),%rdi
call   1030 <get_dnss_for_domain@plt>
cmp    $0x0,%rax
je     12e8 <_nss_dnspq_gethostbyname3_r+0x1a8>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    -0x3c(%rbp),%rcx
lea    -0x3d(%rbp),%r8
call   1060 <dnsq@plt>
cmp    $0x0,%rax
jne    12e8 <_nss_dnspq_gethostbyname3_r+0x1a8>
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x4,0x4(%rax)
mov    -0x28(%rbp),%rcx
add    $0x20,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
movq   $0x0,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x10,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rcx
add    $0x8,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x50(%rbp),%rdx
add    $0x1,%rdx
call   1050 <memcpy@plt>
cmpq   $0x0,0x18(%rbp)
je     12a9 <_nss_dnspq_gethostbyname3_r+0x169>
mov    -0x3c(%rbp),%eax
mov    %eax,%ecx
mov    0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,0x20(%rbp)
je     12c3 <_nss_dnspq_gethostbyname3_r+0x183>
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rcx
mov    0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
movl   $0x0,(%rax)
mov    0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    0x2cfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1312 <_nss_dnspq_gethostbyname3_r+0x1d2>
mov    0x2cd1(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2cca(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2cd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nss_dnspq_gethostbyname3_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r15
mov    %rdx,0x10(%rsp)
mov    0x50(%rsp),%rdx
movq   $0x0,0x8(%rsp)
mov    0x2e7d(%rip),%rax
cmp    %esi,(%rax)
jne    11d0 <_nss_dnspq_gethostbyname3_r+0x90>
mov    %r8,%r14
mov    %rcx,%r12
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    0x50(%rsp),%rdx
test   %rax,%rax
je     11d0 <_nss_dnspq_gethostbyname3_r+0x90>
mov    %rax,%r13
add    $0x1d,%rax
cmp    %r14,%rax
ja     11d0 <_nss_dnspq_gethostbyname3_r+0x90>
lea    0x8(%rsp),%rdi
mov    %rbx,%rsi
call   1030 <get_dnss_for_domain@plt>
mov    0x50(%rsp),%rdx
test   %rax,%rax
je     11d0 <_nss_dnspq_gethostbyname3_r+0x90>
mov    0x8(%rsp),%rdi
lea    0x4(%rsp),%rcx
lea    0x3(%rsp),%r8
mov    %rbx,%rsi
mov    %r12,%rdx
call   1060 <dnsq@plt>
mov    0x50(%rsp),%rdx
test   %rax,%rax
je     11ff <_nss_dnspq_gethostbyname3_r+0xbf>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r15)
mov    0x2de5(%rip),%rax
mov    (%rax),%ecx
mov    0x2df4(%rip),%rax
mov    %ecx,(%rdx)
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x58(%rsp),%r14
add    $0x1,%r13
mov    0x10(%rsp),%rax
mov    %ebp,(%rax)
mov    %rax,%rbp
movl   $0x4,0x4(%rax)
lea    0x20(%r12),%rax
mov    %rax,0x8(%rbp)
mov    %r12,0x20(%r12)
mov    0x8(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    0x8(%rbp),%rax
lea    0x10(%rax),%rcx
mov    %rcx,0x10(%rbp)
movq   $0x0,0x10(%rax)
mov    0x10(%rbp),%rdi
add    $0x8,%rdi
mov    %rdi,0x18(%rbp)
mov    %rbx,%rsi
mov    %r13,%rdx
call   1050 <memcpy@plt>
test   %r14,%r14
je     126a <_nss_dnspq_gethostbyname3_r+0x12a>
mov    0x4(%rsp),%eax
mov    %rax,(%r14)
mov    0x60(%rsp),%rcx
test   %rcx,%rcx
je     127b <_nss_dnspq_gethostbyname3_r+0x13b>
mov    0x18(%rbp),%rax
mov    %rax,(%rcx)
movl   $0x0,(%r15)
mov    0x2d4f(%rip),%rax
xor    %ecx,%ecx
mov    0x50(%rsp),%rdx
jmp    11ec <_nss_dnspq_gethostbyname3_r+0xac>

LLVM IR (-O0):
%struct.hostent = type { i32, i32, i8**, i8**, i8* }
%struct.sockaddr_in = type { i32 }
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@NSS_STATUS_SUCCESS = global i32 0
@EINVAL = global i32 0
@NO_RECOVERY = global i32 0
@NSS_STATUS_UNAVAIL = global i32 0
define i32 @_nss_dnspq_gethostbyname3_r(i8* %0, i32 %1, %struct.hostent* %2, i8* %3, i64 %4, i32* %5, i32* %6, i64* %7, i8** %8) {
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca %struct.hostent*
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i32*
%17 = alloca i32*
%18 = alloca i64*
%19 = alloca i8**
%20 = alloca i32
%21 = alloca i8
%22 = alloca %struct.sockaddr_in**
%23 = alloca i64
store i8* %0, i8** %11
store i32 %1, i32* %12
store %struct.hostent* %2, %struct.hostent** %13
store i8* %3, i8** %14
store i64 %4, i64* %15
store i32* %5, i32** %16
store i32* %6, i32** %17
store i64* %7, i64** %18
store i8** %8, i8*** %19
store %struct.sockaddr_in** null, %struct.sockaddr_in*** %22
store i64 0, i64* %23
%24 = load i32, i32* %12
%25 = load i32, i32* @AF_INET
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %112
27: ; preds = %9
%28 = load i8*, i8** %11
%29 = call i64 @strlen(i8* %28)
store i64 %29, i64* %23
%30 = icmp ugt i64 %29, 0
br i1 %30, label %31, label %112
31: ; preds = %27
%32 = load i64, i64* %15
%33 = load i64, i64* %23
%34 = add i64 %33, 1
%35 = add i64 %34, 16
%36 = add i64 %35, 4
%37 = add i64 %36, 8
%38 = icmp uge i64 %32, %37
br i1 %38, label %39, label %112
39: ; preds = %31
%40 = load i8*, i8** %11
%41 = call i64 @get_dnss_for_domain(%struct.sockaddr_in*** %22, i8* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %112
43: ; preds = %39
%44 = load %struct.sockaddr_in**, %struct.sockaddr_in*** %22
%45 = load i8*, i8** %11
%46 = load i8*, i8** %14
%47 = bitcast i8* %46 to %struct.in_addr*
%48 = call i64 @dnsq(%struct.sockaddr_in** %44, i8* %45, %struct.in_addr* %47, i32* %20, i8* %21)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %112
50: ; preds = %43
%51 = load i32, i32* %12
%52 = load %struct.hostent*, %struct.hostent** %13
%53 = getelementptr inbounds %struct.hostent, %struct.hostent* %52, i32 0, i32 0
store i32 %51, i32* %53
%54 = load %struct.hostent*, %struct.hostent** %13
%55 = getelementptr inbounds %struct.hostent, %struct.hostent* %54, i32 0, i32 1
store i32 4, i32* %55
%56 = load i8*, i8** %14
%57 = bitcast i8* %56 to i8**
%58 = getelementptr inbounds i8*, i8** %57, i64 4
%59 = load %struct.hostent*, %struct.hostent** %13
%60 = getelementptr inbounds %struct.hostent, %struct.hostent* %59, i32 0, i32 2
store i8** %58, i8*** %60
%61 = load i8*, i8** %14
%62 = load %struct.hostent*, %struct.hostent** %13
%63 = getelementptr inbounds %struct.hostent, %struct.hostent* %62, i32 0, i32 2
%64 = load i8**, i8*** %63
%65 = getelementptr inbounds i8*, i8** %64, i64 0
store i8* %61, i8** %65
%66 = load %struct.hostent*, %struct.hostent** %13
%67 = getelementptr inbounds %struct.hostent, %struct.hostent* %66, i32 0, i32 2
%68 = load i8**, i8*** %67
%69 = getelementptr inbounds i8*, i8** %68, i64 1
store i8* null, i8** %69
%70 = load %struct.hostent*, %struct.hostent** %13
%71 = getelementptr inbounds %struct.hostent, %struct.hostent* %70, i32 0, i32 2
%72 = load i8**, i8*** %71
%73 = getelementptr inbounds i8*, i8** %72, i64 2
%74 = load %struct.hostent*, %struct.hostent** %13
%75 = getelementptr inbounds %struct.hostent, %struct.hostent* %74, i32 0, i32 3
store i8** %73, i8*** %75
%76 = load %struct.hostent*, %struct.hostent** %13
%77 = getelementptr inbounds %struct.hostent, %struct.hostent* %76, i32 0, i32 3
%78 = load i8**, i8*** %77
%79 = getelementptr inbounds i8*, i8** %78, i64 0
store i8* null, i8** %79
%80 = load %struct.hostent*, %struct.hostent** %13
%81 = getelementptr inbounds %struct.hostent, %struct.hostent* %80, i32 0, i32 3
%82 = load i8**, i8*** %81
%83 = getelementptr inbounds i8*, i8** %82, i64 1
%84 = bitcast i8** %83 to i8*
%85 = load %struct.hostent*, %struct.hostent** %13
%86 = getelementptr inbounds %struct.hostent, %struct.hostent* %85, i32 0, i32 4
store i8* %84, i8** %86
%87 = load %struct.hostent*, %struct.hostent** %13
%88 = getelementptr inbounds %struct.hostent, %struct.hostent* %87, i32 0, i32 4
%89 = load i8*, i8** %88
%90 = load i8*, i8** %11
%91 = load i64, i64* %23
%92 = add i64 %91, 1
%93 = call i32 @memcpy(i8* %89, i8* %90, i64 %92)
%94 = load i64*, i64** %18
%95 = icmp ne i64* %94, null
br i1 %95, label %96, label %100
96: ; preds = %50
%97 = load i32, i32* %20
%98 = zext i32 %97 to i64
%99 = load i64*, i64** %18
store i64 %98, i64* %99
br label %100
100: ; preds = %96, %50
%101 = load i8**, i8*** %19
%102 = icmp ne i8** %101, null
br i1 %102, label %103, label %108
103: ; preds = %100
%104 = load %struct.hostent*, %struct.hostent** %13
%105 = getelementptr inbounds %struct.hostent, %struct.hostent* %104, i32 0, i32 4
%106 = load i8*, i8** %105
%107 = load i8**, i8*** %19
store i8* %106, i8** %107
br label %108
108: ; preds = %103, %100
%109 = load i32*, i32** %16
store i32 0, i32* %109
%110 = load i32*, i32** %17
store i32 0, i32* %110
%111 = load i32, i32* @NSS_STATUS_SUCCESS
store i32 %111, i32* %10
br label %118
112: ; preds = %43, %39, %31, %27, %9
%113 = load i32, i32* @EINVAL
%114 = load i32*, i32** %16
store i32 %113, i32* %114
%115 = load i32, i32* @NO_RECOVERY
%116 = load i32*, i32** %17
store i32 %115, i32* %116
%117 = load i32, i32* @NSS_STATUS_UNAVAIL
store i32 %117, i32* %10
br label %118
118: ; preds = %112, %108
%119 = load i32, i32* %10
ret i32 %119
}

LLVM IR (-O2):
%struct.hostent = type { i32, i32, i8**, i8**, i8* }
%struct.sockaddr_in = type { i32 }
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@NSS_STATUS_SUCCESS = global i32 0
@EINVAL = global i32 0
@NO_RECOVERY = global i32 0
@NSS_STATUS_UNAVAIL = global i32 0
define i32 @_nss_dnspq_gethostbyname3_r(i8* %0, i32 %1, %struct.hostent* nocapture %2, i8* %3, i64 %4, i32* nocapture writeonly %5, i32* nocapture writeonly %6, i64* writeonly %7, i8** writeonly %8) {
%10 = alloca i32
%11 = alloca i8
%12 = alloca %struct.sockaddr_in**
%13 = bitcast i32* %10 to i8*
%14 = bitcast %struct.sockaddr_in*** %12 to i8*
store %struct.sockaddr_in** null, %struct.sockaddr_in*** %12
%15 = load i32, i32* @AF_INET
%16 = icmp eq i32 %15, %1
br i1 %16, label %17, label %59
17: ; preds = %9
%18 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%19 = icmp eq i64 %18, 0
br i1 %19, label %59, label %20
20: ; preds = %17
%21 = add i64 %18, 1
%22 = add i64 %18, 29
%23 = icmp ugt i64 %22, %4
br i1 %23, label %59, label %24
24: ; preds = %20
%25 = call i64 @get_dnss_for_domain(%struct.sockaddr_in*** %12, i8* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %59, label %27
27: ; preds = %24
%28 = load %struct.sockaddr_in**, %struct.sockaddr_in*** %12
%29 = bitcast i8* %3 to %struct.in_addr*
%30 = call i64 @dnsq(%struct.sockaddr_in** %28, i8* %0, %struct.in_addr* %29, i32* %10, i8* %11)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %59
32: ; preds = %27
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 0
store i32 %1, i32* %33
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 1
store i32 4, i32* %34
%35 = getelementptr inbounds i8, i8* %3, i64 32
%36 = bitcast i8* %35 to i8**
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 2
%38 = bitcast i8*** %37 to i8**
store i8* %35, i8** %38
store i8* %3, i8** %36
%39 = load i8**, i8*** %37
%40 = getelementptr inbounds i8*, i8** %39, i64 1
store i8* null, i8** %40
%41 = load i8**, i8*** %37
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 3
store i8** %42, i8*** %43
store i8* null, i8** %42
%44 = load i8**, i8*** %43
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = bitcast i8** %45 to i8*
%47 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 4
%48 = bitcast i8** %47 to i8***
store i8** %45, i8*** %48
%49 = call i32 @memcpy(i8* %46, i8* %0, i64 %21)
%50 = icmp eq i64* %7, null
br i1 %50, label %54, label %51
51: ; preds = %32
%52 = load i32, i32* %10
%53 = zext i32 %52 to i64
store i64 %53, i64* %7
br label %54
54: ; preds = %51, %32
%55 = icmp eq i8** %8, null
br i1 %55, label %58, label %56
56: ; preds = %54
%57 = load i8*, i8** %47
store i8* %57, i8** %8
br label %58
58: ; preds = %56, %54
store i32 0, i32* %5
br label %62
59: ; preds = %27, %24, %20, %17, %9
%60 = load i32, i32* @EINVAL
store i32 %60, i32* %5
%61 = load i32, i32* @NO_RECOVERY
br label %62
62: ; preds = %59, %58
%63 = phi i32 [ 0, %58 ], [ %61, %59 ]
%64 = phi i32* [ @NSS_STATUS_SUCCESS, %58 ], [ @NSS_STATUS_UNAVAIL, %59 ]
store i32 %63, i32* %6
%65 = load i32, i32* %64
ret i32 %65
}