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

/* Variables and functions */
int EVP_DigestInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                      int /*<<< orphan*/ *);
int EVP_DigestSignFinal(int /*<<< orphan*/ *, unsigned char *, size_t *);
int EVP_DigestSignInit(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ const *, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
int EVP_DigestSignUpdate(int /*<<< orphan*/ *, unsigned char const *, size_t);
int /*<<< orphan*/ *EVP_MD_CTX_create();
int /*<<< orphan*/ EVP_MD_CTX_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_get_digestbyname(char *);
unsigned char *OPENSSL_malloc(size_t);
int sign_it(const unsigned char *msg, size_t mlen, unsigned char **sig,
            size_t *slen, EVP_PKEY *pkey) {
  int result = -1;
  *sig = NULL;
  *slen = 0;
  EVP_MD_CTX *ctx = EVP_MD_CTX_create();
  if (ctx == NULL)
    return -1;
  const EVP_MD *md = EVP_get_digestbyname("SHA256");
  if (md == NULL)
    goto cleanup;
  int rc = EVP_DigestInit_ex(ctx, md, NULL);
  if (rc != 1)
    goto cleanup;
  rc = EVP_DigestSignInit(ctx, NULL, md, NULL, pkey);
  if (rc != 1)
    goto cleanup;
  rc = EVP_DigestSignUpdate(ctx, msg, mlen);
  if (rc != 1)
    goto cleanup;
  size_t req = 0;
  rc = EVP_DigestSignFinal(ctx, NULL, &req);
  if (rc != 1 || !(req > 0))
    goto cleanup;
  *sig = OPENSSL_malloc(req);
  if (*sig == NULL)
    goto cleanup;
  *slen = req;
  rc = EVP_DigestSignFinal(ctx, *sig, slen);
  if (rc != 1 || req != *slen)
    goto cleanup;
  result = 0;
cleanup:
  EVP_MD_CTX_destroy(ctx);
  ctx = NULL;
  return result;
}


x86 assembly (clang, -O0):
<sign_it>:
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   $0xffffffff,-0x34(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   10a0 <EVP_MD_CTX_create@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11db <sign_it+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1327 <sign_it+0x1a7>
lea    0xe1e(%rip),%rdi
call   1070 <EVP_get_digestbyname@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11fb <sign_it+0x7b>
jmp    1310 <sign_it+0x190>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <EVP_DigestInit_ex@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x1,-0x4c(%rbp)
je     121e <sign_it+0x9e>
jmp    1310 <sign_it+0x190>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rdx
mov    -0x30(%rbp),%r8
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
call   1030 <EVP_DigestSignInit@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x1,-0x4c(%rbp)
je     1248 <sign_it+0xc8>
jmp    1310 <sign_it+0x190>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1090 <EVP_DigestSignUpdate@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x1,-0x4c(%rbp)
je     126b <sign_it+0xeb>
jmp    1310 <sign_it+0x190>
movq   $0x0,-0x58(%rbp)
mov    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x58(%rbp),%rdx
call   1050 <EVP_DigestSignFinal@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x1,-0x4c(%rbp)
jne    129c <sign_it+0x11c>
cmpq   $0x0,-0x58(%rbp)
ja     12a1 <sign_it+0x121>
jmp    1310 <sign_it+0x190>
mov    -0x58(%rbp),%rdi
call   1080 <OPENSSL_malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12c7 <sign_it+0x147>
jmp    1310 <sign_it+0x190>
mov    -0x58(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <EVP_DigestSignFinal@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x1,-0x4c(%rbp)
jne    1304 <sign_it+0x184>
mov    -0x58(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%rax
je     1309 <sign_it+0x189>
jmp    1310 <sign_it+0x190>
movl   $0x0,-0x34(%rbp)
mov    -0x40(%rbp),%rdi
call   1060 <EVP_MD_CTX_destroy@plt>
movq   $0x0,-0x40(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sign_it>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r12
mov    %rcx,%rbp
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,(%rdx)
movq   $0x0,(%rcx)
xor    %eax,%eax
call   10a0 <EVP_MD_CTX_create@plt>
test   %rax,%rax
je     1298 <sign_it+0x118>
mov    %rax,%r15
mov    %rbx,0x8(%rsp)
mov    %r14,0x10(%rsp)
lea    0xe31(%rip),%rdi
call   1070 <EVP_get_digestbyname@plt>
test   %rax,%rax
je     1289 <sign_it+0x109>
mov    %rax,%r14
mov    %r15,%rdi
mov    %rax,%rsi
xor    %edx,%edx
call   1040 <EVP_DigestInit_ex@plt>
cmp    $0x1,%eax
jne    1289 <sign_it+0x109>
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
mov    %r12,%r8
call   1030 <EVP_DigestSignInit@plt>
cmp    $0x1,%eax
jne    1289 <sign_it+0x109>
mov    %r15,%rdi
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
call   1090 <EVP_DigestSignUpdate@plt>
cmp    $0x1,%eax
jne    1289 <sign_it+0x109>
movq   $0x0,(%rsp)
mov    %rsp,%rdx
mov    %r15,%rdi
xor    %esi,%esi
call   1050 <EVP_DigestSignFinal@plt>
cmp    $0x1,%eax
jne    1289 <sign_it+0x109>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1289 <sign_it+0x109>
call   1080 <OPENSSL_malloc@plt>
mov    %rax,0x0(%r13)
test   %rax,%rax
je     1289 <sign_it+0x109>
mov    (%rsp),%rcx
mov    %rbp,%rbx
mov    %rcx,0x0(%rbp)
mov    %r15,%rdi
mov    %rax,%rsi
mov    %rbp,%rdx
call   1050 <EVP_DigestSignFinal@plt>
cmp    $0x1,%eax
mov    $0xffffffff,%ebp
jne    128e <sign_it+0x10e>
mov    (%rsp),%rax
xor    %ebp,%ebp
cmp    (%rbx),%rax
setne  %bpl
neg    %ebp
jmp    128e <sign_it+0x10e>
mov    $0xffffffff,%ebp
mov    %r15,%rdi
call   1060 <EVP_MD_CTX_destroy@plt>
jmp    129d <sign_it+0x11d>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"SHA256\00"
define i32 @sign_it(i8* %0, i64 %1, i8** %2, i64* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8**
%10 = alloca i64*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i64
store i8* %0, i8** %7
store i64 %1, i64* %8
store i8** %2, i8*** %9
store i64* %3, i64** %10
store i32* %4, i32** %11
store i32 -1, i32* %12
%17 = load i8**, i8*** %9
store i8* null, i8** %17
%18 = load i64*, i64** %10
store i64 0, i64* %18
%19 = call i32* (...) @EVP_MD_CTX_create()
store i32* %19, i32** %13
%20 = load i32*, i32** %13
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %5
store i32 -1, i32* %6
br label %89
23: ; preds = %5
%24 = call i32* @EVP_get_digestbyname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32* %24, i32** %14
%25 = load i32*, i32** %14
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %28
27: ; preds = %23
br label %85
28: ; preds = %23
%29 = load i32*, i32** %13
%30 = load i32*, i32** %14
%31 = call i32 @EVP_DigestInit_ex(i32* %29, i32* %30, i32* null)
store i32 %31, i32* %15
%32 = load i32, i32* %15
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %28
br label %85
35: ; preds = %28
%36 = load i32*, i32** %13
%37 = load i32*, i32** %14
%38 = load i32*, i32** %11
%39 = call i32 @EVP_DigestSignInit(i32* %36, i32* null, i32* %37, i32* null, i32* %38)
store i32 %39, i32* %15
%40 = load i32, i32* %15
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %43
42: ; preds = %35
br label %85
43: ; preds = %35
%44 = load i32*, i32** %13
%45 = load i8*, i8** %7
%46 = load i64, i64* %8
%47 = call i32 @EVP_DigestSignUpdate(i32* %44, i8* %45, i64 %46)
store i32 %47, i32* %15
%48 = load i32, i32* %15
%49 = icmp ne i32 %48, 1
br i1 %49, label %50, label %51
50: ; preds = %43
br label %85
51: ; preds = %43
store i64 0, i64* %16
%52 = load i32*, i32** %13
%53 = call i32 @EVP_DigestSignFinal(i32* %52, i8* null, i64* %16)
store i32 %53, i32* %15
%54 = load i32, i32* %15
%55 = icmp ne i32 %54, 1
br i1 %55, label %59, label %56
56: ; preds = %51
%57 = load i64, i64* %16
%58 = icmp ugt i64 %57, 0
br i1 %58, label %60, label %59
59: ; preds = %56, %51
br label %85
60: ; preds = %56
%61 = load i64, i64* %16
%62 = call i8* @OPENSSL_malloc(i64 %61)
%63 = load i8**, i8*** %9
store i8* %62, i8** %63
%64 = load i8**, i8*** %9
%65 = load i8*, i8** %64
%66 = icmp eq i8* %65, null
br i1 %66, label %67, label %68
67: ; preds = %60
br label %85
68: ; preds = %60
%69 = load i64, i64* %16
%70 = load i64*, i64** %10
store i64 %69, i64* %70
%71 = load i32*, i32** %13
%72 = load i8**, i8*** %9
%73 = load i8*, i8** %72
%74 = load i64*, i64** %10
%75 = call i32 @EVP_DigestSignFinal(i32* %71, i8* %73, i64* %74)
store i32 %75, i32* %15
%76 = load i32, i32* %15
%77 = icmp ne i32 %76, 1
br i1 %77, label %83, label %78
78: ; preds = %68
%79 = load i64, i64* %16
%80 = load i64*, i64** %10
%81 = load i64, i64* %80
%82 = icmp ne i64 %79, %81
br i1 %82, label %83, label %84
83: ; preds = %78, %68
br label %85
84: ; preds = %78
store i32 0, i32* %12
br label %85
85: ; preds = %84, %83, %67, %59, %50, %42, %34, %27
%86 = load i32*, i32** %13
%87 = call i32 @EVP_MD_CTX_destroy(i32* %86)
store i32* null, i32** %13
%88 = load i32, i32* %12
store i32 %88, i32* %6
br label %89
89: ; preds = %85, %22
%90 = load i32, i32* %6
ret i32 %90
}

LLVM IR (-O2):
@.str = [7 x i8] c"SHA256\00"
define i32 @sign_it(i8* %0, i64 %1, i8** nocapture %2, i64* %3, i32* %4) {
%6 = alloca i64
store i8* null, i8** %2
store i64 0, i64* %3
%7 = tail call i32* (...) @EVP_MD_CTX_create()
%8 = icmp eq i32* %7, null
br i1 %8, label %42, label %9
9: ; preds = %5
%10 = tail call i32* @EVP_get_digestbyname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %39, label %12
12: ; preds = %9
%13 = tail call i32 @EVP_DigestInit_ex(i32* %7, i32* %10, i32* null)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %39
15: ; preds = %12
%16 = tail call i32 @EVP_DigestSignInit(i32* %7, i32* null, i32* %10, i32* null, i32* %4)
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %39
18: ; preds = %15
%19 = tail call i32 @EVP_DigestSignUpdate(i32* %7, i8* %0, i64 %1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %39
21: ; preds = %18
store i64 0, i64* %6
%22 = call i32 @EVP_DigestSignFinal(i32* %7, i8* null, i64* %6)
%23 = icmp eq i32 %22, 1
%24 = load i64, i64* %6
%25 = icmp ne i64 %24, 0
%26 = select i1 %23, i1 %25, i1 false
br i1 %26, label %27, label %39
27: ; preds = %21
%28 = call i8* @OPENSSL_malloc(i64 %24)
store i8* %28, i8** %2
%29 = icmp eq i8* %28, null
br i1 %29, label %39, label %30
30: ; preds = %27
%31 = load i64, i64* %6
store i64 %31, i64* %3
%32 = call i32 @EVP_DigestSignFinal(i32* %7, i8* %28, i64* %3)
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i64, i64* %6
%36 = load i64, i64* %3
%37 = icmp ne i64 %35, %36
%38 = sext i1 %37 to i32
br label %39
39: ; preds = %34, %30, %27, %21, %18, %15, %12, %9
%40 = phi i32 [ -1, %9 ], [ -1, %12 ], [ -1, %15 ], [ -1, %18 ], [ -1, %27 ], [ -1, %30 ], [ -1, %21 ], [ %38, %34 ]
%41 = call i32 @EVP_MD_CTX_destroy(i32* %7)
br label %42
42: ; preds = %5, %39
%43 = phi i32 [ %40, %39 ], [ -1, %5 ]
ret i32 %43
}