#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(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 const *,
                                    int const);
int EVP_MAX_MD_SIZE;
int /*<<< orphan*/ *EVP_MD_CTX_create();
int /*<<< orphan*/ EVP_MD_CTX_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int const);
int cryptoCalculateHash(unsigned char *hash_buf, const int hash_len,
                        const unsigned char *in_buf, const int in_len,
                        const EVP_MD *hash_func) {
  unsigned char hash[EVP_MAX_MD_SIZE];
  int len;
  EVP_MD_CTX *ctx;
  ctx = EVP_MD_CTX_create();
  EVP_DigestInit_ex(ctx, hash_func, NULL);
  EVP_DigestUpdate(ctx, in_buf, in_len);
  EVP_DigestFinal(ctx, hash, (unsigned int *)&len);
  EVP_MD_CTX_destroy(ctx);
  if (len < hash_len)
    return 0;
  memcpy(hash_buf, hash, hash_len);
  return 1;
}


x86 assembly (clang, -O0):
<cryptoCalculateHash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1080 <EVP_MD_CTX_create@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <EVP_DigestInit_ex@plt>
mov    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <EVP_DigestUpdate@plt>
mov    -0x60(%rbp),%rsi
mov    -0x50(%rbp),%rdi
lea    -0x44(%rbp),%rdx
call   1070 <EVP_DigestFinal@plt>
mov    -0x50(%rbp),%rdi
call   1060 <EVP_MD_CTX_destroy@plt>
mov    -0x44(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    120b <cryptoCalculateHash+0xab>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    1229 <cryptoCalculateHash+0xc9>
mov    -0x60(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cryptoCalculateHash>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r15
mov    %ecx,-0x2c(%rbp)
mov    %rdx,%r14
mov    %esi,-0x30(%rbp)
mov    %rdi,-0x40(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %r12d,%r12d
xor    %eax,%eax
call   1080 <EVP_MD_CTX_create@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <EVP_DigestInit_ex@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    -0x2c(%rbp),%edx
call   1050 <EVP_DigestUpdate@plt>
lea    -0x34(%rbp),%rdx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1070 <EVP_DigestFinal@plt>
mov    %r13,%rdi
call   1060 <EVP_MD_CTX_destroy@plt>
mov    -0x30(%rbp),%edx
cmp    %edx,-0x34(%rbp)
jl     11f4 <cryptoCalculateHash+0x94>
mov    -0x40(%rbp),%rdi
mov    %rbx,%rsi
call   1040 <memcpy@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_MAX_MD_SIZE = global i32 0
define i32 @cryptoCalculateHash(i8* %0, i32 %1, i8* %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32
%15 = alloca i32*
%16 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%17 = load i32, i32* @EVP_MAX_MD_SIZE
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %12
%20 = alloca i8, i64 %18
store i64 %18, i64* %13
%21 = call i32* (...) @EVP_MD_CTX_create()
store i32* %21, i32** %15
%22 = load i32*, i32** %15
%23 = load i32*, i32** %11
%24 = call i32 @EVP_DigestInit_ex(i32* %22, i32* %23, i32* null)
%25 = load i32*, i32** %15
%26 = load i8*, i8** %9
%27 = load i32, i32* %10
%28 = call i32 @EVP_DigestUpdate(i32* %25, i8* %26, i32 %27)
%29 = load i32*, i32** %15
%30 = call i32 @EVP_DigestFinal(i32* %29, i8* %20, i32* %14)
%31 = load i32*, i32** %15
%32 = call i32 @EVP_MD_CTX_destroy(i32* %31)
%33 = load i32, i32* %14
%34 = load i32, i32* %8
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %5
store i32 0, i32* %6
store i32 1, i32* %16
br label %41
37: ; preds = %5
%38 = load i8*, i8** %7
%39 = load i32, i32* %8
%40 = call i32 @memcpy(i8* %38, i8* %20, i32 %39)
store i32 1, i32* %6
store i32 1, i32* %16
br label %41
41: ; preds = %37, %36
%42 = load i8*, i8** %12
%43 = load i32, i32* %6
ret i32 %43
}

LLVM IR (-O2):
@EVP_MAX_MD_SIZE = global i32 0
define i32 @cryptoCalculateHash(i8* %0, i32 %1, i8* %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = load i32, i32* @EVP_MAX_MD_SIZE
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = bitcast i32* %6 to i8*
%11 = tail call i32* (...) @EVP_MD_CTX_create()
%12 = tail call i32 @EVP_DigestInit_ex(i32* %11, i32* %4, i32* null)
%13 = tail call i32 @EVP_DigestUpdate(i32* %11, i8* %2, i32 %3)
%14 = call i32 @EVP_DigestFinal(i32* %11, i8* %9, i32* %6)
%15 = call i32 @EVP_MD_CTX_destroy(i32* %11)
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, %1
br i1 %17, label %20, label %18
18: ; preds = %5
%19 = call i32 @memcpy(i8* %0, i8* %9, i32 %1)
br label %20
20: ; preds = %5, %18
%21 = phi i32 [ 1, %18 ], [ 0, %5 ]
ret i32 %21
}