#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 */
typedef scalar_t__ u_int32_t;
struct netent {
  scalar_t__ n_net;
};
struct in_addr {
  scalar_t__ s_addr;
};
struct hostent {
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
struct hostent *gethostbyname(char *);
struct netent *getnetbyname(char *);
scalar_t__ inet_aton(char *, struct in_addr *);
scalar_t__ ntohl(scalar_t__);
int resolv_name(char *s, u_int32_t *a) {
  struct in_addr addr;
  struct hostent *hp;
  struct netent *np;
  if (inet_aton(s, &addr)) {
    *a = (u_int32_t)ntohl(addr.s_addr);
    return 0;
  }
  np = getnetbyname(s);
  if (np) {
    *a = (u_int32_t)np->n_net;
    return 0;
  }
  hp = gethostbyname(s);
  if (hp) {
    *a = (u_int32_t)ntohl(*(u_int32_t *)hp->h_addr_list[0]);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<resolv_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <inet_aton@plt>
cmp    $0x0,%rax
je     1186 <resolv_name+0x46>
mov    -0x20(%rbp),%rdi
call   1060 <ntohl@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11ff <resolv_name+0xbf>
mov    -0x10(%rbp),%rdi
call   1030 <getnetbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11b8 <resolv_name+0x78>
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11ff <resolv_name+0xbf>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f8 <resolv_name+0xb8>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rdi
call   1060 <ntohl@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11ff <resolv_name+0xbf>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolv_name>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1050 <inet_aton@plt>
test   %rax,%rax
je     1162 <resolv_name+0x22>
mov    (%rsp),%rdi
call   1060 <ntohl@plt>
jmp    1172 <resolv_name+0x32>
mov    %rbx,%rdi
call   1030 <getnetbyname@plt>
test   %rax,%rax
je     117f <resolv_name+0x3f>
mov    (%rax),%rax
mov    %rax,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1197 <resolv_name+0x57>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rdi
jmp    115b <resolv_name+0x1b>
mov    $0xffffffff,%eax
jmp    1177 <resolv_name+0x37>

LLVM IR (-O0):
%struct.in_addr = type { i64 }
%struct.hostent = type { i64* }
%struct.netent = type { i64 }
define i32 @resolv_name(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca %struct.in_addr
%7 = alloca %struct.hostent*
%8 = alloca %struct.netent*
store i8* %0, i8** %4
store i64* %1, i64** %5
%9 = load i8*, i8** %4
%10 = call i64 @inet_aton(i8* %9, %struct.in_addr* %6)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %6, i32 0, i32 0
%14 = load i64, i64* %13
%15 = call i64 @ntohl(i64 %14)
%16 = load i64*, i64** %5
store i64 %15, i64* %16
store i32 0, i32* %3
br label %43
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call %struct.netent* @getnetbyname(i8* %18)
store %struct.netent* %19, %struct.netent** %8
%20 = load %struct.netent*, %struct.netent** %8
%21 = icmp ne %struct.netent* %20, null
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load %struct.netent*, %struct.netent** %8
%24 = getelementptr inbounds %struct.netent, %struct.netent* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64*, i64** %5
store i64 %25, i64* %26
store i32 0, i32* %3
br label %43
27: ; preds = %17
%28 = load i8*, i8** %4
%29 = call %struct.hostent* @gethostbyname(i8* %28)
store %struct.hostent* %29, %struct.hostent** %7
%30 = load %struct.hostent*, %struct.hostent** %7
%31 = icmp ne %struct.hostent* %30, null
br i1 %31, label %32, label %42
32: ; preds = %27
%33 = load %struct.hostent*, %struct.hostent** %7
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i64*, i64** %34
%36 = getelementptr inbounds i64, i64* %35, i64 0
%37 = load i64, i64* %36
%38 = inttoptr i64 %37 to i64*
%39 = load i64, i64* %38
%40 = call i64 @ntohl(i64 %39)
%41 = load i64*, i64** %5
store i64 %40, i64* %41
store i32 0, i32* %3
br label %43
42: ; preds = %27
store i32 -1, i32* %3
br label %43
43: ; preds = %42, %32, %22, %12
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.in_addr = type { i64 }
%struct.netent = type { i64 }
%struct.hostent = type { i64* }
define i32 @resolv_name(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.in_addr
%4 = bitcast %struct.in_addr* %3 to i8*
%5 = call i64 @inet_aton(i8* %0, %struct.in_addr* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i64 0, i32 0
%9 = load i64, i64* %8
%10 = call i64 @ntohl(i64 %9)
br label %27
11: ; preds = %2
%12 = call %struct.netent* @getnetbyname(i8* %0)
%13 = icmp eq %struct.netent* %12, null
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.netent, %struct.netent* %12, i64 0, i32 0
%16 = load i64, i64* %15
br label %27
17: ; preds = %11
%18 = call %struct.hostent* @gethostbyname(i8* %0)
%19 = icmp eq %struct.hostent* %18, null
br i1 %19, label %29, label %20
20: ; preds = %17
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i64 0, i32 0
%22 = load i64*, i64** %21
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to i64*
%25 = load i64, i64* %24
%26 = call i64 @ntohl(i64 %25)
br label %27
27: ; preds = %7, %14, %20
%28 = phi i64 [ %26, %20 ], [ %16, %14 ], [ %10, %7 ]
store i64 %28, i64* %1
br label %29
29: ; preds = %27, %17
%30 = phi i32 [ -1, %17 ], [ 0, %27 ]
ret i32 %30
}