#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 h_addrtype;
  int /*<<< orphan*/ **h_addr_list;
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ **h_aliases;
  void *h_name;
};
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ IN6ADDRSZ;
int /*<<< orphan*/ INADDRSZ;
int /*<<< orphan*/ inet_addr(char *);
void *malloc(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ strcpy(void *, char const *);
int strlen(char const *);
int _fill_hostent(const char *name, int af, struct hostent *result) {
  result->h_name = malloc((strlen(name) + 1) * sizeof(char));
  strcpy(result->h_name, name);
  result->h_aliases = malloc(sizeof(char *));
  *result->h_aliases = NULL;
  switch (af) {
  case AF_INET:
    result->h_addrtype = AF_INET;
    result->h_length = INADDRSZ;
    break;
  case AF_INET6:
    result->h_addrtype = AF_INET6;
    result->h_length = IN6ADDRSZ;
  }
  result->h_addr_list = malloc(sizeof(char *) * 2);
  *result->h_addr_list = malloc(sizeof(in_addr_t));
  in_addr_t addr = inet_addr("127.0.0.1");
  memcpy(*result->h_addr_list, &addr, sizeof(in_addr_t));
  *(result->h_addr_list + 1) = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<_fill_hostent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    -0x18(%rbp),%rax
mov    0x20(%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x18(%rbp),%rax
mov    0x18(%rax),%rax
movq   $0x0,(%rax)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0x80,%eax
je     11fe <_fill_hostent+0xae>
jmp    11cc <_fill_hostent+0x7c>
mov    -0x20(%rbp),%eax
sub    $0x81,%eax
jne    1218 <_fill_hostent+0xc8>
jmp    11df <_fill_hostent+0x8f>
mov    -0x18(%rbp),%rax
movl   $0x81,(%rax)
mov    0x2df0(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x10(%rax)
jmp    1218 <_fill_hostent+0xc8>
mov    -0x18(%rbp),%rax
movl   $0x80,(%rax)
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    $0x10,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    $0x4,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rcx,(%rax)
lea    0xdb4(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
movq   $0x0,0x8(%rax)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fill_hostent>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%rbp
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,0x20(%rbx)
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,0x18(%rbx)
movq   $0x0,(%rax)
cmp    $0x81,%r14d
je     11b3 <_fill_hostent+0x63>
cmp    $0x80,%r14d
jne    11c2 <_fill_hostent+0x72>
mov    0x2e25(%rip),%rax
mov    $0x80,%r14d
jmp    11ba <_fill_hostent+0x6a>
mov    0x2e26(%rip),%rax
mov    %r14d,(%rbx)
mov    (%rax),%eax
mov    %eax,0x10(%rbx)
mov    $0x10,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    %rax,0x8(%rbx)
mov    $0x4,%edi
call   1070 <malloc@plt>
mov    %rax,0x0(%rbp)
lea    0xe18(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    0x8(%rbx),%rax
mov    (%rax),%rdi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    0x8(%rbx),%rax
movq   $0x0,0x8(%rax)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32**, i32, i32**, i8* }
@INADDRSZ = global i32 0
@IN6ADDRSZ = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
define i32 @_fill_hostent(i8* %0, i32 %1, %struct.hostent* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store %struct.hostent* %2, %struct.hostent** %6
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = mul i64 %11, 1
%13 = trunc i64 %12 to i32
%14 = call i8* @malloc(i32 %13)
%15 = load %struct.hostent*, %struct.hostent** %6
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i32 0, i32 4
store i8* %14, i8** %16
%17 = load %struct.hostent*, %struct.hostent** %6
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i32 0, i32 4
%19 = load i8*, i8** %18
%20 = load i8*, i8** %4
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = call i8* @malloc(i32 8)
%23 = bitcast i8* %22 to i32**
%24 = load %struct.hostent*, %struct.hostent** %6
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i32 0, i32 3
store i32** %23, i32*** %25
%26 = load %struct.hostent*, %struct.hostent** %6
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 3
%28 = load i32**, i32*** %27
store i32* null, i32** %28
%29 = load i32, i32* %5
switch i32 %29, label %42 [
i32 129, label %30
i32 128, label %36
]
30: ; preds = %3
%31 = load %struct.hostent*, %struct.hostent** %6
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 0
store i32 129, i32* %32
%33 = load i32, i32* @INADDRSZ
%34 = load %struct.hostent*, %struct.hostent** %6
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %34, i32 0, i32 2
store i32 %33, i32* %35
br label %42
36: ; preds = %3
%37 = load %struct.hostent*, %struct.hostent** %6
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i32 0, i32 0
store i32 128, i32* %38
%39 = load i32, i32* @IN6ADDRSZ
%40 = load %struct.hostent*, %struct.hostent** %6
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 2
store i32 %39, i32* %41
br label %42
42: ; preds = %36, %3, %30
%43 = call i8* @malloc(i32 16)
%44 = bitcast i8* %43 to i32**
%45 = load %struct.hostent*, %struct.hostent** %6
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 1
store i32** %44, i32*** %46
%47 = call i8* @malloc(i32 4)
%48 = bitcast i8* %47 to i32*
%49 = load %struct.hostent*, %struct.hostent** %6
%50 = getelementptr inbounds %struct.hostent, %struct.hostent* %49, i32 0, i32 1
%51 = load i32**, i32*** %50
store i32* %48, i32** %51
%52 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 %52, i32* %7
%53 = load %struct.hostent*, %struct.hostent** %6
%54 = getelementptr inbounds %struct.hostent, %struct.hostent* %53, i32 0, i32 1
%55 = load i32**, i32*** %54
%56 = load i32*, i32** %55
%57 = call i32 @memcpy(i32* %56, i32* %7, i32 4)
%58 = load %struct.hostent*, %struct.hostent** %6
%59 = getelementptr inbounds %struct.hostent, %struct.hostent* %58, i32 0, i32 1
%60 = load i32**, i32*** %59
%61 = getelementptr inbounds i32*, i32** %60, i64 1
store i32* null, i32** %61
ret i32 0
}

LLVM IR (-O2):
%struct.hostent = type { i32, i32**, i32, i32**, i8* }
@INADDRSZ = global i32 0
@IN6ADDRSZ = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
define i32 @_fill_hostent(i8* %0, i32 %1, %struct.hostent* nocapture %2) {
%4 = alloca i32
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, 1
%7 = tail call i8* @malloc(i32 %6)
%8 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 4
store i8* %7, i8** %8
%9 = tail call i32 @strcpy(i8* %7, i8* %0)
%10 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%11 = bitcast i8* %10 to i32**
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 3
%13 = bitcast i32*** %12 to i8**
store i8* %10, i8** %13
store i32* null, i32** %11
switch i32 %1, label %21 [
i32 129, label %15
i32 128, label %14
]
14: ; preds = %3
br label %15
15: ; preds = %3, %14
%16 = phi i32 [ 128, %14 ], [ %1, %3 ]
%17 = phi i32* [ @IN6ADDRSZ, %14 ], [ @INADDRSZ, %3 ]
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 0
store i32 %16, i32* %18
%19 = load i32, i32* %17
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 2
store i32 %19, i32* %20
br label %21
21: ; preds = %15, %3
%22 = tail call dereferenceable_or_null(16) i8* @malloc(i32 16)
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 1
%24 = bitcast i32*** %23 to i8**
store i8* %22, i8** %24
%25 = tail call dereferenceable_or_null(4) i8* @malloc(i32 4)
%26 = bitcast i8* %22 to i8**
store i8* %25, i8** %26
%27 = bitcast i32* %4 to i8*
%28 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 %28, i32* %4
%29 = load i32**, i32*** %23
%30 = load i32*, i32** %29
%31 = call i32 @memcpy(i32* %30, i32* %4, i32 4)
%32 = load i32**, i32*** %23
%33 = getelementptr inbounds i32*, i32** %32, i64 1
store i32* null, i32** %33
ret i32 0
}