#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 SHA_DIGEST_LENGTH;
int /*<<< orphan*/ SHA_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA_Update(int /*<<< orphan*/ *, char const *, int);
int /*<<< orphan*/ memcpy(char *, unsigned char *, int);
int /*<<< orphan*/ memset(unsigned char *, int, int);
int openssl_hash_sha1(void *data, int size, char *key, int sizep,
                      const char *passphrase) {
  int len = SHA_DIGEST_LENGTH;
  int round, i;
  unsigned char md[SHA_DIGEST_LENGTH];
  SHA_CTX ctx;
  if (!SHA_Init(&ctx))
    return -1;
  for (round = 0; size; round++) {
    for (i = 0; i < round; i++)
      if (!SHA_Update(&ctx, "A", 1))
        return -1;
    if (!SHA_Update(&ctx, passphrase, sizep))
      return -1;
    if (!SHA_Final(md, &ctx))
      return -1;
    if (len > size)
      len = size;
    memcpy(key, md, len);
    key += len;
    size -= len;
  }
  memset(md, 0x00, SHA_DIGEST_LENGTH);
  return 0;
}


x86 assembly (clang, -O0):
<openssl_hash_sha1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,-0x34(%rbp)
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
lea    -0x54(%rbp),%rdi
call   1060 <SHA_Init@plt>
cmp    $0x0,%eax
jne    11c0 <openssl_hash_sha1+0x70>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x58(%rbp)
jmp    12eb <openssl_hash_sha1+0x19b>
movl   $0x0,-0x38(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12c9 <openssl_hash_sha1+0x179>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    1228 <openssl_hash_sha1+0xd8>
lea    -0x54(%rbp),%rdi
lea    0xe11(%rip),%rsi
mov    $0x1,%edx
call   1030 <SHA_Update@plt>
cmp    $0x0,%eax
jne    1215 <openssl_hash_sha1+0xc5>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x58(%rbp)
jmp    12eb <openssl_hash_sha1+0x19b>
jmp    121a <openssl_hash_sha1+0xca>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11d8 <openssl_hash_sha1+0x88>
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    -0x54(%rbp),%rdi
call   1030 <SHA_Update@plt>
cmp    $0x0,%eax
jne    1254 <openssl_hash_sha1+0x104>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x58(%rbp)
jmp    12eb <openssl_hash_sha1+0x19b>
mov    -0x60(%rbp),%rdi
lea    -0x54(%rbp),%rsi
call   1070 <SHA_Final@plt>
cmp    $0x0,%eax
jne    127d <openssl_hash_sha1+0x12d>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x58(%rbp)
jmp    12eb <openssl_hash_sha1+0x19b>
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    128f <openssl_hash_sha1+0x13f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x60(%rbp),%rsi
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x34(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11c7 <openssl_hash_sha1+0x77>
mov    -0x60(%rbp),%rdi
mov    0x2d14(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x58(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openssl_hash_sha1>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,-0x50(%rbp)
mov    %ecx,-0x38(%rbp)
mov    %rdx,%r14
mov    %esi,%r12d
mov    0x2e73(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x30(%rbp),%rdi
call   1060 <SHA_Init@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
je     127d <openssl_hash_sha1+0x12d>
mov    %r13,-0x48(%rbp)
mov    %rbx,-0x40(%rbp)
mov    %r12d,-0x2c(%rbp)
test   %r12d,%r12d
je     125e <openssl_hash_sha1+0x10e>
xor    %r12d,%r12d
lea    -0x30(%rbp),%r13
lea    0xe3d(%rip),%rbx
mov    %r12d,%r15d
test   %r12d,%r12d
je     11ee <openssl_hash_sha1+0x9e>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <SHA_Update@plt>
test   %eax,%eax
je     1277 <openssl_hash_sha1+0x127>
add    $0xffffffff,%r15d
jne    11d0 <openssl_hash_sha1+0x80>
mov    %r13,%rdi
mov    -0x50(%rbp),%rsi
mov    -0x38(%rbp),%edx
call   1030 <SHA_Update@plt>
test   %eax,%eax
je     1277 <openssl_hash_sha1+0x127>
mov    %r12d,-0x34(%rbp)
mov    -0x40(%rbp),%r12
mov    %r12,%rdi
mov    %r13,%rsi
call   1070 <SHA_Final@plt>
test   %eax,%eax
je     1277 <openssl_hash_sha1+0x127>
mov    -0x2c(%rbp),%r15d
mov    -0x48(%rbp),%rbx
cmp    %r15d,%ebx
cmovg  %r15d,%ebx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
mov    %rbx,-0x48(%rbp)
movslq %ebx,%rax
lea    -0x30(%rbp),%r13
add    %rax,%r14
mov    -0x34(%rbp),%r12d
add    $0x1,%r12d
sub    %eax,%r15d
mov    %r15d,-0x2c(%rbp)
lea    0xda8(%rip),%rbx
jne    11c3 <openssl_hash_sha1+0x73>
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    -0x40(%rbp),%rdi
xor    %esi,%esi
call   1040 <memset@plt>
jmp    127d <openssl_hash_sha1+0x12d>
mov    $0xffffffff,%r15d
mov    %r15d,%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 = [2 x i8] c"A\00"
define i32 @openssl_hash_sha1(i8* %0, i32 %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i8* %4, i8** %11
%19 = load i32, i32* @SHA_DIGEST_LENGTH
store i32 %19, i32* %12
%20 = load i32, i32* @SHA_DIGEST_LENGTH
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %15
%23 = alloca i8, i64 %21
store i64 %21, i64* %16
%24 = call i32 @SHA_Init(i32* %17)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %5
store i32 -1, i32* %6
store i32 1, i32* %18
br label %77
27: ; preds = %5
store i32 0, i32* %13
br label %28
28: ; preds = %71, %27
%29 = load i32, i32* %8
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %74
31: ; preds = %28
store i32 0, i32* %14
br label %32
32: ; preds = %41, %31
%33 = load i32, i32* %14
%34 = load i32, i32* %13
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %44
36: ; preds = %32
%37 = call i32 @SHA_Update(i32* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %36
store i32 -1, i32* %6
store i32 1, i32* %18
br label %77
40: ; preds = %36
br label %41
41: ; preds = %40
%42 = load i32, i32* %14
%43 = add nsw i32 %42, 1
store i32 %43, i32* %14
br label %32
44: ; preds = %32
%45 = load i8*, i8** %11
%46 = load i32, i32* %10
%47 = call i32 @SHA_Update(i32* %17, i8* %45, i32 %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %44
store i32 -1, i32* %6
store i32 1, i32* %18
br label %77
50: ; preds = %44
%51 = call i32 @SHA_Final(i8* %23, i32* %17)
%52 = icmp ne i32 %51, 0
br i1 %52, label %54, label %53
53: ; preds = %50
store i32 -1, i32* %6
store i32 1, i32* %18
br label %77
54: ; preds = %50
%55 = load i32, i32* %12
%56 = load i32, i32* %8
%57 = icmp sgt i32 %55, %56
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = load i32, i32* %8
store i32 %59, i32* %12
br label %60
60: ; preds = %58, %54
%61 = load i8*, i8** %9
%62 = load i32, i32* %12
%63 = call i32 @memcpy(i8* %61, i8* %23, i32 %62)
%64 = load i32, i32* %12
%65 = load i8*, i8** %9
%66 = sext i32 %64 to i64
%67 = getelementptr inbounds i8, i8* %65, i64 %66
store i8* %67, i8** %9
%68 = load i32, i32* %12
%69 = load i32, i32* %8
%70 = sub nsw i32 %69, %68
store i32 %70, i32* %8
br label %71
71: ; preds = %60
%72 = load i32, i32* %13
%73 = add nsw i32 %72, 1
store i32 %73, i32* %13
br label %28
74: ; preds = %28
%75 = load i32, i32* @SHA_DIGEST_LENGTH
%76 = call i32 @memset(i8* %23, i32 0, i32 %75)
store i32 0, i32* %6
store i32 1, i32* %18
br label %77
77: ; preds = %74, %53, %49, %39, %26
%78 = load i8*, i8** %15
%79 = load i32, i32* %6
ret i32 %79
}

LLVM IR (-O2):
@SHA_DIGEST_LENGTH = global i32 0
@.str = [2 x i8] c"A\00"
define i32 @openssl_hash_sha1(i8* nocapture readnone %0, i32 %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = load i32, i32* @SHA_DIGEST_LENGTH
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = bitcast i32* %6 to i8*
%11 = call i32 @SHA_Init(i32* %6)
%12 = icmp eq i32 %11, 0
br i1 %12, label %46, label %13
13: ; preds = %5
%14 = icmp eq i32 %1, 0
br i1 %14, label %43, label %15
15: ; preds = %13, %34
%16 = phi i32 [ %40, %34 ], [ %1, %13 ]
%17 = phi i32 [ %41, %34 ], [ 0, %13 ]
%18 = phi i32 [ %36, %34 ], [ %7, %13 ]
%19 = phi i8* [ %39, %34 ], [ %2, %13 ]
%20 = icmp eq i32 %17, 0
br i1 %20, label %28, label %23
21: ; preds = %23
%22 = icmp eq i32 %27, %17
br i1 %22, label %28, label %23
23: ; preds = %15, %21
%24 = phi i32 [ %27, %21 ], [ 0, %15 ]
%25 = call i32 @SHA_Update(i32* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%26 = icmp eq i32 %25, 0
%27 = add nuw nsw i32 %24, 1
br i1 %26, label %46, label %21
28: ; preds = %21, %15
%29 = call i32 @SHA_Update(i32* %6, i8* %4, i32 %3)
%30 = icmp eq i32 %29, 0
br i1 %30, label %46, label %31
31: ; preds = %28
%32 = call i32 @SHA_Final(i8* %9, i32* %6)
%33 = icmp eq i32 %32, 0
br i1 %33, label %46, label %34
34: ; preds = %31
%35 = icmp sgt i32 %18, %16
%36 = select i1 %35, i32 %16, i32 %18
%37 = call i32 @memcpy(i8* %19, i8* %9, i32 %36)
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds i8, i8* %19, i64 %38
%40 = sub nsw i32 %16, %36
%41 = add nuw nsw i32 %17, 1
%42 = icmp eq i32 %40, 0
br i1 %42, label %43, label %15
43: ; preds = %34, %13
%44 = load i32, i32* @SHA_DIGEST_LENGTH
%45 = call i32 @memset(i8* %9, i32 0, i32 %44)
br label %46
46: ; preds = %31, %28, %23, %5, %43
%47 = phi i32 [ 0, %43 ], [ -1, %5 ], [ -1, %23 ], [ -1, %28 ], [ -1, %31 ]
ret i32 %47
}