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

/* Variables and functions */
int EVP_DigestSignFinal(int /*<<< orphan*/ *, unsigned char *, size_t *);
int EVP_DigestSignInit(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                       int /*<<< orphan*/, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
int EVP_DigestSignUpdate(int /*<<< orphan*/ *, void *, size_t);
int /*<<< orphan*/ *EVP_MD_CTX_create();
int /*<<< orphan*/ EVP_MD_CTX_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_sha224();
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(size_t);
int PICA_do_signature(EVP_PKEY *privkey, void **datapointers,
                      size_t *datalengths, unsigned char **sig,
                      size_t *siglen) {
  int ret = 0;
  EVP_MD_CTX *mdctx = NULL;
  mdctx = EVP_MD_CTX_create();
  if (!mdctx)
    return 0;
  ret = EVP_DigestSignInit(mdctx, NULL, EVP_sha224(), NULL, privkey);
  if (ret != 1)
    goto sig_exit;
  while (*datapointers) {
    ret = EVP_DigestSignUpdate(mdctx, *datapointers, *datalengths);
    if (ret != 1) {
      goto sig_exit;
      break;
    }
    datapointers++;
    datalengths++;
  }
  ret = EVP_DigestSignFinal(mdctx, NULL, (size_t *)siglen);
  if (ret != 1)
    goto sig_exit;
  *sig = (unsigned char *)malloc(*siglen);
  if (!*sig) {
    ret = 0;
    goto sig_exit;
  }
  ret = EVP_DigestSignFinal(mdctx, *sig, (size_t *)siglen);
  if (ret == 1)
    ret = 1;
  else
    free(*sig);
sig_exit:
  EVP_MD_CTX_destroy(mdctx);
  return ret;
}


x86 assembly (clang, -O0):
<PICA_do_signature>:
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)
movl   $0x0,-0x34(%rbp)
movq   $0x0,-0x40(%rbp)
mov    $0x0,%al
call   10a0 <EVP_MD_CTX_create@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11cd <PICA_do_signature+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    12fd <PICA_do_signature+0x17d>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1040 <EVP_sha224@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
mov    -0x10(%rbp),%r8
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
call   1050 <EVP_DigestSignInit@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
je     1204 <PICA_do_signature+0x84>
jmp    12ee <PICA_do_signature+0x16e>
jmp    1209 <PICA_do_signature+0x89>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     125d <PICA_do_signature+0xdd>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
call   1090 <EVP_DigestSignUpdate@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
je     1240 <PICA_do_signature+0xc0>
jmp    12ee <PICA_do_signature+0x16e>
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    1209 <PICA_do_signature+0x89>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <EVP_DigestSignFinal@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
je     1280 <PICA_do_signature+0x100>
jmp    12ee <PICA_do_signature+0x16e>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12b0 <PICA_do_signature+0x130>
movl   $0x0,-0x34(%rbp)
jmp    12ee <PICA_do_signature+0x16e>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <EVP_DigestSignFinal@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
jne    12dd <PICA_do_signature+0x15d>
movl   $0x1,-0x34(%rbp)
jmp    12e9 <PICA_do_signature+0x169>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
jmp    12ee <PICA_do_signature+0x16e>
mov    -0x40(%rbp),%rdi
call   1080 <EVP_MD_CTX_destroy@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PICA_do_signature>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,(%rsp)
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   10a0 <EVP_MD_CTX_create@plt>
test   %rax,%rax
je     1273 <PICA_do_signature+0xf3>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <EVP_sha224@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1050 <EVP_DigestSignInit@plt>
mov    %eax,%ebp
cmp    $0x1,%eax
jne    126b <PICA_do_signature+0xeb>
mov    %rbx,%r14
mov    0x0(%r13),%rsi
test   %rsi,%rsi
je     120f <PICA_do_signature+0x8f>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r12,%rbx,1),%rdx
mov    %r15,%rdi
call   1090 <EVP_DigestSignUpdate@plt>
cmp    $0x1,%eax
jne    1269 <PICA_do_signature+0xe9>
mov    0x8(%r13,%rbx,1),%rsi
add    $0x8,%rbx
test   %rsi,%rsi
jne    11f0 <PICA_do_signature+0x70>
mov    %r15,%rdi
xor    %esi,%esi
mov    (%rsp),%rdx
call   1060 <EVP_DigestSignFinal@plt>
mov    %eax,%ebp
cmp    $0x1,%eax
jne    126b <PICA_do_signature+0xeb>
mov    %r14,%rbx
mov    (%rsp),%rax
mov    (%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,(%r14)
mov    $0x0,%ebp
test   %rax,%rax
je     126b <PICA_do_signature+0xeb>
mov    %r15,%rdi
mov    %rax,%rsi
mov    (%rsp),%rdx
call   1060 <EVP_DigestSignFinal@plt>
mov    $0x1,%ebp
cmp    $0x1,%eax
je     126b <PICA_do_signature+0xeb>
mov    %eax,%r14d
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    %r14d,%ebp
jmp    126b <PICA_do_signature+0xeb>
mov    %eax,%ebp
mov    %r15,%rdi
call   1080 <EVP_MD_CTX_destroy@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @PICA_do_signature(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 0, i32* %12
store i32* null, i32** %13
%14 = call i32* (...) @EVP_MD_CTX_create()
store i32* %14, i32** %13
%15 = load i32*, i32** %13
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %5
store i32 0, i32* %6
br label %81
18: ; preds = %5
%19 = load i32*, i32** %13
%20 = call i32 (...) @EVP_sha224()
%21 = load i32*, i32** %7
%22 = call i32 @EVP_DigestSignInit(i32* %19, i32* null, i32 %20, i32* null, i32* %21)
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %26
25: ; preds = %18
br label %77
26: ; preds = %18
br label %27
27: ; preds = %41, %26
%28 = load i8**, i8*** %8
%29 = load i8*, i8** %28
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %46
31: ; preds = %27
%32 = load i32*, i32** %13
%33 = load i8**, i8*** %8
%34 = load i8*, i8** %33
%35 = load i64*, i64** %9
%36 = load i64, i64* %35
%37 = call i32 @EVP_DigestSignUpdate(i32* %32, i8* %34, i64 %36)
store i32 %37, i32* %12
%38 = load i32, i32* %12
%39 = icmp ne i32 %38, 1
br i1 %39, label %40, label %41
40: ; preds = %31
br label %77
41: ; preds = %31
%42 = load i8**, i8*** %8
%43 = getelementptr inbounds i8*, i8** %42, i32 1
store i8** %43, i8*** %8
%44 = load i64*, i64** %9
%45 = getelementptr inbounds i64, i64* %44, i32 1
store i64* %45, i64** %9
br label %27
46: ; preds = %27
%47 = load i32*, i32** %13
%48 = load i64*, i64** %11
%49 = call i32 @EVP_DigestSignFinal(i32* %47, i8* null, i64* %48)
store i32 %49, i32* %12
%50 = load i32, i32* %12
%51 = icmp ne i32 %50, 1
br i1 %51, label %52, label %53
52: ; preds = %46
br label %77
53: ; preds = %46
%54 = load i64*, i64** %11
%55 = load i64, i64* %54
%56 = call i64 @malloc(i64 %55)
%57 = inttoptr i64 %56 to i8*
%58 = load i8**, i8*** %10
store i8* %57, i8** %58
%59 = load i8**, i8*** %10
%60 = load i8*, i8** %59
%61 = icmp ne i8* %60, null
br i1 %61, label %63, label %62
62: ; preds = %53
store i32 0, i32* %12
br label %77
63: ; preds = %53
%64 = load i32*, i32** %13
%65 = load i8**, i8*** %10
%66 = load i8*, i8** %65
%67 = load i64*, i64** %11
%68 = call i32 @EVP_DigestSignFinal(i32* %64, i8* %66, i64* %67)
store i32 %68, i32* %12
%69 = load i32, i32* %12
%70 = icmp eq i32 %69, 1
br i1 %70, label %71, label %72
71: ; preds = %63
store i32 1, i32* %12
br label %76
72: ; preds = %63
%73 = load i8**, i8*** %10
%74 = load i8*, i8** %73
%75 = call i32 @free(i8* %74)
br label %76
76: ; preds = %72, %71
br label %77
77: ; preds = %76, %62, %52, %40, %25
%78 = load i32*, i32** %13
%79 = call i32 @EVP_MD_CTX_destroy(i32* %78)
%80 = load i32, i32* %12
store i32 %80, i32* %6
br label %81
81: ; preds = %77, %17
%82 = load i32, i32* %6
ret i32 %82
}

LLVM IR (-O2):
define i32 @PICA_do_signature(i32* %0, i8** nocapture %1, i64* nocapture %2, i8** nocapture %3, i64* %4) {
%6 = tail call i32* (...) @EVP_MD_CTX_create()
%7 = icmp eq i32* %6, null
br i1 %7, label %44, label %8
8: ; preds = %5
%9 = tail call i32 (...) @EVP_sha224()
%10 = tail call i32 @EVP_DigestSignInit(i32* %6, i32* null, i32 %9, i32* null, i32* %0)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %41
12: ; preds = %8
%13 = load i8*, i8** %1
%14 = icmp eq i8* %13, null
br i1 %14, label %27, label %15
15: ; preds = %12, %22
%16 = phi i8* [ %25, %22 ], [ %13, %12 ]
%17 = phi i8** [ %23, %22 ], [ %1, %12 ]
%18 = phi i64* [ %24, %22 ], [ %2, %12 ]
%19 = load i64, i64* %18
%20 = tail call i32 @EVP_DigestSignUpdate(i32* %6, i8* %16, i64 %19)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %41
22: ; preds = %15
%23 = getelementptr inbounds i8*, i8** %17, i64 1
%24 = getelementptr inbounds i64, i64* %18, i64 1
%25 = load i8*, i8** %23
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %15
27: ; preds = %22, %12
%28 = tail call i32 @EVP_DigestSignFinal(i32* %6, i8* null, i64* %4)
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %41
30: ; preds = %27
%31 = load i64, i64* %4
%32 = tail call i64 @malloc(i64 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %3
%34 = icmp eq i64 %32, 0
br i1 %34, label %41, label %35
35: ; preds = %30
%36 = tail call i32 @EVP_DigestSignFinal(i32* %6, i8* %33, i64* %4)
%37 = icmp eq i32 %36, 1
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = load i8*, i8** %3
%40 = tail call i32 @free(i8* %39)
br label %41
41: ; preds = %15, %35, %30, %38, %27, %8
%42 = phi i32 [ %10, %8 ], [ %28, %27 ], [ %36, %38 ], [ 0, %30 ], [ 1, %35 ], [ %20, %15 ]
%43 = tail call i32 @EVP_MD_CTX_destroy(i32* %6)
br label %44
44: ; preds = %5, %41
%45 = phi i32 [ %42, %41 ], [ 0, %5 ]
ret i32 %45
}