#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 unsigned int uint32_t;
struct CRYPTO_HASH_CTX {
  int dummy;
};
typedef int /*<<< orphan*/ EVP_MD_CTX;

/* Variables and functions */
scalar_t__ EVP_DigestFinal_ex(int /*<<< orphan*/ *, void *, unsigned int *);
int /*<<< orphan*/ ssh_set_error(char *);
int crypto_sha2_final(struct CRYPTO_HASH_CTX *crypto_ctx, void *out,
                      uint32_t *out_len) {
  EVP_MD_CTX *ctx = ((EVP_MD_CTX *)(crypto_ctx));
  unsigned int digest_len;
  if (EVP_DigestFinal_ex(ctx, out, &digest_len) == 0) {
    ssh_set_error("error finalizing sha2 hash");
    return -1;
  }
  if (out_len != NULL)
    *out_len = digest_len;
  return 0;
}


x86 assembly (clang, -O0):
<crypto_sha2_final>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1030 <EVP_DigestFinal_ex@plt>
cmp    $0x0,%rax
jne    116f <crypto_sha2_final+0x4f>
lea    0xea2(%rip),%rdi
call   1040 <ssh_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    118a <crypto_sha2_final+0x6a>
cmpq   $0x0,-0x20(%rbp)
je     1183 <crypto_sha2_final+0x63>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypto_sha2_final>:
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
lea    0xc(%rsp),%rdx
call   1030 <EVP_DigestFinal_ex@plt>
test   %rax,%rax
je     1146 <crypto_sha2_final+0x26>
xor    %eax,%eax
test   %rbx,%rbx
je     1157 <crypto_sha2_final+0x37>
mov    0xc(%rsp),%ecx
mov    %ecx,(%rbx)
jmp    1157 <crypto_sha2_final+0x37>
lea    0xeb3(%rip),%rdi
call   1040 <ssh_set_error@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.CRYPTO_HASH_CTX = type { i32 }
@.str = [27 x i8] c"error finalizing sha2 hash\00"
define i32 @crypto_sha2_final(%struct.CRYPTO_HASH_CTX* %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca %struct.CRYPTO_HASH_CTX*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store %struct.CRYPTO_HASH_CTX* %0, %struct.CRYPTO_HASH_CTX** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%10 = load %struct.CRYPTO_HASH_CTX*, %struct.CRYPTO_HASH_CTX** %5
%11 = bitcast %struct.CRYPTO_HASH_CTX* %10 to i32*
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = load i8*, i8** %6
%14 = call i64 @EVP_DigestFinal_ex(i32* %12, i8* %13, i32* %9)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 @ssh_set_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %25
18: ; preds = %3
%19 = load i32*, i32** %7
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* %9
%23 = load i32*, i32** %7
store i32 %22, i32* %23
br label %24
24: ; preds = %21, %18
store i32 0, i32* %4
br label %25
25: ; preds = %24, %16
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
%struct.CRYPTO_HASH_CTX = type { i32 }
@.str = [27 x i8] c"error finalizing sha2 hash\00"
define i32 @crypto_sha2_final(%struct.CRYPTO_HASH_CTX* %0, i8* %1, i32* writeonly %2) {
%4 = alloca i32
%5 = getelementptr %struct.CRYPTO_HASH_CTX, %struct.CRYPTO_HASH_CTX* %0, i64 0, i32 0
%6 = bitcast i32* %4 to i8*
%7 = call i64 @EVP_DigestFinal_ex(i32* %5, i8* %1, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = call i32 @ssh_set_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %15
11: ; preds = %3
%12 = icmp eq i32* %2, null
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = load i32, i32* %4
store i32 %14, i32* %2
br label %15
15: ; preds = %11, %13, %9
%16 = phi i32 [ -1, %9 ], [ 0, %13 ], [ 0, %11 ]
ret i32 %16
}