#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;

/* Variables and functions */
int /*<<< orphan*/ *EVP_PKEY_CTX_new(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
scalar_t__ EVP_PKEY_CTX_set_rsa_padding(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/);
scalar_t__ EVP_PKEY_CTX_set_signature_md(int /*<<< orphan*/ *,
                                         int /*<<< orphan*/);
scalar_t__ EVP_PKEY_sign(int /*<<< orphan*/ *, unsigned char *, size_t *,
                         unsigned char *, size_t);
scalar_t__ EVP_PKEY_sign_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_sha256();
unsigned char *OPENSSL_malloc(size_t);
int /*<<< orphan*/ RSA_PKCS1_PADDING;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fwrite(unsigned char *, size_t, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void sign_hashed_data(EVP_PKEY *signing_key, unsigned char *md, size_t mdlen) {
  EVP_PKEY_CTX *ctx;
  unsigned char *sig;
  size_t siglen;
  ctx = EVP_PKEY_CTX_new(signing_key, NULL);
  if ((!ctx) || (EVP_PKEY_sign_init(ctx) <= 0) ||
      (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0) ||
      (EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) <= 0)) {
    fprintf(stderr, "Failure establishing ctx for signature\n");
    exit(1);
  }
  if (EVP_PKEY_sign(ctx, NULL, &siglen, md, mdlen) <= 0) {
    fprintf(stderr, "Unable to determine buffer length for signature\n");
    exit(1);
  }
  sig = OPENSSL_malloc(siglen);
  if (!sig) {
    fprintf(stderr, "Malloc failed\n");
    exit(1);
  }
  if (EVP_PKEY_sign(ctx, sig, &siglen, md, mdlen) <= 0) {
    fprintf(stderr, "Signature error\n");
    exit(1);
  }
  fwrite(sig, siglen, 1, stdout);
}


x86 assembly (clang, -O0):
<sign_hashed_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <EVP_PKEY_CTX_new@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1223 <sign_hashed_data+0x83>
mov    -0x20(%rbp),%rdi
call   1070 <EVP_PKEY_sign_init@plt>
cmp    $0x0,%rax
jle    1223 <sign_hashed_data+0x83>
mov    -0x20(%rbp),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
call   1090 <EVP_PKEY_CTX_set_rsa_padding@plt>
cmp    $0x0,%rax
jle    1223 <sign_hashed_data+0x83>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   10c0 <EVP_sha256@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1040 <EVP_PKEY_CTX_set_signature_md@plt>
cmp    $0x0,%rax
jg     1242 <sign_hashed_data+0xa2>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
xor    %eax,%eax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
call   1080 <EVP_PKEY_sign@plt>
cmp    $0x0,%rax
jg     1284 <sign_hashed_data+0xe4>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rdi
call   1060 <OPENSSL_malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12bb <sign_hashed_data+0x11b>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    -0x30(%rbp),%rdx
call   1080 <EVP_PKEY_sign@plt>
cmp    $0x0,%rax
jg     12fd <sign_hashed_data+0x15d>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2ccc(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   10b0 <fwrite@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sign_hashed_data>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
xor    %esi,%esi
call   1030 <EVP_PKEY_CTX_new@plt>
test   %rax,%rax
je     1266 <sign_hashed_data+0xc6>
mov    %rax,%r12
mov    %rax,%rdi
call   1070 <EVP_PKEY_sign_init@plt>
test   %rax,%rax
jle    1266 <sign_hashed_data+0xc6>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1090 <EVP_PKEY_CTX_set_rsa_padding@plt>
test   %rax,%rax
jle    1266 <sign_hashed_data+0xc6>
xor    %eax,%eax
call   10c0 <EVP_sha256@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1040 <EVP_PKEY_CTX_set_signature_md@plt>
test   %rax,%rax
jle    1266 <sign_hashed_data+0xc6>
mov    %rsp,%rdx
mov    %r12,%rdi
xor    %esi,%esi
mov    %r15,%rcx
mov    %r14,%r8
call   1080 <EVP_PKEY_sign@plt>
test   %rax,%rax
jle    1278 <sign_hashed_data+0xd8>
mov    (%rsp),%rdi
call   1060 <OPENSSL_malloc@plt>
test   %rax,%rax
je     128a <sign_hashed_data+0xea>
mov    %rax,%rbx
mov    %rsp,%rdx
mov    %r12,%rdi
mov    %rax,%rsi
mov    %r15,%rcx
mov    %r14,%r8
call   1080 <EVP_PKEY_sign@plt>
test   %rax,%rax
jle    129c <sign_hashed_data+0xfc>
mov    (%rsp),%rsi
mov    0x2d8d(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xd8a(%rip),%rsi
jmp    12ac <sign_hashed_data+0x10c>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
jmp    12ac <sign_hashed_data+0x10c>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
jmp    12ac <sign_hashed_data+0x10c>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@RSA_PKCS1_PADDING = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"Failure establishing ctx for signature\0A\00"
@.str.1 = [49 x i8] c"Unable to determine buffer length for signature\0A\00"
@.str.2 = [15 x i8] c"Malloc failed\0A\00"
@.str.3 = [17 x i8] c"Signature error\0A\00"
@stdout = global i32 0
define void @sign_hashed_data(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
store i32* %0, i32** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i32*, i32** %4
%11 = call i32* @EVP_PKEY_CTX_new(i32* %10, i32* null)
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %28
14: ; preds = %3
%15 = load i32*, i32** %7
%16 = call i64 @EVP_PKEY_sign_init(i32* %15)
%17 = icmp sle i64 %16, 0
br i1 %17, label %28, label %18
18: ; preds = %14
%19 = load i32*, i32** %7
%20 = load i32, i32* @RSA_PKCS1_PADDING
%21 = call i64 @EVP_PKEY_CTX_set_rsa_padding(i32* %19, i32 %20)
%22 = icmp sle i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = load i32*, i32** %7
%25 = call i32 (...) @EVP_sha256()
%26 = call i64 @EVP_PKEY_CTX_set_signature_md(i32* %24, i32 %25)
%27 = icmp sle i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %23, %18, %14, %3
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %23
%33 = load i32*, i32** %7
%34 = load i8*, i8** %5
%35 = load i64, i64* %6
%36 = call i64 @EVP_PKEY_sign(i32* %33, i8* null, i64* %9, i8* %34, i64 %35)
%37 = icmp sle i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %32
%43 = load i64, i64* %9
%44 = call i8* @OPENSSL_malloc(i64 %43)
store i8* %44, i8** %8
%45 = load i8*, i8** %8
%46 = icmp ne i8* %45, null
br i1 %46, label %51, label %47
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %42
%52 = load i32*, i32** %7
%53 = load i8*, i8** %8
%54 = load i8*, i8** %5
%55 = load i64, i64* %6
%56 = call i64 @EVP_PKEY_sign(i32* %52, i8* %53, i64* %9, i8* %54, i64 %55)
%57 = icmp sle i64 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %51
%59 = load i32, i32* @stderr
%60 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %51
%63 = load i8*, i8** %8
%64 = load i64, i64* %9
%65 = load i32, i32* @stdout
%66 = call i32 @fwrite(i8* %63, i64 %64, i32 1, i32 %65)
ret void
}

LLVM IR (-O2):
@RSA_PKCS1_PADDING = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"Failure establishing ctx for signature\0A\00"
@.str.1 = [49 x i8] c"Unable to determine buffer length for signature\0A\00"
@.str.2 = [15 x i8] c"Malloc failed\0A\00"
@.str.3 = [17 x i8] c"Signature error\0A\00"
@stdout = global i32 0
define void @sign_hashed_data(i32* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = tail call i32* @EVP_PKEY_CTX_new(i32* %0, i32* null)
%7 = icmp eq i32* %6, null
br i1 %7, label %19, label %8
8: ; preds = %3
%9 = tail call i64 @EVP_PKEY_sign_init(i32* %6)
%10 = icmp slt i64 %9, 1
br i1 %10, label %19, label %11
11: ; preds = %8
%12 = load i32, i32* @RSA_PKCS1_PADDING
%13 = tail call i64 @EVP_PKEY_CTX_set_rsa_padding(i32* %6, i32 %12)
%14 = icmp slt i64 %13, 1
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = tail call i32 (...) @EVP_sha256()
%17 = tail call i64 @EVP_PKEY_CTX_set_signature_md(i32* %6, i32 %16)
%18 = icmp slt i64 %17, 1
br i1 %18, label %19, label %23
19: ; preds = %15, %11, %8, %3
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = call i64 @EVP_PKEY_sign(i32* %6, i8* null, i64* %4, i8* %1, i64 %2)
%25 = icmp slt i64 %24, 1
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23
%31 = load i64, i64* %4
%32 = call i8* @OPENSSL_malloc(i64 %31)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = call i64 @EVP_PKEY_sign(i32* %6, i8* %32, i64* %4, i8* %1, i64 %2)
%40 = icmp slt i64 %39, 1
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %38
%46 = load i64, i64* %4
%47 = load i32, i32* @stdout
%48 = call i32 @fwrite(i8* %32, i64 %46, i32 1, i32 %47)
ret void
}