#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, unsigned char *,
                                  unsigned char *);
int EVP_MAX_IV_LENGTH;
int /*<<< orphan*/ EVP_MAX_KEY_LENGTH;
int /*<<< orphan*/ *EVP_get_cipherbyname(char *);
int /*<<< orphan*/ *EVP_get_digestbyname(char *);
int /*<<< orphan*/ OpenSSL_add_all_algorithms();
int /*<<< orphan*/ OpenSSL_add_all_digests();
unsigned char *calloc(int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
unsigned char *createDesKey(char *password, char *keyType) {
  const EVP_CIPHER *cipher;
  const EVP_MD *dgst = NULL;
  unsigned char *key = calloc(1, EVP_MAX_KEY_LENGTH), iv[EVP_MAX_IV_LENGTH];
  const unsigned char *salt = NULL;
  int i;
  OpenSSL_add_all_algorithms();
  cipher = EVP_get_cipherbyname(keyType);
  if (!cipher) {
    fprintf(stderr, "no such cipher\n");
    return "1";
  }
  OpenSSL_add_all_digests();
  dgst = EVP_get_digestbyname("sha1");
  if (!dgst) {
    fprintf(stderr, "no such digest\n");
    return "1";
  }
  if (!EVP_BytesToKey(cipher, dgst, salt, (unsigned char *)password,
                      strlen(password), 1, key, iv)) {
    fprintf(stderr, "EVP_BytesToKey failed\n");
    return "1";
  }
  return key;
}


x86 assembly (clang, -O0):
<createDesKey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    $0x0,%al
call   1080 <OpenSSL_add_all_algorithms@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <EVP_get_cipherbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1231 <createDesKey+0xb1>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
call   1070 <fprintf@plt>
lea    0xdf2(%rip),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    1323 <createDesKey+0x1a3>
mov    $0x0,%al
call   1060 <OpenSSL_add_all_digests@plt>
lea    0xdd1(%rip),%rdi
call   1090 <EVP_get_digestbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    127f <createDesKey+0xff>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
call   1070 <fprintf@plt>
lea    0xda4(%rip),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    1323 <createDesKey+0x1a3>
mov    -0x20(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rsi
mov    -0x68(%rbp),%rdx
mov    -0x60(%rbp),%rcx
mov    %eax,%r8d
mov    -0x58(%rbp),%rax
mov    -0x30(%rbp),%r10
mov    $0x1,%r9d
sub    $0x10,%rsp
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1030 <EVP_BytesToKey@plt>
add    $0x10,%rsp
cmp    $0x0,%eax
jne    1314 <createDesKey+0x194>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
lea    0xd2d(%rip),%rsi
call   1070 <fprintf@plt>
lea    0xd0f(%rip),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    1323 <createDesKey+0x1a3>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createDesKey>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,%r15
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %eax,%eax
call   1080 <OpenSSL_add_all_algorithms@plt>
mov    %r12,%rdi
call   10a0 <EVP_get_cipherbyname@plt>
test   %rax,%rax
je     1234 <createDesKey+0xb4>
mov    %rax,%r13
xor    %eax,%eax
call   1060 <OpenSSL_add_all_digests@plt>
lea    0xe27(%rip),%rdi
call   1090 <EVP_get_digestbyname@plt>
test   %rax,%rax
je     1246 <createDesKey+0xc6>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r13,%rdi
mov    %r12,%rsi
xor    %edx,%edx
mov    %r14,%rcx
mov    %eax,%r8d
mov    $0x1,%r9d
push   %rbx
push   %r15
call   1030 <EVP_BytesToKey@plt>
add    $0x10,%rsp
test   %eax,%eax
jne    1262 <createDesKey+0xe2>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
jmp    1256 <createDesKey+0xd6>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
jmp    1256 <createDesKey+0xd6>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
call   1070 <fprintf@plt>
lea    0xdb1(%rip),%r15
mov    %r15,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_MAX_KEY_LENGTH = global i32 0
@EVP_MAX_IV_LENGTH = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"no such cipher\0A\00"
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [5 x i8] c"sha1\00"
@.str.3 = [16 x i8] c"no such digest\0A\00"
@.str.4 = [23 x i8] c"EVP_BytesToKey failed\0A\00"
define i8* @createDesKey(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* null, i32** %7
%14 = load i32, i32* @EVP_MAX_KEY_LENGTH
%15 = call i8* @calloc(i32 1, i32 %14)
store i8* %15, i8** %8
%16 = load i32, i32* @EVP_MAX_IV_LENGTH
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %9
%19 = alloca i8, i64 %17
store i64 %17, i64* %10
store i8* null, i8** %11
%20 = call i32 (...) @OpenSSL_add_all_algorithms()
%21 = load i8*, i8** %5
%22 = call i32* @EVP_get_cipherbyname(i8* %21)
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp ne i32* %23, null
br i1 %24, label %28, label %25
25: ; preds = %2
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %3
store i32 1, i32* %13
br label %51
28: ; preds = %2
%29 = call i32 (...) @OpenSSL_add_all_digests()
%30 = call i32* @EVP_get_digestbyname(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %7
%31 = load i32*, i32** %7
%32 = icmp ne i32* %31, null
br i1 %32, label %36, label %33
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %3
store i32 1, i32* %13
br label %51
36: ; preds = %28
%37 = load i32*, i32** %6
%38 = load i32*, i32** %7
%39 = load i8*, i8** %11
%40 = load i8*, i8** %4
%41 = load i8*, i8** %4
%42 = call i32 @strlen(i8* %41)
%43 = load i8*, i8** %8
%44 = call i32 @EVP_BytesToKey(i32* %37, i32* %38, i8* %39, i8* %40, i32 %42, i32 1, i8* %43, i8* %19)
%45 = icmp ne i32 %44, 0
br i1 %45, label %49, label %46
46: ; preds = %36
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %3
store i32 1, i32* %13
br label %51
49: ; preds = %36
%50 = load i8*, i8** %8
store i8* %50, i8** %3
store i32 1, i32* %13
br label %51
51: ; preds = %49, %46, %33, %25
%52 = load i8*, i8** %9
%53 = load i8*, i8** %3
ret i8* %53
}

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