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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int INADDRSZ;
int /*<<< orphan*/ LOG_ERR;
int *NO_RECOVERY;
int NSS_STATUS_SUCCESS;
int NSS_STATUS_UNAVAIL;
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/ *, int);
char *strsep(char **, char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
enum nss_status do_gethostbyname(char *content, char *name,
                                 struct hostent *result_buf, char *buf,
                                 size_t buflen, int *errnop, int *h_errnop) {
  size_t host_len = 0;
  char *host = NULL;
  char *addr_next = NULL;
  in_addr_t host_addr;
  char **h_addrs = NULL;
  if (content == NULL) {
    h_errnop = NO_RECOVERY;
    return NSS_STATUS_UNAVAIL;
  }
  result_buf->h_name = name;
  result_buf->h_addrtype = AF_INET;
  result_buf->h_length = INADDRSZ;
  char *start_mem = buf + sizeof(struct hostent) + 1;
  h_addrs = (char **)buf;
  h_addrs[0] = start_mem;
  while ((host = strsep(&content, " ")) != NULL) {
    if (!inet_aton(host, &host_addr)) {
      syslog(LOG_ERR, "do_gethostbyname(): failed inet_aton on %s", host);
      continue;
    }
    memcpy(h_addrs[host_len], &host_addr, INADDRSZ);
    host_len += 1;
    h_addrs[host_len] = h_addrs[host_len - 1] + INADDRSZ;
  }
  h_addrs[host_len] = NULL;
  result_buf->h_addr_list = (char **)(uintptr_t)buf;
  return NSS_STATUS_SUCCESS;
}


x86 assembly (clang, -O0):
<do_gethostbyname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x60(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ae <do_gethostbyname+0x6e>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ce <do_gethostbyname+0x18e>
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2e28(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x18(%rax)
mov    0x2df0(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x28(%rbp),%rax
add    $0x20,%rax
add    $0x1,%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x68(%rbp),%rcx
mov    -0x60(%rbp),%rax
mov    %rcx,(%rax)
lea    -0x10(%rbp),%rdi
lea    0xdf9(%rip),%rsi
call   1050 <strsep@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
je     12a6 <do_gethostbyname+0x166>
mov    -0x48(%rbp),%rdi
lea    -0x54(%rbp),%rsi
call   1040 <inet_aton@plt>
cmp    $0x0,%eax
jne    124e <do_gethostbyname+0x10e>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xdbe(%rip),%rsi
call   1060 <syslog@plt>
jmp    11fc <do_gethostbyname+0xbc>
mov    -0x60(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2d5f(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rsi
call   1030 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x60(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    $0x1,%rcx
mov    (%rax,%rcx,8),%rdx
mov    0x2d31(%rip),%rax
movslq (%rax),%rax
add    %rax,%rdx
mov    -0x60(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    11fc <do_gethostbyname+0xbc>
mov    -0x60(%rbp),%rax
mov    -0x40(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    0x2d0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_gethostbyname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,0x18(%rsp)
test   %rdi,%rdi
je     1265 <do_gethostbyname+0x125>
mov    %rsi,(%rdx)
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rdx)
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %rdx,0x28(%rsp)
mov    %eax,0x8(%rdx)
lea    0x21(%rcx),%rax
mov    %rcx,0x10(%rsp)
mov    %rax,(%rcx)
lea    0xe71(%rip),%rsi
lea    0x18(%rsp),%rdi
call   1050 <strsep@plt>
test   %rax,%rax
je     1275 <do_gethostbyname+0x135>
mov    %rax,%r14
xor    %eax,%eax
mov    %rax,0x8(%rsp)
lea    0x34(%rsp),%r13
mov    0x2e00(%rip),%r15
lea    0xe43(%rip),%r12
lea    0xe3a(%rip),%rbx
lea    0x18(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r13,%rsi
call   1040 <inet_aton@plt>
test   %eax,%eax
jne    1202 <do_gethostbyname+0xc2>
mov    (%r15),%edi
mov    %r12,%rsi
mov    %r14,%rdx
call   1060 <syslog@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <strsep@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11d0 <do_gethostbyname+0x90>
jmp    126e <do_gethostbyname+0x12e>
mov    0x8(%rsp),%r14
mov    0x10(%rsp),%rax
mov    (%rax,%r14,8),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rsi
call   1030 <memcpy@plt>
lea    0x1(%r14),%rax
mov    %rax,0x20(%rsp)
mov    0x2d97(%rip),%rax
movslq (%rax),%rax
mov    0x10(%rsp),%rcx
add    (%rcx,%r14,8),%rax
mov    %rax,0x8(%rcx,%r14,8)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <strsep@plt>
mov    0x20(%rsp),%rcx
mov    %rax,%r14
mov    %rcx,0x8(%rsp)
test   %rax,%rax
jne    11d0 <do_gethostbyname+0x90>
jmp    1277 <do_gethostbyname+0x137>
mov    0x2d74(%rip),%rax
jmp    1294 <do_gethostbyname+0x154>
mov    0x8(%rsp),%rcx
jmp    1277 <do_gethostbyname+0x137>
xor    %ecx,%ecx
mov    0x10(%rsp),%rax
movq   $0x0,(%rax,%rcx,8)
mov    0x28(%rsp),%rcx
mov    %rax,0x10(%rcx)
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i8*, i32, i8**, i32 }
@NO_RECOVERY = global i32* null
@NSS_STATUS_UNAVAIL = global i32 0
@AF_INET = global i32 0
@INADDRSZ = global i32 0
@.str = [2 x i8] c" \00"
@LOG_ERR = global i32 0
@.str.1 = [43 x i8] c"do_gethostbyname(): failed inet_aton on %s\00"
@NSS_STATUS_SUCCESS = global i32 0
define i32 @do_gethostbyname(i8* %0, i8* %1, %struct.hostent* %2, i8* %3, i64 %4, i32* %5, i32* %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.hostent*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i64
%17 = alloca i8*
%18 = alloca i8*
%19 = alloca i32
%20 = alloca i8**
%21 = alloca i8*
store i8* %0, i8** %9
store i8* %1, i8** %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
store i64 0, i64* %16
store i8* null, i8** %17
store i8* null, i8** %18
store i8** null, i8*** %20
%22 = load i8*, i8** %9
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %7
%25 = load i32*, i32** @NO_RECOVERY
store i32* %25, i32** %15
%26 = load i32, i32* @NSS_STATUS_UNAVAIL
store i32 %26, i32* %8
br label %86
27: ; preds = %7
%28 = load i8*, i8** %10
%29 = load %struct.hostent*, %struct.hostent** %11
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
store i8* %28, i8** %30
%31 = load i32, i32* @AF_INET
%32 = load %struct.hostent*, %struct.hostent** %11
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 3
store i32 %31, i32* %33
%34 = load i32, i32* @INADDRSZ
%35 = load %struct.hostent*, %struct.hostent** %11
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 1
store i32 %34, i32* %36
%37 = load i8*, i8** %12
%38 = getelementptr inbounds i8, i8* %37, i64 32
%39 = getelementptr inbounds i8, i8* %38, i64 1
store i8* %39, i8** %21
%40 = load i8*, i8** %12
%41 = bitcast i8* %40 to i8**
store i8** %41, i8*** %20
%42 = load i8*, i8** %21
%43 = load i8**, i8*** %20
%44 = getelementptr inbounds i8*, i8** %43, i64 0
store i8* %42, i8** %44
br label %45
45: ; preds = %56, %52, %27
%46 = call i8* @strsep(i8** %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %46, i8** %17
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %76
48: ; preds = %45
%49 = load i8*, i8** %17
%50 = call i32 @inet_aton(i8* %49, i32* %19)
%51 = icmp ne i32 %50, 0
br i1 %51, label %56, label %52
52: ; preds = %48
%53 = load i32, i32* @LOG_ERR
%54 = load i8*, i8** %17
%55 = call i32 @syslog(i32 %53, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %54)
br label %45
56: ; preds = %48
%57 = load i8**, i8*** %20
%58 = load i64, i64* %16
%59 = getelementptr inbounds i8*, i8** %57, i64 %58
%60 = load i8*, i8** %59
%61 = load i32, i32* @INADDRSZ
%62 = call i32 @memcpy(i8* %60, i32* %19, i32 %61)
%63 = load i64, i64* %16
%64 = add i64 %63, 1
store i64 %64, i64* %16
%65 = load i8**, i8*** %20
%66 = load i64, i64* %16
%67 = sub i64 %66, 1
%68 = getelementptr inbounds i8*, i8** %65, i64 %67
%69 = load i8*, i8** %68
%70 = load i32, i32* @INADDRSZ
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
%73 = load i8**, i8*** %20
%74 = load i64, i64* %16
%75 = getelementptr inbounds i8*, i8** %73, i64 %74
store i8* %72, i8** %75
br label %45
76: ; preds = %45
%77 = load i8**, i8*** %20
%78 = load i64, i64* %16
%79 = getelementptr inbounds i8*, i8** %77, i64 %78
store i8* null, i8** %79
%80 = load i8*, i8** %12
%81 = ptrtoint i8* %80 to i64
%82 = inttoptr i64 %81 to i8**
%83 = load %struct.hostent*, %struct.hostent** %11
%84 = getelementptr inbounds %struct.hostent, %struct.hostent* %83, i32 0, i32 2
store i8** %82, i8*** %84
%85 = load i32, i32* @NSS_STATUS_SUCCESS
store i32 %85, i32* %8
br label %86
86: ; preds = %76, %24
%87 = load i32, i32* %8
ret i32 %87
}

LLVM IR (-O2):
%struct.hostent = type { i8*, i32, i8**, i32 }
@NO_RECOVERY = global i32* null
@NSS_STATUS_UNAVAIL = global i32 0
@AF_INET = global i32 0
@INADDRSZ = global i32 0
@.str = [2 x i8] c" \00"
@LOG_ERR = global i32 0
@.str.1 = [43 x i8] c"do_gethostbyname(): failed inet_aton on %s\00"
@NSS_STATUS_SUCCESS = global i32 0
define i32 @do_gethostbyname(i8* %0, i8* %1, %struct.hostent* nocapture writeonly %2, i8* %3, i64 %4, i32* nocapture readnone %5, i32* nocapture readnone %6) {
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %8
%10 = bitcast i32* %9 to i8*
%11 = icmp eq i8* %0, null
br i1 %11, label %52, label %12
12: ; preds = %7
%13 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 0
store i8* %1, i8** %13
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 3
store i32 %14, i32* %15
%16 = load i32, i32* @INADDRSZ
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds i8, i8* %3, i64 33
%19 = bitcast i8* %3 to i8**
store i8* %18, i8** %19
%20 = call i8* @strsep(i8** %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %47, label %22
22: ; preds = %12, %34
%23 = phi i8* [ %45, %34 ], [ %20, %12 ]
%24 = phi i64 [ %39, %34 ], [ 0, %12 ]
br label %25
25: ; preds = %22, %29
%26 = phi i8* [ %23, %22 ], [ %32, %29 ]
%27 = call i32 @inet_aton(i8* %26, i32* %9)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i32, i32* @LOG_ERR
%31 = call i32 @syslog(i32 %30, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%32 = call i8* @strsep(i8** %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%33 = icmp eq i8* %32, null
br i1 %33, label %47, label %25
34: ; preds = %25
%35 = getelementptr inbounds i8*, i8** %19, i64 %24
%36 = load i8*, i8** %35
%37 = load i32, i32* @INADDRSZ
%38 = call i32 @memcpy(i8* %36, i32* %9, i32 %37)
%39 = add i64 %24, 1
%40 = load i8*, i8** %35
%41 = load i32, i32* @INADDRSZ
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = getelementptr inbounds i8*, i8** %19, i64 %39
store i8* %43, i8** %44
%45 = call i8* @strsep(i8** %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %22
47: ; preds = %34, %29, %12
%48 = phi i64 [ 0, %12 ], [ %24, %29 ], [ %39, %34 ]
%49 = getelementptr inbounds i8*, i8** %19, i64 %48
store i8* null, i8** %49
%50 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 2
%51 = bitcast i8*** %50 to i8**
store i8* %3, i8** %51
br label %52
52: ; preds = %7, %47
%53 = phi i32* [ @NSS_STATUS_SUCCESS, %47 ], [ @NSS_STATUS_UNAVAIL, %7 ]
%54 = load i32, i32* %53
ret i32 %54
}