#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 */

/* Type definitions */
typedef int /*<<< orphan*/ X509_NAME_ENTRY;
typedef int /*<<< orphan*/ X509;
typedef int /*<<< orphan*/ ASN1_STRING;

/* Variables and functions */
scalar_t__ ASN1_STRING_data(int /*<<< orphan*/ *);
scalar_t__ ASN1_STRING_length(int /*<<< orphan*/ *);
int /*<<< orphan*/ NID_commonName;
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 /*<<< orphan*/, int);
int /*<<< orphan*/ X509_get_subject_name(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcasecmp(char const *, char *);
scalar_t__ strlen(char *);
int matches_common_name(const char *hostname, const X509 *server_cert) {
  int common_name_loc = -1;
  X509_NAME_ENTRY *common_name_entry = 0;
  ASN1_STRING *common_name_asn1 = 0;
  char *common_name_str = 0;
  common_name_loc = X509_NAME_get_index_by_NID(
      X509_get_subject_name((X509 *)server_cert), NID_commonName, -1);
  if (common_name_loc < 0) {
    return 4;
  }
  common_name_entry = X509_NAME_get_entry(
      X509_get_subject_name((X509 *)server_cert), common_name_loc);
  if (!common_name_entry) {
    return 4;
  }
  common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);
  if (!common_name_asn1) {
    return 4;
  }
  common_name_str = (char *)ASN1_STRING_data(common_name_asn1);
  if (ASN1_STRING_length(common_name_asn1) != strlen(common_name_str)) {
    return 3;
  }
  if (!strcasecmp(hostname, common_name_str)) {
    return 0;
  } else {
    return 1;
  }
}


x86 assembly (clang, -O0):
<matches_common_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <X509_get_subject_name@plt>
mov    %eax,%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edx
call   10a0 <X509_NAME_get_index_by_NID@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e6 <matches_common_name+0x66>
movl   $0x4,-0x4(%rbp)
jmp    12a0 <matches_common_name+0x120>
mov    -0x18(%rbp),%rdi
call   1080 <X509_get_subject_name@plt>
mov    %eax,%edi
mov    -0x1c(%rbp),%esi
call   1090 <X509_NAME_get_entry@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1214 <matches_common_name+0x94>
movl   $0x4,-0x4(%rbp)
jmp    12a0 <matches_common_name+0x120>
mov    -0x28(%rbp),%rdi
call   1050 <X509_NAME_ENTRY_get_data@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1238 <matches_common_name+0xb8>
movl   $0x4,-0x4(%rbp)
jmp    12a0 <matches_common_name+0x120>
mov    -0x30(%rbp),%rdi
call   1070 <ASN1_STRING_data@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <ASN1_STRING_length@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
je     1277 <matches_common_name+0xf7>
movl   $0x3,-0x4(%rbp)
jmp    12a0 <matches_common_name+0x120>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%eax
jne    1299 <matches_common_name+0x119>
movl   $0x0,-0x4(%rbp)
jmp    12a0 <matches_common_name+0x120>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matches_common_name>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1080 <X509_get_subject_name@plt>
mov    0x2e43(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
mov    $0xffffffff,%edx
call   10a0 <X509_NAME_get_index_by_NID@plt>
mov    $0x4,%r12d
test   %eax,%eax
js     121a <matches_common_name+0x9a>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <X509_get_subject_name@plt>
mov    %eax,%edi
mov    %ebp,%esi
call   1090 <X509_NAME_get_entry@plt>
test   %rax,%rax
je     121a <matches_common_name+0x9a>
mov    %rax,%rdi
call   1050 <X509_NAME_ENTRY_get_data@plt>
test   %rax,%rax
je     121a <matches_common_name+0x9a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <ASN1_STRING_data@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <ASN1_STRING_length@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    $0x3,%r12d
cmp    %rax,%rbx
jne    121a <matches_common_name+0x9a>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <strcasecmp@plt>
xor    %r12d,%r12d
test   %eax,%eax
setne  %r12b
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@NID_commonName = global i32 0
define i32 @matches_common_name(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 -1, i32* %6
store i32* null, i32** %7
store i32* null, i32** %8
store i8* null, i8** %9
%10 = load i32*, i32** %5
%11 = call i32 @X509_get_subject_name(i32* %10)
%12 = load i32, i32* @NID_commonName
%13 = call i32 @X509_NAME_get_index_by_NID(i32 %11, i32 %12, i32 -1)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 4, i32* %3
br label %48
17: ; preds = %2
%18 = load i32*, i32** %5
%19 = call i32 @X509_get_subject_name(i32* %18)
%20 = load i32, i32* %6
%21 = call i32* @X509_NAME_get_entry(i32 %19, i32 %20)
store i32* %21, i32** %7
%22 = load i32*, i32** %7
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %17
store i32 4, i32* %3
br label %48
25: ; preds = %17
%26 = load i32*, i32** %7
%27 = call i32* @X509_NAME_ENTRY_get_data(i32* %26)
store i32* %27, i32** %8
%28 = load i32*, i32** %8
%29 = icmp ne i32* %28, null
br i1 %29, label %31, label %30
30: ; preds = %25
store i32 4, i32* %3
br label %48
31: ; preds = %25
%32 = load i32*, i32** %8
%33 = call i64 @ASN1_STRING_data(i32* %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %9
%35 = load i32*, i32** %8
%36 = call i64 @ASN1_STRING_length(i32* %35)
%37 = load i8*, i8** %9
%38 = call i64 @strlen(i8* %37)
%39 = icmp ne i64 %36, %38
br i1 %39, label %40, label %41
40: ; preds = %31
store i32 3, i32* %3
br label %48
41: ; preds = %31
%42 = load i8*, i8** %4
%43 = load i8*, i8** %9
%44 = call i32 @strcasecmp(i8* %42, i8* %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %41
store i32 0, i32* %3
br label %48
47: ; preds = %41
store i32 1, i32* %3
br label %48
48: ; preds = %47, %46, %40, %30, %24, %16
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@NID_commonName = global i32 0
define i32 @matches_common_name(i8* nocapture %0, i32* %1) {
%3 = tail call i32 @X509_get_subject_name(i32* %1)
%4 = load i32, i32* @NID_commonName
%5 = tail call i32 @X509_NAME_get_index_by_NID(i32 %3, i32 %4, i32 -1)
%6 = icmp slt i32 %5, 0
br i1 %6, label %24, label %7
7: ; preds = %2
%8 = tail call i32 @X509_get_subject_name(i32* %1)
%9 = tail call i32* @X509_NAME_get_entry(i32 %8, i32 %5)
%10 = icmp eq i32* %9, null
br i1 %10, label %24, label %11
11: ; preds = %7
%12 = tail call i32* @X509_NAME_ENTRY_get_data(i32* %9)
%13 = icmp eq i32* %12, null
br i1 %13, label %24, label %14
14: ; preds = %11
%15 = tail call i64 @ASN1_STRING_data(i32* %12)
%16 = inttoptr i64 %15 to i8*
%17 = tail call i64 @ASN1_STRING_length(i32* %12)
%18 = tail call i64 @strlen(i8* dereferenceable(1) %16)
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = tail call i32 @strcasecmp(i8* %0, i8* %16)
%22 = icmp ne i32 %21, 0
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %20, %14, %11, %7, %2
%25 = phi i32 [ 4, %2 ], [ 4, %7 ], [ 4, %11 ], [ 3, %14 ], [ %23, %20 ]
ret i32 %25
}