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

/* Variables and functions */
struct hostent *gethostbyname2(char const *, int);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ memcpy(void *, char *, int /*<<< orphan*/);
int getTargetIp(const char *target, void *dst, int af) {
  struct hostent *he;
  char **addr_list;
  int i;
  if (NULL == (he = gethostbyname2(target, af))) {
    herror("gethostbyname2() failed");
    return 1;
  }
  addr_list = he->h_addr_list;
  for (i = 0; addr_list[i] != NULL; i++) {
    memcpy(dst, addr_list[i], he->h_length);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<getTargetIp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <gethostbyname2@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1179 <getTargetIp+0x49>
lea    0xe98(%rip),%rdi
call   1050 <herror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11cd <getTargetIp+0x9d>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11c6 <getTargetIp+0x96>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    11cd <getTargetIp+0x9d>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getTargetIp>:
push   %rbx
mov    %rsi,%rbx
mov    %edx,%esi
call   1030 <gethostbyname2@plt>
test   %rax,%rax
je     115a <getTargetIp+0x2a>
mov    (%rax),%rcx
mov    (%rcx),%rsi
test   %rsi,%rsi
je     116d <getTargetIp+0x3d>
mov    0x8(%rax),%edx
mov    %rbx,%rdi
call   1040 <memcpy@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe9f(%rip),%rdi
call   1050 <herror@plt>
mov    $0x1,%eax
pop    %rbx
ret
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.hostent = type { i8**, i32 }
@.str = [24 x i8] c"gethostbyname2() failed\00"
define i32 @getTargetIp(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.hostent*
%9 = alloca i8**
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = load i32, i32* %7
%13 = call %struct.hostent* @gethostbyname2(i8* %11, i32 %12)
store %struct.hostent* %13, %struct.hostent** %8
%14 = icmp eq %struct.hostent* null, %13
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @herror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %43
17: ; preds = %3
%18 = load %struct.hostent*, %struct.hostent** %8
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i32 0, i32 0
%20 = load i8**, i8*** %19
store i8** %20, i8*** %9
store i32 0, i32* %10
br label %21
21: ; preds = %39, %17
%22 = load i8**, i8*** %9
%23 = load i32, i32* %10
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %22, i64 %24
%26 = load i8*, i8** %25
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %42
28: ; preds = %21
%29 = load i8*, i8** %6
%30 = load i8**, i8*** %9
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8*, i8** %30, i64 %32
%34 = load i8*, i8** %33
%35 = load %struct.hostent*, %struct.hostent** %8
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = call i32 @memcpy(i8* %29, i8* %34, i32 %37)
store i32 0, i32* %4
br label %43
39: ; No predecessors!
%40 = load i32, i32* %10
%41 = add nsw i32 %40, 1
store i32 %41, i32* %10
br label %21
42: ; preds = %21
store i32 -1, i32* %4
br label %43
43: ; preds = %42, %28, %15
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
%struct.hostent = type { i8**, i32 }
@.str = [24 x i8] c"gethostbyname2() failed\00"
define i32 @getTargetIp(i8* %0, i8* %1, i32 %2) {
%4 = tail call %struct.hostent* @gethostbyname2(i8* %0, i32 %2)
%5 = icmp eq %struct.hostent* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @herror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %17
8: ; preds = %3
%9 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 0
%10 = load i8**, i8*** %9
%11 = load i8*, i8** %10
%12 = icmp eq i8* %11, null
br i1 %12, label %17, label %13
13: ; preds = %8
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 1
%15 = load i32, i32* %14
%16 = tail call i32 @memcpy(i8* %1, i8* %11, i32 %15)
br label %17
17: ; preds = %8, %13, %6
%18 = phi i32 [ 1, %6 ], [ 0, %13 ], [ -1, %8 ]
ret i32 %18
}