#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_storage {
  int dummy;
};
struct addrinfo {
  scalar_t__ ai_family;
  size_t ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ ai;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
scalar_t__ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char const *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, int /*<<< orphan*/,
                          size_t);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int resolve_ip_addr(struct sockaddr_storage saddr_both[2], size_t size_both[2],
                    const char *addr, const char *port) {
  struct addrinfo ai;
  struct addrinfo *aip = NULL;
  int err = -1;
  size_t i;
  memset(&ai, 0, sizeof(ai));
  ai.ai_family = AF_UNSPEC;
  ai.ai_socktype = SOCK_STREAM;
  ai.ai_flags = AI_PASSIVE;
  if (getaddrinfo(addr, port, &ai, &aip))
    goto on_error;
  size_both[0] = 0;
  size_both[1] = 0;
  i = 0;
  for (; (i != 2) && (aip != NULL); aip = aip->ai_next) {
    if ((aip->ai_family != AF_INET) && (aip->ai_family != AF_INET6))
      continue;
    if (aip->ai_addrlen == 0)
      continue;
    memcpy(&saddr_both[i], aip->ai_addr, aip->ai_addrlen);
    size_both[i] = aip->ai_addrlen;
    ++i;
  }
  if (i == 0)
    goto on_error;
  err = 0;
on_error:
  if (aip != NULL)
    freeaddrinfo(aip);
  return err;
}


