#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*/ *, unsigned char *,
                                      unsigned int *);
int /*<<< orphan*/ EVP_DigestInit_ex(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ const *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DigestUpdate(int /*<<< orphan*/ *, unsigned char *,
                                    unsigned int);
int /*<<< orphan*/ EVP_MD_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_MD_CTX_new();
int /*<<< orphan*/ *EVP_get_digestbyname(char *);
int /*<<< orphan*/ assert(int);
void sha256(unsigned char *buffer, unsigned int buffer_size,
            unsigned char *output) {
  EVP_MD_CTX *mdctx;
  const EVP_MD *md;
  unsigned int md_len;
  md = EVP_get_digestbyname("SHA256");
  assert(md != NULL);
  mdctx = EVP_MD_CTX_new();
  EVP_DigestInit_ex(mdctx, md, NULL);
  EVP_DigestUpdate(mdctx, buffer, buffer_size);
  EVP_DigestFinal_ex(mdctx, output, &md_len);
  EVP_MD_CTX_free(mdctx);
  assert(md_len == 32);
}


x86 assembly (clang, -O0):
<sha256>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe76(%rip),%rdi
call   1080 <EVP_get_digestbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x0,%al
call   1090 <EVP_MD_CTX_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <EVP_DigestInit_ex@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1070 <EVP_DigestUpdate@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1040 <EVP_DigestFinal_ex@plt>
mov    -0x20(%rbp),%rdi
call   1030 <EVP_MD_CTX_free@plt>
cmpl   $0x20,-0x2c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha256>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%r12
lea    0xe74(%rip),%rdi
call   1080 <EVP_get_digestbyname@plt>
mov    %rax,%rbp
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
xor    %eax,%eax
call   1090 <EVP_MD_CTX_new@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
xor    %edx,%edx
call   1050 <EVP_DigestInit_ex@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1070 <EVP_DigestUpdate@plt>
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <EVP_DigestFinal_ex@plt>
mov    %rbx,%rdi
call   1030 <EVP_MD_CTX_free@plt>
xor    %edi,%edi
cmpl   $0x20,0xc(%rsp)
sete   %dil
call   1060 <assert@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"SHA256\00"
define void @sha256(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = call i32* @EVP_get_digestbyname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp ne i32* %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = call i32* (...) @EVP_MD_CTX_new()
store i32* %15, i32** %7
%16 = load i32*, i32** %7
%17 = load i32*, i32** %8
%18 = call i32 @EVP_DigestInit_ex(i32* %16, i32* %17, i32* null)
%19 = load i32*, i32** %7
%20 = load i8*, i8** %4
%21 = load i32, i32* %5
%22 = call i32 @EVP_DigestUpdate(i32* %19, i8* %20, i32 %21)
%23 = load i32*, i32** %7
%24 = load i8*, i8** %6
%25 = call i32 @EVP_DigestFinal_ex(i32* %23, i8* %24, i32* %9)
%26 = load i32*, i32** %7
%27 = call i32 @EVP_MD_CTX_free(i32* %26)
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, 32
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"SHA256\00"
define void @sha256(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32* @EVP_get_digestbyname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = icmp ne i32* %6, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = tail call i32* (...) @EVP_MD_CTX_new()
%11 = tail call i32 @EVP_DigestInit_ex(i32* %10, i32* %6, i32* null)
%12 = tail call i32 @EVP_DigestUpdate(i32* %10, i8* %0, i32 %1)
%13 = call i32 @EVP_DigestFinal_ex(i32* %10, i8* %2, i32* %4)
%14 = call i32 @EVP_MD_CTX_free(i32* %10)
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 32
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
ret void
}