#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 {
  scalar_t__ s_addr;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
scalar_t__ INADDR_ANY;
scalar_t__ INADDR_NONE;
int /*<<< orphan*/ bzero(struct in_addr *, int);
struct hostent *gethostbyname(char *);
scalar_t__ inet_addr(char *);
int /*<<< orphan*/ memcpy(struct in_addr *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int findip(char *hostname, struct in_addr *addr) {
  struct hostent *hent;
  hent = gethostbyname(hostname);
  if (hent == NULL)
    return -1;
  bzero(addr, sizeof(struct in_addr));
  memcpy(addr, hent->h_addr, hent->h_length);
  if ((addr->s_addr == INADDR_ANY) || (addr->s_addr == INADDR_NONE))
    addr->s_addr = inet_addr(hostname);
  if ((addr->s_addr == INADDR_ANY) || (addr->s_addr == INADDR_NONE))
    return -1;
  else
    return 1;
}


x86 assembly (clang, -O0):
<findip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1174 <findip+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <findip+0xda>
mov    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e3a(%rip),%rcx
cmp    (%rcx),%rax
je     11c6 <findip+0x86>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
jne    11d9 <findip+0x99>
mov    -0x10(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
je     1207 <findip+0xc7>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%rax
jne    1213 <findip+0xd3>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <findip+0xda>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findip>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11c1 <findip+0x81>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r15,%rdi
call   1050 <memcpy@plt>
mov    (%r15),%rax
mov    0x2e61(%rip),%rbx
mov    (%rbx),%rcx
mov    0x2e5f(%rip),%r12
cmp    %rcx,%rax
je     1197 <findip+0x57>
mov    (%r12),%rdx
cmp    %rdx,%rax
jne    11a9 <findip+0x69>
mov    %r14,%rdi
call   1030 <inet_addr@plt>
mov    %rax,(%r15)
mov    (%rbx),%rcx
mov    (%r12),%rdx
cmp    %rcx,%rax
setne  %cl
cmp    %rdx,%rax
setne  %al
and    %cl,%al
movzbl %al,%eax
add    %eax,%eax
add    $0xffffffff,%eax
jmp    11c6 <findip+0x86>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.in_addr = type { i64 }
%struct.hostent = type { i32, i32 }
@INADDR_ANY = global i64 0
@INADDR_NONE = global i64 0
define i32 @findip(i8* %0, %struct.in_addr* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.in_addr*
%6 = alloca %struct.hostent*
store i8* %0, i8** %4
store %struct.in_addr* %1, %struct.in_addr** %5
%7 = load i8*, i8** %4
%8 = call %struct.hostent* @gethostbyname(i8* %7)
store %struct.hostent* %8, %struct.hostent** %6
%9 = load %struct.hostent*, %struct.hostent** %6
%10 = icmp eq %struct.hostent* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %53
12: ; preds = %2
%13 = load %struct.in_addr*, %struct.in_addr** %5
%14 = call i32 @bzero(%struct.in_addr* %13, i32 8)
%15 = load %struct.in_addr*, %struct.in_addr** %5
%16 = load %struct.hostent*, %struct.hostent** %6
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load %struct.hostent*, %struct.hostent** %6
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @memcpy(%struct.in_addr* %15, i32 %18, i32 %21)
%23 = load %struct.in_addr*, %struct.in_addr** %5
%24 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @INADDR_ANY
%27 = icmp eq i64 %25, %26
br i1 %27, label %34, label %28
28: ; preds = %12
%29 = load %struct.in_addr*, %struct.in_addr** %5
%30 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = load i64, i64* @INADDR_NONE
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %39
34: ; preds = %28, %12
%35 = load i8*, i8** %4
%36 = call i64 @inet_addr(i8* %35)
%37 = load %struct.in_addr*, %struct.in_addr** %5
%38 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %37, i32 0, i32 0
store i64 %36, i64* %38
br label %39
39: ; preds = %34, %28
%40 = load %struct.in_addr*, %struct.in_addr** %5
%41 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = load i64, i64* @INADDR_ANY
%44 = icmp eq i64 %42, %43
br i1 %44, label %51, label %45
45: ; preds = %39
%46 = load %struct.in_addr*, %struct.in_addr** %5
%47 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = load i64, i64* @INADDR_NONE
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %45, %39
store i32 -1, i32* %3
br label %53
52: ; preds = %45
store i32 1, i32* %3
br label %53
53: ; preds = %52, %51, %11
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.in_addr = type { i64 }
%struct.hostent = type { i32, i32 }
@INADDR_ANY = global i64 0
@INADDR_NONE = global i64 0
define i32 @findip(i8* %0, %struct.in_addr* %1) {
%3 = tail call %struct.hostent* @gethostbyname(i8* %0)
%4 = icmp eq %struct.hostent* %3, null
br i1 %4, label %31, label %5
5: ; preds = %2
%6 = tail call i32 @bzero(%struct.in_addr* %1, i32 8)
%7 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 1
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @memcpy(%struct.in_addr* %1, i32 %8, i32 %10)
%12 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %1, i64 0, i32 0
%13 = load i64, i64* %12
%14 = load i64, i64* @INADDR_ANY
%15 = icmp eq i64 %13, %14
%16 = load i64, i64* @INADDR_NONE
%17 = icmp eq i64 %13, %16
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %19, label %23
19: ; preds = %5
%20 = tail call i64 @inet_addr(i8* %0)
store i64 %20, i64* %12
%21 = load i64, i64* @INADDR_ANY
%22 = load i64, i64* @INADDR_NONE
br label %23
23: ; preds = %5, %19
%24 = phi i64 [ %16, %5 ], [ %22, %19 ]
%25 = phi i64 [ %14, %5 ], [ %21, %19 ]
%26 = phi i64 [ %13, %5 ], [ %20, %19 ]
%27 = icmp eq i64 %26, %25
%28 = icmp eq i64 %26, %24
%29 = select i1 %27, i1 true, i1 %28
%30 = select i1 %29, i32 -1, i32 1
br label %31
31: ; preds = %23, %2
%32 = phi i32 [ -1, %2 ], [ %30, %23 ]
ret i32 %32
}