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

/* Variables and functions */
int AF_INET;
int ERANGE;
int NO_RECOVERY;
int NSS_STATUS_NOTFOUND;
int NSS_STATUS_SUCCESS;
int NSS_STATUS_TRYAGAIN;
int ends_with(char const *, char *);
int /*<<< orphan*/ memcpy(char *, int *, int);
int str2num(char const *, int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int unbit_ip(int);
enum nss_status _nss_unbit_gethostbyname2_r(const char *name, int af,
                                            struct hostent *result,
                                            char *buffer, size_t buflen,
                                            int *errnop, int *h_errnop) {
  if (af != AF_INET)
    goto end;
  int l = ends_with(name, ".local");
  if (l <= 0)
    goto end;
  uint32_t n = str2num(name, l);
  if (n <= 30000)
    goto end;
  if (buflen < sizeof(char *) + strlen(name) + 4 + (sizeof(void *) * 2)) {
    *errnop = ERANGE;
    *h_errnop = NO_RECOVERY;
    return NSS_STATUS_TRYAGAIN;
  }
  *((char **)buffer) = NULL;
  result->h_aliases = (char **)buffer;
  size_t idx = sizeof(char *);
  strcpy(buffer + idx, name);
  result->h_name = buffer + idx;
  idx += strlen(name) + 1;
  result->h_addrtype = af;
  result->h_length = 4;
  uint32_t ip = unbit_ip(n);
  memcpy(buffer + idx, &ip, 4);
  idx += 4;
  ((char **)(buffer + idx))[0] = buffer + (idx - 4);
  ((char **)(buffer + idx))[1] = NULL;
  result->h_addr_list = (char **)(buffer + idx);
  return NSS_STATUS_SUCCESS;
end:
  return NSS_STATUS_NOTFOUND;
}


x86 assembly (clang, -O0):
<_nss_unbit_gethostbyname2_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%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)
mov    -0x14(%rbp),%eax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%eax
je     119a <_nss_unbit_gethostbyname2_r+0x3a>
jmp    131f <_nss_unbit_gethostbyname2_r+0x1bf>
mov    -0x10(%rbp),%rdi
lea    0xe5b(%rip),%rsi
call   1070 <ends_with@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     11bc <_nss_unbit_gethostbyname2_r+0x5c>
jmp    131f <_nss_unbit_gethostbyname2_r+0x1bf>
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1080 <str2num@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x7530,-0x40(%rbp)
jg     11dd <_nss_unbit_gethostbyname2_r+0x7d>
jmp    131f <_nss_unbit_gethostbyname2_r+0x1bf>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
movslq %ecx,%rcx
add    $0x8,%rcx
add    $0x4,%rcx
add    $0x10,%rcx
cmp    %rcx,%rax
jae    123b <_nss_unbit_gethostbyname2_r+0xdb>
mov    0x2dcd(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2d9e(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    132b <_nss_unbit_gethostbyname2_r+0x1cb>
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x8,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
add    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rcx
add    -0x48(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
add    -0x48(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x20(%rbp),%rax
movl   $0x4,0x14(%rax)
mov    -0x40(%rbp),%edi
call   1050 <unbit_ip@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x28(%rbp),%rdi
add    -0x48(%rbp),%rdi
lea    -0x4c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x48(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdx
mov    -0x48(%rbp),%rax
sub    $0x4,%rax
add    %rax,%rdx
mov    -0x28(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    %rdx,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x48(%rbp),%rcx
movq   $0x0,0x8(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
add    -0x48(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    0x2cc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    132b <_nss_unbit_gethostbyname2_r+0x1cb>
mov    0x2c92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nss_unbit_gethostbyname2_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e73(%rip),%rax
mov    0x2e3c(%rip),%r14
cmp    %esi,(%rax)
jne    1281 <_nss_unbit_gethostbyname2_r+0x121>
mov    %r9,%r12
mov    %r8,%r13
mov    %rcx,%r15
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rdx,0x10(%rsp)
lea    0xe62(%rip),%rsi
call   1070 <ends_with@plt>
test   %eax,%eax
jle    1281 <_nss_unbit_gethostbyname2_r+0x121>
mov    %rbx,%rdi
mov    %eax,%esi
call   1080 <str2num@plt>
cmp    $0x7531,%eax
jl     1281 <_nss_unbit_gethostbyname2_r+0x121>
mov    %eax,0x8(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
add    $0x1c,%rax
cmp    %r13,%rax
jbe    1200 <_nss_unbit_gethostbyname2_r+0xa0>
mov    0x50(%rsp),%rax
mov    0x2dfd(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%r12)
mov    0x2dd0(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    0x2dcd(%rip),%r14
jmp    1281 <_nss_unbit_gethostbyname2_r+0x121>
movq   $0x0,(%r15)
mov    0x10(%rsp),%r14
mov    %r15,(%r14)
lea    0x8(%r15),%r12
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r12,0x8(%r14)
mov    %rbx,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbx
mov    %ebp,0x10(%r14)
movl   $0x4,0x14(%r14)
mov    0x8(%rsp),%edi
call   1050 <unbit_ip@plt>
mov    %eax,0xc(%rsp)
lea    (%rbx,%r15,1),%rbp
add    $0x9,%rbp
lea    0xc(%rsp),%rsi
mov    %rbp,%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
lea    (%rbx,%r15,1),%rax
add    $0xd,%rax
mov    %rbp,0xd(%rbx,%r15,1)
movq   $0x0,0x15(%rbx,%r15,1)
mov    %rax,0x18(%r14)
mov    0x2d57(%rip),%r14
mov    (%r14),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i8**, i8*, i32, i32, i8** }
@AF_INET = global i32 0
@.str = [7 x i8] c".local\00"
@ERANGE = global i32 0
@NO_RECOVERY = global i32 0
@NSS_STATUS_TRYAGAIN = global i32 0
@NSS_STATUS_SUCCESS = global i32 0
@NSS_STATUS_NOTFOUND = global i32 0
define i32 @_nss_unbit_gethostbyname2_r(i8* %0, i32 %1, %struct.hostent* %2, i8* %3, i64 %4, i32* %5, i32* %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca %struct.hostent*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i64
%19 = alloca i32
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 i32* %5, i32** %14
store i32* %6, i32** %15
%20 = load i32, i32* %10
%21 = load i32, i32* @AF_INET
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %7
br label %109
24: ; preds = %7
%25 = load i8*, i8** %9
%26 = call i32 @ends_with(i8* %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %26, i32* %16
%27 = load i32, i32* %16
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
br label %109
30: ; preds = %24
%31 = load i8*, i8** %9
%32 = load i32, i32* %16
%33 = call i32 @str2num(i8* %31, i32 %32)
store i32 %33, i32* %17
%34 = load i32, i32* %17
%35 = icmp sle i32 %34, 30000
br i1 %35, label %36, label %37
36: ; preds = %30
br label %109
37: ; preds = %30
%38 = load i64, i64* %13
%39 = load i8*, i8** %9
%40 = call i32 @strlen(i8* %39)
%41 = sext i32 %40 to i64
%42 = add i64 8, %41
%43 = add i64 %42, 4
%44 = add i64 %43, 16
%45 = icmp ult i64 %38, %44
br i1 %45, label %46, label %52
46: ; preds = %37
%47 = load i32, i32* @ERANGE
%48 = load i32*, i32** %14
store i32 %47, i32* %48
%49 = load i32, i32* @NO_RECOVERY
%50 = load i32*, i32** %15
store i32 %49, i32* %50
%51 = load i32, i32* @NSS_STATUS_TRYAGAIN
store i32 %51, i32* %8
br label %111
52: ; preds = %37
%53 = load i8*, i8** %12
%54 = bitcast i8* %53 to i8**
store i8* null, i8** %54
%55 = load i8*, i8** %12
%56 = bitcast i8* %55 to i8**
%57 = load %struct.hostent*, %struct.hostent** %11
%58 = getelementptr inbounds %struct.hostent, %struct.hostent* %57, i32 0, i32 0
store i8** %56, i8*** %58
store i64 8, i64* %18
%59 = load i8*, i8** %12
%60 = load i64, i64* %18
%61 = getelementptr inbounds i8, i8* %59, i64 %60
%62 = load i8*, i8** %9
%63 = call i32 @strcpy(i8* %61, i8* %62)
%64 = load i8*, i8** %12
%65 = load i64, i64* %18
%66 = getelementptr inbounds i8, i8* %64, i64 %65
%67 = load %struct.hostent*, %struct.hostent** %11
%68 = getelementptr inbounds %struct.hostent, %struct.hostent* %67, i32 0, i32 1
store i8* %66, i8** %68
%69 = load i8*, i8** %9
%70 = call i32 @strlen(i8* %69)
%71 = add nsw i32 %70, 1
%72 = sext i32 %71 to i64
%73 = load i64, i64* %18
%74 = add i64 %73, %72
store i64 %74, i64* %18
%75 = load i32, i32* %10
%76 = load %struct.hostent*, %struct.hostent** %11
%77 = getelementptr inbounds %struct.hostent, %struct.hostent* %76, i32 0, i32 2
store i32 %75, i32* %77
%78 = load %struct.hostent*, %struct.hostent** %11
%79 = getelementptr inbounds %struct.hostent, %struct.hostent* %78, i32 0, i32 3
store i32 4, i32* %79
%80 = load i32, i32* %17
%81 = call i32 @unbit_ip(i32 %80)
store i32 %81, i32* %19
%82 = load i8*, i8** %12
%83 = load i64, i64* %18
%84 = getelementptr inbounds i8, i8* %82, i64 %83
%85 = call i32 @memcpy(i8* %84, i32* %19, i32 4)
%86 = load i64, i64* %18
%87 = add i64 %86, 4
store i64 %87, i64* %18
%88 = load i8*, i8** %12
%89 = load i64, i64* %18
%90 = sub i64 %89, 4
%91 = getelementptr inbounds i8, i8* %88, i64 %90
%92 = load i8*, i8** %12
%93 = load i64, i64* %18
%94 = getelementptr inbounds i8, i8* %92, i64 %93
%95 = bitcast i8* %94 to i8**
%96 = getelementptr inbounds i8*, i8** %95, i64 0
store i8* %91, i8** %96
%97 = load i8*, i8** %12
%98 = load i64, i64* %18
%99 = getelementptr inbounds i8, i8* %97, i64 %98
%100 = bitcast i8* %99 to i8**
%101 = getelementptr inbounds i8*, i8** %100, i64 1
store i8* null, i8** %101
%102 = load i8*, i8** %12
%103 = load i64, i64* %18
%104 = getelementptr inbounds i8, i8* %102, i64 %103
%105 = bitcast i8* %104 to i8**
%106 = load %struct.hostent*, %struct.hostent** %11
%107 = getelementptr inbounds %struct.hostent, %struct.hostent* %106, i32 0, i32 4
store i8** %105, i8*** %107
%108 = load i32, i32* @NSS_STATUS_SUCCESS
store i32 %108, i32* %8
br label %111
109: ; preds = %36, %29, %23
%110 = load i32, i32* @NSS_STATUS_NOTFOUND
store i32 %110, i32* %8
br label %111
111: ; preds = %109, %52, %46
%112 = load i32, i32* %8
ret i32 %112
}

LLVM IR (-O2):
%struct.hostent = type { i8**, i8*, i32, i32, i8** }
@AF_INET = global i32 0
@.str = [7 x i8] c".local\00"
@ERANGE = global i32 0
@NO_RECOVERY = global i32 0
@NSS_STATUS_TRYAGAIN = global i32 0
@NSS_STATUS_SUCCESS = global i32 0
@NSS_STATUS_NOTFOUND = global i32 0
define i32 @_nss_unbit_gethostbyname2_r(i8* %0, i32 %1, %struct.hostent* nocapture writeonly %2, i8* %3, i64 %4, i32* nocapture writeonly %5, i32* nocapture writeonly %6) {
%8 = alloca i32
%9 = load i32, i32* @AF_INET
%10 = icmp eq i32 %9, %1
br i1 %10, label %11, label %46
11: ; preds = %7
%12 = tail call i32 @ends_with(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = icmp slt i32 %12, 1
br i1 %13, label %46, label %14
14: ; preds = %11
%15 = tail call i32 @str2num(i8* %0, i32 %12)
%16 = icmp slt i32 %15, 30001
br i1 %16, label %46, label %17
17: ; preds = %14
%18 = tail call i32 @strlen(i8* %0)
%19 = sext i32 %18 to i64
%20 = add nsw i64 %19, 28
%21 = icmp ugt i64 %20, %4
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @ERANGE
store i32 %23, i32* %5
%24 = load i32, i32* @NO_RECOVERY
store i32 %24, i32* %6
br label %46
25: ; preds = %17
%26 = bitcast i8* %3 to i8**
store i8* null, i8** %26
%27 = bitcast %struct.hostent* %2 to i8**
store i8* %3, i8** %27
%28 = getelementptr inbounds i8, i8* %3, i64 8
%29 = tail call i32 @strcpy(i8* %28, i8* %0)
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 1
store i8* %28, i8** %30
%31 = tail call i32 @strlen(i8* %0)
%32 = sext i32 %31 to i64
%33 = add nsw i64 %32, 9
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 2
store i32 %1, i32* %34
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 3
store i32 4, i32* %35
%36 = tail call i32 @unbit_ip(i32 %15)
store i32 %36, i32* %8
%37 = getelementptr inbounds i8, i8* %3, i64 %33
%38 = call i32 @memcpy(i8* %37, i32* %8, i32 4)
%39 = add nsw i64 %32, 13
%40 = getelementptr inbounds i8, i8* %3, i64 %39
%41 = bitcast i8* %40 to i8**
store i8* %37, i8** %41
%42 = getelementptr inbounds i8, i8* %40, i64 8
%43 = bitcast i8* %42 to i8**
store i8* null, i8** %43
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 4
%45 = bitcast i8*** %44 to i8**
store i8* %40, i8** %45
br label %46
46: ; preds = %7, %11, %14, %25, %22
%47 = phi i32* [ @NSS_STATUS_TRYAGAIN, %22 ], [ @NSS_STATUS_SUCCESS, %25 ], [ @NSS_STATUS_NOTFOUND, %14 ], [ @NSS_STATUS_NOTFOUND, %11 ], [ @NSS_STATUS_NOTFOUND, %7 ]
%48 = load i32, i32* %47
ret i32 %48
}