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

/* Variables and functions */
int /*<<< orphan*/ EVP_DigestFinal_ex(int /*<<< orphan*/ *, void *, int *);
int /*<<< orphan*/ EVP_DigestInit_ex(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ const *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DigestUpdate(int /*<<< orphan*/ *, void const *, size_t);
int /*<<< orphan*/ *EVP_MD_CTX_create();
int /*<<< orphan*/ EVP_MD_CTX_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_get_digestbyname(char const *);
void *sha256(const void *in, size_t inlen, void *md, int mdlen) {
  EVP_MD_CTX *mdctx;
  const EVP_MD *evpmd;
  const char digestname[10] = "SHA256";
  mdctx = EVP_MD_CTX_create();
  evpmd = EVP_get_digestbyname(digestname);
  EVP_DigestInit_ex(mdctx, evpmd, NULL);
  EVP_DigestUpdate(mdctx, in, inlen);
  EVP_DigestFinal_ex(mdctx, md, &mdlen);
  EVP_MD_CTX_destroy(mdctx);
  return md;
}


x86 assembly (clang, -O0):
<sha256>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    0xe82(%rip),%rax
mov    %rax,-0x3a(%rbp)
mov    0xe7f(%rip),%ax
mov    %ax,-0x32(%rbp)
mov    $0x0,%al
call   1080 <EVP_MD_CTX_create@plt>
mov    %rax,-0x28(%rbp)
lea    -0x3a(%rbp),%rdi
call   1070 <EVP_get_digestbyname@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <EVP_DigestInit_ex@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <EVP_DigestUpdate@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1030 <EVP_DigestFinal_ex@plt>
mov    -0x28(%rbp),%rdi
call   1060 <EVP_MD_CTX_destroy@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha256>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %ecx,0x4(%rsp)
movabs $0x363532414853,%rax
mov    %rax,0x8(%rsp)
movw   $0x0,0x10(%rsp)
xor    %eax,%eax
call   1080 <EVP_MD_CTX_create@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
call   1070 <EVP_get_digestbyname@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %edx,%edx
call   1040 <EVP_DigestInit_ex@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <EVP_DigestUpdate@plt>
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <EVP_DigestFinal_ex@plt>
mov    %rbx,%rdi
call   1060 <EVP_MD_CTX_destroy@plt>
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.sha256.digestname = [10 x i8] c"SHA256\00\00\00\00"
define i8* @sha256(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca [10 x i8]
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%12 = bitcast [10 x i8]* %11 to i8*
%13 = call i32* (...) @EVP_MD_CTX_create()
store i32* %13, i32** %9
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%15 = call i32* @EVP_get_digestbyname(i8* %14)
store i32* %15, i32** %10
%16 = load i32*, i32** %9
%17 = load i32*, i32** %10
%18 = call i32 @EVP_DigestInit_ex(i32* %16, i32* %17, i32* null)
%19 = load i32*, i32** %9
%20 = load i8*, i8** %5
%21 = load i64, i64* %6
%22 = call i32 @EVP_DigestUpdate(i32* %19, i8* %20, i64 %21)
%23 = load i32*, i32** %9
%24 = load i8*, i8** %7
%25 = call i32 @EVP_DigestFinal_ex(i32* %23, i8* %24, i32* %8)
%26 = load i32*, i32** %9
%27 = call i32 @EVP_MD_CTX_destroy(i32* %26)
%28 = load i8*, i8** %7
ret i8* %28
}

LLVM IR (-O2):
@__const.sha256.digestname = [10 x i8] c"SHA256\00\00\00\00"
define i8* @sha256(i8* %0, i64 %1, i8* returned %2, i32 %3) {
%5 = alloca i32
%6 = alloca [10 x i8]
store i32 %3, i32* %5
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%8 = tail call i32* (...) @EVP_MD_CTX_create()
%9 = call i32* @EVP_get_digestbyname(i8* %7)
%10 = call i32 @EVP_DigestInit_ex(i32* %8, i32* %9, i32* null)
%11 = call i32 @EVP_DigestUpdate(i32* %8, i8* %0, i64 %1)
%12 = call i32 @EVP_DigestFinal_ex(i32* %8, i8* %2, i32* %5)
%13 = call i32 @EVP_MD_CTX_destroy(i32* %8)
ret i8* %2
}