#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;
typedef int /*<<< orphan*/ EVP_CIPHER;

/* Variables and functions */
int /*<<< orphan*/ EVP_BytesToKey(int /*<<< orphan*/ const *,
                                  int /*<<< orphan*/ const *,
                                  unsigned char const *, unsigned char *,
                                  int /*<<< orphan*/, int const,
                                  unsigned char *, unsigned char *);
int /*<<< orphan*/ *EVP_aes_256_cbc();
int /*<<< orphan*/ *EVP_get_digestbyname(char *);
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *);
void derive_password(const char *password, const unsigned char *salt,
                     const int derivation_count, unsigned char *key,
                     unsigned char *iv) {
  const EVP_CIPHER *cipher;
  const EVP_MD *dgst;
  cipher = EVP_aes_256_cbc();
  if (!cipher) {
    fprintf(stderr, "no such cipher\n");
    abort();
  }
  dgst = EVP_get_digestbyname("sha512");
  if (!dgst) {
    fprintf(stderr, "no such digest\n");
    abort();
  }
  if (!EVP_BytesToKey(cipher, dgst, salt, (unsigned char *)password,
                      strlen(password), derivation_count, key, iv)) {
    fprintf(stderr, "EVP_BytesToKey failed\n");
    abort();
  }
}


x86 assembly (clang, -O0):
<derive_password>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    $0x0,%al
call   1050 <EVP_aes_256_cbc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ad <derive_password+0x4d>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
lea    0xe5c(%rip),%rdi
call   1080 <EVP_get_digestbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11e4 <derive_password+0x84>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    %eax,%r8d
mov    -0x14(%rbp),%r9d
mov    -0x20(%rbp),%r10
mov    -0x28(%rbp),%rax
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1040 <EVP_BytesToKey@plt>
cmp    $0x0,%eax
jne    125f <derive_password+0xff>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<derive_password>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r15
mov    %rcx,(%rsp)
mov    %edx,%r12d
mov    %rsi,%r13
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <EVP_aes_256_cbc@plt>
test   %rax,%rax
je     11da <derive_password+0x7a>
mov    %rax,%rbp
lea    0xe7f(%rip),%rdi
call   1080 <EVP_get_digestbyname@plt>
test   %rax,%rax
je     11ec <derive_password+0x8c>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
mov    %rbx,%rcx
mov    %eax,%r8d
mov    %r12d,%r9d
push   %r15
push   0x8(%rsp)
call   1040 <EVP_BytesToKey@plt>
add    $0x10,%rsp
test   %eax,%eax
je     11fe <derive_password+0x9e>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
jmp    120e <derive_password+0xae>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
jmp    120e <derive_password+0xae>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"no such cipher\0A\00"
@.str.1 = [7 x i8] c"sha512\00"
@.str.2 = [16 x i8] c"no such digest\0A\00"
@.str.3 = [23 x i8] c"EVP_BytesToKey failed\0A\00"
define void @derive_password(i8* %0, i8* %1, i32 %2, i8* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%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 i8* %4, i8** %10
%13 = call i32* (...) @EVP_aes_256_cbc()
store i32* %13, i32** %11
%14 = load i32*, i32** %11
%15 = icmp ne i32* %14, null
br i1 %15, label %20, label %16
16: ; preds = %5
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%19 = call i32 (...) @abort()
unreachable
20: ; preds = %5
%21 = call i32* @EVP_get_digestbyname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %12
%22 = load i32*, i32** %12
%23 = icmp ne i32* %22, null
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 (...) @abort()
unreachable
28: ; preds = %20
%29 = load i32*, i32** %11
%30 = load i32*, i32** %12
%31 = load i8*, i8** %7
%32 = load i8*, i8** %6
%33 = load i8*, i8** %6
%34 = call i32 @strlen(i8* %33)
%35 = load i32, i32* %8
%36 = load i8*, i8** %9
%37 = load i8*, i8** %10
%38 = call i32 @EVP_BytesToKey(i32* %29, i32* %30, i8* %31, i8* %32, i32 %34, i32 %35, i8* %36, i8* %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %28
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 (...) @abort()
unreachable
44: ; preds = %28
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"no such cipher\0A\00"
@.str.1 = [7 x i8] c"sha512\00"
@.str.2 = [16 x i8] c"no such digest\0A\00"
@.str.3 = [23 x i8] c"EVP_BytesToKey failed\0A\00"
define void @derive_password(i8* %0, i8* %1, i32 %2, i8* %3, i8* %4) {
%6 = tail call i32* (...) @EVP_aes_256_cbc()
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 (...) @abort()
unreachable
12: ; preds = %5
%13 = tail call i32* @EVP_get_digestbyname(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 (...) @abort()
unreachable
19: ; preds = %12
%20 = tail call i32 @strlen(i8* %0)
%21 = tail call i32 @EVP_BytesToKey(i32* %6, i32* %13, i8* %1, i8* %0, i32 %20, i32 %2, i8* %3, i8* %4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%26 = tail call i32 (...) @abort()
unreachable
27: ; preds = %19
ret void
}