#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 /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int EVP_EncodeBlock(unsigned char *, unsigned char *, unsigned int);
scalar_t__ EVP_PKEY_size(int /*<<< orphan*/ *);
scalar_t__ EVP_SignFinal(int /*<<< orphan*/ *, unsigned char *, unsigned int *,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ PEM_F_PEM_SIGNFINAL;
int /*<<< orphan*/ PEMerr(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(scalar_t__);
int PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, unsigned int *siglen,
                  EVP_PKEY *pkey) {
  unsigned char *m;
  int i, ret = 0;
  unsigned int m_len;
  m = malloc(EVP_PKEY_size(pkey) + 2);
  if (m == NULL) {
    PEMerr(PEM_F_PEM_SIGNFINAL, ERR_R_MALLOC_FAILURE);
    goto err;
  }
  if (EVP_SignFinal(ctx, m, &m_len, pkey) <= 0)
    goto err;
  i = EVP_EncodeBlock(sigret, m, m_len);
  *siglen = i;
  ret = 1;
err:
  free(m);
  return (ret);
}


x86 assembly (clang, -O0):
<PEM_SignFinal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <EVP_PKEY_size@plt>
mov    %rax,%rdi
add    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bf <PEM_SignFinal+0x5f>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
jmp    1206 <PEM_SignFinal+0xa6>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    -0x34(%rbp),%rdx
call   1030 <EVP_SignFinal@plt>
cmp    $0x0,%rax
jg     11e3 <PEM_SignFinal+0x83>
jmp    1206 <PEM_SignFinal+0xa6>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1080 <EVP_EncodeBlock@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x30(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PEM_SignFinal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    %rcx,%rdi
call   1070 <EVP_PKEY_size@plt>
lea    0x2(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c1 <PEM_SignFinal+0x61>
lea    0x4(%rsp),%rdx
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rcx
call   1030 <EVP_SignFinal@plt>
test   %rax,%rax
jle    11d8 <PEM_SignFinal+0x78>
mov    0x4(%rsp),%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <EVP_EncodeBlock@plt>
mov    %eax,(%r14)
mov    $0x1,%ebp
jmp    11da <PEM_SignFinal+0x7a>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
call   1050 <PEMerr@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PEM_F_PEM_SIGNFINAL = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i32 @PEM_SignFinal(i32* %0, i8* %1, i32* %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
store i32 0, i32* %11
%13 = load i32*, i32** %8
%14 = call i64 @EVP_PKEY_size(i32* %13)
%15 = add nsw i64 %14, 2
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %9
%17 = load i8*, i8** %9
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %4
%20 = load i32, i32* @PEM_F_PEM_SIGNFINAL
%21 = load i32, i32* @ERR_R_MALLOC_FAILURE
%22 = call i32 @PEMerr(i32 %20, i32 %21)
br label %37
23: ; preds = %4
%24 = load i32*, i32** %5
%25 = load i8*, i8** %9
%26 = load i32*, i32** %8
%27 = call i64 @EVP_SignFinal(i32* %24, i8* %25, i32* %12, i32* %26)
%28 = icmp sle i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %23
br label %37
30: ; preds = %23
%31 = load i8*, i8** %6
%32 = load i8*, i8** %9
%33 = load i32, i32* %12
%34 = call i32 @EVP_EncodeBlock(i8* %31, i8* %32, i32 %33)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = load i32*, i32** %7
store i32 %35, i32* %36
store i32 1, i32* %11
br label %37
37: ; preds = %30, %29, %19
%38 = load i8*, i8** %9
%39 = call i32 @free(i8* %38)
%40 = load i32, i32* %11
ret i32 %40
}

LLVM IR (-O2):
@PEM_F_PEM_SIGNFINAL = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i32 @PEM_SignFinal(i32* %0, i8* %1, i32* nocapture writeonly %2, i32* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i64 @EVP_PKEY_size(i32* %3)
%8 = add nsw i64 %7, 2
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %4
%12 = load i32, i32* @PEM_F_PEM_SIGNFINAL
%13 = load i32, i32* @ERR_R_MALLOC_FAILURE
%14 = tail call i32 @PEMerr(i32 %12, i32 %13)
br label %21
15: ; preds = %4
%16 = call i64 @EVP_SignFinal(i32* %0, i8* %9, i32* %5, i32* %3)
%17 = icmp slt i64 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i32, i32* %5
%20 = call i32 @EVP_EncodeBlock(i8* %1, i8* %9, i32 %19)
store i32 %20, i32* %2
br label %21
21: ; preds = %15, %18, %11
%22 = phi i32 [ 0, %11 ], [ 0, %15 ], [ 1, %18 ]
%23 = call i32 @free(i8* %9)
ret i32 %22
}