#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 in_addr {
  int dummy;
};
struct in6_addr {
  int dummy;
};
struct hostent {
  int h_length;
  char **h_addr_list;
  char *h_name;
  char **h_aliases;
  void *h_addrtype;
};
typedef int /*<<< orphan*/ sqlite3_stmt;
typedef enum nss_status { ____Placeholder_nss_status } nss_status;

/* Variables and functions */
void *AF_INET;
void *AF_INET6;
int ERANGE;
int NSS_STATUS_NOTFOUND;
int NSS_STATUS_SUCCESS;
int NSS_STATUS_TRYAGAIN;
scalar_t__ inet_pton(void *, char const *, char *);
int /*<<< orphan*/ memset(struct hostent *, int /*<<< orphan*/, int);
scalar_t__ sqlite3_column_text(int /*<<< orphan*/ *, int);
int strlcpy(char *, char const *, size_t);
enum nss_status populate(sqlite3_stmt *stmt, struct hostent *result,
                         char *buffer, size_t length, int *errnop) {
  const char *hostname;
  const char *address;
  int delta;
  char *bufp;
  struct in6_addr *inet6_addr;
  struct in_addr *inet_addr;
  if (stmt == NULL) {
    return NSS_STATUS_NOTFOUND;
  }
  memset(result, 0, sizeof(*result));
  hostname = (const char *)sqlite3_column_text(stmt, 0);
  address = (const char *)sqlite3_column_text(stmt, 1);
  bufp = buffer;
  if (inet_pton(AF_INET6, address, buffer) > 0) {
    inet6_addr = (struct in6_addr *)bufp;
    delta = sizeof(struct in6_addr);
    result->h_addrtype = AF_INET6;
    result->h_length = sizeof(struct in6_addr);
  }
  if (inet_pton(AF_INET, address, buffer) > 0) {
    inet_addr = (struct in_addr *)bufp;
    delta = sizeof(struct in_addr);
    result->h_addrtype = AF_INET;
    result->h_length = sizeof(struct in_addr);
  }
  if (length < delta)
    goto range_error;
  bufp += delta;
  length -= delta;
  result->h_addr_list = (char **)bufp;
  delta = sizeof(char *) * 2;
  if (length < delta)
    goto range_error;
  bufp += delta;
  length -= delta;
  result->h_addr_list[0] = buffer;
  result->h_addr_list[1] = 0;
  result->h_name = bufp;
  delta = strlcpy(result->h_name, hostname, length);
  if (length < delta)
    goto range_error;
  bufp += delta;
  length -= delta;
  result->h_aliases = (char **)bufp;
  result->h_aliases[0] = 0;
  return NSS_STATUS_SUCCESS;
range_error:
  *errnop = ERANGE;
  return NSS_STATUS_TRYAGAIN;
}


