#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*/ chunk;
typedef int /*<<< orphan*/ SHA_CTX;
typedef int /*<<< orphan*/ FILE;

/* 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 fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int HashStream(FILE *fp, unsigned char *hash, int bytes_to_read) {
  SHA_CTX ctx;
  const int CHUNK_SIZE = 1 * 1024;
  unsigned char chunk[CHUNK_SIZE];
  int bytes_read;
  int total_read = 0;
  SHA1_Init(&ctx);
  while (
      bytes_to_read > 0 &&
      (bytes_read = fread(
           chunk, sizeof(char),
           ((bytes_to_read) < (sizeof(chunk)) ? bytes_to_read : sizeof(chunk)),
           fp)) != 0) {
    SHA1_Update(&ctx, chunk, bytes_read);
    total_read += bytes_read;
    bytes_to_read -= bytes_read;
  }
  SHA1_Final(hash, &ctx);
  return total_read;
}


x86 assembly (clang, -O0):
<HashStream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x400,-0x1c(%rbp)
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <SHA1_Init@plt>
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x431(%rbp)
jle    11e4 <HashStream+0xa4>
movslq -0x14(%rbp),%rax
cmp    $0x400,%rax
jae    11a6 <HashStream+0x66>
movslq -0x14(%rbp),%rax
mov    %rax,-0x440(%rbp)
jmp    11b7 <HashStream+0x77>
mov    $0x400,%eax
mov    %rax,-0x440(%rbp)
jmp    11b7 <HashStream+0x77>
mov    -0x440(%rbp),%rax
mov    %eax,%edx
mov    -0x8(%rbp),%rcx
lea    -0x430(%rbp),%rdi
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x431(%rbp)
mov    -0x431(%rbp),%al
test   $0x1,%al
jne    11f7 <HashStream+0xb7>
jmp    1223 <HashStream+0xe3>
mov    -0x2c(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1040 <SHA1_Update@plt>
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1174 <HashStream+0x34>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <SHA1_Final@plt>
mov    -0x30(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HashStream>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rbp
lea    0xc(%rsp),%rdi
call   1050 <SHA1_Init@plt>
test   %ebx,%ebx
mov    %rbp,0x18(%rsp)
mov    %r14,0x10(%rsp)
jle    11bc <HashStream+0x7c>
xor    %ebp,%ebp
lea    0x20(%rsp),%r12
lea    0xc(%rsp),%r13
cmp    $0x400,%ebx
mov    $0x400,%edx
cmovb  %ebx,%edx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     11be <HashStream+0x7e>
mov    %eax,%r14d
mov    %r13,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <SHA1_Update@plt>
add    %r14d,%ebp
sub    %r14d,%ebx
jg     1180 <HashStream+0x40>
jmp    11be <HashStream+0x7e>
xor    %ebp,%ebp
lea    0xc(%rsp),%rsi
mov    0x10(%rsp),%rdi
call   1060 <SHA1_Final@plt>
mov    0x18(%rsp),%rsp
mov    %ebp,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @HashStream(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 1024, i32* %8
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %9
%13 = alloca i8, i64 1024
store i32 0, i32* %11
%14 = call i32 @SHA1_Init(i32* %7)
br label %15
15: ; preds = %34, %3
%16 = load i32, i32* %6
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %32
18: ; preds = %15
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = icmp ult i64 %20, 1024
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
br label %26
25: ; preds = %18
br label %26
26: ; preds = %25, %22
%27 = phi i64 [ %24, %22 ], [ 1024, %25 ]
%28 = trunc i64 %27 to i32
%29 = load i32*, i32** %4
%30 = call i32 @fread(i8* %13, i32 1, i32 %28, i32* %29)
store i32 %30, i32* %10
%31 = icmp ne i32 %30, 0
br label %32
32: ; preds = %26, %15
%33 = phi i1 [ false, %15 ], [ %31, %26 ]
br i1 %33, label %34, label %43
34: ; preds = %32
%35 = load i32, i32* %10
%36 = call i32 @SHA1_Update(i32* %7, i8* %13, i32 %35)
%37 = load i32, i32* %10
%38 = load i32, i32* %11
%39 = add nsw i32 %38, %37
store i32 %39, i32* %11
%40 = load i32, i32* %10
%41 = load i32, i32* %6
%42 = sub nsw i32 %41, %40
store i32 %42, i32* %6
br label %15
43: ; preds = %32
%44 = load i8*, i8** %5
%45 = call i32 @SHA1_Final(i8* %44, i32* %7)
%46 = load i32, i32* %11
%47 = load i8*, i8** %9
ret i32 %46
}

LLVM IR (-O2):
define i32 @HashStream(i32* nocapture %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i8* @llvm.stacksave()
%7 = alloca [1024 x i8]
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%9 = call i32 @SHA1_Init(i32* %4)
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %23
11: ; preds = %3, %18
%12 = phi i32 [ %20, %18 ], [ 0, %3 ]
%13 = phi i32 [ %21, %18 ], [ %2, %3 ]
%14 = icmp ult i32 %13, 1024
%15 = select i1 %14, i32 %13, i32 1024
%16 = call i32 @fread(i8* %8, i32 1, i32 %15, i32* %0)
%17 = icmp eq i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %11
%19 = call i32 @SHA1_Update(i32* %4, i8* %8, i32 %16)
%20 = add nsw i32 %16, %12
%21 = sub nsw i32 %13, %16
%22 = icmp sgt i32 %21, 0
br i1 %22, label %11, label %23
23: ; preds = %11, %18, %3
%24 = phi i32 [ 0, %3 ], [ %20, %18 ], [ %12, %11 ]
%25 = call i32 @SHA1_Final(i8* %1, i32* %4)
ret i32 %24
}