#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 */
typedef struct TYPE_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ X509;
struct TYPE_4__ {
  int /*<<< orphan*/ dNSName;
};
struct TYPE_5__ {
  scalar_t__ type;
  TYPE_1__ d;
};
typedef int /*<<< orphan*/ GENERAL_NAMES;
typedef TYPE_2__ GENERAL_NAME;

/* Variables and functions */
int ASN1_STRING_length(int /*<<< orphan*/);
int /*<<< orphan*/ ASN1_STRING_to_UTF8(unsigned char **, int /*<<< orphan*/);
int /*<<< orphan*/ GENERAL_NAMES_free(int /*<<< orphan*/ *);
scalar_t__ GEN_DNS;
int /*<<< orphan*/ NID_subject_alt_name;
int /*<<< orphan*/ OPENSSL_free(char *);
int /*<<< orphan*/ *X509_get_ext_d2i(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                     int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int sk_GENERAL_NAME_num(int /*<<< orphan*/ *);
TYPE_2__ *sk_GENERAL_NAME_value(int /*<<< orphan*/ *, int);
scalar_t__ ssl_dnsname_match(char *, size_t, char const *, size_t);
char *ssl_x509_subject_cn(int /*<<< orphan*/ *, size_t *);
size_t strlen(char const *);
int ssl_x509_names_match(X509 *crt, const char *dnsname) {
  GENERAL_NAMES *altnames;
  char *cn;
  size_t cnsz, dnsnamesz;
  dnsnamesz = strlen(dnsname);
  cn = ssl_x509_subject_cn(crt, &cnsz);
  if (cn && ssl_dnsname_match(cn, cnsz, dnsname, dnsnamesz)) {
    free(cn);
    return 1;
  }
  if (cn) {
    free(cn);
  }
  altnames = X509_get_ext_d2i(crt, NID_subject_alt_name, 0, 0);
  if (!altnames)
    return 0;
  for (int i = 0; i < sk_GENERAL_NAME_num(altnames); i++) {
    GENERAL_NAME *gn = sk_GENERAL_NAME_value(altnames, i);
    if (gn->type == GEN_DNS) {
      unsigned char *altname;
      int altnamesz;
      ASN1_STRING_to_UTF8(&altname, gn->d.dNSName);
      altnamesz = ASN1_STRING_length(gn->d.dNSName);
      if (altnamesz < 0)
        break;
      if (ssl_dnsname_match((char *)altname, (size_t)altnamesz, dnsname,
                            dnsnamesz)) {
        OPENSSL_free((char *)altname);
        GENERAL_NAMES_free(altnames);
        return 1;
      }
      OPENSSL_free((char *)altname);
    }
  }
  GENERAL_NAMES_free(altnames);
  return 0;
}


x86 assembly (clang, -O0):
<ssl_x509_names_match>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <ssl_x509_subject_cn@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     121d <ssl_x509_names_match+0x6d>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   10a0 <ssl_dnsname_match@plt>
cmp    $0x0,%rax
je     121d <ssl_x509_names_match+0x6d>
mov    -0x28(%rbp),%rdi
call   1050 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1343 <ssl_x509_names_match+0x193>
cmpq   $0x0,-0x28(%rbp)
je     1231 <ssl_x509_names_match+0x81>
mov    -0x28(%rbp),%rdi
call   1050 <free@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1090 <X509_get_ext_d2i@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1262 <ssl_x509_names_match+0xb2>
movl   $0x0,-0x4(%rbp)
jmp    1343 <ssl_x509_names_match+0x193>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <sk_GENERAL_NAME_num@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%eax
cmp    %ecx,%eax
jge    1333 <ssl_x509_names_match+0x183>
mov    -0x20(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1070 <sk_GENERAL_NAME_value@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2d3d(%rip),%rcx
cmp    (%rcx),%rax
jne    1320 <ssl_x509_names_match+0x170>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%esi
lea    -0x50(%rbp),%rdi
call   10d0 <ASN1_STRING_to_UTF8@plt>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edi
call   1030 <ASN1_STRING_length@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    12da <ssl_x509_names_match+0x12a>
jmp    1333 <ssl_x509_names_match+0x183>
mov    -0x50(%rbp),%rdi
movslq -0x54(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   10a0 <ssl_dnsname_match@plt>
cmp    $0x0,%rax
je     1317 <ssl_x509_names_match+0x167>
mov    -0x50(%rbp),%rdi
call   10c0 <OPENSSL_free@plt>
mov    -0x20(%rbp),%rdi
call   1080 <GENERAL_NAMES_free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1343 <ssl_x509_names_match+0x193>
mov    -0x50(%rbp),%rdi
call   10c0 <OPENSSL_free@plt>
jmp    1325 <ssl_x509_names_match+0x175>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1269 <ssl_x509_names_match+0xb9>
mov    -0x20(%rbp),%rdi
call   1080 <GENERAL_NAMES_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssl_x509_names_match>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %rdi,%r13
mov    %rsi,%rdi
call   1060 <strlen@plt>
mov    %rax,%r14
lea    0x10(%rsp),%rsi
mov    %r13,%rdi
call   10b0 <ssl_x509_subject_cn@plt>
test   %rax,%rax
je     1210 <ssl_x509_names_match+0x60>
mov    %rax,%rbp
mov    0x10(%rsp),%rsi
mov    %rax,%rdi
mov    %r12,%rdx
mov    %r14,%rcx
call   10a0 <ssl_dnsname_match@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1050 <free@plt>
mov    $0x1,%eax
test   %rbx,%rbx
jne    12cd <ssl_x509_names_match+0x11d>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <X509_get_ext_d2i@plt>
test   %rax,%rax
je     12cb <ssl_x509_names_match+0x11b>
mov    %rax,%r15
mov    %rax,%rdi
call   1040 <sk_GENERAL_NAME_num@plt>
test   %eax,%eax
jle    12c3 <ssl_x509_names_match+0x113>
xor    %ebx,%ebx
mov    0x2d96(%rip),%rbp
jmp    125f <ssl_x509_names_match+0xaf>
nopl   0x0(%rax)
add    $0x1,%ebx
mov    %r15,%rdi
call   1040 <sk_GENERAL_NAME_num@plt>
cmp    %eax,%ebx
jge    12c3 <ssl_x509_names_match+0x113>
mov    %r15,%rdi
mov    %ebx,%esi
call   1070 <sk_GENERAL_NAME_value@plt>
mov    %rax,%r13
mov    (%rax),%rax
cmp    0x0(%rbp),%rax
jne    1250 <ssl_x509_names_match+0xa0>
mov    0x8(%r13),%esi
lea    0x8(%rsp),%rdi
call   10d0 <ASN1_STRING_to_UTF8@plt>
mov    0x8(%r13),%edi
call   1030 <ASN1_STRING_length@plt>
test   %eax,%eax
js     12c3 <ssl_x509_names_match+0x113>
mov    0x8(%rsp),%rdi
mov    %eax,%esi
mov    %r12,%rdx
mov    %r14,%rcx
call   10a0 <ssl_dnsname_match@plt>
mov    %rax,%r13
mov    0x8(%rsp),%rdi
call   10c0 <OPENSSL_free@plt>
test   %r13,%r13
je     1250 <ssl_x509_names_match+0xa0>
mov    %r15,%rdi
call   1080 <GENERAL_NAMES_free@plt>
mov    $0x1,%eax
jmp    12cd <ssl_x509_names_match+0x11d>
mov    %r15,%rdi
call   1080 <GENERAL_NAMES_free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i64, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32 }
@NID_subject_alt_name = global i32 0
@GEN_DNS = global i64 0
define i32 @ssl_x509_names_match(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca %struct.TYPE_5__*
%12 = alloca i8*
%13 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%14 = load i8*, i8** %5
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %9
%16 = load i32*, i32** %4
%17 = call i8* @ssl_x509_subject_cn(i32* %16, i64* %8)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %30
20: ; preds = %2
%21 = load i8*, i8** %7
%22 = load i64, i64* %8
%23 = load i8*, i8** %5
%24 = load i64, i64* %9
%25 = call i64 @ssl_dnsname_match(i8* %21, i64 %22, i8* %23, i64 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i8*, i8** %7
%29 = call i32 @free(i8* %28)
store i32 1, i32* %3
br label %95
30: ; preds = %20, %2
%31 = load i8*, i8** %7
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i8*, i8** %7
%35 = call i32 @free(i8* %34)
br label %36
36: ; preds = %33, %30
%37 = load i32*, i32** %4
%38 = load i32, i32* @NID_subject_alt_name
%39 = call i32* @X509_get_ext_d2i(i32* %37, i32 %38, i32 0, i32 0)
store i32* %39, i32** %6
%40 = load i32*, i32** %6
%41 = icmp ne i32* %40, null
br i1 %41, label %43, label %42
42: ; preds = %36
store i32 0, i32* %3
br label %95
43: ; preds = %36
store i32 0, i32* %10
br label %44
44: ; preds = %89, %43
%45 = load i32, i32* %10
%46 = load i32*, i32** %6
%47 = call i32 @sk_GENERAL_NAME_num(i32* %46)
%48 = icmp slt i32 %45, %47
br i1 %48, label %49, label %92
49: ; preds = %44
%50 = load i32*, i32** %6
%51 = load i32, i32* %10
%52 = call %struct.TYPE_5__* @sk_GENERAL_NAME_value(i32* %50, i32 %51)
store %struct.TYPE_5__* %52, %struct.TYPE_5__** %11
%53 = load %struct.TYPE_5__*, %struct.TYPE_5__** %11
%54 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %53, i32 0, i32 0
%55 = load i64, i64* %54
%56 = load i64, i64* @GEN_DNS
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %88
58: ; preds = %49
%59 = load %struct.TYPE_5__*, %struct.TYPE_5__** %11
%60 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %59, i32 0, i32 1
%61 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @ASN1_STRING_to_UTF8(i8** %12, i32 %62)
%64 = load %struct.TYPE_5__*, %struct.TYPE_5__** %11
%65 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %64, i32 0, i32 1
%66 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i32 @ASN1_STRING_length(i32 %67)
store i32 %68, i32* %13
%69 = load i32, i32* %13
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %58
br label %92
72: ; preds = %58
%73 = load i8*, i8** %12
%74 = load i32, i32* %13
%75 = sext i32 %74 to i64
%76 = load i8*, i8** %5
%77 = load i64, i64* %9
%78 = call i64 @ssl_dnsname_match(i8* %73, i64 %75, i8* %76, i64 %77)
%79 = icmp ne i64 %78, 0
br i1 %79, label %80, label %85
80: ; preds = %72
%81 = load i8*, i8** %12
%82 = call i32 @OPENSSL_free(i8* %81)
%83 = load i32*, i32** %6
%84 = call i32 @GENERAL_NAMES_free(i32* %83)
store i32 1, i32* %3
br label %95
85: ; preds = %72
%86 = load i8*, i8** %12
%87 = call i32 @OPENSSL_free(i8* %86)
br label %88
88: ; preds = %85, %49
br label %89
89: ; preds = %88
%90 = load i32, i32* %10
%91 = add nsw i32 %90, 1
store i32 %91, i32* %10
br label %44
92: ; preds = %71, %44
%93 = load i32*, i32** %6
%94 = call i32 @GENERAL_NAMES_free(i32* %93)
store i32 0, i32* %3
br label %95
95: ; preds = %92, %80, %42, %27
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i64, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32 }
@NID_subject_alt_name = global i32 0
@GEN_DNS = global i64 0
define i32 @ssl_x509_names_match(i32* %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = bitcast i64* %3 to i8*
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = call i8* @ssl_x509_subject_cn(i32* %0, i64* %3)
%8 = icmp eq i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %2
%10 = load i64, i64* %3
%11 = call i64 @ssl_dnsname_match(i8* %7, i64 %10, i8* %1, i64 %6)
%12 = icmp eq i64 %11, 0
%13 = call i32 @free(i8* %7)
br i1 %12, label %14, label %54
14: ; preds = %9, %2
%15 = load i32, i32* @NID_subject_alt_name
%16 = call i32* @X509_get_ext_d2i(i32* %0, i32 %15, i32 0, i32 0)
%17 = icmp eq i32* %16, null
br i1 %17, label %54, label %18
18: ; preds = %14
%19 = call i32 @sk_GENERAL_NAME_num(i32* %16)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %52
21: ; preds = %18
%22 = bitcast i8** %4 to i8*
br label %23
23: ; preds = %21, %46
%24 = phi i32 [ 0, %21 ], [ %47, %46 ]
%25 = call %struct.TYPE_5__* @sk_GENERAL_NAME_value(i32* %16, i32 %24)
%26 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %25, i64 0, i32 0
%27 = load i64, i64* %26
%28 = load i64, i64* @GEN_DNS
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %46
30: ; preds = %23
%31 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %25, i64 0, i32 1, i32 0
%32 = load i32, i32* %31
%33 = call i32 @ASN1_STRING_to_UTF8(i8** %4, i32 %32)
%34 = load i32, i32* %31
%35 = call i32 @ASN1_STRING_length(i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %30
br label %52
38: ; preds = %30
%39 = load i8*, i8** %4
%40 = zext i32 %35 to i64
%41 = call i64 @ssl_dnsname_match(i8* %39, i64 %40, i8* %1, i64 %6)
%42 = icmp eq i64 %41, 0
%43 = load i8*, i8** %4
%44 = call i32 @OPENSSL_free(i8* %43)
br i1 %42, label %45, label %50
45: ; preds = %38
br label %46
46: ; preds = %45, %23
%47 = add nuw nsw i32 %24, 1
%48 = call i32 @sk_GENERAL_NAME_num(i32* %16)
%49 = icmp slt i32 %47, %48
br i1 %49, label %23, label %52
50: ; preds = %38
%51 = call i32 @GENERAL_NAMES_free(i32* %16)
br label %54
52: ; preds = %46, %18, %37
%53 = call i32 @GENERAL_NAMES_free(i32* %16)
br label %54
54: ; preds = %9, %50, %14, %52
%55 = phi i32 [ 0, %52 ], [ 1, %50 ], [ 0, %14 ], [ 1, %9 ]
ret i32 %55
}