#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 hostent {
  scalar_t__ h_addrtype;
  int h_length;
  char **h_addr_list;
};

/* Variables and functions */
scalar_t__ AF_INET;
struct in_addr *fw_calloc(unsigned int, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ inaddrcpy(struct in_addr *, struct in_addr *);
struct in_addr *host_to_addr(const char *name, unsigned int *naddr) {
  struct hostent *host;
  struct in_addr *addr;
  unsigned int i;
  *naddr = 0;
  if ((host = gethostbyname(name)) != NULL) {
    if (host->h_addrtype != AF_INET || host->h_length != sizeof(struct in_addr))
      return (struct in_addr *)NULL;
    while (host->h_addr_list[*naddr] != (char *)NULL)
      (*naddr)++;
    addr = fw_calloc(*naddr, sizeof(struct in_addr));
    for (i = 0; i < *naddr; i++)
      inaddrcpy(&(addr[i]), (struct in_addr *)host->h_addr_list[i]);
    return addr;
  } else
    return (struct in_addr *)NULL;
}


x86 assembly (clang, -O0):
<host_to_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     122c <host_to_addr+0xfc>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <host_to_addr+0x5a>
mov    -0x20(%rbp),%rax
movslq 0x8(%rax),%rax
cmp    $0x4,%rax
je     1197 <host_to_addr+0x67>
movq   $0x0,-0x8(%rbp)
jmp    1234 <host_to_addr+0x104>
jmp    119c <host_to_addr+0x6c>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
cmpq   $0x0,(%rax,%rcx,8)
je     11c5 <host_to_addr+0x95>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    119c <host_to_addr+0x6c>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    $0x4,%esi
call   1040 <fw_calloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jae    121f <host_to_addr+0xef>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%eax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    -0x2c(%rbp),%ecx
mov    (%rax,%rcx,8),%rsi
call   1050 <inaddrcpy@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e0 <host_to_addr+0xb0>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <host_to_addr+0x104>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_to_addr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
movl   $0x0,(%rsi)
call   1030 <gethostbyname@plt>
test   %rax,%rax
je     11d3 <host_to_addr+0xa3>
mov    %rax,%r12
mov    (%rax),%rax
mov    0x2e8b(%rip),%rcx
cmp    (%rcx),%rax
jne    11d3 <host_to_addr+0xa3>
cmpl   $0x4,0x8(%r12)
jne    11d3 <host_to_addr+0xa3>
mov    0x10(%r12),%rax
mov    (%r14),%edi
cmpq   $0x0,(%rax,%rdi,8)
je     118d <host_to_addr+0x5d>
nopl   0x0(%rax)
add    $0x1,%edi
cmpq   $0x0,(%rax,%rdi,8)
jne    1180 <host_to_addr+0x50>
mov    %edi,(%r14)
mov    $0x4,%esi
call   1040 <fw_calloc@plt>
mov    %rax,%r15
cmpl   $0x0,(%r14)
je     11d6 <host_to_addr+0xa6>
xor    %ebx,%ebx
mov    %r15,%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%r12),%rax
mov    (%rax,%rbx,8),%rsi
mov    %r13,%rdi
call   1050 <inaddrcpy@plt>
add    $0x1,%rbx
mov    (%r14),%eax
add    $0x4,%r13
cmp    %rax,%rbx
jb     11b0 <host_to_addr+0x80>
jmp    11d6 <host_to_addr+0xa6>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.hostent = type { i64, i32, i8** }
@AF_INET = global i64 0
define %struct.in_addr* @host_to_addr(i8* %0, i32* %1) {
%3 = alloca %struct.in_addr*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.hostent*
%7 = alloca %struct.in_addr*
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = load i32*, i32** %5
store i32 0, i32* %9
%10 = load i8*, i8** %4
%11 = call %struct.hostent* @gethostbyname(i8* %10)
store %struct.hostent* %11, %struct.hostent** %6
%12 = icmp ne %struct.hostent* %11, null
br i1 %12, label %13, label %69
13: ; preds = %2
%14 = load %struct.hostent*, %struct.hostent** %6
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i32 0, i32 0
%16 = load i64, i64* %15
%17 = load i64, i64* @AF_INET
%18 = icmp ne i64 %16, %17
br i1 %18, label %25, label %19
19: ; preds = %13
%20 = load %struct.hostent*, %struct.hostent** %6
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = sext i32 %22 to i64
%24 = icmp ne i64 %23, 4
br i1 %24, label %25, label %26
25: ; preds = %19, %13
store %struct.in_addr* null, %struct.in_addr** %3
br label %70
26: ; preds = %19
br label %27
27: ; preds = %37, %26
%28 = load %struct.hostent*, %struct.hostent** %6
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 2
%30 = load i8**, i8*** %29
%31 = load i32*, i32** %5
%32 = load i32, i32* %31
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %30, i64 %33
%35 = load i8*, i8** %34
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %41
37: ; preds = %27
%38 = load i32*, i32** %5
%39 = load i32, i32* %38
%40 = add i32 %39, 1
store i32 %40, i32* %38
br label %27
41: ; preds = %27
%42 = load i32*, i32** %5
%43 = load i32, i32* %42
%44 = call %struct.in_addr* @fw_calloc(i32 %43, i32 4)
store %struct.in_addr* %44, %struct.in_addr** %7
store i32 0, i32* %8
br label %45
45: ; preds = %64, %41
%46 = load i32, i32* %8
%47 = load i32*, i32** %5
%48 = load i32, i32* %47
%49 = icmp ult i32 %46, %48
br i1 %49, label %50, label %67
50: ; preds = %45
%51 = load %struct.in_addr*, %struct.in_addr** %7
%52 = load i32, i32* %8
%53 = zext i32 %52 to i64
%54 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %51, i64 %53
%55 = load %struct.hostent*, %struct.hostent** %6
%56 = getelementptr inbounds %struct.hostent, %struct.hostent* %55, i32 0, i32 2
%57 = load i8**, i8*** %56
%58 = load i32, i32* %8
%59 = zext i32 %58 to i64
%60 = getelementptr inbounds i8*, i8** %57, i64 %59
%61 = load i8*, i8** %60
%62 = bitcast i8* %61 to %struct.in_addr*
%63 = call i32 @inaddrcpy(%struct.in_addr* %54, %struct.in_addr* %62)
br label %64
64: ; preds = %50
%65 = load i32, i32* %8
%66 = add i32 %65, 1
store i32 %66, i32* %8
br label %45
67: ; preds = %45
%68 = load %struct.in_addr*, %struct.in_addr** %7
store %struct.in_addr* %68, %struct.in_addr** %3
br label %70
69: ; preds = %2
store %struct.in_addr* null, %struct.in_addr** %3
br label %70
70: ; preds = %69, %67, %25
%71 = load %struct.in_addr*, %struct.in_addr** %3
ret %struct.in_addr* %71
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i64, i32, i8** }
@AF_INET = global i64 0
define %struct.in_addr* @host_to_addr(i8* %0, i32* nocapture %1) {
store i32 0, i32* %1
%3 = tail call %struct.hostent* @gethostbyname(i8* %0)
%4 = icmp eq %struct.hostent* %3, null
br i1 %4, label %47, label %5
5: ; preds = %2
%6 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 0
%7 = load i64, i64* %6
%8 = load i64, i64* @AF_INET
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %47
10: ; preds = %5
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = icmp eq i32 %12, 4
br i1 %13, label %14, label %47
14: ; preds = %10
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 2
%16 = load i8**, i8*** %15
%17 = load i32, i32* %1
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8*, i8** %16, i64 %18
%20 = load i8*, i8** %19
%21 = icmp eq i8* %20, null
br i1 %21, label %30, label %22
22: ; preds = %14, %22
%23 = phi i32 [ %24, %22 ], [ %17, %14 ]
%24 = add i32 %23, 1
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %16, i64 %25
%27 = load i8*, i8** %26
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %22
29: ; preds = %22
store i32 %24, i32* %1
br label %30
30: ; preds = %29, %14
%31 = phi i32 [ %24, %29 ], [ %17, %14 ]
%32 = tail call %struct.in_addr* @fw_calloc(i32 %31, i32 4)
%33 = load i32, i32* %1
%34 = icmp eq i32 %33, 0
br i1 %34, label %47, label %35
35: ; preds = %30, %35
%36 = phi i64 [ %43, %35 ], [ 0, %30 ]
%37 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %32, i64 %36
%38 = load i8**, i8*** %15
%39 = getelementptr inbounds i8*, i8** %38, i64 %36
%40 = bitcast i8** %39 to %struct.in_addr**
%41 = load %struct.in_addr*, %struct.in_addr** %40
%42 = tail call i32 @inaddrcpy(%struct.in_addr* %37, %struct.in_addr* %41)
%43 = add nuw nsw i64 %36, 1
%44 = load i32, i32* %1
%45 = zext i32 %44 to i64
%46 = icmp ult i64 %43, %45
br i1 %46, label %35, label %47
47: ; preds = %35, %30, %2, %5, %10
%48 = phi %struct.in_addr* [ null, %10 ], [ null, %5 ], [ null, %2 ], [ %32, %30 ], [ %32, %35 ]
ret %struct.in_addr* %48
}