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

/* Variables and functions */
scalar_t__ AF_INET6;
int EAI_NONAME;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ stderr;
int resolve_ipv6(const char *hostname, struct sockaddr_in6 *res,
                 int res_length) {
  struct addrinfo hints = {0};
  struct addrinfo *result = NULL, *rp;
  int ret;
  hints.ai_family = AF_INET6;
  hints.ai_socktype = SOCK_STREAM;
  ret = getaddrinfo(hostname, NULL, &hints, &result);
  if (ret) {
    if (ret != EAI_NONAME)
      fprintf(stderr, "getaddrinfo: %s(%d)\n", gai_strerror(ret), ret);
    return 0;
  }
  for (rp = result, ret = 0; rp && ret < res_length; rp = rp->ai_next) {
    struct sockaddr *sa = rp->ai_addr;
    if (sa->sa_family != AF_INET6) {
      fprintf(stderr, "Assertion error, address was not IPv6\n");
      continue;
    }
    struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
    res[ret++] = *sa6;
  }
  if (result) {
    freeaddrinfo(result);
  }
  return ret;
}


x86 assembly (clang, -O0):
<resolve_ipv6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
movq   $0x0,-0x50(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     1203 <resolve_ipv6+0xb3>
mov    -0x5c(%rbp),%eax
mov    0x2e24(%rip),%rcx
cmp    (%rcx),%eax
je     11f7 <resolve_ipv6+0xa7>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x5c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x74(%rbp),%edi
mov    %rax,%rdx
mov    -0x5c(%rbp),%ecx
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ca <resolve_ipv6+0x17a>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x58(%rbp)
mov    %al,-0x75(%rbp)
je     122e <resolve_ipv6+0xde>
mov    -0x5c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
setl   %al
mov    %al,-0x75(%rbp)
mov    -0x75(%rbp),%al
test   $0x1,%al
jne    123e <resolve_ipv6+0xee>
jmp    12b0 <resolve_ipv6+0x160>
mov    -0x58(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
mov    (%rax),%rax
mov    0x2d89(%rip),%rcx
cmp    (%rcx),%rax
je     127c <resolve_ipv6+0x12c>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    129f <resolve_ipv6+0x14f>
mov    -0x68(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x5c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x5c(%rbp)
movslq %ecx,%rcx
mov    -0x70(%rbp),%rdx
mov    (%rdx),%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    1212 <resolve_ipv6+0xc2>
cmpq   $0x0,-0x50(%rbp)
je     12c4 <resolve_ipv6+0x174>
mov    -0x50(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_ipv6>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x30(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2e66(%rip),%r13
mov    0x0(%r13),%rax
mov    %rax,0x28(%rsp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xor    %ebx,%ebx
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
xor    %esi,%esi
call   1050 <getaddrinfo@plt>
test   %eax,%eax
je     11e6 <resolve_ipv6+0xa6>
mov    %eax,%ebp
mov    0x2e38(%rip),%rax
cmp    (%rax),%ebp
je     1267 <resolve_ipv6+0x127>
mov    0x2e11(%rip),%rax
mov    (%rax),%r14d
mov    %ebp,%edi
call   1030 <gai_strerror@plt>
lea    0xe30(%rip),%rsi
xor    %ebx,%ebx
mov    %r14d,%edi
mov    %rax,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1267 <resolve_ipv6+0x127>
mov    0x8(%rsp),%rbp
xor    %ebx,%ebx
test   %rbp,%rbp
je     125a <resolve_ipv6+0x11a>
test   %r15d,%r15d
jle    125a <resolve_ipv6+0x11a>
mov    0x0(%r13),%rax
lea    0xe13(%rip),%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%rbp),%rcx
mov    (%rcx),%rcx
cmp    %rax,%rcx
jne    1230 <resolve_ipv6+0xf0>
movslq %ebx,%rdx
add    $0x1,%ebx
mov    %ecx,(%r14,%rdx,4)
jmp    1247 <resolve_ipv6+0x107>
nopl   0x0(%rax,%rax,1)
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x0(%r13),%rax
mov    0x8(%rbp),%rbp
test   %rbp,%rbp
je     1255 <resolve_ipv6+0x115>
cmp    %r15d,%ebx
jl     1210 <resolve_ipv6+0xd0>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
je     1267 <resolve_ipv6+0x127>
mov    %rbp,%rdi
call   1060 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32 }
%struct.addrinfo = type { %struct.sockaddr*, %struct.addrinfo*, i32, i64, i32 }
%struct.sockaddr = type { i64 }
@AF_INET6 = global i64 0
@SOCK_STREAM = global i32 0
@EAI_NONAME = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"getaddrinfo: %s(%d)\0A\00"
@.str.1 = [39 x i8] c"Assertion error, address was not IPv6\0A\00"
define i32 @resolve_ipv6(i8* %0, %struct.sockaddr_in6* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.sockaddr_in6*
%7 = alloca i32
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca i32
%12 = alloca %struct.sockaddr*
%13 = alloca %struct.sockaddr_in6*
store i8* %0, i8** %5
store %struct.sockaddr_in6* %1, %struct.sockaddr_in6** %6
store i32 %2, i32* %7
%14 = bitcast %struct.addrinfo* %8 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %9
%15 = load i64, i64* @AF_INET6
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i64 %15, i64* %16
%17 = load i32, i32* @SOCK_STREAM
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i8*, i8** %5
%20 = call i32 @getaddrinfo(i8* %19, i32* null, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %3
%24 = load i32, i32* %11
%25 = load i32, i32* @EAI_NONAME
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = load i32, i32* %11
%30 = call i8* @gai_strerror(i32 %29)
%31 = load i32, i32* %11
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %30, i32 %31)
br label %33
33: ; preds = %27, %23
store i32 0, i32* %4
br label %80
34: ; preds = %3
%35 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %35, %struct.addrinfo** %10
store i32 0, i32* %11
br label %36
36: ; preds = %68, %34
%37 = load %struct.addrinfo*, %struct.addrinfo** %10
%38 = icmp ne %struct.addrinfo* %37, null
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i32, i32* %11
%41 = load i32, i32* %7
%42 = icmp slt i32 %40, %41
br label %43
43: ; preds = %39, %36
%44 = phi i1 [ false, %36 ], [ %42, %39 ]
br i1 %44, label %45, label %72
45: ; preds = %43
%46 = load %struct.addrinfo*, %struct.addrinfo** %10
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 0
%48 = load %struct.sockaddr*, %struct.sockaddr** %47
store %struct.sockaddr* %48, %struct.sockaddr** %12
%49 = load %struct.sockaddr*, %struct.sockaddr** %12
%50 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %49, i32 0, i32 0
%51 = load i64, i64* %50
%52 = load i64, i64* @AF_INET6
%53 = icmp ne i64 %51, %52
br i1 %53, label %54, label %57
54: ; preds = %45
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %68
57: ; preds = %45
%58 = load %struct.sockaddr*, %struct.sockaddr** %12
%59 = bitcast %struct.sockaddr* %58 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %59, %struct.sockaddr_in6** %13
%60 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %6
%61 = load i32, i32* %11
%62 = add nsw i32 %61, 1
store i32 %62, i32* %11
%63 = sext i32 %61 to i64
%64 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %60, i64 %63
%65 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %13
%66 = bitcast %struct.sockaddr_in6* %64 to i8*
%67 = bitcast %struct.sockaddr_in6* %65 to i8*
br label %68
68: ; preds = %57, %54
%69 = load %struct.addrinfo*, %struct.addrinfo** %10
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i32 0, i32 1
%71 = load %struct.addrinfo*, %struct.addrinfo** %70
store %struct.addrinfo* %71, %struct.addrinfo** %10
br label %36
72: ; preds = %43
%73 = load %struct.addrinfo*, %struct.addrinfo** %9
%74 = icmp ne %struct.addrinfo* %73, null
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load %struct.addrinfo*, %struct.addrinfo** %9
%77 = call i32 @freeaddrinfo(%struct.addrinfo* %76)
br label %78
78: ; preds = %75, %72
%79 = load i32, i32* %11
store i32 %79, i32* %4
br label %80
80: ; preds = %78, %33
%81 = load i32, i32* %4
ret i32 %81
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
%struct.addrinfo = type { %struct.sockaddr*, %struct.addrinfo*, i32, i64, i32 }
%struct.sockaddr = type { i64 }
@AF_INET6 = global i64 0
@SOCK_STREAM = global i32 0
@EAI_NONAME = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"getaddrinfo: %s(%d)\0A\00"
@.str.1 = [39 x i8] c"Assertion error, address was not IPv6\0A\00"
define i32 @resolve_ipv6(i8* %0, %struct.sockaddr_in6* nocapture writeonly %1, i32 %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %5
%8 = load i64, i64* @AF_INET6
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i64 %8, i64* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %10, i32* %11
%12 = call i32 @getaddrinfo(i8* %0, i32* null, %struct.addrinfo* %4, %struct.addrinfo** %5)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %3
%15 = load %struct.addrinfo*, %struct.addrinfo** %5
%16 = icmp ne %struct.addrinfo* %15, null
%17 = icmp sgt i32 %2, 0
%18 = and i1 %16, %17
br i1 %18, label %19, label %56
19: ; preds = %14
%20 = load i64, i64* @AF_INET6
br label %28
21: ; preds = %3
%22 = load i32, i32* @EAI_NONAME
%23 = icmp eq i32 %12, %22
br i1 %23, label %62, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i8* @gai_strerror(i32 %12)
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %26, i32 %12)
br label %62
28: ; preds = %19, %46
%29 = phi i64 [ %47, %46 ], [ %20, %19 ]
%30 = phi %struct.addrinfo* [ %50, %46 ], [ %15, %19 ]
%31 = phi i32 [ %48, %46 ], [ 0, %19 ]
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 0
%33 = load %struct.sockaddr*, %struct.sockaddr** %32
%34 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %33, i64 0, i32 0
%35 = load i64, i64* %34
%36 = icmp eq i64 %35, %29
br i1 %36, label %41, label %37
37: ; preds = %28
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%40 = load i64, i64* @AF_INET6
br label %46
41: ; preds = %28
%42 = trunc i64 %35 to i32
%43 = add nsw i32 %31, 1
%44 = sext i32 %31 to i64
%45 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %1, i64 %44, i32 0
store i32 %42, i32* %45
br label %46
46: ; preds = %41, %37
%47 = phi i64 [ %40, %37 ], [ %29, %41 ]
%48 = phi i32 [ %31, %37 ], [ %43, %41 ]
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 1
%50 = load %struct.addrinfo*, %struct.addrinfo** %49
%51 = icmp ne %struct.addrinfo* %50, null
%52 = icmp slt i32 %48, %2
%53 = select i1 %51, i1 %52, i1 false
br i1 %53, label %28, label %54
54: ; preds = %46
%55 = load %struct.addrinfo*, %struct.addrinfo** %5
br label %56
56: ; preds = %54, %14
%57 = phi %struct.addrinfo* [ %15, %14 ], [ %55, %54 ]
%58 = phi i32 [ 0, %14 ], [ %48, %54 ]
%59 = icmp eq %struct.addrinfo* %57, null
br i1 %59, label %62, label %60
60: ; preds = %56
%61 = call i32 @freeaddrinfo(%struct.addrinfo* %57)
br label %62
62: ; preds = %56, %60, %21, %24
%63 = phi i32 [ 0, %24 ], [ 0, %21 ], [ %58, %60 ], [ %58, %56 ]
ret i32 %63
}