#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 {
  char **h_addr_list;
  int /*<<< orphan*/ h_addrtype;
};

/* Variables and functions */
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ *inet_ntop(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ printf(char *, char const *);
int get_ip_by_domain(const char *domain, char *ip) {
  char **pptr;
  struct hostent *hptr;
  hptr = gethostbyname(domain);
  if (NULL == hptr) {
    printf("gethostbyname error for host:%s/n", domain);
    return -1;
  }
  for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++) {
    if (NULL != inet_ntop(hptr->h_addrtype, *pptr, ip, 16)) {
      return 0;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<get_ip_by_domain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1175 <get_ip_by_domain+0x45>
mov    -0x10(%rbp),%rsi
lea    0xe9c(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <get_ip_by_domain+0xb1>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11da <get_ip_by_domain+0xaa>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11c4 <get_ip_by_domain+0x94>
movl   $0x0,-0x4(%rbp)
jmp    11e1 <get_ip_by_domain+0xb1>
jmp    11c9 <get_ip_by_domain+0x99>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    1180 <get_ip_by_domain+0x50>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_by_domain>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1183 <get_ip_by_domain+0x53>
mov    %rax,%rbp
mov    (%rax),%rbx
mov    (%rbx),%rsi
mov    $0xffffffff,%r14d
test   %rsi,%rsi
je     119d <get_ip_by_domain+0x6d>
add    $0x8,%rbx
nop
mov    0x8(%rbp),%edi
mov    %r15,%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
jne    119a <get_ip_by_domain+0x6a>
mov    (%rbx),%rsi
add    $0x8,%rbx
test   %rsi,%rsi
jne    1160 <get_ip_by_domain+0x30>
jmp    119d <get_ip_by_domain+0x6d>
lea    0xe76(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%r14d
jmp    119d <get_ip_by_domain+0x6d>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i8**, i32 }
@.str = [34 x i8] c"gethostbyname error for host:%s/n\00"
define i32 @get_ip_by_domain(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca %struct.hostent*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call %struct.hostent* @gethostbyname(i8* %8)
store %struct.hostent* %9, %struct.hostent** %7
%10 = load %struct.hostent*, %struct.hostent** %7
%11 = icmp eq %struct.hostent* null, %10
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %3
br label %38
15: ; preds = %2
%16 = load %struct.hostent*, %struct.hostent** %7
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i32 0, i32 0
%18 = load i8**, i8*** %17
store i8** %18, i8*** %6
br label %19
19: ; preds = %34, %15
%20 = load i8**, i8*** %6
%21 = load i8*, i8** %20
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %37
23: ; preds = %19
%24 = load %struct.hostent*, %struct.hostent** %7
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i32 0, i32 1
%26 = load i32, i32* %25
%27 = load i8**, i8*** %6
%28 = load i8*, i8** %27
%29 = load i8*, i8** %5
%30 = call i32* @inet_ntop(i32 %26, i8* %28, i8* %29, i32 16)
%31 = icmp ne i32* null, %30
br i1 %31, label %32, label %33
32: ; preds = %23
store i32 0, i32* %3
br label %38
33: ; preds = %23
br label %34
34: ; preds = %33
%35 = load i8**, i8*** %6
%36 = getelementptr inbounds i8*, i8** %35, i32 1
store i8** %36, i8*** %6
br label %19
37: ; preds = %19
store i32 -1, i32* %3
br label %38
38: ; preds = %37, %32, %12
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.hostent = type { i8**, i32 }
@.str = [34 x i8] c"gethostbyname error for host:%s/n\00"
define i32 @get_ip_by_domain(i8* %0, i8* %1) {
%3 = tail call %struct.hostent* @gethostbyname(i8* %0)
%4 = icmp eq %struct.hostent* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %23
7: ; preds = %2
%8 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 0
%9 = load i8**, i8*** %8
%10 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 1
%11 = load i8*, i8** %9
%12 = icmp eq i8* %11, null
br i1 %12, label %23, label %16
13: ; preds = %16
%14 = load i8*, i8** %22
%15 = icmp eq i8* %14, null
br i1 %15, label %23, label %16
16: ; preds = %7, %13
%17 = phi i8* [ %14, %13 ], [ %11, %7 ]
%18 = phi i8** [ %22, %13 ], [ %9, %7 ]
%19 = load i32, i32* %10
%20 = tail call i32* @inet_ntop(i32 %19, i8* %17, i8* %1, i32 16)
%21 = icmp eq i32* %20, null
%22 = getelementptr inbounds i8*, i8** %18, i64 1
br i1 %21, label %13, label %23
23: ; preds = %16, %13, %7, %5
%24 = phi i32 [ -1, %5 ], [ -1, %7 ], [ 0, %16 ], [ -1, %13 ]
ret i32 %24
}