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

/* Variables and functions */
int /*<<< orphan*/ EVP_DigestFinal(int /*<<< orphan*/ *, unsigned char *,
                                   int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DigestInit(int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ const *);
int /*<<< orphan*/ EVP_DigestUpdate(int /*<<< orphan*/ *, ...);
int /*<<< orphan*/ EVP_MD_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_MD_CTX_new();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, unsigned char *, int);
int /*<<< orphan*/ printf(char *);
int hash_buf(char *src, char *dst, uint32_t iv, int len,
             const EVP_MD *hash_id) {
  EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
  if (mdctx == NULL) {
    printf("Unable to create new EVP_MD_CTX structure\n");
    return 1;
  };
  unsigned char digest[128];
  iv = htonl(iv);
  EVP_DigestInit(mdctx, hash_id);
  EVP_DigestUpdate(mdctx, (unsigned char *)&iv, sizeof(iv));
  EVP_DigestUpdate(mdctx, src, len);
  EVP_DigestFinal(mdctx, digest, NULL);
  memcpy(dst, digest, len);
  EVP_MD_CTX_free(mdctx);
  return 0;
}


x86 assembly (clang, -O0):
<hash_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    $0x0,%al
call   1090 <EVP_MD_CTX_new@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11cb <hash_buf+0x4b>
lea    0xe46(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1240 <hash_buf+0xc0>
mov    -0x1c(%rbp),%edi
call   1060 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <EVP_DigestInit@plt>
mov    -0x30(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1080 <EVP_DigestUpdate@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1080 <EVP_DigestUpdate@plt>
mov    -0x30(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <EVP_DigestFinal@plt>
mov    -0x18(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <EVP_MD_CTX_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hash_buf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %r8,%rbp
mov    %ecx,%r13d
mov    %edx,%r14d
mov    %rsi,0x8(%rsp)
mov    %rdi,%r12
xor    %r15d,%r15d
xor    %eax,%eax
call   1090 <EVP_MD_CTX_new@plt>
test   %rax,%rax
je     121b <hash_buf+0x9b>
mov    %rax,%rbx
mov    %r14d,%edi
call   1060 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <EVP_DigestInit@plt>
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <EVP_DigestUpdate@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1080 <EVP_DigestUpdate@plt>
lea    0x10(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %edx,%edx
call   10a0 <EVP_DigestFinal@plt>
mov    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    %r13d,%edx
call   1070 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <EVP_MD_CTX_free@plt>
jmp    122d <hash_buf+0xad>
lea    0xdde(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"Unable to create new EVP_MD_CTX structure\0A\00"
define i32 @hash_buf(i8* %0, i8* %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca [128 x i8]
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%14 = call i32* (...) @EVP_MD_CTX_new()
store i32* %14, i32** %12
%15 = load i32*, i32** %12
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %5
%18 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %6
br label %41
19: ; preds = %5
%20 = load i32, i32* %9
%21 = call i32 @htonl(i32 %20)
store i32 %21, i32* %9
%22 = load i32*, i32** %12
%23 = load i32*, i32** %11
%24 = call i32 @EVP_DigestInit(i32* %22, i32* %23)
%25 = load i32*, i32** %12
%26 = bitcast i32* %9 to i8*
%27 = call i32 (i32*, ...) @EVP_DigestUpdate(i32* %25, i8* %26, i64 4)
%28 = load i32*, i32** %12
%29 = load i8*, i8** %7
%30 = load i32, i32* %10
%31 = call i32 (i32*, ...) @EVP_DigestUpdate(i32* %28, i8* %29, i32 %30)
%32 = load i32*, i32** %12
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%34 = call i32 @EVP_DigestFinal(i32* %32, i8* %33, i32* null)
%35 = load i8*, i8** %8
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%37 = load i32, i32* %10
%38 = call i32 @memcpy(i8* %35, i8* %36, i32 %37)
%39 = load i32*, i32** %12
%40 = call i32 @EVP_MD_CTX_free(i32* %39)
store i32 0, i32* %6
br label %41
41: ; preds = %19, %17
%42 = load i32, i32* %6
ret i32 %42
}

LLVM IR (-O2):
@str = [42 x i8] c"Unable to create new EVP_MD_CTX structure\00"
define i32 @hash_buf(i8* %0, i8* %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca [128 x i8]
%8 = tail call i32* (...) @EVP_MD_CTX_new()
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
br label %21
12: ; preds = %5
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%14 = tail call i32 @htonl(i32 %2)
store i32 %14, i32* %6
%15 = tail call i32 @EVP_DigestInit(i32* %8, i32* %4)
%16 = call i32 (i32*, ...) @EVP_DigestUpdate(i32* %8, i32* %6, i64 4)
%17 = call i32 (i32*, ...) @EVP_DigestUpdate(i32* %8, i8* %0, i32 %3)
%18 = call i32 @EVP_DigestFinal(i32* %8, i8* %13, i32* null)
%19 = call i32 @memcpy(i8* %1, i8* %13, i32 %3)
%20 = call i32 @EVP_MD_CTX_free(i32* %8)
br label %21
21: ; preds = %12, %10
%22 = phi i32 [ 1, %10 ], [ 0, %12 ]
ret i32 %22
}