x86 assembly (clang, -O0):
<resolve_ip_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x50(%rbp)
movl   $0xffffffff,-0x54(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1050 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11c1 <resolve_ip_addr+0x81>
jmp    12bd <resolve_ip_addr+0x17d>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,0x8(%rax)
movq   $0x0,-0x60(%rbp)
xor    %eax,%eax
cmpq   $0x2,-0x60(%rbp)
mov    %al,-0x61(%rbp)
je     11fb <resolve_ip_addr+0xbb>
cmpq   $0x0,-0x50(%rbp)
setne  %al
mov    %al,-0x61(%rbp)
mov    -0x61(%rbp),%al
test   $0x1,%al
jne    120b <resolve_ip_addr+0xcb>
jmp    12a6 <resolve_ip_addr+0x166>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
je     123e <resolve_ip_addr+0xfe>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    0x2da0(%rip),%rcx
cmp    (%rcx),%rax
je     123e <resolve_ip_addr+0xfe>
jmp    1295 <resolve_ip_addr+0x155>
mov    -0x50(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    1252 <resolve_ip_addr+0x112>
jmp    1295 <resolve_ip_addr+0x155>
mov    -0x8(%rbp),%rdi
mov    -0x60(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rdx
call   1040 <memcpy@plt>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    -0x60(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x60(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    11e0 <resolve_ip_addr+0xa0>
cmpq   $0x0,-0x60(%rbp)
jne    12b6 <resolve_ip_addr+0x176>
jmp    12bd <resolve_ip_addr+0x17d>
movl   $0x0,-0x54(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12d1 <resolve_ip_addr+0x191>
mov    -0x50(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
mov    -0x54(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_ip_addr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
movq   $0x0,(%rsp)
lea    0x8(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1050 <getaddrinfo@plt>
test   %rax,%rax
je     11d7 <resolve_ip_addr+0x97>
mov    $0xffffffff,%ebx
mov    (%rsp),%rdi
test   %rdi,%rdi
je     11c7 <resolve_ip_addr+0x87>
call   1060 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
xorps  %xmm0,%xmm0
movups %xmm0,(%r15)
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1252 <resolve_ip_addr+0x112>
mov    0x2df2(%rip),%r12
mov    (%r12),%rax
mov    0x2dd7(%rip),%r13
mov    0x0(%r13),%rcx
xor    %ebx,%ebx
nop
mov    (%rdi),%rdx
cmp    %rax,%rdx
je     120d <resolve_ip_addr+0xcd>
cmp    %rcx,%rdx
jne    123d <resolve_ip_addr+0xfd>
mov    0x8(%rdi),%rdx
test   %rdx,%rdx
je     123d <resolve_ip_addr+0xfd>
lea    (%r14,%rbx,4),%rax
mov    0x10(%rdi),%esi
mov    %rax,%rdi
call   1040 <memcpy@plt>
mov    (%rsp),%rdi
mov    0x8(%rdi),%rax
mov    %rax,(%r15,%rbx,8)
add    $0x1,%rbx
mov    (%r12),%rax
mov    0x0(%r13),%rcx
mov    0x18(%rdi),%rdi
mov    %rdi,(%rsp)
cmp    $0x2,%rbx
je     1256 <resolve_ip_addr+0x116>
test   %rdi,%rdi
jne    1200 <resolve_ip_addr+0xc0>
jmp    1256 <resolve_ip_addr+0x116>
xor    %edi,%edi
xor    %ebx,%ebx
cmp    $0x1,%rbx
sbb    %ebx,%ebx
test   %rdi,%rdi
jne    11c2 <resolve_ip_addr+0x82>
jmp    11c7 <resolve_ip_addr+0x87>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i64, i64, i32, %struct.addrinfo*, i32, i32 }
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @resolve_ip_addr(%struct.sockaddr_storage* %0, i64* %1, i8* %2, i8* %3) {
%5 = alloca %struct.sockaddr_storage*
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.addrinfo
%10 = alloca %struct.addrinfo*
%11 = alloca i32
%12 = alloca i64
store %struct.sockaddr_storage* %0, %struct.sockaddr_storage** %5
store i64* %1, i64** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
store %struct.addrinfo* null, %struct.addrinfo** %10
store i32 -1, i32* %11
%13 = call i32 @memset(%struct.addrinfo* %9, i32 0, i32 40)
%14 = load i64, i64* @AF_UNSPEC
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 0
store i64 %14, i64* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 5
store i32 %16, i32* %17
%18 = load i32, i32* @AI_PASSIVE
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 4
store i32 %18, i32* %19
%20 = load i8*, i8** %7
%21 = load i8*, i8** %8
%22 = call i64 @getaddrinfo(i8* %20, i8* %21, %struct.addrinfo* %9, %struct.addrinfo** %10)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %4
br label %85
25: ; preds = %4
%26 = load i64*, i64** %6
%27 = getelementptr inbounds i64, i64* %26, i64 0
store i64 0, i64* %27
%28 = load i64*, i64** %6
%29 = getelementptr inbounds i64, i64* %28, i64 1
store i64 0, i64* %29
store i64 0, i64* %12
br label %30
30: ; preds = %76, %25
%31 = load i64, i64* %12
%32 = icmp ne i64 %31, 2
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load %struct.addrinfo*, %struct.addrinfo** %10
%35 = icmp ne %struct.addrinfo* %34, null
br label %36
36: ; preds = %33, %30
%37 = phi i1 [ false, %30 ], [ %35, %33 ]
br i1 %37, label %38, label %80
38: ; preds = %36
%39 = load %struct.addrinfo*, %struct.addrinfo** %10
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 0
%41 = load i64, i64* %40
%42 = load i64, i64* @AF_INET
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %51
44: ; preds = %38
%45 = load %struct.addrinfo*, %struct.addrinfo** %10
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 0
%47 = load i64, i64* %46
%48 = load i64, i64* @AF_INET6
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %44
br label %76
51: ; preds = %44, %38
%52 = load %struct.addrinfo*, %struct.addrinfo** %10
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 1
%54 = load i64, i64* %53
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %51
br label %76
57: ; preds = %51
%58 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%59 = load i64, i64* %12
%60 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %58, i64 %59
%61 = load %struct.addrinfo*, %struct.addrinfo** %10
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i32 0, i32 2
%63 = load i32, i32* %62
%64 = load %struct.addrinfo*, %struct.addrinfo** %10
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %64, i32 0, i32 1
%66 = load i64, i64* %65
%67 = call i32 @memcpy(%struct.sockaddr_storage* %60, i32 %63, i64 %66)
%68 = load %struct.addrinfo*, %struct.addrinfo** %10
%69 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %68, i32 0, i32 1
%70 = load i64, i64* %69
%71 = load i64*, i64** %6
%72 = load i64, i64* %12
%73 = getelementptr inbounds i64, i64* %71, i64 %72
store i64 %70, i64* %73
%74 = load i64, i64* %12
%75 = add i64 %74, 1
store i64 %75, i64* %12
br label %76
76: ; preds = %57, %56, %50
%77 = load %struct.addrinfo*, %struct.addrinfo** %10
%78 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %77, i32 0, i32 3
%79 = load %struct.addrinfo*, %struct.addrinfo** %78
store %struct.addrinfo* %79, %struct.addrinfo** %10
br label %30
80: ; preds = %36
%81 = load i64, i64* %12
%82 = icmp eq i64 %81, 0
br i1 %82, label %83, label %84
83: ; preds = %80
br label %85
84: ; preds = %80
store i32 0, i32* %11
br label %85
85: ; preds = %84, %83, %24
%86 = load %struct.addrinfo*, %struct.addrinfo** %10
%87 = icmp ne %struct.addrinfo* %86, null
br i1 %87, label %88, label %91
88: ; preds = %85
%89 = load %struct.addrinfo*, %struct.addrinfo** %10
%90 = call i32 @freeaddrinfo(%struct.addrinfo* %89)
br label %91
91: ; preds = %88, %85
%92 = load i32, i32* %11
ret i32 %92
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i64, i64, i32, %struct.addrinfo*, i32, i32 }
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @resolve_ip_addr(%struct.sockaddr_storage* %0, i64* nocapture writeonly %1, i8* %2, i8* %3) {
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = bitcast %struct.addrinfo* %5 to i8*
%8 = bitcast %struct.addrinfo** %6 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %6
%9 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 40)
%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 5
store i32 %12, i32* %13
%14 = load i32, i32* @AI_PASSIVE
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
store i32 %14, i32* %15
%16 = call i64 @getaddrinfo(i8* %2, i8* %3, %struct.addrinfo* %5, %struct.addrinfo** %6)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %4
%19 = load %struct.addrinfo*, %struct.addrinfo** %6
br label %68
20: ; preds = %4
%21 = bitcast i64* %1 to i8*
%22 = load %struct.addrinfo*, %struct.addrinfo** %6
%23 = icmp eq %struct.addrinfo* %22, null
br i1 %23, label %63, label %24
24: ; preds = %20
%25 = load i64, i64* @AF_INET
%26 = load i64, i64* @AF_INET6
br label %27
27: ; preds = %24, %53
%28 = phi i64 [ %55, %53 ], [ %26, %24 ]
%29 = phi i64 [ %56, %53 ], [ %25, %24 ]
%30 = phi %struct.addrinfo* [ %59, %53 ], [ %22, %24 ]
%31 = phi i64 [ %57, %53 ], [ 0, %24 ]
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 0
%33 = load i64, i64* %32
%34 = icmp eq i64 %33, %29
%35 = icmp eq i64 %33, %28
%36 = select i1 %34, i1 true, i1 %35
br i1 %36, label %37, label %53
37: ; preds = %27
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 1
%39 = load i64, i64* %38
%40 = icmp eq i64 %39, 0
br i1 %40, label %53, label %41
41: ; preds = %37
%42 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %0, i64 %31
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 2
%44 = load i32, i32* %43
%45 = call i32 @memcpy(%struct.sockaddr_storage* %42, i32 %44, i64 %39)
%46 = load %struct.addrinfo*, %struct.addrinfo** %6
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i64 0, i32 1
%48 = load i64, i64* %47
%49 = getelementptr inbounds i64, i64* %1, i64 %31
store i64 %48, i64* %49
%50 = add i64 %31, 1
%51 = load i64, i64* @AF_INET
%52 = load i64, i64* @AF_INET6
br label %53
53: ; preds = %27, %37, %41
%54 = phi %struct.addrinfo* [ %30, %37 ], [ %46, %41 ], [ %30, %27 ]
%55 = phi i64 [ %28, %37 ], [ %52, %41 ], [ %28, %27 ]
%56 = phi i64 [ %29, %37 ], [ %51, %41 ], [ %29, %27 ]
%57 = phi i64 [ %31, %37 ], [ %50, %41 ], [ %31, %27 ]
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i64 0, i32 3
%59 = load %struct.addrinfo*, %struct.addrinfo** %58
store %struct.addrinfo* %59, %struct.addrinfo** %6
%60 = icmp ne i64 %57, 2
%61 = icmp ne %struct.addrinfo* %59, null
%62 = select i1 %60, i1 %61, i1 false
br i1 %62, label %27, label %63
63: ; preds = %53, %20
%64 = phi %struct.addrinfo* [ null, %20 ], [ %59, %53 ]
%65 = phi i64 [ 0, %20 ], [ %57, %53 ]
%66 = icmp eq i64 %65, 0
%67 = sext i1 %66 to i32
br label %68
68: ; preds = %18, %63
%69 = phi %struct.addrinfo* [ %19, %18 ], [ %64, %63 ]
%70 = phi i32 [ -1, %18 ], [ %67, %63 ]
%71 = icmp eq %struct.addrinfo* %69, null
br i1 %71, label %74, label %72
72: ; preds = %68
%73 = call i32 @freeaddrinfo(%struct.addrinfo* %69)
br label %74
74: ; preds = %72, %68
ret i32 %70
}