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

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ *EVP_get_digestbyname(char const *);
int /*<<< orphan*/ PKCS5_PBKDF2_HMAC(char const *, int, unsigned char *, int,
                                     int, int /*<<< orphan*/ const *, int,
                                     unsigned char *);
scalar_t__ strncmp(char const *, char *, int);
int crypt_pbkdf(const char *kdf, const char *hash, const char *password,
                size_t password_length, const char *salt, size_t salt_length,
                char *key, size_t key_length, unsigned int iterations) {
  const EVP_MD *hash_id;
  if (!kdf || strncmp(kdf, "pbkdf2", 6))
    return -EINVAL;
  hash_id = EVP_get_digestbyname(hash);
  if (!hash_id)
    return -EINVAL;
  if (!PKCS5_PBKDF2_HMAC(password, (int)password_length, (unsigned char *)salt,
                         (int)salt_length, (int)iterations, hash_id,
                         (int)key_length, (unsigned char *)key))
    return -EINVAL;
  return 0;
}


x86 assembly (clang, -O0):
<crypt_pbkdf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x20(%rbp),%eax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1185 <crypt_pbkdf+0x55>
mov    -0x10(%rbp),%rdi
lea    0xe8f(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1198 <crypt_pbkdf+0x68>
xor    %eax,%eax
mov    0x2e52(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    121b <crypt_pbkdf+0xeb>
mov    -0x18(%rbp),%rdi
call   1050 <EVP_get_digestbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11c3 <crypt_pbkdf+0x93>
xor    %eax,%eax
mov    0x2e27(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    121b <crypt_pbkdf+0xeb>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rax
mov    %eax,%ecx
mov    0x20(%rbp),%r8d
mov    -0x40(%rbp),%r9
mov    0x18(%rbp),%rax
mov    %eax,%r10d
mov    0x10(%rbp),%rax
mov    %r10d,(%rsp)
mov    %rax,0x8(%rsp)
call   1040 <PKCS5_PBKDF2_HMAC@plt>
cmp    $0x0,%eax
jne    1214 <crypt_pbkdf+0xe4>
xor    %eax,%eax
mov    0x2dd6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    121b <crypt_pbkdf+0xeb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypt_pbkdf>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rdi,%rdi
je     1163 <crypt_pbkdf+0x33>
mov    %r9,%r14
mov    %r8,%r15
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,%rbx
lea    0xeac(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1178 <crypt_pbkdf+0x48>
mov    0x2e76(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1050 <EVP_get_digestbyname@plt>
test   %rax,%rax
je     1163 <crypt_pbkdf+0x33>
mov    0x40(%rsp),%r8d
mov    0x38(%rsp),%rbx
mov    %r13,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
mov    %rax,%r9
push   0x30(%rsp)
push   %rbx
call   1040 <PKCS5_PBKDF2_HMAC@plt>
add    $0x10,%rsp
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    116e <crypt_pbkdf+0x3e>
jmp    1163 <crypt_pbkdf+0x33>

LLVM IR (-O0):
@.str = [7 x i8] c"pbkdf2\00"
@EINVAL = global i32 0
define i32 @crypt_pbkdf(i8* %0, i8* %1, i8* %2, i64 %3, i8* %4, i64 %5, i8* %6, i64 %7, i32 %8) {
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i8*
%16 = alloca i64
%17 = alloca i8*
%18 = alloca i64
%19 = alloca i32
%20 = alloca i32*
store i8* %0, i8** %11
store i8* %1, i8** %12
store i8* %2, i8** %13
store i64 %3, i64* %14
store i8* %4, i8** %15
store i64 %5, i64* %16
store i8* %6, i8** %17
store i64 %7, i64* %18
store i32 %8, i32* %19
%21 = load i8*, i8** %11
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %9
%24 = load i8*, i8** %11
%25 = call i64 @strncmp(i8* %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23, %9
%28 = load i32, i32* @EINVAL
%29 = sub nsw i32 0, %28
store i32 %29, i32* %10
br label %56
30: ; preds = %23
%31 = load i8*, i8** %12
%32 = call i32* @EVP_get_digestbyname(i8* %31)
store i32* %32, i32** %20
%33 = load i32*, i32** %20
%34 = icmp ne i32* %33, null
br i1 %34, label %38, label %35
35: ; preds = %30
%36 = load i32, i32* @EINVAL
%37 = sub nsw i32 0, %36
store i32 %37, i32* %10
br label %56
38: ; preds = %30
%39 = load i8*, i8** %13
%40 = load i64, i64* %14
%41 = trunc i64 %40 to i32
%42 = load i8*, i8** %15
%43 = load i64, i64* %16
%44 = trunc i64 %43 to i32
%45 = load i32, i32* %19
%46 = load i32*, i32** %20
%47 = load i64, i64* %18
%48 = trunc i64 %47 to i32
%49 = load i8*, i8** %17
%50 = call i32 @PKCS5_PBKDF2_HMAC(i8* %39, i32 %41, i8* %42, i32 %44, i32 %45, i32* %46, i32 %48, i8* %49)
%51 = icmp ne i32 %50, 0
br i1 %51, label %55, label %52
52: ; preds = %38
%53 = load i32, i32* @EINVAL
%54 = sub nsw i32 0, %53
store i32 %54, i32* %10
br label %56
55: ; preds = %38
store i32 0, i32* %10
br label %56
56: ; preds = %55, %52, %35, %27
%57 = load i32, i32* %10
ret i32 %57
}

LLVM IR (-O2):
@.str = [7 x i8] c"pbkdf2\00"
@EINVAL = global i32 0
define i32 @crypt_pbkdf(i8* %0, i8* %1, i8* %2, i64 %3, i8* %4, i64 %5, i8* %6, i64 %7, i32 %8) {
%10 = icmp eq i8* %0, null
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11, %9
%15 = load i32, i32* @EINVAL
%16 = sub nsw i32 0, %15
br label %32
17: ; preds = %11
%18 = tail call i32* @EVP_get_digestbyname(i8* %1)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @EINVAL
%22 = sub nsw i32 0, %21
br label %32
23: ; preds = %17
%24 = trunc i64 %3 to i32
%25 = trunc i64 %5 to i32
%26 = trunc i64 %7 to i32
%27 = tail call i32 @PKCS5_PBKDF2_HMAC(i8* %2, i32 %24, i8* %4, i32 %25, i32 %8, i32* %18, i32 %26, i8* %6)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* @EINVAL
%31 = sub nsw i32 0, %30
br label %32
32: ; preds = %23, %29, %20, %14
%33 = phi i32 [ %16, %14 ], [ %31, %29 ], [ %22, %20 ], [ 0, %23 ]
ret i32 %33
}