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

/* Variables and functions */
int /*<<< orphan*/ EVP_DigestFinal_ex(int /*<<< orphan*/ *, unsigned char *,
                                      unsigned int *);
int /*<<< orphan*/ EVP_DigestInit_ex(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ const *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DigestUpdate(int /*<<< orphan*/ *, char const *,
                                    int /*<<< orphan*/);
int EVP_MAX_MD_SIZE;
int /*<<< orphan*/ EVP_MD_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_MD_CTX_new();
int /*<<< orphan*/ *EVP_get_digestbyname(char *);
int /*<<< orphan*/ OpenSSL_add_all_digests();
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ strlen(char const *);
char *md5_hash(const char *msg) {
  EVP_MD_CTX *mdctx;
  const EVP_MD *md;
  unsigned char md_value[EVP_MAX_MD_SIZE];
  unsigned int md_len, i;
  char *buf = (char *)malloc(EVP_MAX_MD_SIZE * sizeof(char));
  bzero(buf, EVP_MAX_MD_SIZE);
  OpenSSL_add_all_digests();
  md = EVP_get_digestbyname("md5");
  if (!md) {
    free(buf);
    return NULL;
  }
  mdctx = EVP_MD_CTX_new();
  EVP_DigestInit_ex(mdctx, md, NULL);
  EVP_DigestUpdate(mdctx, msg, strlen(msg));
  EVP_DigestFinal_ex(mdctx, md_value, &md_len);
  EVP_MD_CTX_free(mdctx);
  for (i = 0; i < md_len; i++)
    sprintf(buf + i + i, "%02x", md_value[i]);
  return buf;
}


x86 assembly (clang, -O0):
<md5_hash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    0x2de9(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
call   10d0 <bzero@plt>
mov    $0x0,%al
call   1080 <OpenSSL_add_all_digests@plt>
lea    0xdcf(%rip),%rdi
call   10b0 <EVP_get_digestbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1262 <md5_hash+0xa2>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1317 <md5_hash+0x157>
mov    $0x0,%al
call   10c0 <EVP_MD_CTX_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <EVP_DigestInit_ex@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1090 <EVP_DigestUpdate@plt>
mov    -0x50(%rbp),%rsi
mov    -0x18(%rbp),%rdi
lea    -0x34(%rbp),%rdx
call   1050 <EVP_DigestFinal_ex@plt>
mov    -0x18(%rbp),%rdi
call   1040 <EVP_MD_CTX_free@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jae    1308 <md5_hash+0x148>
mov    -0x50(%rbp),%rax
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%ecx
add    %rcx,%rdi
mov    -0x38(%rbp),%ecx
add    %rcx,%rdi
mov    -0x38(%rbp),%ecx
lea    0xd13(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   10e0 <sprintf@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    12c7 <md5_hash+0x107>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<md5_hash>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2e10(%rip),%rbx
mov    (%rbx),%edi
mov    %rsp,%r12
mov    %rdi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    (%rbx),%esi
mov    %rax,%rdi
call   10d0 <bzero@plt>
xor    %eax,%eax
call   1080 <OpenSSL_add_all_digests@plt>
lea    0xdf2(%rip),%rdi
call   10b0 <EVP_get_digestbyname@plt>
test   %rax,%rax
je     1292 <md5_hash+0xd2>
mov    %rax,%rbx
xor    %eax,%eax
call   10c0 <EVP_MD_CTX_new@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1070 <EVP_DigestInit_ex@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1090 <EVP_DigestUpdate@plt>
lea    -0x2c(%rbp),%rdx
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <EVP_DigestFinal_ex@plt>
mov    %r13,%rdi
call   1040 <EVP_MD_CTX_free@plt>
cmpl   $0x0,-0x2c(%rbp)
je     129d <md5_hash+0xdd>
xor    %ebx,%ebx
lea    0xd97(%rip),%r15
mov    %r14,%r13
movzbl (%r12,%rbx,1),%edx
mov    %r13,%rdi
mov    %r15,%rsi
call   10e0 <sprintf@plt>
add    $0x1,%rbx
mov    -0x2c(%rbp),%eax
add    $0x2,%r13
cmp    %rax,%rbx
jb     1270 <md5_hash+0xb0>
jmp    129d <md5_hash+0xdd>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%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 = [4 x i8] c"md5\00"
@.str.1 = [5 x i8] c"%02x\00"
define i8* @md5_hash(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %3
%12 = load i32, i32* @EVP_MAX_MD_SIZE
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
%16 = load i32, i32* @EVP_MAX_MD_SIZE
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 1
%19 = trunc i64 %18 to i32
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %10
%22 = load i8*, i8** %10
%23 = load i32, i32* @EVP_MAX_MD_SIZE
%24 = call i32 @bzero(i8* %22, i32 %23)
%25 = call i32 (...) @OpenSSL_add_all_digests()
%26 = call i32* @EVP_get_digestbyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %26, i32** %5
%27 = load i32*, i32** %5
%28 = icmp ne i32* %27, null
br i1 %28, label %32, label %29
29: ; preds = %1
%30 = load i8*, i8** %10
%31 = call i32 @free(i8* %30)
store i8* null, i8** %2
store i32 1, i32* %11
br label %68
32: ; preds = %1
%33 = call i32* (...) @EVP_MD_CTX_new()
store i32* %33, i32** %4
%34 = load i32*, i32** %4
%35 = load i32*, i32** %5
%36 = call i32 @EVP_DigestInit_ex(i32* %34, i32* %35, i32* null)
%37 = load i32*, i32** %4
%38 = load i8*, i8** %3
%39 = load i8*, i8** %3
%40 = call i32 @strlen(i8* %39)
%41 = call i32 @EVP_DigestUpdate(i32* %37, i8* %38, i32 %40)
%42 = load i32*, i32** %4
%43 = call i32 @EVP_DigestFinal_ex(i32* %42, i8* %15, i32* %8)
%44 = load i32*, i32** %4
%45 = call i32 @EVP_MD_CTX_free(i32* %44)
store i32 0, i32* %9
br label %46
46: ; preds = %63, %32
%47 = load i32, i32* %9
%48 = load i32, i32* %8
%49 = icmp ult i32 %47, %48
br i1 %49, label %50, label %66
50: ; preds = %46
%51 = load i8*, i8** %10
%52 = load i32, i32* %9
%53 = zext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = load i32, i32* %9
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i32, i32* %9
%59 = zext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %15, i64 %59
%61 = load i8, i8* %60
%62 = call i32 @sprintf(i8* %57, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %61)
br label %63
63: ; preds = %50
%64 = load i32, i32* %9
%65 = add i32 %64, 1
store i32 %65, i32* %9
br label %46
66: ; preds = %46
%67 = load i8*, i8** %10
store i8* %67, i8** %2
store i32 1, i32* %11
br label %68
68: ; preds = %66, %29
%69 = load i8*, i8** %6
%70 = load i8*, i8** %2
ret i8* %70
}

LLVM IR (-O2):
@EVP_MAX_MD_SIZE = global i32 0
@.str = [4 x i8] c"md5\00"
@.str.1 = [5 x i8] c"%02x\00"
define i8* @md5_hash(i8* %0) {
%2 = alloca i32
%3 = load i32, i32* @EVP_MAX_MD_SIZE
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = bitcast i32* %2 to i8*
%7 = tail call i64 @malloc(i32 %3)
%8 = inttoptr i64 %7 to i8*
%9 = load i32, i32* @EVP_MAX_MD_SIZE
%10 = tail call i32 @bzero(i8* %8, i32 %9)
%11 = tail call i32 (...) @OpenSSL_add_all_digests()
%12 = tail call i32* @EVP_get_digestbyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = tail call i32 @free(i8* %8)
br label %36
16: ; preds = %1
%17 = tail call i32* (...) @EVP_MD_CTX_new()
%18 = tail call i32 @EVP_DigestInit_ex(i32* %17, i32* %12, i32* null)
%19 = tail call i32 @strlen(i8* %0)
%20 = tail call i32 @EVP_DigestUpdate(i32* %17, i8* %0, i32 %19)
%21 = call i32 @EVP_DigestFinal_ex(i32* %17, i8* %5, i32* %2)
%22 = call i32 @EVP_MD_CTX_free(i32* %17)
%23 = load i32, i32* %2
%24 = icmp eq i32 %23, 0
br i1 %24, label %36, label %25
25: ; preds = %16, %25
%26 = phi i64 [ %32, %25 ], [ 0, %16 ]
%27 = getelementptr inbounds i8, i8* %8, i64 %26
%28 = getelementptr inbounds i8, i8* %27, i64 %26
%29 = getelementptr inbounds i8, i8* %5, i64 %26
%30 = load i8, i8* %29
%31 = call i32 @sprintf(i8* dereferenceable(1) %28, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %30)
%32 = add nuw nsw i64 %26, 1
%33 = load i32, i32* %2
%34 = zext i32 %33 to i64
%35 = icmp ult i64 %32, %34
br i1 %35, label %25, label %36
36: ; preds = %25, %16, %14
%37 = phi i8* [ null, %14 ], [ %8, %16 ], [ %8, %25 ]
ret i8* %37
}