#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*/ uint8_t;
typedef int /*<<< orphan*/ EVP_MD_CTX;
typedef int /*<<< orphan*/ EVP_MD;

/* Variables and functions */
int EVP_DigestFinal_ex(int /*<<< orphan*/ *, unsigned char *, unsigned int *);
int EVP_DigestInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                      int /*<<< orphan*/ *);
int EVP_DigestUpdate(int /*<<< orphan*/ *, int /*<<< orphan*/ *, size_t);
int EVP_MAX_MD_SIZE;
int /*<<< orphan*/ *EVP_MD_CTX_create();
int /*<<< orphan*/ EVP_MD_CTX_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_md5();
char *malloc(unsigned int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char);
char *key_signature(uint8_t *key, size_t keylen) {
  const EVP_MD *md = NULL;
  EVP_MD_CTX *mdctx = NULL;
  unsigned char md_value[EVP_MAX_MD_SIZE];
  unsigned int md_len = 0;
  char *ret = NULL;
  unsigned int i;
  md = EVP_md5();
  if ((md = EVP_md5()) == NULL)
    return (NULL);
  if ((mdctx = EVP_MD_CTX_create()) == NULL)
    return (NULL);
  if (EVP_DigestInit_ex(mdctx, md, NULL) != 1 ||
      EVP_DigestUpdate(mdctx, key, keylen) != 1 ||
      EVP_DigestFinal_ex(mdctx, md_value, &md_len) != 1) {
    goto out;
  }
  ret = malloc(md_len * 3);
  if (ret == NULL)
    goto out;
  for (i = 0; i < md_len; i++) {
    snprintf(ret + i * 3, 3, "%02x", md_value[i]);
    ret[i * 3 + 2] = (i == md_len - 1) ? '\0' : ':';
  }
out:
  if (mdctx != NULL)
    EVP_MD_CTX_destroy(mdctx);
  return (ret);
}


