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

/* Variables and functions */
int /*<<< orphan*/ SHA1_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Update(int /*<<< orphan*/ *, unsigned char *, int);
int SHA_DIGEST_LENGTH;
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int sha1encrypt(unsigned char *data, char **out, int *out_len) {
  SHA_CTX ctx;
  unsigned char md[SHA_DIGEST_LENGTH];
  char buf[64] = {'\0'};
  char tmp[3] = {'\0'};
  int i;
  SHA1_Init(&ctx);
  SHA1_Update(&ctx, data, strlen((char *)data));
  SHA1_Final(md, &ctx);
  for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
    sprintf(tmp, "%02X", md[i]);
    strcat(buf, tmp);
  }
  *out_len = strlen(buf);
  *out = (char *)malloc(*out_len + 1);
  strcpy(*out, buf);
  return 0;
}


x86 assembly (clang, -O0):
<sha1encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e3a(%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,-0x80(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
lea    -0x73(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1050 <memset@plt>
lea    -0x1c(%rbp),%rdi
call   1080 <SHA1_Init@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x88(%rbp),%rsi
mov    %eax,%edx
lea    -0x1c(%rbp),%rdi
call   1070 <SHA1_Update@plt>
mov    -0x80(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   10b0 <SHA1_Final@plt>
movl   $0x0,-0x78(%rbp)
mov    -0x78(%rbp),%eax
mov    0x2da8(%rip),%rcx
cmp    (%rcx),%eax
jge    127f <sha1encrypt+0xef>
mov    -0x80(%rbp),%rax
lea    -0x73(%rbp),%rdi
movslq -0x78(%rbp),%rcx
lea    0xda5(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   10a0 <sprintf@plt>
lea    -0x70(%rbp),%rdi
lea    -0x73(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    1236 <sha1encrypt+0xa6>
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x70(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha1encrypt>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdx,-0x38(%rbp)
mov    %rsi,-0x40(%rbp)
mov    %rdi,%r12
mov    %rsp,-0x48(%rbp)
mov    0x2e41(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,-0x60(%rbp)
movaps %xmm0,-0x70(%rbp)
movaps %xmm0,-0x80(%rbp)
movaps %xmm0,-0x90(%rbp)
movb   $0x0,-0x2a(%rbp)
movw   $0x0,-0x2c(%rbp)
lea    -0x4c(%rbp),%rbx
mov    %rbx,%rdi
call   1070 <SHA1_Init@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1060 <SHA1_Update@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   10a0 <SHA1_Final@plt>
cmpl   $0x0,(%r14)
jle    124d <sha1encrypt+0xcd>
mov    %r14,%r12
xor    %r15d,%r15d
lea    -0x2c(%rbp),%rbx
lea    -0x90(%rbp),%r14
xchg   %ax,%ax
movzbl 0x0(%r13,%r15,1),%edx
mov    %rbx,%rdi
lea    0xdd0(%rip),%rsi
call   1090 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <strcat@plt>
add    $0x1,%r15
movslq (%r12),%rax
cmp    %rax,%r15
jl     1220 <sha1encrypt+0xa0>
lea    -0x90(%rbp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rcx
mov    %eax,(%rcx)
lea    0x1(%rax),%edi
call   1050 <malloc@plt>
mov    -0x40(%rbp),%rcx
mov    %rax,(%rcx)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    -0x48(%rbp),%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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