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

/* Variables and functions */
int EVP_DigestVerifyFinal(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ EVP_DigestVerifyInit(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DigestVerifyUpdate(int /*<<< orphan*/ *,
                                          unsigned char const *, int);
int /*<<< orphan*/ *EVP_MD_CTX_create();
int /*<<< orphan*/ EVP_MD_CTX_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_sha256();
int /*<<< orphan*/ *X509_get_pubkey(int /*<<< orphan*/ *);
bool verifySignature(const unsigned char *data, int dataLength,
                     unsigned char *signature, int signatureLength,
                     X509 *cert) {
  EVP_PKEY *pubKey = X509_get_pubkey(cert);
  EVP_MD_CTX *mdctx = NULL;
  mdctx = EVP_MD_CTX_create();
  EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pubKey);
  EVP_DigestVerifyUpdate(mdctx, data, dataLength);
  int result = EVP_DigestVerifyFinal(mdctx, signature, signatureLength);
  EVP_PKEY_free(pubKey);
  EVP_MD_CTX_destroy(mdctx);
  return result > 0;
}


x86 assembly (clang, -O0):
<verifySignature>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <X509_get_pubkey@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    $0x0,%al
call   10a0 <EVP_MD_CTX_create@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1080 <EVP_sha256@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
mov    -0x30(%rbp),%r8
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
call   1040 <EVP_DigestVerifyInit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1070 <EVP_DigestVerifyUpdate@plt>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1090 <EVP_DigestVerifyFinal@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <EVP_PKEY_free@plt>
mov    -0x38(%rbp),%rdi
call   1060 <EVP_MD_CTX_destroy@plt>
cmpl   $0x0,-0x3c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<verifySignature>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%r13
mov    %r8,%rdi
call   1050 <X509_get_pubkey@plt>
mov    %rax,%r14
xor    %eax,%eax
call   10a0 <EVP_MD_CTX_create@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1080 <EVP_sha256@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1040 <EVP_DigestVerifyInit@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1070 <EVP_DigestVerifyUpdate@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1090 <EVP_DigestVerifyFinal@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1030 <EVP_PKEY_free@plt>
mov    %rbx,%rdi
call   1060 <EVP_MD_CTX_destroy@plt>
xor    %eax,%eax
test   %ebp,%ebp
setg   %al
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @verifySignature(i8* %0, i32 %1, i8* %2, i32 %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32* %4, i32** %10
%14 = load i32*, i32** %10
%15 = call i32* @X509_get_pubkey(i32* %14)
store i32* %15, i32** %11
store i32* null, i32** %12
%16 = call i32* (...) @EVP_MD_CTX_create()
store i32* %16, i32** %12
%17 = load i32*, i32** %12
%18 = call i32 (...) @EVP_sha256()
%19 = load i32*, i32** %11
%20 = call i32 @EVP_DigestVerifyInit(i32* %17, i32* null, i32 %18, i32* null, i32* %19)
%21 = load i32*, i32** %12
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = call i32 @EVP_DigestVerifyUpdate(i32* %21, i8* %22, i32 %23)
%25 = load i32*, i32** %12
%26 = load i8*, i8** %8
%27 = load i32, i32* %9
%28 = call i32 @EVP_DigestVerifyFinal(i32* %25, i8* %26, i32 %27)
store i32 %28, i32* %13
%29 = load i32*, i32** %11
%30 = call i32 @EVP_PKEY_free(i32* %29)
%31 = load i32*, i32** %12
%32 = call i32 @EVP_MD_CTX_destroy(i32* %31)
%33 = load i32, i32* %13
%34 = icmp sgt i32 %33, 0
%35 = zext i1 %34 to i32
ret i32 %35
}

LLVM IR (-O2):
define i32 @verifySignature(i8* %0, i32 %1, i8* %2, i32 %3, i32* %4) {
%6 = tail call i32* @X509_get_pubkey(i32* %4)
%7 = tail call i32* (...) @EVP_MD_CTX_create()
%8 = tail call i32 (...) @EVP_sha256()
%9 = tail call i32 @EVP_DigestVerifyInit(i32* %7, i32* null, i32 %8, i32* null, i32* %6)
%10 = tail call i32 @EVP_DigestVerifyUpdate(i32* %7, i8* %0, i32 %1)
%11 = tail call i32 @EVP_DigestVerifyFinal(i32* %7, i8* %2, i32 %3)
%12 = tail call i32 @EVP_PKEY_free(i32* %6)
%13 = tail call i32 @EVP_MD_CTX_destroy(i32* %7)
%14 = icmp sgt i32 %11, 0
%15 = zext i1 %14 to i32
ret i32 %15
}