// File: /workspace/github_c_makefile_star2_O3/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 $0x78, %rsp
movq %rsi, %r15
movl $0x55, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %ebx, %ebx
callq 0x8760
testq %rax, %rax
je 0x13d4c
movq %rax, %rdi
movq %rax, %r14
callq 0x8860
xorl %ebx, %ebx
testl %eax, %eax
jle 0x13d3d
leaq 0x40(%rsp), %r12
movq %r14, %rdi
movl %ebx, %esi
callq 0x8af0
movl (%rax), %ecx
cmpl $0x2, %ecx
je 0x13cb8
cmpl $0x7, %ecx
jne 0x13d25
movq 0x8(%rax), %r13
movq %r13, %rdi
callq 0x80b0
movq %r13, %rdi
movl %eax, %ebp
callq 0x8880
cmpl $0x10, %ebp
je 0x13cc9
cmpl $0x4, %ebp
jne 0x13d25
movl $0x2, %edi
movq %rax, %rsi
movq %r12, %rdx
movl $0x10, %ecx
callq 0x80a0
testq %rax, %rax
je 0x13d25
movq %r15, %rdi
jmp 0x13d19
movq 0x8(%rax), %rdi
callq 0x8880
movq %r15, %rdi
movq %rax, %rsi
jmp 0x13d1c
movl $0xa, %edi
movq %rax, %rsi
movq %r12, %rdx
movl $0x2e, %ecx
callq 0x80a0
testq %rax, %rax
je 0x13d25
movl $0xa, %edi
movq %r15, %rsi
movq %rsp, %rdx
callq 0x85d0
cmpl $0x1, %eax
jne 0x13d25
movl $0xa, %edi
movq %rsp, %rsi
leaq 0x10(%rsp), %rdx
movl $0x2e, %ecx
callq 0x80a0
testq %rax, %rax
je 0x13d25
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x8560
testl %eax, %eax
je 0x13d3b
movq %r14, %rdi
incl %ebx
callq 0x8860
cmpl %eax, %ebx
jl 0x13c5b
xorl %ebx, %ebx
jmp 0x13d3d
movb $0x1, %bl
movq 0xc264(%rip), %rsi      # 0x1ffa8
movq %r14, %rdi
callq 0x87d0
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq