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

/* Variables and functions */
unsigned char *SSL_get_ex_data(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ SSL_get_ex_data_X509_STORE_CTX_idx();
int /*<<< orphan*/ *X509_STORE_CTX_get_current_cert(int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_STORE_CTX_get_ex_data(int /*<<< orphan*/ *,
                                               int /*<<< orphan*/);
int /*<<< orphan*/ create_fingerprint(unsigned char *, int /*<<< orphan*/ *);
scalar_t__ memcmp(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ printf(char *);
int tls_verify_callback(int ok, X509_STORE_CTX *ctx) {
  X509 *x509 = X509_STORE_CTX_get_current_cert(ctx);
  SSL *ssl =
      X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
  unsigned char *hash = SSL_get_ex_data(ssl, 0);
  if (hash != NULL) {
    unsigned char expected_hash[20];
    create_fingerprint(expected_hash, x509);
    if (memcmp(hash, expected_hash, 20)) {
      printf("public key doesn't match fingerprint\n");
      return 0;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<tls_verify_callback>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <X509_STORE_CTX_get_current_cert@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1090 <SSL_get_ex_data_X509_STORE_CTX_idx@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
call   1080 <X509_STORE_CTX_get_ex_data@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1030 <SSL_get_ex_data@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     120a <tls_verify_callback+0x9a>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <create_fingerprint@plt>
mov    -0x28(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
je     1205 <tls_verify_callback+0x95>
lea    0xe0c(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1211 <tls_verify_callback+0xa1>
jmp    120a <tls_verify_callback+0x9a>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tls_verify_callback>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rsi,%rdi
call   1060 <X509_STORE_CTX_get_current_cert@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1090 <SSL_get_ex_data_X509_STORE_CTX_idx@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1080 <X509_STORE_CTX_get_ex_data@plt>
mov    %rax,%rdi
xor    %esi,%esi
call   1040 <SSL_get_ex_data@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     11e1 <tls_verify_callback+0x71>
mov    %rax,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <create_fingerprint@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x14,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     11e1 <tls_verify_callback+0x71>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"public key doesn't match fingerprint\0A\00"
define i32 @tls_verify_callback(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca [20 x i8]
store i32 %0, i32* %4
store i32* %1, i32** %5
%10 = load i32*, i32** %5
%11 = call i32* @X509_STORE_CTX_get_current_cert(i32* %10)
store i32* %11, i32** %6
%12 = load i32*, i32** %5
%13 = call i32 (...) @SSL_get_ex_data_X509_STORE_CTX_idx()
%14 = call i32* @X509_STORE_CTX_get_ex_data(i32* %12, i32 %13)
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = call i8* @SSL_get_ex_data(i32* %15, i32 0)
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %30
19: ; preds = %2
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%21 = load i32*, i32** %6
%22 = call i32 @create_fingerprint(i8* %20, i32* %21)
%23 = load i8*, i8** %8
%24 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%25 = call i64 @memcmp(i8* %23, i8* %24, i32 20)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %31
29: ; preds = %19
br label %30
30: ; preds = %29, %2
store i32 1, i32* %3
br label %31
31: ; preds = %30, %27
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@str = [37 x i8] c"public key doesn't match fingerprint\00"
define i32 @tls_verify_callback(i32 %0, i32* %1) {
%3 = alloca [20 x i8]
%4 = tail call i32* @X509_STORE_CTX_get_current_cert(i32* %1)
%5 = tail call i32 (...) @SSL_get_ex_data_X509_STORE_CTX_idx()
%6 = tail call i32* @X509_STORE_CTX_get_ex_data(i32* %1, i32 %5)
%7 = tail call i8* @SSL_get_ex_data(i32* %6, i32 0)
%8 = icmp eq i8* %7, null
br i1 %8, label %17, label %9
9: ; preds = %2
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%11 = call i32 @create_fingerprint(i8* %10, i32* %4)
%12 = call i64 @memcmp(i8* %7, i8* %10, i32 20)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
br label %17
16: ; preds = %9
br label %17
17: ; preds = %2, %16, %14
%18 = phi i32 [ 0, %14 ], [ 1, %16 ], [ 1, %2 ]
ret i32 %18
}