#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_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  scalar_t__ ai_family;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
scalar_t__ AF_UNSPEC;
int /*<<< orphan*/ AI_NUMERICHOST;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ anetSetError(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *, size_t);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int anetGenericResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len,
                       int flags) {
  struct addrinfo hints, *info;
  int rv;
  memset(&hints, 0, sizeof(hints));
  if (flags & (1 << 0))
    hints.ai_flags = AI_NUMERICHOST;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(host, NULL, &hints, &info)) != 0) {
    anetSetError(err, "%s", gai_strerror(rv));
    return -1;
  }
  if (info->ai_family == AF_INET) {
    struct sockaddr_in *sa = (struct sockaddr_in *)info->ai_addr;
    inet_ntop(AF_INET, &(sa->sin_addr), ipbuf, ipbuf_len);
  } else {
    struct sockaddr_in6 *sa = (struct sockaddr_in6 *)info->ai_addr;
    inet_ntop(AF_INET6, &(sa->sin6_addr), ipbuf, ipbuf_len);
  }
  freeaddrinfo(info);
  return 0;
}


x86 assembly (clang, -O0):
<anetGenericResolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    -0x2c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11a7 <anetGenericResolve+0x47>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     1210 <anetGenericResolve+0xb0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x54(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%edx
lea    0xe01(%rip),%rsi
call   1060 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <anetGenericResolve+0x12a>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
jne    1253 <anetGenericResolve+0xf3>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x60(%rbp)
mov    0x2dae(%rip),%rax
mov    (%rax),%rdi
mov    -0x60(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <inet_ntop@plt>
jmp    127a <anetGenericResolve+0x11a>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x68(%rbp)
mov    0x2d6a(%rip),%rax
mov    (%rax),%rdi
mov    -0x68(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <inet_ntop@plt>
mov    -0x50(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetGenericResolve>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r8d,%ebp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
test   $0x1,%bpl
je     119f <anetGenericResolve+0x3f>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11f0 <anetGenericResolve+0x90>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe21(%rip),%rsi
mov    %r12,%rdi
mov    %eax,%edx
call   1060 <anetSetError@plt>
mov    $0xffffffff,%eax
jmp    1227 <anetGenericResolve+0xc7>
mov    (%rsp),%rax
mov    (%rax),%rdi
mov    0x8(%rax),%rsi
mov    0x2de6(%rip),%rax
cmp    (%rax),%rdi
je     1211 <anetGenericResolve+0xb1>
mov    0x2dc2(%rip),%rax
mov    (%rax),%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1030 <inet_ntop@plt>
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i64, i32, i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AI_NUMERICHOST = global i32 0
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%s\00"
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @anetGenericResolve(i8* %0, i8* %1, i8* %2, i64 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca %struct.addrinfo
%13 = alloca %struct.addrinfo*
%14 = alloca i32
%15 = alloca %struct.sockaddr_in*
%16 = alloca %struct.sockaddr_in6*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store i32 %4, i32* %11
%17 = call i32 @memset(%struct.addrinfo* %12, i32 0, i32 24)
%18 = load i32, i32* %11
%19 = and i32 %18, 1
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %5
%22 = load i32, i32* @AI_NUMERICHOST
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 3
store i32 %22, i32* %23
br label %24
24: ; preds = %21, %5
%25 = load i64, i64* @AF_UNSPEC
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 0
store i64 %25, i64* %26
%27 = load i32, i32* @SOCK_STREAM
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i8*, i8** %8
%30 = call i32 @getaddrinfo(i8* %29, i32* null, %struct.addrinfo* %12, %struct.addrinfo** %13)
store i32 %30, i32* %14
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %24
%33 = load i8*, i8** %7
%34 = load i32, i32* %14
%35 = call i32 @gai_strerror(i32 %34)
%36 = call i32 @anetSetError(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %35)
store i32 -1, i32* %6
br label %68
37: ; preds = %24
%38 = load %struct.addrinfo*, %struct.addrinfo** %13
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i64, i64* @AF_INET
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %54
43: ; preds = %37
%44 = load %struct.addrinfo*, %struct.addrinfo** %13
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 1
%46 = load i64, i64* %45
%47 = inttoptr i64 %46 to %struct.sockaddr_in*
store %struct.sockaddr_in* %47, %struct.sockaddr_in** %15
%48 = load i64, i64* @AF_INET
%49 = load %struct.sockaddr_in*, %struct.sockaddr_in** %15
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %49, i32 0, i32 0
%51 = load i8*, i8** %9
%52 = load i64, i64* %10
%53 = call i32 @inet_ntop(i64 %48, i32* %50, i8* %51, i64 %52)
br label %65
54: ; preds = %37
%55 = load %struct.addrinfo*, %struct.addrinfo** %13
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 1
%57 = load i64, i64* %56
%58 = inttoptr i64 %57 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %58, %struct.sockaddr_in6** %16
%59 = load i64, i64* @AF_INET6
%60 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %16
%61 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %60, i32 0, i32 0
%62 = load i8*, i8** %9
%63 = load i64, i64* %10
%64 = call i32 @inet_ntop(i64 %59, i32* %61, i8* %62, i64 %63)
br label %65
65: ; preds = %54, %43
%66 = load %struct.addrinfo*, %struct.addrinfo** %13
%67 = call i32 @freeaddrinfo(%struct.addrinfo* %66)
store i32 0, i32* %6
br label %68
68: ; preds = %65, %32
%69 = load i32, i32* %6
ret i32 %69
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i64, i32, i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AI_NUMERICHOST = global i32 0
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%s\00"
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @anetGenericResolve(i8* %0, i8* %1, i8* %2, i64 %3, i32 %4) {
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = bitcast %struct.addrinfo* %6 to i8*
%9 = bitcast %struct.addrinfo** %7 to i8*
%10 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 24)
%11 = and i32 %4, 1
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %5
%14 = load i32, i32* @AI_NUMERICHOST
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 3
store i32 %14, i32* %15
br label %16
16: ; preds = %13, %5
%17 = load i64, i64* @AF_UNSPEC
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 0
store i64 %17, i64* %18
%19 = load i32, i32* @SOCK_STREAM
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @getaddrinfo(i8* %1, i32* null, %struct.addrinfo* %6, %struct.addrinfo** %7)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %16
%24 = call i32 @gai_strerror(i32 %21)
%25 = call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %46
26: ; preds = %16
%27 = load %struct.addrinfo*, %struct.addrinfo** %7
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* @AF_INET
%31 = icmp eq i64 %29, %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 1
%33 = load i64, i64* %32
br i1 %31, label %34, label %38
34: ; preds = %26
%35 = inttoptr i64 %33 to %struct.sockaddr_in*
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %35, i64 0, i32 0
%37 = call i32 @inet_ntop(i64 %29, i32* %36, i8* %2, i64 %3)
br label %43
38: ; preds = %26
%39 = inttoptr i64 %33 to %struct.sockaddr_in6*
%40 = load i64, i64* @AF_INET6
%41 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %39, i64 0, i32 0
%42 = call i32 @inet_ntop(i64 %40, i32* %41, i8* %2, i64 %3)
br label %43
43: ; preds = %38, %34
%44 = load %struct.addrinfo*, %struct.addrinfo** %7
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
br label %46
46: ; preds = %43, %23
%47 = phi i32 [ -1, %23 ], [ 0, %43 ]
ret i32 %47
}