#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;
};
typedef enum nss_status { ____Placeholder_nss_status } nss_status;

/* Variables and functions */
int NSS_STATUS_NOTFOUND;
int NSS_STATUS_SUCCESS;
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ localtld_fill_hostent(char const *, int, struct hostent *);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ strcasecmp(char *, char *);
char *strdup(char *);
int strlen(char *);
char *strrchr(char const *, char const);
char *strtok(char *, char *);
enum nss_status _nss_localtld_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;
  const char dot[1] = ".";
  char *ext;
  int len = 0;
  char *env = getenv("LOCALTLD");
  char *domains;
  if (env == NULL) {
    domains = strdup("dev");
  } else {
    domains = strdup(env);
  }
  char *domain = strtok(domains, ",");
  while (domain != NULL) {
    char *name_ext = strrchr(name, *dot);
    if (name_ext != NULL) {
      ext = malloc(sizeof(char) * (strlen(domain) + 2));
      sprintf(ext, ".%s", domain);
      if (strcasecmp(name_ext, ext) == 0) {
        status = NSS_STATUS_SUCCESS;
        localtld_fill_hostent(name, af, result);
        free(ext);
        break;
      }
      free(ext);
    }
    domain = strtok(NULL, ",");
    len++;
  }
  free(domains);
  return status;
}


