#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;
};
struct addrinfo {
  scalar_t__ ai_family;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_socktype;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
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 /*<<< orphan*/ net_err(char *, char *, int /*<<< orphan*/);
int resolve(char *err, char *host, char *ip, size_t ip_len) {
  struct addrinfo hints, *info;
  int rv;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(host, NULL, &hints, &info)) != 0) {
    net_err(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), ip, ip_len);
  }
  freeaddrinfo(info);
  return 0;
}


x86 assembly (clang, -O0):
<resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     11f1 <resolve+0x91>
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x4c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%edx
lea    0xe20(%rip),%rsi
call   1060 <net_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <resolve+0xdf>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%rax
jne    122f <resolve+0xcf>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x58(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <inet_ntop@plt>
mov    -0x48(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0x8(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r13,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11dc <resolve+0x7c>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe35(%rip),%rsi
mov    %r12,%rdi
mov    %eax,%edx
call   1060 <net_err@plt>
mov    $0xffffffff,%eax
jmp    120c <resolve+0xac>
mov    (%rsp),%rdi
mov    (%rdi),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
jne    1205 <resolve+0xa5>
mov    0x8(%rdi),%rsi
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    %r13
pop    %r14
pop    %r15
ret

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

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