#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 hostent {
  int dummy;
};
struct dirent {
  char *d_name;
  scalar_t__ d_type;
};
typedef enum nss_status { ____Placeholder_nss_status } nss_status;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_LNK;
int NSS_STATUS_NOTFOUND;
int NSS_STATUS_SUCCESS;
int /*<<< orphan*/ _fill_hostent(char const *, int, struct hostent *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *malloc(int);
int /*<<< orphan*/ *opendir(int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char const *);
scalar_t__ strncmp(char *, char const *, int);
enum nss_status _nss_hoof_gethostbyname2_r(const char *name, int af,
                                           struct hostent *result, char *buffer,
                                           size_t buflen, int *errnop,
                                           int *h_errnop) {
  enum nss_status status = NSS_STATUS_NOTFOUND;
  char *home = getenv("HOME");
  char *home_cpy = malloc((strlen(home) + 6) * sizeof(char));
  strcpy(home_cpy, home);
  DIR *hosts_dir = opendir(strcat(home_cpy, "/.hoof"));
  if (hosts_dir != NULL) {
    struct dirent *entry;
    while ((entry = readdir(hosts_dir)) != NULL) {
      int d_len = strlen(entry->d_name) + 4;
      int n_len = strlen(name);
      if (entry->d_type == DT_LNK && n_len >= d_len) {
        char *domain = malloc((d_len + 2) * sizeof(char));
        if (n_len > d_len) {
          sprintf(domain, ".%s.dev", entry->d_name);
        } else {
          sprintf(domain, "%s.dev", entry->d_name);
        }
        if (strncmp(domain, name + (n_len - strlen(domain)), n_len) == 0) {
          _fill_hostent(name, af, result);
          status = NSS_STATUS_SUCCESS;
        }
        free(domain);
      }
    }
    closedir(hosts_dir);
  }
  return status;
}


x86 assembly (clang, -O0):
<_nss_hoof_gethostbyname2_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
lea    0xe07(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1080 <strlen@plt>
add    $0x6,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1060 <strcpy@plt>
mov    -0x48(%rbp),%rdi
lea    0xdce(%rip),%rsi
call   10d0 <strcat@plt>
mov    %eax,%edi
call   1070 <opendir@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     137e <_nss_hoof_gethostbyname2_r+0x1be>
jmp    1257 <_nss_hoof_gethostbyname2_r+0x97>
mov    -0x50(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x58(%rbp)
cmp    $0x0,%rax
je     1375 <_nss_hoof_gethostbyname2_r+0x1b5>
mov    -0x58(%rbp),%rax
mov    (%rax),%rdi
call   1080 <strlen@plt>
add    $0x4,%eax
mov    %eax,-0x5c(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strlen@plt>
mov    %eax,-0x60(%rbp)
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d3d(%rip),%rcx
cmp    (%rcx),%rax
jne    1370 <_nss_hoof_gethostbyname2_r+0x1b0>
mov    -0x60(%rbp),%eax
cmp    -0x5c(%rbp),%eax
jl     1370 <_nss_hoof_gethostbyname2_r+0x1b0>
mov    -0x5c(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x68(%rbp)
mov    -0x60(%rbp),%eax
cmp    -0x5c(%rbp),%eax
jle    12ef <_nss_hoof_gethostbyname2_r+0x12f>
mov    -0x68(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    (%rax),%rdx
lea    0xd27(%rip),%rsi
call   10e0 <sprintf@plt>
jmp    1306 <_nss_hoof_gethostbyname2_r+0x146>
mov    -0x68(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    (%rax),%rdx
lea    0xd0c(%rip),%rsi
call   10e0 <sprintf@plt>
mov    -0x68(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x60(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x68(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x80(%rbp),%rsi
mov    -0x78(%rbp),%rdi
mov    %eax,%ecx
mov    -0x6c(%rbp),%eax
sub    %ecx,%eax
cltq
add    %rax,%rsi
mov    -0x60(%rbp),%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    1367 <_nss_hoof_gethostbyname2_r+0x1a7>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   10c0 <_fill_hostent@plt>
mov    0x2c86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x68(%rbp),%rdi
call   1040 <free@plt>
jmp    1257 <_nss_hoof_gethostbyname2_r+0x97>
mov    -0x50(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x34(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nss_hoof_gethostbyname2_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x8(%rsp)
mov    %esi,0x4(%rsp)
mov    %rdi,%r12
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
lea    0xe13(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <strlen@plt>
lea    0x6(%rax),%edi
call   10b0 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <strcpy@plt>
lea    0xdeb(%rip),%rsi
mov    %rbp,%rdi
call   10d0 <strcat@plt>
mov    %eax,%edi
call   1070 <opendir@plt>
test   %rax,%rax
je     131d <_nss_hoof_gethostbyname2_r+0x15d>
mov    %rax,%r14
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1315 <_nss_hoof_gethostbyname2_r+0x155>
mov    %rax,%rbx
mov    %r14,0x10(%rsp)
jmp    1271 <_nss_hoof_gethostbyname2_r+0xb1>
mov    %r14,%rdi
call   1040 <free@plt>
mov    0x10(%rsp),%r14
mov    %r14,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1315 <_nss_hoof_gethostbyname2_r+0x155>
mov    (%rbx),%rdi
call   1080 <strlen@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1080 <strlen@plt>
mov    %eax,%r15d
mov    0x8(%rbx),%rax
mov    0x2d47(%rip),%rcx
cmp    (%rcx),%rax
jne    125d <_nss_hoof_gethostbyname2_r+0x9d>
lea    0x4(%rbp),%r13d
cmp    %r13d,%r15d
jl     125d <_nss_hoof_gethostbyname2_r+0x9d>
add    $0x6,%ebp
mov    %ebp,%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
cmp    %r13d,%r15d
mov    (%rbx),%rdx
lea    0xd54(%rip),%rsi
lea    0xd4c(%rip),%rax
cmovg  %rax,%rsi
mov    %r14,%rdi
call   10e0 <sprintf@plt>
mov    %r14,%rdi
call   1080 <strlen@plt>
mov    %r15d,%ecx
sub    %eax,%ecx
movslq %ecx,%rsi
add    %r12,%rsi
mov    %r14,%rdi
mov    %r15d,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    1250 <_nss_hoof_gethostbyname2_r+0x90>
mov    %r12,%rdi
mov    0x4(%rsp),%esi
mov    0x8(%rsp),%rdx
call   10c0 <_fill_hostent@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
jmp    1250 <_nss_hoof_gethostbyname2_r+0x90>
mov    %r14,%rdi
call   1090 <closedir@plt>
mov    (%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32 }
%struct.dirent = type { i8*, i64 }
@NSS_STATUS_NOTFOUND = global i32 0
@.str = [5 x i8] c"HOME\00"
@.str.1 = [7 x i8] c"/.hoof\00"
@DT_LNK = global i64 0
@.str.2 = [8 x i8] c".%s.dev\00"
@.str.3 = [7 x i8] c"%s.dev\00"
@NSS_STATUS_SUCCESS = global i32 0
define i32 @_nss_hoof_gethostbyname2_r(i8* %0, i32 %1, %struct.hostent* %2, i8* %3, i64 %4, i32* %5, i32* %6) {
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.hostent*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i8*
%18 = alloca i32*
%19 = alloca %struct.dirent*
%20 = alloca i32
%21 = alloca i32
%22 = alloca i8*
store i8* %0, i8** %8
store i32 %1, i32* %9
store %struct.hostent* %2, %struct.hostent** %10
store i8* %3, i8** %11
store i64 %4, i64* %12
store i32* %5, i32** %13
store i32* %6, i32** %14
%23 = load i32, i32* @NSS_STATUS_NOTFOUND
store i32 %23, i32* %15
%24 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %24, i8** %16
%25 = load i8*, i8** %16
%26 = call i32 @strlen(i8* %25)
%27 = add nsw i32 %26, 6
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 1
%30 = trunc i64 %29 to i32
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %17
%32 = load i8*, i8** %17
%33 = load i8*, i8** %16
%34 = call i32 @strcpy(i8* %32, i8* %33)
%35 = load i8*, i8** %17
%36 = call i32 @strcat(i8* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32* @opendir(i32 %36)
store i32* %37, i32** %18
%38 = load i32*, i32** %18
%39 = icmp ne i32* %38, null
br i1 %39, label %40, label %109
40: ; preds = %7
br label %41
41: ; preds = %105, %40
%42 = load i32*, i32** %18
%43 = call %struct.dirent* @readdir(i32* %42)
store %struct.dirent* %43, %struct.dirent** %19
%44 = icmp ne %struct.dirent* %43, null
br i1 %44, label %45, label %106
45: ; preds = %41
%46 = load %struct.dirent*, %struct.dirent** %19
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i32 @strlen(i8* %48)
%50 = add nsw i32 %49, 4
store i32 %50, i32* %20
%51 = load i8*, i8** %8
%52 = call i32 @strlen(i8* %51)
store i32 %52, i32* %21
%53 = load %struct.dirent*, %struct.dirent** %19
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 1
%55 = load i64, i64* %54
%56 = load i64, i64* @DT_LNK
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %105
58: ; preds = %45
%59 = load i32, i32* %21
%60 = load i32, i32* %20
%61 = icmp sge i32 %59, %60
br i1 %61, label %62, label %105
62: ; preds = %58
%63 = load i32, i32* %20
%64 = add nsw i32 %63, 2
%65 = sext i32 %64 to i64
%66 = mul i64 %65, 1
%67 = trunc i64 %66 to i32
%68 = call i8* @malloc(i32 %67)
store i8* %68, i8** %22
%69 = load i32, i32* %21
%70 = load i32, i32* %20
%71 = icmp sgt i32 %69, %70
br i1 %71, label %72, label %78
72: ; preds = %62
%73 = load i8*, i8** %22
%74 = load %struct.dirent*, %struct.dirent** %19
%75 = getelementptr inbounds %struct.dirent, %struct.dirent* %74, i32 0, i32 0
%76 = load i8*, i8** %75
%77 = call i32 @sprintf(i8* %73, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %76)
br label %84
78: ; preds = %62
%79 = load i8*, i8** %22
%80 = load %struct.dirent*, %struct.dirent** %19
%81 = getelementptr inbounds %struct.dirent, %struct.dirent* %80, i32 0, i32 0
%82 = load i8*, i8** %81
%83 = call i32 @sprintf(i8* %79, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %82)
br label %84
84: ; preds = %78, %72
%85 = load i8*, i8** %22
%86 = load i8*, i8** %8
%87 = load i32, i32* %21
%88 = load i8*, i8** %22
%89 = call i32 @strlen(i8* %88)
%90 = sub nsw i32 %87, %89
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %86, i64 %91
%93 = load i32, i32* %21
%94 = call i64 @strncmp(i8* %85, i8* %92, i32 %93)
%95 = icmp eq i64 %94, 0
br i1 %95, label %96, label %102
96: ; preds = %84
%97 = load i8*, i8** %8
%98 = load i32, i32* %9
%99 = load %struct.hostent*, %struct.hostent** %10
%100 = call i32 @_fill_hostent(i8* %97, i32 %98, %struct.hostent* %99)
%101 = load i32, i32* @NSS_STATUS_SUCCESS
store i32 %101, i32* %15
br label %102
102: ; preds = %96, %84
%103 = load i8*, i8** %22
%104 = call i32 @free(i8* %103)
br label %105
105: ; preds = %102, %58, %45
br label %41
106: ; preds = %41
%107 = load i32*, i32** %18
%108 = call i32 @closedir(i32* %107)
br label %109
109: ; preds = %106, %7
%110 = load i32, i32* %15
ret i32 %110
}

LLVM IR (-O2):
%struct.hostent = type { i32 }
%struct.dirent = type { i8*, i64 }
@NSS_STATUS_NOTFOUND = global i32 0
@.str = [5 x i8] c"HOME\00"
@.str.1 = [7 x i8] c"/.hoof\00"
@DT_LNK = global i64 0
@.str.2 = [8 x i8] c".%s.dev\00"
@.str.3 = [7 x i8] c"%s.dev\00"
@NSS_STATUS_SUCCESS = global i32 0
define i32 @_nss_hoof_gethostbyname2_r(i8* %0, i32 %1, %struct.hostent* %2, i8* nocapture readnone %3, i64 %4, i32* nocapture readnone %5, i32* nocapture readnone %6) {
%8 = load i32, i32* @NSS_STATUS_NOTFOUND
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 6
%12 = tail call i8* @malloc(i32 %11)
%13 = tail call i32 @strcpy(i8* %12, i8* %9)
%14 = tail call i32 @strcat(i8* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32* @opendir(i32 %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %60, label %17
17: ; preds = %7
%18 = tail call %struct.dirent* @readdir(i32* %15)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %57, label %20
20: ; preds = %17, %53
%21 = phi %struct.dirent* [ %55, %53 ], [ %18, %17 ]
%22 = phi i32 [ %54, %53 ], [ %8, %17 ]
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = tail call i32 @strlen(i8* %24)
%26 = add nsw i32 %25, 4
%27 = tail call i32 @strlen(i8* %0)
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 1
%29 = load i64, i64* %28
%30 = load i64, i64* @DT_LNK
%31 = icmp ne i64 %29, %30
%32 = icmp slt i32 %27, %26
%33 = select i1 %31, i1 true, i1 %32
br i1 %33, label %53, label %34
34: ; preds = %20
%35 = add nsw i32 %25, 6
%36 = tail call i8* @malloc(i32 %35)
%37 = icmp sgt i32 %27, %26
%38 = load i8*, i8** %23
%39 = select i1 %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0)
%40 = tail call i32 @sprintf(i8* dereferenceable(1) %36, i8* dereferenceable(1) %39, i8* %38)
%41 = tail call i32 @strlen(i8* %36)
%42 = sub nsw i32 %27, %41
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %0, i64 %43
%45 = tail call i64 @strncmp(i8* %36, i8* %44, i32 %27)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %34
%48 = tail call i32 @_fill_hostent(i8* %0, i32 %1, %struct.hostent* %2)
%49 = load i32, i32* @NSS_STATUS_SUCCESS
br label %50
50: ; preds = %47, %34
%51 = phi i32 [ %49, %47 ], [ %22, %34 ]
%52 = tail call i32 @free(i8* %36)
br label %53
53: ; preds = %50, %20
%54 = phi i32 [ %51, %50 ], [ %22, %20 ]
%55 = tail call %struct.dirent* @readdir(i32* %15)
%56 = icmp eq %struct.dirent* %55, null
br i1 %56, label %57, label %20
57: ; preds = %53, %17
%58 = phi i32 [ %8, %17 ], [ %54, %53 ]
%59 = tail call i32 @closedir(i32* %15)
br label %60
60: ; preds = %57, %7
%61 = phi i32 [ %58, %57 ], [ %8, %7 ]
ret i32 %61
}