// File: /workspace/github_c_makefile_star2_O2/aerospike[P]aerospike-common/src/main/aerospike/ssl_util.c
// Function: as_tls_match_san
bool as_tls_match_san(X509 *x509, const char *name) {
	STACK_OF(GENERAL_NAME) *san_names = X509_get_ext_d2i(x509,
			NID_subject_alt_name, NULL, NULL);

	if (san_names == NULL) {
		return false;
	}

	bool match = false;

	for (int j = 0; j < sk_GENERAL_NAME_num(san_names); j++) {
		GENERAL_NAME *san_ele = sk_GENERAL_NAME_value(san_names, j);

		if (san_ele->type == GEN_IPADD) {
			ASN1_OCTET_STRING *ip_str = san_ele->d.iPAddress;
			int ip_length = ASN1_STRING_length(ip_str);

#if OPENSSL_VERSION_NUMBER < 0x10100000
			const unsigned char *ip = ASN1_STRING_data(ip_str);
#else
			const unsigned char *ip = ASN1_STRING_get0_data(ip_str);
#endif

			if (ip_length == 4) {
				// IPv4 address.
				char san[INET_ADDRSTRLEN];

				if (inet_ntop(AF_INET, ip, san, INET_ADDRSTRLEN) == NULL) {
					continue;
				}

				if (strcmp(name, san) == 0) {
					match = true;
					break;
				}
			}
			else if (ip_length == 16) {
				// IPv6 address.
				char san[INET6_ADDRSTRLEN];
				char name_short[INET6_ADDRSTRLEN];
				struct in6_addr name_in6;

				if (inet_ntop(AF_INET6, ip, san, INET6_ADDRSTRLEN) == NULL) {
					continue;
				}

				// Convert peer name to short form and compare.

				if (inet_pton(AF_INET6, name, &name_in6) != 1) {
					continue;
				}

				if (inet_ntop(AF_INET6, &name_in6, name_short,
						INET6_ADDRSTRLEN) == NULL) {
					continue;
				}

				if (strcmp(name_short, san) == 0) {
					match = true;
					break;
				}
			}
		}
		else if (san_ele->type == GEN_DNS) {
			ASN1_STRING *dns_name = san_ele->d.dNSName;

#if OPENSSL_VERSION_NUMBER < 0x10100000
			char *san = (char*)ASN1_STRING_data(dns_name);
#else
			char *san = (char*)ASN1_STRING_get0_data(dns_name);
#endif

			if (strcmp(name, san) == 0) {
				match = true;
				break;
			}
		}
	}

	sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);

	return match;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
xorl %r14d, %r14d
movq %rsi, 0x8(%rsp)
pushq $0x55
popq %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x87e0
testq %rax, %rax
je 0x11cc9
movq %rax, %r15
pushq $0xa
popq %rbp
leaq 0x50(%rsp), %rbx
movq %rax, (%rsp)
movq %r15, %rdi
callq 0x8900
cmpl %eax, %r14d
setl %r13b
jge 0x11cb8
movq %r15, %rdi
movl %r14d, %esi
callq 0x8bc0
movl (%rax), %ecx
cmpl $0x2, %ecx
je 0x11c34
cmpl $0x7, %ecx
jne 0x11c4e
movq 0x8(%rax), %r12
movq %rbx, %r15
movl %ebp, %ebx
movq %r12, %rdi
callq 0x80b0
movq %r12, %rdi
movl %eax, %ebp
callq 0x8920
cmpl $0x10, %ebp
je 0x11c56
cmpl $0x4, %ebp
movl %ebx, %ebp
movq %r15, %rbx
movq (%rsp), %r15
jne 0x11c4e
pushq $0x2
popq %rdi
movq %rax, %rsi
movq %rbx, %rdx
pushq $0x10
popq %rcx
callq 0x80a0
testq %rax, %rax
je 0x11c4e
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
jmp 0x11c45
movq 0x8(%rax), %rdi
callq 0x8920
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x85a0
testl %eax, %eax
je 0x11cb8
incl %r14d
jmp 0x11bba
movl %ebx, %ebp
movl %ebx, %edi
movq %r15, %rbx
movq %rax, %rsi
movq %r15, %rdx
pushq $0x2e
popq %rcx
callq 0x80a0
testq %rax, %rax
je 0x11cb2
movq 0x8(%rsp), %rsi
movl %ebp, %edi
leaq 0x10(%rsp), %rdx
callq 0x8640
cmpl $0x1, %eax
jne 0x11cb2
movl %ebp, %edi
leaq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
pushq $0x2e
popq %rcx
callq 0x80a0
testq %rax, %rax
je 0x11cb2
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x85a0
movq (%rsp), %r15
jmp 0x11c4a
movq (%rsp), %r15
jmp 0x11c4e
movq 0xc2e9(%rip), %rsi      # 0x1dfa8
movq %r15, %rdi
callq 0x8870
jmp 0x11ccc
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq