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

/* Variables and functions */
int MD5_DIGEST_LENGTH;
int /*<<< orphan*/ MD5_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ MD5_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ MD5_Update(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ strcat(char *, char *);
int http_md5sum(char *input, int len, char *out) {
  int ret = 0, i = 0;
  MD5_CTX ctx;
  char buf[3] = {'\0'};
  unsigned char md5[MD5_DIGEST_LENGTH];
  if (input == NULL || len < 1 || out == NULL)
    return -1;
  MD5_Init(&ctx);
  MD5_Update(&ctx, input, len);
  MD5_Final(md5, &ctx);
  out[0] = '\0';
  for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
    sprintf(buf, "%02x", md5[i]);
    strcat(out, buf);
  }
  return ret;
}


x86 assembly (clang, -O0):
<http_md5sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movb   $0x0,-0x2e(%rbp)
movw   $0x0,-0x30(%rbp)
mov    0x2e5e(%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,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c7 <http_md5sum+0x77>
cmpl   $0x1,-0x14(%rbp)
jl     11c7 <http_md5sum+0x77>
cmpq   $0x0,-0x20(%rbp)
jne    11da <http_md5sum+0x8a>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1264 <http_md5sum+0x114>
lea    -0x2c(%rbp),%rdi
call   1050 <MD5_Init@plt>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x2c(%rbp),%rdi
call   1070 <MD5_Update@plt>
mov    -0x50(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1030 <MD5_Final@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%eax
jge    1257 <http_md5sum+0x107>
mov    -0x50(%rbp),%rax
lea    -0x30(%rbp),%rdi
movslq -0x28(%rbp),%rcx
lea    0xdcd(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1060 <sprintf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <strcat@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    120e <http_md5sum+0xbe>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_md5sum>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,%r13
movb   $0x0,-0x2a(%rbp)
movw   $0x0,-0x2c(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1234 <http_md5sum+0xe4>
mov    %esi,%r12d
test   %esi,%esi
jle    1234 <http_md5sum+0xe4>
mov    %rdx,%r14
test   %rdx,%rdx
je     1234 <http_md5sum+0xe4>
mov    %rdi,%rbx
lea    -0x30(%rbp),%rdi
call   1050 <MD5_Init@plt>
lea    -0x30(%rbp),%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1070 <MD5_Update@plt>
mov    %r15,%rdi
lea    -0x30(%rbp),%rsi
call   1030 <MD5_Final@plt>
movb   $0x0,(%r14)
mov    0x2e01(%rip),%rax
cmpl   $0x0,(%rax)
jle    1232 <http_md5sum+0xe2>
mov    %r13,-0x38(%rbp)
lea    0xe11(%rip),%r12
lea    -0x2c(%rbp),%rbx
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
movzbl (%r15,%r13,1),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcat@plt>
add    $0x1,%r13
mov    0x2dba(%rip),%rax
movslq (%rax),%rax
cmp    %rax,%r13
jl     1200 <http_md5sum+0xb0>
mov    -0x38(%rbp),%r13
xor    %eax,%eax
mov    %r13,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MD5_DIGEST_LENGTH = global i32 0
@.str = [5 x i8] c"%02x\00"
define i32 @http_md5sum(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [3 x i8]
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 0, i32* %8
store i32 0, i32* %9
%15 = bitcast [3 x i8]* %11 to i8*
%16 = load i32, i32* @MD5_DIGEST_LENGTH
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %12
%19 = alloca i8, i64 %17
store i64 %17, i64* %13
%20 = load i8*, i8** %5
%21 = icmp eq i8* %20, null
br i1 %21, label %28, label %22
22: ; preds = %3
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 1
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i8*, i8** %7
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %25, %22, %3
store i32 -1, i32* %4
store i32 1, i32* %14
br label %56
29: ; preds = %25
%30 = call i32 @MD5_Init(i32* %10)
%31 = load i8*, i8** %5
%32 = load i32, i32* %6
%33 = call i32 @MD5_Update(i32* %10, i8* %31, i32 %32)
%34 = call i32 @MD5_Final(i8* %19, i32* %10)
%35 = load i8*, i8** %7
%36 = getelementptr inbounds i8, i8* %35, i64 0
store i8 0, i8* %36
store i32 0, i32* %9
br label %37
37: ; preds = %51, %29
%38 = load i32, i32* %9
%39 = load i32, i32* @MD5_DIGEST_LENGTH
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %54
41: ; preds = %37
%42 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 0
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %19, i64 %44
%46 = load i8, i8* %45
%47 = call i32 @sprintf(i8* %42, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %46)
%48 = load i8*, i8** %7
%49 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 0
%50 = call i32 @strcat(i8* %48, i8* %49)
br label %51
51: ; preds = %41
%52 = load i32, i32* %9
%53 = add nsw i32 %52, 1
store i32 %53, i32* %9
br label %37
54: ; preds = %37
%55 = load i32, i32* %8
store i32 %55, i32* %4
store i32 1, i32* %14
br label %56
56: ; preds = %54, %28
%57 = load i8*, i8** %12
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
@MD5_DIGEST_LENGTH = global i32 0
@.str = [5 x i8] c"%02x\00"
define i32 @http_md5sum(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca [3 x i8]
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* @MD5_DIGEST_LENGTH
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = icmp eq i8* %0, null
%13 = icmp slt i32 %1, 1
%14 = or i1 %12, %13
%15 = icmp eq i8* %2, null
%16 = or i1 %14, %15
br i1 %16, label %33, label %17
17: ; preds = %3
%18 = call i32 @MD5_Init(i32* %4)
%19 = call i32 @MD5_Update(i32* %4, i8* %0, i32 %1)
%20 = call i32 @MD5_Final(i8* %11, i32* %4)
store i8 0, i8* %2
%21 = load i32, i32* @MD5_DIGEST_LENGTH
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %17, %23
%24 = phi i64 [ %29, %23 ], [ 0, %17 ]
%25 = getelementptr inbounds i8, i8* %11, i64 %24
%26 = load i8, i8* %25
%27 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %26)
%28 = call i32 @strcat(i8* %2, i8* %7)
%29 = add nuw nsw i64 %24, 1
%30 = load i32, i32* @MD5_DIGEST_LENGTH
%31 = sext i32 %30 to i64
%32 = icmp slt i64 %29, %31
br i1 %32, label %23, label %33
33: ; preds = %23, %17, %3
%34 = phi i32 [ -1, %3 ], [ 0, %17 ], [ 0, %23 ]
ret i32 %34
}