#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*/ _fill_hostent(char const *, int, struct hostent *);
char *get_tld();
int /*<<< orphan*/ printf(char *, char const *);
scalar_t__ strlen(char *);
char *strstr(char const *, char *);
enum nss_status _nss_tld_mapper_gethostbyname2_r(const char *name, int af,
                                                 struct hostent *result,
                                                 char *buffer, size_t buflen,
                                                 int *errnop, int *h_errnop) {
  printf("Called our resolver with name %s\n", name);
  char *tld = get_tld();
  char *tld_part = strstr(name, tld);
  if (tld_part != NULL && strlen(tld_part) == strlen(tld)) {
    _fill_hostent(name, af, result);
    return NSS_STATUS_SUCCESS;
  } else {
    return NSS_STATUS_NOTFOUND;
  }
}


x86 assembly (clang, -O0):
<_nss_tld_mapper_gethostbyname2_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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    -0x10(%rbp),%rsi
lea    0xe82(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <get_tld@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     11f1 <_nss_tld_mapper_gethostbyname2_r+0xa1>
mov    -0x48(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
cmp    %rcx,%rax
jne    11f1 <_nss_tld_mapper_gethostbyname2_r+0xa1>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <_fill_hostent@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11fd <_nss_tld_mapper_gethostbyname2_r+0xad>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nss_tld_mapper_gethostbyname2_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0xe95(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <get_tld@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %rax,%rsi
call   1070 <strstr@plt>
mov    0x2e49(%rip),%r13
test   %rax,%rax
je     11c1 <_nss_tld_mapper_gethostbyname2_r+0x71>
mov    %rax,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
mov    %rbp,%rdi
call   1030 <strlen@plt>
cmp    %rax,%r12
jne    11c1 <_nss_tld_mapper_gethostbyname2_r+0x71>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rdx
call   1050 <_fill_hostent@plt>
mov    0x2e27(%rip),%r13
mov    0x0(%r13),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32 }
@.str = [34 x i8] c"Called our resolver with name %s\0A\00"
@NSS_STATUS_SUCCESS = global i32 0
@NSS_STATUS_NOTFOUND = global i32 0
define i32 @_nss_tld_mapper_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 i8*
%17 = 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 i32* %5, i32** %14
store i32* %6, i32** %15
%18 = load i8*, i8** %9
%19 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i8* (...) @get_tld()
store i8* %20, i8** %16
%21 = load i8*, i8** %9
%22 = load i8*, i8** %16
%23 = call i8* @strstr(i8* %21, i8* %22)
store i8* %23, i8** %17
%24 = load i8*, i8** %17
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %38
26: ; preds = %7
%27 = load i8*, i8** %17
%28 = call i64 @strlen(i8* %27)
%29 = load i8*, i8** %16
%30 = call i64 @strlen(i8* %29)
%31 = icmp eq i64 %28, %30
br i1 %31, label %32, label %38
32: ; preds = %26
%33 = load i8*, i8** %9
%34 = load i32, i32* %10
%35 = load %struct.hostent*, %struct.hostent** %11
%36 = call i32 @_fill_hostent(i8* %33, i32 %34, %struct.hostent* %35)
%37 = load i32, i32* @NSS_STATUS_SUCCESS
store i32 %37, i32* %8
br label %40
38: ; preds = %26, %7
%39 = load i32, i32* @NSS_STATUS_NOTFOUND
store i32 %39, i32* %8
br label %40
40: ; preds = %38, %32
%41 = load i32, i32* %8
ret i32 %41
}

LLVM IR (-O2):
%struct.hostent = type { i32 }
@.str = [34 x i8] c"Called our resolver with name %s\0A\00"
@NSS_STATUS_SUCCESS = global i32 0
@NSS_STATUS_NOTFOUND = global i32 0
define i32 @_nss_tld_mapper_gethostbyname2_r(i8* %0, i32 %1, %struct.hostent* %2, i8* nocapture readnone %3, i64 %4, i32* nocapture readnone %5, i32* nocapture readnone %6) {
%8 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = tail call i8* (...) @get_tld()
%10 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %18, label %12
12: ; preds = %7
%13 = tail call i64 @strlen(i8* %10)
%14 = tail call i64 @strlen(i8* dereferenceable(1) %9)
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @_fill_hostent(i8* %0, i32 %1, %struct.hostent* %2)
br label %18
18: ; preds = %7, %12, %16
%19 = phi i32* [ @NSS_STATUS_SUCCESS, %16 ], [ @NSS_STATUS_NOTFOUND, %12 ], [ @NSS_STATUS_NOTFOUND, %7 ]
%20 = load i32, i32* %19
ret i32 %20
}