#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*/ *EVP_PKEY_CTX_get0_pkey(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_get0(int /*<<< orphan*/ *);
int /*<<< orphan*/ *gost_do_sign(unsigned char const *, size_t,
                                 int /*<<< orphan*/);
int pack_sign_cp(int /*<<< orphan*/ *, int, unsigned char *, size_t *);
int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
                        const unsigned char *tbs, size_t tbs_len) {
  DSA_SIG *unpacked_sig = NULL;
  EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
  if (!siglen)
    return 0;
  if (!sig) {
    *siglen = 64;
    return 1;
  }
  unpacked_sig = gost_do_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
  if (!unpacked_sig) {
    return 0;
  }
  return pack_sign_cp(unpacked_sig, 32, sig, siglen);
}


x86 assembly (clang, -O0):
<pkey_gost94_cp_sign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <EVP_PKEY_CTX_get0_pkey@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1188 <pkey_gost94_cp_sign+0x48>
movl   $0x0,-0x4(%rbp)
jmp    1206 <pkey_gost94_cp_sign+0xc6>
cmpq   $0x0,-0x18(%rbp)
jne    11aa <pkey_gost94_cp_sign+0x6a>
mov    -0x20(%rbp),%rax
movq   $0x40,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1206 <pkey_gost94_cp_sign+0xc6>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <EVP_PKEY_get0@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <gost_do_sign@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11ed <pkey_gost94_cp_sign+0xad>
movl   $0x0,-0x4(%rbp)
jmp    1206 <pkey_gost94_cp_sign+0xc6>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x20,%esi
call   1050 <pack_sign_cp@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkey_gost94_cp_sign>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r15
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %rsi,%r14
call   1060 <EVP_PKEY_CTX_get0_pkey@plt>
xor    %ebp,%ebp
test   %rbx,%rbx
je     11a6 <pkey_gost94_cp_sign+0x66>
test   %r14,%r14
je     119a <pkey_gost94_cp_sign+0x5a>
mov    %rax,%rdi
call   1040 <EVP_PKEY_get0@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <gost_do_sign@plt>
test   %rax,%rax
je     11a6 <pkey_gost94_cp_sign+0x66>
mov    %rax,%rdi
mov    $0x20,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <pack_sign_cp@plt>
movq   $0x40,(%rbx)
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @pkey_gost94_cp_sign(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*
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* null, i32** %12
%14 = load i32*, i32** %7
%15 = call i32* @EVP_PKEY_CTX_get0_pkey(i32* %14)
store i32* %15, i32** %13
%16 = load i64*, i64** %9
%17 = icmp ne i64* %16, null
br i1 %17, label %19, label %18
18: ; preds = %5
store i32 0, i32* %6
br label %38
19: ; preds = %5
%20 = load i8*, i8** %8
%21 = icmp ne i8* %20, null
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = load i64*, i64** %9
store i64 64, i64* %23
store i32 1, i32* %6
br label %38
24: ; preds = %19
%25 = load i8*, i8** %10
%26 = load i64, i64* %11
%27 = load i32*, i32** %13
%28 = call i32 @EVP_PKEY_get0(i32* %27)
%29 = call i32* @gost_do_sign(i8* %25, i64 %26, i32 %28)
store i32* %29, i32** %12
%30 = load i32*, i32** %12
%31 = icmp ne i32* %30, null
br i1 %31, label %33, label %32
32: ; preds = %24
store i32 0, i32* %6
br label %38
33: ; preds = %24
%34 = load i32*, i32** %12
%35 = load i8*, i8** %8
%36 = load i64*, i64** %9
%37 = call i32 @pack_sign_cp(i32* %34, i32 32, i8* %35, i64* %36)
store i32 %37, i32* %6
br label %38
38: ; preds = %33, %32, %22, %18
%39 = load i32, i32* %6
ret i32 %39
}

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