#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  char *h_name;
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
  int /*<<< orphan*/ h_addrtype;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
struct hostent *gethostbyname(char *);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strcpy(char *, char *);
int ip_findaddr(char *spec, struct sockaddr *addr, char *hostname) {
  struct sockaddr_in *to = (struct sockaddr_in *)addr;
  memset(addr, 0, sizeof(struct sockaddr));
  to->sin_family = AF_INET;
  if (inet_aton(spec, &to->sin_addr)) {
    if (hostname)
      strcpy(hostname, spec);
  } else {
    struct hostent *hp = gethostbyname(spec);
    if (hp) {
      to->sin_family = hp->h_addrtype;
      memcpy(&to->sin_addr, hp->h_addr, hp->h_length);
      if (hostname)
        strcpy(hostname, hp->h_name);
    } else {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<ip_findaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%rax
je     11c0 <ip_findaddr+0x70>
cmpq   $0x0,-0x20(%rbp)
je     11bb <ip_findaddr+0x6b>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    122e <ip_findaddr+0xde>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     121d <ip_findaddr+0xcd>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edx
call   1060 <memcpy@plt>
cmpq   $0x0,-0x20(%rbp)
je     1218 <ip_findaddr+0xc8>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
jmp    1229 <ip_findaddr+0xd9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <ip_findaddr+0xe5>
jmp    122e <ip_findaddr+0xde>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ip_findaddr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%rbx
xor    %r14d,%r14d
mov    %rsi,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbp)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <inet_aton@plt>
test   %rax,%rax
je     1195 <ip_findaddr+0x45>
test   %r15,%r15
jne    11c1 <ip_findaddr+0x71>
jmp    11d4 <ip_findaddr+0x84>
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11ce <ip_findaddr+0x7e>
mov    %rax,%rbx
mov    0x10(%rax),%eax
mov    %eax,0x4(%rbp)
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
mov    %rbp,%rdi
call   1060 <memcpy@plt>
test   %r15,%r15
je     11d4 <ip_findaddr+0x84>
mov    (%rbx),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    11d4 <ip_findaddr+0x84>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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