// File: /workspace/github_c_makefile_star2_O2/jfriesse[P]omping/aiifunc.c
// Function: aii_find_local
int
aii_find_local(const struct aii_list *aii_list, int *ip_ver, struct ifaddrs **ifa_list,
    struct ifaddrs **ifa_local, struct ai_item **ai_item, unsigned int if_flags)
{
	struct addrinfo *ai_i;
	struct ai_item *aip;
	struct ifaddrs *ifa, *ifa_i;
	char sa_str[LOGGING_SA_TO_STR_LEN];
	char sa_str2[LOGGING_SA_TO_STR_LEN];
	int ipv4_fallback;
	int res;

	*ifa_local = NULL;
	ifa_i = NULL;
	ipv4_fallback = 0;

	if (getifaddrs(&ifa) == -1) {
		err(1, "getifaddrs");
	}

	TAILQ_FOREACH(aip, aii_list, entries) {
		for (ai_i = aip->ai; ai_i != NULL; ai_i = ai_i->ai_next) {
			if (af_ai_is_dup(aip->ai, ai_i)) {
				logging_sa_to_str(ai_i->ai_addr, sa_str, sizeof(sa_str));
				DEBUG2_PRINTF("Found duplicate addr %s", sa_str);
				continue ;
			}

			for (ifa_i = ifa; ifa_i != NULL; ifa_i = ifa_i->ifa_next) {
				if (ifa_i->ifa_addr == NULL ||
				    (ifa_i->ifa_addr->sa_family != AF_INET &&
				    ifa_i->ifa_addr->sa_family != AF_INET6)) {
					continue ;
				}

				logging_sa_to_str(ifa_i->ifa_addr, sa_str, sizeof(sa_str));
				logging_sa_to_str(ai_i->ai_addr, sa_str2, sizeof(sa_str2));
				DEBUG2_PRINTF("Comparing %s(%s) with %s", sa_str, ifa_i->ifa_name,
				    sa_str2);

				if (af_sockaddr_eq(ifa_i->ifa_addr, ai_i->ai_addr)) {
					res = af_is_supported_local_ifa(ifa_i, *ip_ver, if_flags);

					if (res == 1 || res == 2) {
						if (*ifa_local != NULL && ipv4_fallback == 0)
							goto multiple_match_error;

						*ifa_list = ifa;
						*ifa_local = ifa_i;
						*ai_item = aip;

						if (*ip_ver == 0) {
							/*
							 * Device supports ipv6
							 */
							*ip_ver = 6;
							DEBUG2_PRINTF("Supports ipv6");
						}

						if (res == 2) {
							/*
							 * Set this item as ipv4 fallback
							 */
							ipv4_fallback++;
							DEBUG2_PRINTF("Supports ipv4 - fallback");
						}
					}
				}
			}
		}
	}

	if (*ip_ver == 0 && *ifa_local != NULL) {
		if (ipv4_fallback > 1)
			goto multiple_match_error;

		*ip_ver = 4;
	}

	if (*ifa_local != NULL) {
		return (0);
	}

	DEBUG_PRINTF("Can't find local addr");
	return (-1);

multiple_match_error:
	errx(1, "Multiple local interfaces (%s and %s) match parameters.",
	    ifa_i->ifa_name, (*ifa_local)->ifa_name);
	return (-1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %r9d, 0x2c(%rsp)
movq %r8, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
movq %rcx, 0x20(%rsp)
andq $0x0, (%rcx)
leaq 0x38(%rsp), %rdi
callq 0x2260
cmpl $-0x1, %eax
je 0x308e
leaq 0x90(%rsp), %r15
pushq $0x3e
popq %r12
leaq 0x50(%rsp), %r13
movl $0x0, 0x14(%rsp)
movq 0x20(%rsp), %rcx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x301e
movq %rbx, %rbp
movq %rbx, 0x30(%rsp)
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x3012
movq (%rbx), %rdi
movq %rbp, %rsi
callq 0x26ea
testl %eax, %eax
je 0x2eca
movq 0x18(%rbp), %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x4877
leaq 0x6362(%rip), %rdi      # 0x920e
movl $0x81, %esi
pushq $0x3
popq %rdx
leaq 0x635d(%rip), %rcx      # 0x9218
movq %r15, %r8
xorl %eax, %eax
callq 0x4998
jmp 0x3004
leaq 0x38(%rsp), %r14
movq (%r14), %r14
testq %r14, %r14
je 0x2fff
movq 0x18(%r14), %rdi
testq %rdi, %rdi
je 0x2ecf
movzwl (%rdi), %eax
orl $0x8, %eax
cmpl $0xa, %eax
jne 0x2ecf
movq %r15, %rsi
movl %r12d, %edx
callq 0x4877
movq 0x18(%rbp), %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x4877
movq 0x8(%r14), %r9
movq %r13, (%rsp)
leaq 0x62f6(%rip), %rdi      # 0x920e
movl $0x8f, %esi
pushq $0x3
popq %rdx
leaq 0x6309(%rip), %rcx      # 0x9230
movq %r15, %r8
xorl %eax, %eax
callq 0x4998
movq 0x18(%r14), %rdi
movq 0x18(%rbp), %rsi
callq 0x2474
testl %eax, %eax
je 0x2ecf
movq 0x18(%rsp), %rax
movl (%rax), %esi
movq %r14, %rdi
movl 0x2c(%rsp), %edx
callq 0x2a11
movl %eax, %ebx
leal -0x1(%rbx), %eax
cmpl $0x1, %eax
ja 0x2ecf
cmpl $0x0, 0x14(%rsp)
sete %cl
movq 0x20(%rsp), %rsi
movq (%rsi), %rax
testq %rax, %rax
setne %dl
testb %cl, %dl
jne 0x3075
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rax, (%rcx)
movq %r14, (%rsi)
movq 0x48(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rcx, (%rax)
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x2fd0
movq 0x18(%rsp), %rax
movl $0x6, (%rax)
leaq 0x6254(%rip), %rdi      # 0x920e
movl $0xa1, %esi
pushq $0x3
popq %rdx
leaq 0x6280(%rip), %rcx      # 0x9249
xorl %eax, %eax
callq 0x4998
cmpl $0x2, %ebx
jne 0x2ecf
incl 0x14(%rsp)
leaq 0x622a(%rip), %rdi      # 0x920e
movl $0xa9, %esi
pushq $0x3
popq %rdx
leaq 0x6264(%rip), %rcx      # 0x9257
xorl %eax, %eax
callq 0x4998
jmp 0x2ecf
movq 0x30(%rsp), %rbx
addq $0x28, %rbp
movq 0x20(%rsp), %rcx
jmp 0x2e7a
addq $0x88, %rbx
jmp 0x2e66
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
movq (%rcx), %rcx
je 0x3034
xorl %eax, %eax
testq %rcx, %rcx
jne 0x3063
jmp 0x3043
testq %rcx, %rcx
je 0x3043
movl $0x4, (%rax)
xorl %eax, %eax
jmp 0x3063
leaq 0x61c4(%rip), %rdi      # 0x920e
leaq 0x621f(%rip), %rcx      # 0x9270
pushq $0x2
popq %rdx
movl $0xbc, %esi
xorl %eax, %eax
callq 0x4998
pushq $-0x1
popq %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r14), %rdx
movq 0x8(%rax), %rcx
leaq 0x6202(%rip), %rsi      # 0x9286
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x20f0
leaq 0x616e(%rip), %rsi      # 0x9203
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x2240