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

/* Variables and functions */
int /*<<< orphan*/ DSA_SIG_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_CTX_get0_pkey(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_get0(int /*<<< orphan*/ *);
int gost2001_do_verify(unsigned char const *, size_t, int /*<<< orphan*/ *,
                       int /*<<< orphan*/);
int /*<<< orphan*/ *unpack_cp_signature(unsigned char const *, size_t);
int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
                          size_t siglen, const unsigned char *tbs,
                          size_t tbs_len) {
  int ok = 0;
  EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
  DSA_SIG *s = unpack_cp_signature(sig, siglen);
  if (!s)
    return 0;
  if (pub_key)
    ok = gost2001_do_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
  DSA_SIG_free(s);
  return ok;
}


x86 assembly (clang, -O0):
<pkey_gost01_cp_verify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <EVP_PKEY_CTX_get0_pkey@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <unpack_cp_signature@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11a8 <pkey_gost01_cp_verify+0x58>
movl   $0x0,-0x4(%rbp)
jmp    11f9 <pkey_gost01_cp_verify+0xa9>
cmpq   $0x0,-0x40(%rbp)
je     11ea <pkey_gost01_cp_verify+0x9a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <EVP_PKEY_get0@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    %eax,%ecx
call   1060 <gost2001_do_verify@plt>
mov    %eax,-0x34(%rbp)
mov    -0x48(%rbp),%rdi
call   1050 <DSA_SIG_free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkey_gost01_cp_verify>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%rbp
call   1070 <EVP_PKEY_CTX_get0_pkey@plt>
mov    %rax,%r12
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1030 <unpack_cp_signature@plt>
test   %rax,%rax
je     11a0 <pkey_gost01_cp_verify+0x50>
mov    %rax,%rbx
test   %r12,%r12
je     11a4 <pkey_gost01_cp_verify+0x54>
mov    %r12,%rdi
call   1040 <EVP_PKEY_get0@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1060 <gost2001_do_verify@plt>
mov    %eax,%ebp
jmp    11a6 <pkey_gost01_cp_verify+0x56>
xor    %ebp,%ebp
jmp    11ae <pkey_gost01_cp_verify+0x5e>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1050 <DSA_SIG_free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @pkey_gost01_cp_verify(i32* %0, i8* %1, i64 %2, i8* %3, i64 %4) {
%6 = tail call i32* @EVP_PKEY_CTX_get0_pkey(i32* %0)
%7 = tail call i32* @unpack_cp_signature(i8* %1, i64 %2)
%8 = icmp eq i32* %7, null
br i1 %8, label %17, label %9
9: ; preds = %5
%10 = icmp eq i32* %6, null
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = tail call i32 @EVP_PKEY_get0(i32* %6)
%13 = tail call i32 @gost2001_do_verify(i8* %3, i64 %4, i32* %7, i32 %12)
br label %14
14: ; preds = %11, %9
%15 = phi i32 [ %13, %11 ], [ 0, %9 ]
%16 = tail call i32 @DSA_SIG_free(i32* %7)
br label %17
17: ; preds = %5, %14
%18 = phi i32 [ %15, %14 ], [ 0, %5 ]
ret i32 %18
}