#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*/ PEMerror(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) {
    PEMerror(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    11b6 <PEM_SignFinal+0x56>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1040 <PEMerror@plt>
jmp    11fd <PEM_SignFinal+0x9d>
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     11da <PEM_SignFinal+0x7a>
jmp    11fd <PEM_SignFinal+0x9d>
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   1050 <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    11cf <PEM_SignFinal+0x6f>
mov    0x4(%rsp),%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <EVP_EncodeBlock@plt>
mov    %eax,(%r14)
mov    $0x1,%ebp
jmp    11d1 <PEM_SignFinal+0x71>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1040 <PEMerror@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1050 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@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 %22
19: ; preds = %4
%20 = load i32, i32* @ERR_R_MALLOC_FAILURE
%21 = call i32 @PEMerror(i32 %20)
br label %36
22: ; preds = %4
%23 = load i32*, i32** %5
%24 = load i8*, i8** %9
%25 = load i32*, i32** %8
%26 = call i64 @EVP_SignFinal(i32* %23, i8* %24, i32* %12, i32* %25)
%27 = icmp sle i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
br label %36
29: ; preds = %22
%30 = load i8*, i8** %6
%31 = load i8*, i8** %9
%32 = load i32, i32* %12
%33 = call i32 @EVP_EncodeBlock(i8* %30, i8* %31, i32 %32)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = load i32*, i32** %7
store i32 %34, i32* %35
store i32 1, i32* %11
br label %36
36: ; preds = %29, %28, %19
%37 = load i8*, i8** %9
%38 = call i32 @free(i8* %37)
%39 = load i32, i32* %11
ret i32 %39
}

LLVM IR (-O2):
@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 %14
11: ; preds = %4
%12 = load i32, i32* @ERR_R_MALLOC_FAILURE
%13 = tail call i32 @PEMerror(i32 %12)
br label %20
14: ; preds = %4
%15 = call i64 @EVP_SignFinal(i32* %0, i8* %9, i32* %5, i32* %3)
%16 = icmp slt i64 %15, 1
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i32, i32* %5
%19 = call i32 @EVP_EncodeBlock(i8* %1, i8* %9, i32 %18)
store i32 %19, i32* %2
br label %20
20: ; preds = %14, %17, %11
%21 = phi i32 [ 0, %11 ], [ 0, %14 ], [ 1, %17 ]
%22 = call i32 @free(i8* %9)
ret i32 %21
}