#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 int32_t;
typedef int /*<<< orphan*/ X509_NAME_ENTRY;
typedef int /*<<< orphan*/ X509_NAME;
typedef int /*<<< orphan*/ X509;
struct TYPE_4__ {
  int /*<<< orphan*/ *dNSName;
};
struct TYPE_5__ {
  int type;
  TYPE_1__ d;
};
typedef int /*<<< orphan*/ GENERAL_NAMES;
typedef TYPE_2__ GENERAL_NAME;
typedef int /*<<< orphan*/ ASN1_STRING;

/* Variables and functions */
scalar_t__ CheckX509HostnameMatch(int /*<<< orphan*/ *, char const *, int, int);
int /*<<< orphan*/ GENERAL_NAMES_free(int /*<<< orphan*/ *);
int GEN_DNS;
int NID_commonName;
int /*<<< orphan*/ NID_subject_alt_name;
int /*<<< orphan*/ *X509_NAME_ENTRY_get_data(int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_NAME_get_entry(int /*<<< orphan*/ *, int);
int X509_NAME_get_index_by_NID(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ *X509_get_ext_d2i(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                     int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_get_subject_name(int /*<<< orphan*/ *);
int sk_GENERAL_NAME_num(int /*<<< orphan*/ *);
TYPE_2__ *sk_GENERAL_NAME_value(int /*<<< orphan*/ *, int);
int32_t CheckX509Hostname(X509 *x509, const char *hostname,
                          int32_t cchHostname) {
  if (!x509)
    return -2;
  if (cchHostname > 0 && !hostname)
    return -3;
  if (cchHostname < 0)
    return -4;
  int subjectNid = NID_commonName;
  int sanGenType = GEN_DNS;
  GENERAL_NAMES *san = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL);
  char readSubject = 1;
  int success = 0;
  if (san) {
    int i;
    int count = sk_GENERAL_NAME_num(san);
    for (i = 0; i < count; ++i) {
      GENERAL_NAME *sanEntry = sk_GENERAL_NAME_value(san, i);
      if (sanEntry->type != sanGenType) {
        continue;
      }
      readSubject = 0;
      if (CheckX509HostnameMatch(sanEntry->d.dNSName, hostname, cchHostname,
                                 1)) {
        success = 1;
        break;
      }
    }
    GENERAL_NAMES_free(san);
  }
  if (readSubject && !success) {
    X509_NAME *subject = X509_get_subject_name(x509);
    if (subject) {
      int i = -1;
      while ((i = X509_NAME_get_index_by_NID(subject, subjectNid, i)) >= 0) {
        X509_NAME_ENTRY *nameEnt = X509_NAME_get_entry(subject, i);
        ASN1_STRING *cn = X509_NAME_ENTRY_get_data(nameEnt);
        if (CheckX509HostnameMatch(cn, hostname, cchHostname, 0)) {
          success = 1;
          break;
        }
      }
    }
  }
  return success;
}


x86 assembly (clang, -O0):
<CheckX509Hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ba <CheckX509Hostname+0x2a>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1379 <CheckX509Hostname+0x1e9>
cmpl   $0x0,-0x1c(%rbp)
jle    11db <CheckX509Hostname+0x4b>
cmpq   $0x0,-0x18(%rbp)
jne    11db <CheckX509Hostname+0x4b>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1379 <CheckX509Hostname+0x1e9>
cmpl   $0x0,-0x1c(%rbp)
jge    11f1 <CheckX509Hostname+0x61>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1379 <CheckX509Hostname+0x1e9>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   10a0 <X509_get_ext_d2i@plt>
mov    %rax,-0x30(%rbp)
movb   $0x1,-0x31(%rbp)
movl   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12ce <CheckX509Hostname+0x13e>
mov    -0x30(%rbp),%rdi
call   1030 <sk_GENERAL_NAME_num@plt>
mov    %eax,-0x40(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    12c5 <CheckX509Hostname+0x135>
mov    -0x30(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1070 <sk_GENERAL_NAME_value@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
cmp    -0x24(%rbp),%eax
je     127f <CheckX509Hostname+0xef>
jmp    12b7 <CheckX509Hostname+0x127>
movb   $0x0,-0x31(%rbp)
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    $0x1,%ecx
call   1040 <CheckX509HostnameMatch@plt>
cmp    $0x0,%rax
je     12b2 <CheckX509Hostname+0x122>
movl   $0x1,-0x38(%rbp)
jmp    12c5 <CheckX509Hostname+0x135>
jmp    12b7 <CheckX509Hostname+0x127>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    124f <CheckX509Hostname+0xbf>
mov    -0x30(%rbp),%rdi
call   1080 <GENERAL_NAMES_free@plt>
movsbl -0x31(%rbp),%eax
cmp    $0x0,%eax
je     1373 <CheckX509Hostname+0x1e3>
cmpl   $0x0,-0x38(%rbp)
jne    1373 <CheckX509Hostname+0x1e3>
mov    -0x10(%rbp),%rdi
call   1060 <X509_get_subject_name@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     136e <CheckX509Hostname+0x1de>
movl   $0xffffffff,-0x54(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x54(%rbp),%edx
call   10b0 <X509_NAME_get_index_by_NID@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jl     1369 <CheckX509Hostname+0x1d9>
mov    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%esi
call   1090 <X509_NAME_get_entry@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
call   1050 <X509_NAME_ENTRY_get_data@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1040 <CheckX509HostnameMatch@plt>
cmp    $0x0,%rax
je     1364 <CheckX509Hostname+0x1d4>
movl   $0x1,-0x38(%rbp)
jmp    1369 <CheckX509Hostname+0x1d9>
jmp    1304 <CheckX509Hostname+0x174>
jmp    136e <CheckX509Hostname+0x1de>
jmp    1373 <CheckX509Hostname+0x1e3>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CheckX509Hostname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     1272 <CheckX509Hostname+0xe2>
mov    %edx,%r12d
mov    %rsi,%r13
mov    %rdi,%rbx
test   %rsi,%rsi
jne    11c3 <CheckX509Hostname+0x33>
mov    $0xfffffffd,%ebp
test   %r12d,%r12d
jg     1313 <CheckX509Hostname+0x183>
test   %r12d,%r12d
js     127c <CheckX509Hostname+0xec>
mov    0x2dfd(%rip),%rax
mov    (%rax),%r15d
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   10a0 <X509_get_ext_d2i@plt>
test   %rax,%rax
mov    %r13,0x10(%rsp)
mov    %r15d,0x8(%rsp)
je     1286 <CheckX509Hostname+0xf6>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <sk_GENERAL_NAME_num@plt>
test   %eax,%eax
mov    %r12d,0x4(%rsp)
jle    128b <CheckX509Hostname+0xfb>
mov    %eax,%r15d
xor    %r12d,%r12d
mov    $0x1,%r13b
jmp    1239 <CheckX509Hostname+0xa9>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12d
cmp    %r12d,%r15d
je     128e <CheckX509Hostname+0xfe>
mov    %r14,%rdi
mov    %r12d,%esi
call   1070 <sk_GENERAL_NAME_value@plt>
mov    0xc(%rsp),%ecx
cmp    %ecx,(%rax)
jne    1230 <CheckX509Hostname+0xa0>
mov    0x8(%rax),%rdi
mov    $0x1,%ebp
mov    0x10(%rsp),%rsi
mov    0x4(%rsp),%edx
mov    $0x1,%ecx
call   1040 <CheckX509HostnameMatch@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     1230 <CheckX509Hostname+0xa0>
jmp    1290 <CheckX509Hostname+0x100>
mov    $0xfffffffe,%ebp
jmp    1313 <CheckX509Hostname+0x183>
mov    $0xfffffffc,%ebp
jmp    1313 <CheckX509Hostname+0x183>
mov    $0x1,%r13b
jmp    129d <CheckX509Hostname+0x10d>
mov    $0x1,%r13b
xor    %ebp,%ebp
mov    %r14,%rdi
call   1080 <GENERAL_NAMES_free@plt>
mov    0x4(%rsp),%r12d
test   %r13b,%r13b
je     1313 <CheckX509Hostname+0x183>
test   %ebp,%ebp
mov    0x10(%rsp),%r14
mov    0x8(%rsp),%r13d
jne    1313 <CheckX509Hostname+0x183>
mov    %rbx,%rdi
call   1060 <X509_get_subject_name@plt>
test   %rax,%rax
je     1311 <CheckX509Hostname+0x181>
mov    %rax,%r15
mov    $0xffffffff,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %r13d,%esi
mov    %ebx,%edx
call   10b0 <X509_NAME_get_index_by_NID@plt>
test   %eax,%eax
js     1311 <CheckX509Hostname+0x181>
mov    %eax,%ebx
mov    %r15,%rdi
mov    %eax,%esi
call   1090 <X509_NAME_get_entry@plt>
mov    %rax,%rdi
call   1050 <X509_NAME_ENTRY_get_data@plt>
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
call   1040 <CheckX509HostnameMatch@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     12d0 <CheckX509Hostname+0x140>
jmp    1313 <CheckX509Hostname+0x183>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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