#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;
typedef int /*<<< orphan*/ X509;

/* Variables and functions */
int /*<<< orphan*/ NID_commonName;
int X509_NAME_get_text_by_NID(int /*<<< orphan*/ *, int /*<<< orphan*/, char *,
                              int);
int /*<<< orphan*/ *X509_get_subject_name(int /*<<< orphan*/ *);
char *calloc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *getprogname();
scalar_t__ ressl_match_hostname(char *, char const *);
int /*<<< orphan*/ stdout;
scalar_t__ strlen(char *);
int ressl_check_common_name(X509 *cert, const char *host) {
  X509_NAME *name;
  char *common_name = NULL;
  int common_name_len;
  int rv = -1;
  name = X509_get_subject_name(cert);
  if (name == NULL)
    goto out;
  common_name_len = X509_NAME_get_text_by_NID(name, NID_commonName, NULL, 0);
  if (common_name_len < 0)
    goto out;
  common_name = calloc(common_name_len + 1, 1);
  if (common_name == NULL)
    goto out;
  X509_NAME_get_text_by_NID(name, NID_commonName, common_name,
                            common_name_len + 1);
  if (common_name_len != (int)strlen(common_name)) {
    fprintf(stdout,
            "%s: NUL byte in Common Name field, "
            "probably a malicious certificate.\n",
            getprogname());
    rv = -2;
    goto out;
  }
  if (ressl_match_hostname(common_name, host) == 0)
    rv = 0;
out:
  free(common_name);
  return rv;
}