x86 assembly (clang, -O0):
<populate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1178 <populate+0x38>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1388 <populate+0x248>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1050 <sqlite3_column_text@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1050 <sqlite3_column_text@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <inet_pton@plt>
cmp    $0x0,%rax
jle    11fd <populate+0xbd>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x4,-0x44(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    -0x18(%rbp),%rax
movl   $0x4,(%rax)
mov    0x2de4(%rip),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <inet_pton@plt>
cmp    $0x0,%rax
jle    1249 <populate+0x109>
mov    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x4,-0x44(%rbp)
mov    0x2db4(%rip),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    -0x18(%rbp),%rax
movl   $0x4,(%rax)
mov    -0x28(%rbp),%rax
movslq -0x44(%rbp),%rcx
cmp    %rcx,%rax
jae    125f <populate+0x11f>
jmp    136d <populate+0x22d>
mov    -0x44(%rbp),%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
movslq -0x44(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x50(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
movl   $0x10,-0x44(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x44(%rbp),%rcx
cmp    %rcx,%rax
jae    12a8 <populate+0x168>
jmp    136d <populate+0x22d>
mov    -0x44(%rbp),%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
movslq -0x44(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
movq   $0x0,0x8(%rax)
mov    -0x50(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <strlcpy@plt>
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x44(%rbp),%rcx
cmp    %rcx,%rax
jae    1321 <populate+0x1e1>
jmp    136d <populate+0x22d>
mov    -0x44(%rbp),%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
movslq -0x44(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x50(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x18(%rbp),%rax
mov    0x18(%rax),%rax
movq   $0x0,(%rax)
mov    0x2c6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1388 <populate+0x248>
mov    0x2c64(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2c3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<populate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1244 <populate+0x104>
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rsi,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    %rbp,%rdi
xor    %esi,%esi
call   1050 <sqlite3_column_text@plt>
mov    %rax,(%rsp)
mov    %rbp,%rdi
mov    $0x1,%esi
call   1050 <sqlite3_column_text@plt>
mov    %rax,%rbp
mov    0x2e49(%rip),%r13
mov    0x0(%r13),%rdi
mov    %rax,%rsi
mov    %r12,%rdx
call   1040 <inet_pton@plt>
test   %rax,%rax
jle    11b9 <populate+0x79>
mov    0x0(%r13),%rax
mov    %rax,0x20(%rbx)
movl   $0x4,(%rbx)
mov    0x2e28(%rip),%r13
mov    0x0(%r13),%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1040 <inet_pton@plt>
test   %rax,%rax
jle    11e2 <populate+0xa2>
mov    0x0(%r13),%rax
mov    %rax,0x20(%rbx)
movl   $0x4,(%rbx)
cmp    $0x4,%r15
jb     122f <populate+0xef>
lea    0x4(%r12),%rax
lea    -0x4(%r15),%rcx
mov    %rax,0x8(%rbx)
cmp    $0x10,%rcx
jb     122f <populate+0xef>
lea    0x14(%r12),%rbp
add    $0xffffffffffffffec,%r15
mov    %r12,0x4(%r12)
mov    0x8(%rbx),%rax
movq   $0x0,0x8(%rax)
mov    %rbp,0x10(%rbx)
mov    %rbp,%rdi
mov    (%rsp),%rsi
mov    %r15,%rdx
call   1060 <strlcpy@plt>
cltq
cmp    %rax,%r15
jae    125c <populate+0x11c>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
mov    0x2d7e(%rip),%rax
jmp    124b <populate+0x10b>
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    (%rax,%rbp,1),%rcx
mov    %rcx,0x18(%rbx)
movq   $0x0,0x0(%rbp,%rax,1)
mov    0x2d5c(%rip),%rax
jmp    124b <populate+0x10b>

LLVM IR (-O0):
%struct.hostent = type { i32, i8**, i8*, i8**, i8* }
%struct.in6_addr = type { i32 }
%struct.in_addr = type { i32 }
@NSS_STATUS_NOTFOUND = global i32 0
@AF_INET6 = global i8* null
@AF_INET = global i8* null
@NSS_STATUS_SUCCESS = global i32 0
@ERANGE = global i32 0
@NSS_STATUS_TRYAGAIN = global i32 0
define i32 @populate(i32* %0, %struct.hostent* %1, i8* %2, i64 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.hostent*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca %struct.in6_addr*
%17 = alloca %struct.in_addr*
store i32* %0, i32** %7
store %struct.hostent* %1, %struct.hostent** %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store i32* %4, i32** %11
%18 = load i32*, i32** %7
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %5
%21 = load i32, i32* @NSS_STATUS_NOTFOUND
store i32 %21, i32* %6
br label %137
22: ; preds = %5
%23 = load %struct.hostent*, %struct.hostent** %8
%24 = call i32 @memset(%struct.hostent* %23, i32 0, i32 40)
%25 = load i32*, i32** %7
%26 = call i64 @sqlite3_column_text(i32* %25, i32 0)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %12
%28 = load i32*, i32** %7
%29 = call i64 @sqlite3_column_text(i32* %28, i32 1)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %13
%31 = load i8*, i8** %9
store i8* %31, i8** %15
%32 = load i8*, i8** @AF_INET6
%33 = load i8*, i8** %13
%34 = load i8*, i8** %9
%35 = call i64 @inet_pton(i8* %32, i8* %33, i8* %34)
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %22
%38 = load i8*, i8** %15
%39 = bitcast i8* %38 to %struct.in6_addr*
store %struct.in6_addr* %39, %struct.in6_addr** %16
store i32 4, i32* %14
%40 = load i8*, i8** @AF_INET6
%41 = load %struct.hostent*, %struct.hostent** %8
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %41, i32 0, i32 4
store i8* %40, i8** %42
%43 = load %struct.hostent*, %struct.hostent** %8
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 0
store i32 4, i32* %44
br label %45
45: ; preds = %37, %22
%46 = load i8*, i8** @AF_INET
%47 = load i8*, i8** %13
%48 = load i8*, i8** %9
%49 = call i64 @inet_pton(i8* %46, i8* %47, i8* %48)
%50 = icmp sgt i64 %49, 0
br i1 %50, label %51, label %59
51: ; preds = %45
%52 = load i8*, i8** %15
%53 = bitcast i8* %52 to %struct.in_addr*
store %struct.in_addr* %53, %struct.in_addr** %17
store i32 4, i32* %14
%54 = load i8*, i8** @AF_INET
%55 = load %struct.hostent*, %struct.hostent** %8
%56 = getelementptr inbounds %struct.hostent, %struct.hostent* %55, i32 0, i32 4
store i8* %54, i8** %56
%57 = load %struct.hostent*, %struct.hostent** %8
%58 = getelementptr inbounds %struct.hostent, %struct.hostent* %57, i32 0, i32 0
store i32 4, i32* %58
br label %59
59: ; preds = %51, %45
%60 = load i64, i64* %10
%61 = load i32, i32* %14
%62 = sext i32 %61 to i64
%63 = icmp ult i64 %60, %62
br i1 %63, label %64, label %65
64: ; preds = %59
br label %133
65: ; preds = %59
%66 = load i32, i32* %14
%67 = load i8*, i8** %15
%68 = sext i32 %66 to i64
%69 = getelementptr inbounds i8, i8* %67, i64 %68
store i8* %69, i8** %15
%70 = load i32, i32* %14
%71 = sext i32 %70 to i64
%72 = load i64, i64* %10
%73 = sub i64 %72, %71
store i64 %73, i64* %10
%74 = load i8*, i8** %15
%75 = bitcast i8* %74 to i8**
%76 = load %struct.hostent*, %struct.hostent** %8
%77 = getelementptr inbounds %struct.hostent, %struct.hostent* %76, i32 0, i32 1
store i8** %75, i8*** %77
store i32 16, i32* %14
%78 = load i64, i64* %10
%79 = load i32, i32* %14
%80 = sext i32 %79 to i64
%81 = icmp ult i64 %78, %80
br i1 %81, label %82, label %83
82: ; preds = %65
br label %133
83: ; preds = %65
%84 = load i32, i32* %14
%85 = load i8*, i8** %15
%86 = sext i32 %84 to i64
%87 = getelementptr inbounds i8, i8* %85, i64 %86
store i8* %87, i8** %15
%88 = load i32, i32* %14
%89 = sext i32 %88 to i64
%90 = load i64, i64* %10
%91 = sub i64 %90, %89
store i64 %91, i64* %10
%92 = load i8*, i8** %9
%93 = load %struct.hostent*, %struct.hostent** %8
%94 = getelementptr inbounds %struct.hostent, %struct.hostent* %93, i32 0, i32 1
%95 = load i8**, i8*** %94
%96 = getelementptr inbounds i8*, i8** %95, i64 0
store i8* %92, i8** %96
%97 = load %struct.hostent*, %struct.hostent** %8
%98 = getelementptr inbounds %struct.hostent, %struct.hostent* %97, i32 0, i32 1
%99 = load i8**, i8*** %98
%100 = getelementptr inbounds i8*, i8** %99, i64 1
store i8* null, i8** %100
%101 = load i8*, i8** %15
%102 = load %struct.hostent*, %struct.hostent** %8
%103 = getelementptr inbounds %struct.hostent, %struct.hostent* %102, i32 0, i32 2
store i8* %101, i8** %103
%104 = load %struct.hostent*, %struct.hostent** %8
%105 = getelementptr inbounds %struct.hostent, %struct.hostent* %104, i32 0, i32 2
%106 = load i8*, i8** %105
%107 = load i8*, i8** %12
%108 = load i64, i64* %10
%109 = call i32 @strlcpy(i8* %106, i8* %107, i64 %108)
store i32 %109, i32* %14
%110 = load i64, i64* %10
%111 = load i32, i32* %14
%112 = sext i32 %111 to i64
%113 = icmp ult i64 %110, %112
br i1 %113, label %114, label %115
114: ; preds = %83
br label %133
115: ; preds = %83
%116 = load i32, i32* %14
%117 = load i8*, i8** %15
%118 = sext i32 %116 to i64
%119 = getelementptr inbounds i8, i8* %117, i64 %118
store i8* %119, i8** %15
%120 = load i32, i32* %14
%121 = sext i32 %120 to i64
%122 = load i64, i64* %10
%123 = sub i64 %122, %121
store i64 %123, i64* %10
%124 = load i8*, i8** %15
%125 = bitcast i8* %124 to i8**
%126 = load %struct.hostent*, %struct.hostent** %8
%127 = getelementptr inbounds %struct.hostent, %struct.hostent* %126, i32 0, i32 3
store i8** %125, i8*** %127
%128 = load %struct.hostent*, %struct.hostent** %8
%129 = getelementptr inbounds %struct.hostent, %struct.hostent* %128, i32 0, i32 3
%130 = load i8**, i8*** %129
%131 = getelementptr inbounds i8*, i8** %130, i64 0
store i8* null, i8** %131
%132 = load i32, i32* @NSS_STATUS_SUCCESS
store i32 %132, i32* %6
br label %137
133: ; preds = %114, %82, %64
%134 = load i32, i32* @ERANGE
%135 = load i32*, i32** %11
store i32 %134, i32* %135
%136 = load i32, i32* @NSS_STATUS_TRYAGAIN
store i32 %136, i32* %6
br label %137
137: ; preds = %133, %115, %20
%138 = load i32, i32* %6
ret i32 %138
}

LLVM IR (-O2):
%struct.hostent = type { i32, i8**, i8*, i8**, i8* }
@NSS_STATUS_NOTFOUND = global i32 0
@AF_INET6 = global i8* null
@AF_INET = global i8* null
@NSS_STATUS_SUCCESS = global i32 0
@ERANGE = global i32 0
@NSS_STATUS_TRYAGAIN = global i32 0
define i32 @populate(i32* %0, %struct.hostent* %1, i8* %2, i64 %3, i32* nocapture writeonly %4) {
%6 = icmp eq i32* %0, null
br i1 %6, label %53, label %7
7: ; preds = %5
%8 = tail call i32 @memset(%struct.hostent* %1, i32 0, i32 40)
%9 = tail call i64 @sqlite3_column_text(i32* %0, i32 0)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i64 @sqlite3_column_text(i32* %0, i32 1)
%12 = inttoptr i64 %11 to i8*
%13 = load i8*, i8** @AF_INET6
%14 = tail call i64 @inet_pton(i8* %13, i8* %12, i8* %2)
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %7
%17 = load i8*, i8** @AF_INET6
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %1, i64 0, i32 4
store i8* %17, i8** %18
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %1, i64 0, i32 0
store i32 4, i32* %19
br label %20
20: ; preds = %16, %7
%21 = load i8*, i8** @AF_INET
%22 = tail call i64 @inet_pton(i8* %21, i8* %12, i8* %2)
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i8*, i8** @AF_INET
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %1, i64 0, i32 4
store i8* %25, i8** %26
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %1, i64 0, i32 0
store i32 4, i32* %27
br label %28
28: ; preds = %24, %20
%29 = icmp ult i64 %3, 4
br i1 %29, label %51, label %30
30: ; preds = %28
%31 = getelementptr inbounds i8, i8* %2, i64 4
%32 = add i64 %3, -4
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %1, i64 0, i32 1
%34 = bitcast i8*** %33 to i8**
store i8* %31, i8** %34
%35 = icmp ult i64 %32, 16
br i1 %35, label %51, label %36
36: ; preds = %30
%37 = bitcast i8* %31 to i8**
%38 = getelementptr inbounds i8, i8* %2, i64 20
%39 = add i64 %3, -20
store i8* %2, i8** %37
%40 = load i8**, i8*** %33
%41 = getelementptr inbounds i8*, i8** %40, i64 1
store i8* null, i8** %41
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %1, i64 0, i32 2
store i8* %38, i8** %42
%43 = tail call i32 @strlcpy(i8* %38, i8* %10, i64 %39)
%44 = sext i32 %43 to i64
%45 = icmp ult i64 %39, %44
br i1 %45, label %51, label %46
46: ; preds = %36
%47 = getelementptr inbounds i8, i8* %38, i64 %44
%48 = bitcast i8* %47 to i8**
%49 = getelementptr inbounds %struct.hostent, %struct.hostent* %1, i64 0, i32 3
%50 = bitcast i8*** %49 to i8**
store i8* %47, i8** %50
store i8* null, i8** %48
br label %53
51: ; preds = %36, %30, %28
%52 = load i32, i32* @ERANGE
store i32 %52, i32* %4
br label %53
53: ; preds = %5, %51, %46
%54 = phi i32* [ @NSS_STATUS_TRYAGAIN, %51 ], [ @NSS_STATUS_SUCCESS, %46 ], [ @NSS_STATUS_NOTFOUND, %5 ]
%55 = load i32, i32* %54
ret i32 %55
}