x86 assembly (clang, -O0):
<key_signature>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%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,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movq   $0x0,-0x48(%rbp)
mov    $0x0,%al
call   1030 <EVP_md5@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <EVP_md5@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    120f <key_signature+0x8f>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    1341 <key_signature+0x1c1>
mov    $0x0,%al
call   10a0 <EVP_MD_CTX_create@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1238 <key_signature+0xb8>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    1341 <key_signature+0x1c1>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <EVP_DigestInit_ex@plt>
cmp    $0x1,%eax
jne    1286 <key_signature+0x106>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <EVP_DigestUpdate@plt>
cmp    $0x1,%eax
jne    1286 <key_signature+0x106>
mov    -0x58(%rbp),%rsi
mov    -0x28(%rbp),%rdi
lea    -0x3c(%rbp),%rdx
call   1040 <EVP_DigestFinal_ex@plt>
cmp    $0x1,%eax
je     128b <key_signature+0x10b>
jmp    131e <key_signature+0x19e>
imul   $0x3,-0x3c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12a8 <key_signature+0x128>
jmp    131e <key_signature+0x19e>
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jae    1319 <key_signature+0x199>
mov    -0x58(%rbp),%rax
mov    -0x48(%rbp),%rdi
imul   $0x3,-0x4c(%rbp),%ecx
mov    %ecx,%ecx
add    %rcx,%rdi
mov    -0x4c(%rbp),%ecx
mov    $0x3,%esi
lea    0xd25(%rip),%rdx
movzbl (%rax,%rcx,1),%ecx
call   1050 <snprintf@plt>
mov    -0x4c(%rbp),%edx
mov    -0x3c(%rbp),%esi
sub    $0x1,%esi
mov    $0x3a,%eax
xor    %ecx,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    %al,%dl
mov    -0x48(%rbp),%rax
imul   $0x3,-0x4c(%rbp),%ecx
add    $0x2,%ecx
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    12af <key_signature+0x12f>
jmp    131e <key_signature+0x19e>
cmpq   $0x0,-0x28(%rbp)
je     1332 <key_signature+0x1b2>
mov    -0x28(%rbp),%rdi
call   1090 <EVP_MD_CTX_destroy@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<key_signature>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
movl   $0x0,-0x2c(%rbp)
xor    %r13d,%r13d
xor    %eax,%eax
call   1030 <EVP_md5@plt>
xor    %eax,%eax
call   1030 <EVP_md5@plt>
test   %rax,%rax
je     12b2 <key_signature+0x132>
mov    %rax,%r12
mov    %rbx,-0x38(%rbp)
mov    %r14,%rbx
xor    %r13d,%r13d
xor    %eax,%eax
call   10a0 <EVP_MD_CTX_create@plt>
test   %rax,%rax
je     12b2 <key_signature+0x132>
mov    %rax,%r14
xor    %r13d,%r13d
mov    %rax,%rdi
mov    %r12,%rsi
xor    %edx,%edx
call   1060 <EVP_DigestInit_ex@plt>
cmp    $0x1,%eax
jne    12aa <key_signature+0x12a>
mov    %r14,%rdi
mov    -0x38(%rbp),%rsi
mov    %rbx,%rdx
call   1070 <EVP_DigestUpdate@plt>
cmp    $0x1,%eax
jne    12a7 <key_signature+0x127>
lea    -0x2c(%rbp),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <EVP_DigestFinal_ex@plt>
cmp    $0x1,%eax
jne    12a7 <key_signature+0x127>
mov    -0x2c(%rbp),%ebx
lea    (%rbx,%rbx,2),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12a7 <key_signature+0x127>
mov    %rax,%r13
test   %ebx,%ebx
je     12aa <key_signature+0x12a>
mov    $0x2,%ebx
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
lea    -0x2(%rbx),%edi
add    %r13,%rdi
movzbl (%r15,%r12,1),%ecx
mov    $0x3,%esi
lea    0xd89(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x2c(%rbp),%eax
lea    -0x1(%rax),%ecx
cmp    %rcx,%r12
mov    $0x3a,%ecx
mov    $0x0,%edx
cmove  %edx,%ecx
mov    %ebx,%edx
mov    %cl,0x0(%r13,%rdx,1)
add    $0x1,%r12
add    $0x3,%ebx
cmp    %rax,%r12
jb     1260 <key_signature+0xe0>
jmp    12aa <key_signature+0x12a>
xor    %r13d,%r13d
mov    %r14,%rdi
call   1090 <EVP_MD_CTX_destroy@plt>
mov    %r13,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_MAX_MD_SIZE = global i32 0
@.str = [5 x i8] c"%02x\00"
define i8* @key_signature(i32* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %4
store i64 %1, i64* %5
store i32* null, i32** %6
store i32* null, i32** %7
%14 = load i32, i32* @EVP_MAX_MD_SIZE
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
store i32 0, i32* %10
store i8* null, i8** %11
%18 = call i32* (...) @EVP_md5()
store i32* %18, i32** %6
%19 = call i32* (...) @EVP_md5()
store i32* %19, i32** %6
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %2
store i8* null, i8** %3
store i32 1, i32* %13
br label %90
22: ; preds = %2
%23 = call i32* (...) @EVP_MD_CTX_create()
store i32* %23, i32** %7
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %22
store i8* null, i8** %3
store i32 1, i32* %13
br label %90
26: ; preds = %22
%27 = load i32*, i32** %7
%28 = load i32*, i32** %6
%29 = call i32 @EVP_DigestInit_ex(i32* %27, i32* %28, i32* null)
%30 = icmp ne i32 %29, 1
br i1 %30, label %41, label %31
31: ; preds = %26
%32 = load i32*, i32** %7
%33 = load i32*, i32** %4
%34 = load i64, i64* %5
%35 = call i32 @EVP_DigestUpdate(i32* %32, i32* %33, i64 %34)
%36 = icmp ne i32 %35, 1
br i1 %36, label %41, label %37
37: ; preds = %31
%38 = load i32*, i32** %7
%39 = call i32 @EVP_DigestFinal_ex(i32* %38, i8* %17, i32* %10)
%40 = icmp ne i32 %39, 1
br i1 %40, label %41, label %42
41: ; preds = %37, %31, %26
br label %82
42: ; preds = %37
%43 = load i32, i32* %10
%44 = mul i32 %43, 3
%45 = call i8* @malloc(i32 %44)
store i8* %45, i8** %11
%46 = load i8*, i8** %11
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %49
48: ; preds = %42
br label %82
49: ; preds = %42
store i32 0, i32* %12
br label %50
50: ; preds = %78, %49
%51 = load i32, i32* %12
%52 = load i32, i32* %10
%53 = icmp ult i32 %51, %52
br i1 %53, label %54, label %81
54: ; preds = %50
%55 = load i8*, i8** %11
%56 = load i32, i32* %12
%57 = mul i32 %56, 3
%58 = zext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
%60 = load i32, i32* %12
%61 = zext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %17, i64 %61
%63 = load i8, i8* %62
%64 = call i32 @snprintf(i8* %59, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %63)
%65 = load i32, i32* %12
%66 = load i32, i32* %10
%67 = sub i32 %66, 1
%68 = icmp eq i32 %65, %67
%69 = zext i1 %68 to i64
%70 = select i1 %68, i32 0, i32 58
%71 = trunc i32 %70 to i8
%72 = load i8*, i8** %11
%73 = load i32, i32* %12
%74 = mul i32 %73, 3
%75 = add i32 %74, 2
%76 = zext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %72, i64 %76
store i8 %71, i8* %77
br label %78
78: ; preds = %54
%79 = load i32, i32* %12
%80 = add i32 %79, 1
store i32 %80, i32* %12
br label %50
81: ; preds = %50
br label %82
82: ; preds = %81, %48, %41
%83 = load i32*, i32** %7
%84 = icmp ne i32* %83, null
br i1 %84, label %85, label %88
85: ; preds = %82
%86 = load i32*, i32** %7
%87 = call i32 @EVP_MD_CTX_destroy(i32* %86)
br label %88
88: ; preds = %85, %82
%89 = load i8*, i8** %11
store i8* %89, i8** %3
store i32 1, i32* %13
br label %90
90: ; preds = %88, %25, %21
%91 = load i8*, i8** %8
%92 = load i8*, i8** %3
ret i8* %92
}

LLVM IR (-O2):
@EVP_MAX_MD_SIZE = global i32 0
@.str = [5 x i8] c"%02x\00"
define i8* @key_signature(i32* %0, i64 %1) {
%3 = alloca i32
%4 = load i32, i32* @EVP_MAX_MD_SIZE
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%8 = tail call i32* (...) @EVP_md5()
%9 = tail call i32* (...) @EVP_md5()
%10 = icmp eq i32* %9, null
br i1 %10, label %53, label %11
11: ; preds = %2
%12 = tail call i32* (...) @EVP_MD_CTX_create()
%13 = icmp eq i32* %12, null
br i1 %13, label %53, label %14
14: ; preds = %11
%15 = tail call i32 @EVP_DigestInit_ex(i32* %12, i32* %9, i32* null)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %50
17: ; preds = %14
%18 = tail call i32 @EVP_DigestUpdate(i32* %12, i32* %0, i64 %1)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %50
20: ; preds = %17
%21 = call i32 @EVP_DigestFinal_ex(i32* %12, i8* %6, i32* %3)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %50
23: ; preds = %20
%24 = load i32, i32* %3
%25 = mul i32 %24, 3
%26 = call i8* @malloc(i32 %25)
%27 = icmp eq i8* %26, null
br i1 %27, label %50, label %28
28: ; preds = %23
%29 = icmp eq i32 %24, 0
br i1 %29, label %50, label %30
30: ; preds = %28, %30
%31 = phi i64 [ %47, %30 ], [ 0, %28 ]
%32 = trunc i64 %31 to i32
%33 = mul i32 %32, 3
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %26, i64 %34
%36 = getelementptr inbounds i8, i8* %6, i64 %31
%37 = load i8, i8* %36
%38 = call i32 @snprintf(i8* %35, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %37)
%39 = load i32, i32* %3
%40 = add i32 %39, -1
%41 = zext i32 %40 to i64
%42 = icmp eq i64 %31, %41
%43 = select i1 %42, i8 0, i8 58
%44 = add i32 %33, 2
%45 = zext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %26, i64 %45
store i8 %43, i8* %46
%47 = add nuw nsw i64 %31, 1
%48 = zext i32 %39 to i64
%49 = icmp ult i64 %47, %48
br i1 %49, label %30, label %50
50: ; preds = %30, %28, %20, %17, %14, %23
%51 = phi i8* [ null, %14 ], [ null, %17 ], [ null, %20 ], [ null, %23 ], [ %26, %28 ], [ %26, %30 ]
%52 = call i32 @EVP_MD_CTX_destroy(i32* %12)
br label %53
53: ; preds = %11, %2, %50
%54 = phi i8* [ %51, %50 ], [ null, %2 ], [ null, %11 ]
ret i8* %54
}