x86 assembly (clang, -O0):
<ressl_check_common_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <X509_get_subject_name@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bc <ressl_check_common_name+0x3c>
jmp    1297 <ressl_check_common_name+0x117>
mov    -0x18(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
call   1030 <X509_NAME_get_text_by_NID@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11e6 <ressl_check_common_name+0x66>
jmp    1297 <ressl_check_common_name+0x117>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120a <ressl_check_common_name+0x8a>
jmp    1297 <ressl_check_common_name+0x117>
mov    -0x18(%rbp),%rdi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
call   1030 <X509_NAME_get_text_by_NID@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
je     1274 <ressl_check_common_name+0xf4>
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1070 <getprogname@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xd9d(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0xfffffffe,-0x28(%rbp)
jmp    1297 <ressl_check_common_name+0x117>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <ressl_match_hostname@plt>
cmp    $0x0,%rax
jne    1292 <ressl_check_common_name+0x112>
movl   $0x0,-0x28(%rbp)
jmp    1297 <ressl_check_common_name+0x117>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ressl_check_common_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
call   1080 <X509_get_subject_name@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     120e <ressl_check_common_name+0x8e>
mov    %rax,%rbp
mov    0x2e38(%rip),%r14
mov    (%r14),%esi
xor    %r12d,%r12d
mov    %rax,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <X509_NAME_get_text_by_NID@plt>
test   %eax,%eax
js     1211 <ressl_check_common_name+0x91>
mov    %eax,%ebx
mov    %r13,(%rsp)
lea    0x1(%rbx),%r13d
mov    %r13d,%edi
mov    $0x1,%esi
call   1090 <calloc@plt>
test   %rax,%rax
je     120e <ressl_check_common_name+0x8e>
mov    %rax,%r12
mov    (%r14),%esi
mov    %rbp,%rdi
mov    %rax,%rdx
mov    %r13d,%ecx
call   1030 <X509_NAME_get_text_by_NID@plt>
mov    %r12,%rdi
call   1060 <strlen@plt>
cmp    %eax,%ebx
jne    122b <ressl_check_common_name+0xab>
mov    %r12,%rdi
mov    (%rsp),%rsi
call   1050 <ressl_match_hostname@plt>
neg    %rax
sbb    %r15d,%r15d
jmp    1211 <ressl_check_common_name+0x91>
xor    %r12d,%r12d
mov    %r12,%rdi
call   1040 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2da6(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1070 <getprogname@plt>
lea    0xdbe(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
mov    $0xfffffffe,%r15d
jmp    1211 <ressl_check_common_name+0x91>

LLVM IR (-O0):
@NID_commonName = global i32 0
@stdout = global i32 0
@.str = [70 x i8] c"%s: NUL byte in Common Name field, probably a malicious certificate.\0A\00"
define i32 @ressl_check_common_name(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
store i8* null, i8** %6
store i32 -1, i32* %8
%9 = load i32*, i32** %3
%10 = call i32* @X509_get_subject_name(i32* %9)
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
br label %51
14: ; preds = %2
%15 = load i32*, i32** %5
%16 = load i32, i32* @NID_commonName
%17 = call i32 @X509_NAME_get_text_by_NID(i32* %15, i32 %16, i8* null, i32 0)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
br label %51
21: ; preds = %14
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 1
%24 = call i8* @calloc(i32 %23, i32 1)
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %21
br label %51
28: ; preds = %21
%29 = load i32*, i32** %5
%30 = load i32, i32* @NID_commonName
%31 = load i8*, i8** %6
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
%34 = call i32 @X509_NAME_get_text_by_NID(i32* %29, i32 %30, i8* %31, i32 %33)
%35 = load i32, i32* %7
%36 = load i8*, i8** %6
%37 = call i64 @strlen(i8* %36)
%38 = trunc i64 %37 to i32
%39 = icmp ne i32 %35, %38
br i1 %39, label %40, label %44
40: ; preds = %28
%41 = load i32, i32* @stdout
%42 = call i8* (...) @getprogname()
%43 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str, i64 0, i64 0), i8* %42)
store i32 -2, i32* %8
br label %51
44: ; preds = %28
%45 = load i8*, i8** %6
%46 = load i8*, i8** %4
%47 = call i64 @ressl_match_hostname(i8* %45, i8* %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44
store i32 0, i32* %8
br label %50
50: ; preds = %49, %44
br label %51
51: ; preds = %50, %40, %27, %20, %13
%52 = load i8*, i8** %6
%53 = call i32 @free(i8* %52)
%54 = load i32, i32* %8
ret i32 %54
}

LLVM IR (-O2):
@NID_commonName = global i32 0
@stdout = global i32 0
@.str = [70 x i8] c"%s: NUL byte in Common Name field, probably a malicious certificate.\0A\00"
define i32 @ressl_check_common_name(i32* %0, i8* %1) {
%3 = tail call i32* @X509_get_subject_name(i32* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %27, label %5
5: ; preds = %2
%6 = load i32, i32* @NID_commonName
%7 = tail call i32 @X509_NAME_get_text_by_NID(i32* %3, i32 %6, i8* null, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %27, label %9
9: ; preds = %5
%10 = add nuw nsw i32 %7, 1
%11 = tail call i8* @calloc(i32 %10, i32 1)
%12 = icmp eq i8* %11, null
br i1 %12, label %27, label %13
13: ; preds = %9
%14 = load i32, i32* @NID_commonName
%15 = tail call i32 @X509_NAME_get_text_by_NID(i32* %3, i32 %14, i8* %11, i32 %10)
%16 = tail call i64 @strlen(i8* %11)
%17 = trunc i64 %16 to i32
%18 = icmp eq i32 %7, %17
br i1 %18, label %23, label %19
19: ; preds = %13
%20 = load i32, i32* @stdout
%21 = tail call i8* (...) @getprogname()
%22 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %27
23: ; preds = %13
%24 = tail call i64 @ressl_match_hostname(i8* %11, i8* %1)
%25 = icmp ne i64 %24, 0
%26 = sext i1 %25 to i32
br label %27
27: ; preds = %23, %9, %5, %2, %19
%28 = phi i8* [ null, %2 ], [ null, %5 ], [ null, %9 ], [ %11, %19 ], [ %11, %23 ]
%29 = phi i32 [ -1, %2 ], [ -1, %5 ], [ -1, %9 ], [ -2, %19 ], [ %26, %23 ]
%30 = tail call i32 @free(i8* %28)
ret i32 %29
}