#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 */

/* Variables and functions */
size_t EVP_BytesToKey(int /*<<< orphan*/, int /*<<< orphan*/, char const *,
                      char const *, int /*<<< orphan*/, int, void *, void *);
int /*<<< orphan*/ EVP_aes_256_cbc();
int /*<<< orphan*/ EVP_md5();
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *);
int crypto_derive_key(const char *passphrase, void *key, void *iv) {
  const char *salt = "microsyncdaemon";
  size_t keysize;
  if (!(keysize = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_md5(), salt, passphrase,
                                 strlen(passphrase), 1, key, iv))) {
    fprintf(stderr, "crypto_derive_key"
                    ": EVP_BytesToKey failed\n");
    return 1;
  }
  assert(keysize == 32 && "too big key, stack overflow");
  return 0;
}


x86 assembly (clang, -O0):
<crypto_derive_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xe85(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <EVP_aes_256_cbc@plt>
mov    %eax,-0x48(%rbp)
mov    $0x0,%al
call   1030 <EVP_md5@plt>
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x48(%rbp),%edi
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    %eax,%r8d
mov    -0x18(%rbp),%r10
mov    -0x20(%rbp),%rax
mov    $0x1,%r9d
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1040 <EVP_BytesToKey@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1208 <crypto_derive_key+0xa8>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1236 <crypto_derive_key+0xd6>
xor    %eax,%eax
cmpq   $0x20,-0x30(%rbp)
mov    %al,-0x49(%rbp)
jne    1222 <crypto_derive_key+0xc2>
mov    $0x1,%al
mov    %al,-0x49(%rbp)
jmp    1222 <crypto_derive_key+0xc2>
mov    -0x49(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypto_derive_key>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <EVP_aes_256_cbc@plt>
mov    %eax,%r13d
xor    %eax,%eax
call   1030 <EVP_md5@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0xe6a(%rip),%rdx
mov    $0x1,%r12d
mov    %r13d,%edi
mov    %ebp,%esi
mov    %rbx,%rcx
mov    %eax,%r8d
mov    $0x1,%r9d
push   %r14
push   %r15
call   1040 <EVP_BytesToKey@plt>
add    $0x10,%rsp
test   %rax,%rax
je     11d3 <crypto_derive_key+0x73>
xor    %edi,%edi
cmp    $0x20,%rax
sete   %dil
call   1070 <assert@plt>
xor    %r12d,%r12d
jmp    11e8 <crypto_derive_key+0x88>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
call   1080 <fprintf@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"microsyncdaemon\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"crypto_derive_key: EVP_BytesToKey failed\0A\00"
@.str.2 = [28 x i8] c"too big key, stack overflow\00"
define i32 @crypto_derive_key(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %8
%10 = call i32 (...) @EVP_aes_256_cbc()
%11 = call i32 (...) @EVP_md5()
%12 = load i8*, i8** %8
%13 = load i8*, i8** %5
%14 = load i8*, i8** %5
%15 = call i32 @strlen(i8* %14)
%16 = load i8*, i8** %6
%17 = load i8*, i8** %7
%18 = call i64 @EVP_BytesToKey(i32 %10, i32 %11, i8* %12, i8* %13, i32 %15, i32 1, i8* %16, i8* %17)
store i64 %18, i64* %9
%19 = icmp ne i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %31
23: ; preds = %3
%24 = load i64, i64* %9
%25 = icmp eq i64 %24, 32
br i1 %25, label %26, label %27
26: ; preds = %23
br label %27
27: ; preds = %26, %23
%28 = phi i1 [ false, %23 ], [ true, %26 ]
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
store i32 0, i32* %4
br label %31
31: ; preds = %27, %20
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@.str = [16 x i8] c"microsyncdaemon\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"crypto_derive_key: EVP_BytesToKey failed\0A\00"
define i32 @crypto_derive_key(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 (...) @EVP_aes_256_cbc()
%5 = tail call i32 (...) @EVP_md5()
%6 = tail call i32 @strlen(i8* %0)
%7 = tail call i64 @EVP_BytesToKey(i32 %4, i32 %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %6, i32 1, i8* %1, i8* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
br label %16
12: ; preds = %3
%13 = icmp eq i64 %7, 32
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
br label %16
16: ; preds = %12, %9
%17 = phi i32 [ 0, %12 ], [ 1, %9 ]
ret i32 %17
}