x86 assembly (clang, -O0):
<_nss_localtld_gethostbyname2_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%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    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0xe2b(%rip),%al
mov    %al,-0x35(%rbp)
movl   $0x0,-0x44(%rbp)
lea    0xe1b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    120f <_nss_localtld_gethostbyname2_r+0x6f>
lea    0xe09(%rip),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x58(%rbp)
jmp    121c <_nss_localtld_gethostbyname2_r+0x7c>
mov    -0x50(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
lea    0xde7(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     12f4 <_nss_localtld_gethostbyname2_r+0x154>
mov    -0x8(%rbp),%rdi
movsbl -0x35(%rbp),%esi
call   1080 <strrchr@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
je     12d2 <_nss_localtld_gethostbyname2_r+0x132>
mov    -0x60(%rbp),%rdi
call   1070 <strlen@plt>
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x60(%rbp),%rdx
lea    0xd8d(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x68(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1050 <strcasecmp@plt>
cmp    $0x0,%rax
jne    12c9 <_nss_localtld_gethostbyname2_r+0x129>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1060 <localtld_fill_hostent@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
jmp    12f4 <_nss_localtld_gethostbyname2_r+0x154>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd31(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x60(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1230 <_nss_localtld_gethostbyname2_r+0x90>
mov    -0x58(%rbp),%rdi
call   1040 <free@plt>
mov    -0x34(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nss_localtld_gethostbyname2_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%rbp
mov    0x2e1a(%rip),%rax
mov    (%rax),%r13d
lea    0xe38(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11d7 <_nss_localtld_gethostbyname2_r+0x37>
mov    %rax,%rdi
jmp    11de <_nss_localtld_gethostbyname2_r+0x3e>
lea    0xe2b(%rip),%rdi
call   10c0 <strdup@plt>
mov    %rax,%rbx
lea    0xe20(%rip),%rsi
mov    %rax,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
je     12c0 <_nss_localtld_gethostbyname2_r+0x120>
mov    %rax,%r15
mov    %rbx,0x8(%rsp)
mov    %r12d,(%rsp)
mov    %r14,0x10(%rsp)
mov    %r13d,0x4(%rsp)
lea    0xdf2(%rip),%rbx
lea    0xded(%rip),%r14
jmp    1242 <_nss_localtld_gethostbyname2_r+0xa2>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
mov    %rbx,%rsi
call   10a0 <strtok@plt>
mov    %rax,%r15
test   %rax,%rax
je     1292 <_nss_localtld_gethostbyname2_r+0xf2>
mov    %rbp,%rdi
mov    $0x2e,%esi
call   1080 <strrchr@plt>
test   %rax,%rax
je     1230 <_nss_localtld_gethostbyname2_r+0x90>
mov    %rax,%r13
mov    %r15,%rdi
call   1070 <strlen@plt>
lea    0x2(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   10b0 <sprintf@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <strcasecmp@plt>
test   %rax,%rax
je     1299 <_nss_localtld_gethostbyname2_r+0xf9>
mov    %r12,%rdi
call   1040 <free@plt>
jmp    1230 <_nss_localtld_gethostbyname2_r+0x90>
mov    0x4(%rsp),%r13d
jmp    12bb <_nss_localtld_gethostbyname2_r+0x11b>
mov    0x2d48(%rip),%rax
mov    (%rax),%r13d
mov    %rbp,%rdi
mov    (%rsp),%esi
mov    0x10(%rsp),%rdx
call   1060 <localtld_fill_hostent@plt>
mov    %r12,%rdi
call   1040 <free@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32 }
@NSS_STATUS_NOTFOUND = global i32 0
@__const._nss_localtld_gethostbyname2_r.dot = [1 x i8] c"."
@.str = [9 x i8] c"LOCALTLD\00"
@.str.1 = [4 x i8] c"dev\00"
@.str.2 = [2 x i8] c",\00"
@.str.3 = [4 x i8] c".%s\00"
@NSS_STATUS_SUCCESS = global i32 0
define i32 @_nss_localtld_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 [1 x i8]
%17 = alloca i8*
%18 = alloca i32
%19 = alloca i8*
%20 = alloca i8*
%21 = alloca i8*
%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 = bitcast [1 x i8]* %16 to i8*
store i32 0, i32* %18
%25 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %25, i8** %19
%26 = load i8*, i8** %19
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %7
%29 = call i8* @strdup(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %29, i8** %20
br label %33
30: ; preds = %7
%31 = load i8*, i8** %19
%32 = call i8* @strdup(i8* %31)
store i8* %32, i8** %20
br label %33
33: ; preds = %30, %28
%34 = load i8*, i8** %20
%35 = call i8* @strtok(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %35, i8** %21
br label %36
36: ; preds = %72, %33
%37 = load i8*, i8** %21
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %76
39: ; preds = %36
%40 = load i8*, i8** %8
%41 = getelementptr inbounds [1 x i8], [1 x i8]* %16, i64 0, i64 0
%42 = load i8, i8* %41
%43 = call i8* @strrchr(i8* %40, i8 signext %42)
store i8* %43, i8** %22
%44 = load i8*, i8** %22
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %72
46: ; preds = %39
%47 = load i8*, i8** %21
%48 = call i32 @strlen(i8* %47)
%49 = add nsw i32 %48, 2
%50 = sext i32 %49 to i64
%51 = mul i64 1, %50
%52 = trunc i64 %51 to i32
%53 = call i8* @malloc(i32 %52)
store i8* %53, i8** %17
%54 = load i8*, i8** %17
%55 = load i8*, i8** %21
%56 = call i32 @sprintf(i8* %54, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %55)
%57 = load i8*, i8** %22
%58 = load i8*, i8** %17
%59 = call i64 @strcasecmp(i8* %57, i8* %58)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %69
61: ; preds = %46
%62 = load i32, i32* @NSS_STATUS_SUCCESS
store i32 %62, i32* %15
%63 = load i8*, i8** %8
%64 = load i32, i32* %9
%65 = load %struct.hostent*, %struct.hostent** %10
%66 = call i32 @localtld_fill_hostent(i8* %63, i32 %64, %struct.hostent* %65)
%67 = load i8*, i8** %17
%68 = call i32 @free(i8* %67)
br label %76
69: ; preds = %46
%70 = load i8*, i8** %17
%71 = call i32 @free(i8* %70)
br label %72
72: ; preds = %69, %39
%73 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %73, i8** %21
%74 = load i32, i32* %18
%75 = add nsw i32 %74, 1
store i32 %75, i32* %18
br label %36
76: ; preds = %61, %36
%77 = load i8*, i8** %20
%78 = call i32 @free(i8* %77)
%79 = load i32, i32* %15
ret i32 %79
}

LLVM IR (-O2):
%struct.hostent = type { i32 }
@NSS_STATUS_NOTFOUND = global i32 0
@.str = [9 x i8] c"LOCALTLD\00"
@.str.1 = [4 x i8] c"dev\00"
@.str.2 = [2 x i8] c",\00"
@.str.3 = [4 x i8] c".%s\00"
@NSS_STATUS_SUCCESS = global i32 0
define i32 @_nss_localtld_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 ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call dereferenceable_or_null(4) i8* @strdup(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %15
13: ; preds = %7
%14 = tail call i8* @strdup(i8* %9)
br label %15
15: ; preds = %13, %11
%16 = phi i8* [ %12, %11 ], [ %14, %13 ]
%17 = tail call i8* @strtok(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %39, label %19
19: ; preds = %15, %36
%20 = phi i8* [ %37, %36 ], [ %17, %15 ]
%21 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%22 = icmp eq i8* %21, null
br i1 %22, label %36, label %23
23: ; preds = %19
%24 = tail call i32 @strlen(i8* %20)
%25 = add nsw i32 %24, 2
%26 = tail call i8* @malloc(i32 %25)
%27 = tail call i32 @sprintf(i8* dereferenceable(1) %26, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %20)
%28 = tail call i64 @strcasecmp(i8* %21, i8* %26)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %23
%31 = load i32, i32* @NSS_STATUS_SUCCESS
%32 = tail call i32 @localtld_fill_hostent(i8* %0, i32 %1, %struct.hostent* %2)
%33 = tail call i32 @free(i8* %26)
br label %39
34: ; preds = %23
%35 = tail call i32 @free(i8* %26)
br label %36
36: ; preds = %19, %34
%37 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %19
39: ; preds = %36, %15, %30
%40 = phi i32 [ %31, %30 ], [ %8, %15 ], [ %8, %36 ]
%41 = tail call i32 @free(i8* %16)
ret i32 %40
}