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

/* Variables and functions */
int /*<<< orphan*/ *SSL_get_peer_certificate(int /*<<< orphan*/ const *);
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
int matches_common_name(char const *, int /*<<< orphan*/ *);
int matches_subject_alternative_name(char const *, int /*<<< orphan*/ *);
int32_t validate_hostname(const char *hostname, const SSL *server) {
  int32_t result;
  X509 *server_cert = 0;
  if (!hostname || !server) {
    return 4;
  }
  server_cert = SSL_get_peer_certificate(server);
  if (!server_cert) {
    return 4;
  }
  result = matches_subject_alternative_name(hostname, server_cert);
  if (result == 2) {
    result = matches_common_name(hostname, server_cert);
  }
  X509_free(server_cert);
  return result;
}


x86 assembly (clang, -O0):
<validate_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116e <validate_hostname+0x2e>
cmpq   $0x0,-0x18(%rbp)
jne    117a <validate_hostname+0x3a>
movl   $0x4,-0x4(%rbp)
jmp    11d7 <validate_hostname+0x97>
mov    -0x18(%rbp),%rdi
call   1050 <SSL_get_peer_certificate@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119e <validate_hostname+0x5e>
movl   $0x4,-0x4(%rbp)
jmp    11d7 <validate_hostname+0x97>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <matches_subject_alternative_name@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jne    11c8 <validate_hostname+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <matches_common_name@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <X509_free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_hostname>:
push   %r15
push   %r14
push   %rbx
mov    $0x4,%ebx
test   %rdi,%rdi
je     118e <validate_hostname+0x4e>
test   %rsi,%rsi
je     118e <validate_hostname+0x4e>
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <SSL_get_peer_certificate@plt>
test   %rax,%rax
je     118e <validate_hostname+0x4e>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <matches_subject_alternative_name@plt>
mov    %eax,%ebx
cmp    $0x2,%eax
jne    1186 <validate_hostname+0x46>
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <matches_common_name@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1060 <X509_free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @validate_hostname(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* null, i32** %7
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %10, %2
store i32 4, i32* %3
br label %34
14: ; preds = %10
%15 = load i32*, i32** %5
%16 = call i32* @SSL_get_peer_certificate(i32* %15)
store i32* %16, i32** %7
%17 = load i32*, i32** %7
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %14
store i32 4, i32* %3
br label %34
20: ; preds = %14
%21 = load i8*, i8** %4
%22 = load i32*, i32** %7
%23 = call i32 @matches_subject_alternative_name(i8* %21, i32* %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, 2
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i8*, i8** %4
%28 = load i32*, i32** %7
%29 = call i32 @matches_common_name(i8* %27, i32* %28)
store i32 %29, i32* %6
br label %30
30: ; preds = %26, %20
%31 = load i32*, i32** %7
%32 = call i32 @X509_free(i32* %31)
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %34
34: ; preds = %30, %19, %13
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
define i32 @validate_hostname(i8* %0, i32* %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i32* %1, null
%5 = and i1 %3, %4
br i1 %5, label %6, label %17
6: ; preds = %2
%7 = tail call i32* @SSL_get_peer_certificate(i32* %1)
%8 = icmp eq i32* %7, null
br i1 %8, label %17, label %9
9: ; preds = %6
%10 = tail call i32 @matches_subject_alternative_name(i8* %0, i32* %7)
%11 = icmp eq i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @matches_common_name(i8* %0, i32* %7)
br label %14
14: ; preds = %12, %9
%15 = phi i32 [ %13, %12 ], [ %10, %9 ]
%16 = tail call i32 @X509_free(i32* %7)
br label %17
17: ; preds = %6, %2, %14
%18 = phi i32 [ %15, %14 ], [ 4, %2 ], [ 4, %6 ]
ret i32 %18
}