#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_addr_list;
};

/* Variables and functions */
scalar_t__ TRY_AGAIN;
struct hostent *gethostbyname(char const *);
scalar_t__ h_errno;
int /*<<< orphan*/ snprintf(char *, int, char *, int, int, int, int);
int net_host2ip(const char *hostname, char *ip, int len) {
  struct hostent *host;
  do {
    host = gethostbyname(hostname);
    if (0 == host) {
      if (TRY_AGAIN == h_errno) {
        continue;
      }
      return -1;
    } else
      break;
  } while (1);
  snprintf(ip, len, "%d.%d.%d.%d", (host->h_addr_list[0][0] & 0xff),
           (host->h_addr_list[0][1] & 0xff), (host->h_addr_list[0][2] & 0xff),
           (host->h_addr_list[0][3] & 0xff));
  return 0;
}


x86 assembly (clang, -O0):
<net_host2ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1177 <net_host2ip+0x57>
mov    0x2e85(%rip),%rax
mov    (%rax),%rax
mov    0x2e8b(%rip),%rcx
cmp    (%rcx),%rax
jne    116b <net_host2ip+0x4b>
jmp    117c <net_host2ip+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <net_host2ip+0xd6>
jmp    118b <net_host2ip+0x6b>
mov    $0x1,%al
test   $0x1,%al
jne    1133 <net_host2ip+0x13>
jmp    118b <net_host2ip+0x6b>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%ecx
and    $0xff,%ecx
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x4(%rax),%r8d
and    $0xff,%r8d
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x8(%rax),%r9d
and    $0xff,%r9d
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0xc(%rax),%eax
and    $0xff,%eax
lea    0xe19(%rip),%rdx
mov    %eax,(%rsp)
call   1030 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_host2ip>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
mov    0x2e9c(%rip),%r12
mov    0x2ea5(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
jne    116d <net_host2ip+0x4d>
mov    (%r12),%rax
cmp    (%rbx),%rax
je     1150 <net_host2ip+0x30>
mov    $0xffffffff,%eax
jmp    119b <net_host2ip+0x7b>
mov    (%rax),%rax
mov    (%rax),%rax
movzbl (%rax),%ecx
movzbl 0x4(%rax),%r8d
movzbl 0x8(%rax),%r9d
movzbl 0xc(%rax),%eax
mov    %eax,(%rsp)
lea    0xe72(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
call   1030 <snprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32** }
@TRY_AGAIN = global i64 0
@h_errno = global i64 0
@.str = [12 x i8] c"%d.%d.%d.%d\00"
define i32 @net_host2ip(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.hostent*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
br label %9
9: ; preds = %21, %3
%10 = load i8*, i8** %5
%11 = call %struct.hostent* @gethostbyname(i8* %10)
store %struct.hostent* %11, %struct.hostent** %8
%12 = load %struct.hostent*, %struct.hostent** %8
%13 = icmp eq %struct.hostent* null, %12
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = load i64, i64* @TRY_AGAIN
%16 = load i64, i64* @h_errno
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14
br label %21
19: ; preds = %14
store i32 -1, i32* %4
br label %58
20: ; preds = %9
br label %22
21: ; preds = %18
br i1 true, label %9, label %22
22: ; preds = %21, %20
%23 = load i8*, i8** %6
%24 = load i32, i32* %7
%25 = load %struct.hostent*, %struct.hostent** %8
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 0
%27 = load i32**, i32*** %26
%28 = getelementptr inbounds i32*, i32** %27, i64 0
%29 = load i32*, i32** %28
%30 = getelementptr inbounds i32, i32* %29, i64 0
%31 = load i32, i32* %30
%32 = and i32 %31, 255
%33 = load %struct.hostent*, %struct.hostent** %8
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i32**, i32*** %34
%36 = getelementptr inbounds i32*, i32** %35, i64 0
%37 = load i32*, i32** %36
%38 = getelementptr inbounds i32, i32* %37, i64 1
%39 = load i32, i32* %38
%40 = and i32 %39, 255
%41 = load %struct.hostent*, %struct.hostent** %8
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %41, i32 0, i32 0
%43 = load i32**, i32*** %42
%44 = getelementptr inbounds i32*, i32** %43, i64 0
%45 = load i32*, i32** %44
%46 = getelementptr inbounds i32, i32* %45, i64 2
%47 = load i32, i32* %46
%48 = and i32 %47, 255
%49 = load %struct.hostent*, %struct.hostent** %8
%50 = getelementptr inbounds %struct.hostent, %struct.hostent* %49, i32 0, i32 0
%51 = load i32**, i32*** %50
%52 = getelementptr inbounds i32*, i32** %51, i64 0
%53 = load i32*, i32** %52
%54 = getelementptr inbounds i32, i32* %53, i64 3
%55 = load i32, i32* %54
%56 = and i32 %55, 255
%57 = call i32 @snprintf(i8* %23, i32 %24, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %32, i32 %40, i32 %48, i32 %56)
store i32 0, i32* %4
br label %58
58: ; preds = %22, %19
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
%struct.hostent = type { i32** }
@TRY_AGAIN = global i64 0
@h_errno = global i64 0
@.str = [12 x i8] c"%d.%d.%d.%d\00"
define i32 @net_host2ip(i8* %0, i8* %1, i32 %2) {
br label %4
4: ; preds = %7, %3
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = load i64, i64* @TRY_AGAIN
%9 = load i64, i64* @h_errno
%10 = icmp eq i64 %8, %9
br i1 %10, label %4, label %27
11: ; preds = %4
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%13 = load i32**, i32*** %12
%14 = load i32*, i32** %13
%15 = load i32, i32* %14
%16 = and i32 %15, 255
%17 = getelementptr inbounds i32, i32* %14, i64 1
%18 = load i32, i32* %17
%19 = and i32 %18, 255
%20 = getelementptr inbounds i32, i32* %14, i64 2
%21 = load i32, i32* %20
%22 = and i32 %21, 255
%23 = getelementptr inbounds i32, i32* %14, i64 3
%24 = load i32, i32* %23
%25 = and i32 %24, 255
%26 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %19, i32 %22, i32 %25)
br label %27
27: ; preds = %7, %11
%28 = phi i32 [ 0, %11 ], [ -1, %7 ]
ret i32 %28
}