#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;

/* Variables and functions */
int /*<<< orphan*/ EVP_DigestFinal_ex(int /*<<< orphan*/ *, unsigned char *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DigestInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DigestUpdate(int /*<<< orphan*/ *, unsigned char const *,
                                    int);
int /*<<< orphan*/ EVP_MD_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_MD_CTX_new();
int /*<<< orphan*/ EVP_sha1();
int derive_pvk_key(unsigned char *key, const unsigned char *salt,
                   unsigned int saltlen, const unsigned char *pass,
                   int passlen) {
  EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  int rv = 1;
  if (mctx == NULL || !EVP_DigestInit_ex(mctx, EVP_sha1(), NULL) ||
      !EVP_DigestUpdate(mctx, salt, saltlen) ||
      !EVP_DigestUpdate(mctx, pass, passlen) ||
      !EVP_DigestFinal_ex(mctx, key, NULL))
    rv = 0;
  EVP_MD_CTX_free(mctx);
  return rv;
}


x86 assembly (clang, -O0):
<derive_pvk_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    $0x0,%al
call   1080 <EVP_MD_CTX_new@plt>
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     120b <derive_pvk_key+0xab>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1050 <EVP_sha1@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <EVP_DigestInit_ex@plt>
cmp    $0x0,%eax
je     120b <derive_pvk_key+0xab>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <EVP_DigestUpdate@plt>
cmp    $0x0,%eax
je     120b <derive_pvk_key+0xab>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <EVP_DigestUpdate@plt>
cmp    $0x0,%eax
je     120b <derive_pvk_key+0xab>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <EVP_DigestFinal_ex@plt>
cmp    $0x0,%eax
jne    1212 <derive_pvk_key+0xb2>
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <EVP_MD_CTX_free@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<derive_pvk_key>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r15d
mov    %rcx,%r12
mov    %edx,%r13d
mov    %rsi,%rbp
mov    %rdi,%r14
xor    %eax,%eax
call   1080 <EVP_MD_CTX_new@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11da <derive_pvk_key+0x7a>
xor    %eax,%eax
call   1050 <EVP_sha1@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1060 <EVP_DigestInit_ex@plt>
test   %eax,%eax
je     11da <derive_pvk_key+0x7a>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r13d,%edx
call   1070 <EVP_DigestUpdate@plt>
test   %eax,%eax
je     11da <derive_pvk_key+0x7a>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1070 <EVP_DigestUpdate@plt>
test   %eax,%eax
je     11da <derive_pvk_key+0x7a>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <EVP_DigestFinal_ex@plt>
mov    $0x1,%ebp
test   %eax,%eax
jne    11dc <derive_pvk_key+0x7c>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <EVP_MD_CTX_free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @derive_pvk_key(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%13 = call i32* (...) @EVP_MD_CTX_new()
store i32* %13, i32** %11
store i32 1, i32* %12
%14 = load i32*, i32** %11
%15 = icmp eq i32* %14, null
br i1 %15, label %38, label %16
16: ; preds = %5
%17 = load i32*, i32** %11
%18 = call i32 (...) @EVP_sha1()
%19 = call i32 @EVP_DigestInit_ex(i32* %17, i32 %18, i32* null)
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %38
21: ; preds = %16
%22 = load i32*, i32** %11
%23 = load i8*, i8** %7
%24 = load i32, i32* %8
%25 = call i32 @EVP_DigestUpdate(i32* %22, i8* %23, i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %38
27: ; preds = %21
%28 = load i32*, i32** %11
%29 = load i8*, i8** %9
%30 = load i32, i32* %10
%31 = call i32 @EVP_DigestUpdate(i32* %28, i8* %29, i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %27
%34 = load i32*, i32** %11
%35 = load i8*, i8** %6
%36 = call i32 @EVP_DigestFinal_ex(i32* %34, i8* %35, i32* null)
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %33, %27, %21, %16, %5
store i32 0, i32* %12
br label %39
39: ; preds = %38, %33
%40 = load i32*, i32** %11
%41 = call i32 @EVP_MD_CTX_free(i32* %40)
%42 = load i32, i32* %12
ret i32 %42
}

LLVM IR (-O2):
define i32 @derive_pvk_key(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = tail call i32* (...) @EVP_MD_CTX_new()
%7 = icmp eq i32* %6, null
br i1 %7, label %21, label %8
8: ; preds = %5
%9 = tail call i32 (...) @EVP_sha1()
%10 = tail call i32 @EVP_DigestInit_ex(i32* %6, i32 %9, i32* null)
%11 = icmp eq i32 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %8
%13 = tail call i32 @EVP_DigestUpdate(i32* %6, i8* %1, i32 %2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %12
%16 = tail call i32 @EVP_DigestUpdate(i32* %6, i8* %3, i32 %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = tail call i32 @EVP_DigestFinal_ex(i32* %6, i8* %0, i32* null)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18, %15, %12, %8, %5
br label %22
22: ; preds = %21, %18
%23 = phi i32 [ 0, %21 ], [ 1, %18 ]
%24 = tail call i32 @EVP_MD_CTX_free(i32* %6)
ret i32 %23
}