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

/* Variables and functions */
int /*<<< orphan*/ SHA256_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA256_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA256_Update(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int const);
int /*<<< orphan*/ perror(char *);
int sha256_compute(char *path, unsigned char hash[]) {
  SHA256_CTX sha256;
  FILE *file;
  char *buffer;
  const int buffer_size = 32768;
  int bytes_read = 0;
  file = fopen(path, "rb");
  if (file == NULL) {
    perror("fopen");
    return -1;
  }
  SHA256_Init(&sha256);
  buffer = malloc(buffer_size);
  if (buffer == NULL) {
    perror("malloc");
    return -1;
  }
  bytes_read = fread(buffer, 1, 0x10, file);
  SHA256_Update(&sha256, buffer, bytes_read);
  fread(buffer, 1, (0x200 - 0x10), file);
  while ((bytes_read = fread(buffer, 1, buffer_size, file))) {
    SHA256_Update(&sha256, buffer, bytes_read);
  }
  SHA256_Final(hash, &sha256);
  fclose(file);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<sha256_compute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x8000,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe47(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e5 <sha256_compute+0x55>
lea    0xe2f(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <sha256_compute+0x12e>
lea    -0x1c(%rbp),%rdi
call   1060 <SHA256_Init@plt>
mov    $0x8000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    121f <sha256_compute+0x8f>
lea    0xdfb(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <sha256_compute+0x12e>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1040 <fread@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%edx
lea    -0x1c(%rbp),%rdi
call   10b0 <SHA256_Update@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1f0,%edx
call   1040 <fread@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8000,%edx
call   1040 <fread@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
je     1298 <sha256_compute+0x108>
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%edx
lea    -0x1c(%rbp),%rdi
call   10b0 <SHA256_Update@plt>
jmp    1260 <sha256_compute+0xd0>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1080 <SHA256_Final@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha256_compute>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe5e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1281 <sha256_compute+0xf1>
mov    %rax,%r15
lea    0x4(%rsp),%rdi
call   1060 <SHA256_Init@plt>
mov    $0x8000,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     128a <sha256_compute+0xfa>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
lea    0x4(%rsp),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   10b0 <SHA256_Update@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1f0,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x8000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     1256 <sha256_compute+0xc6>
lea    0x4(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   10b0 <SHA256_Update@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x8000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jne    1230 <sha256_compute+0xa0>
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
call   1080 <SHA256_Final@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd7b(%rip),%rdi
jmp    1291 <sha256_compute+0x101>
lea    0xd78(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1275 <sha256_compute+0xe5>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [6 x i8] c"fopen\00"
@.str.2 = [7 x i8] c"malloc\00"
define i32 @sha256_compute(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 32768, i32* %9
store i32 0, i32* %10
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %7
%13 = load i32*, i32** %7
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
17: ; preds = %2
%18 = call i32 @SHA256_Init(i32* %6)
%19 = call i8* @malloc(i32 32768)
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
24: ; preds = %17
%25 = load i8*, i8** %8
%26 = load i32*, i32** %7
%27 = call i32 @fread(i8* %25, i32 1, i32 16, i32* %26)
store i32 %27, i32* %10
%28 = load i8*, i8** %8
%29 = load i32, i32* %10
%30 = call i32 @SHA256_Update(i32* %6, i8* %28, i32 %29)
%31 = load i8*, i8** %8
%32 = load i32*, i32** %7
%33 = call i32 @fread(i8* %31, i32 1, i32 496, i32* %32)
br label %34
34: ; preds = %39, %24
%35 = load i8*, i8** %8
%36 = load i32*, i32** %7
%37 = call i32 @fread(i8* %35, i32 1, i32 32768, i32* %36)
store i32 %37, i32* %10
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %34
%40 = load i8*, i8** %8
%41 = load i32, i32* %10
%42 = call i32 @SHA256_Update(i32* %6, i8* %40, i32 %41)
br label %34
43: ; preds = %34
%44 = load i8*, i8** %5
%45 = call i32 @SHA256_Final(i8* %44, i32* %6)
%46 = load i32*, i32** %7
%47 = call i32 @fclose(i32* %46)
%48 = load i8*, i8** %8
%49 = call i32 @free(i8* %48)
store i32 0, i32* %3
br label %50
50: ; preds = %43, %22, %15
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [6 x i8] c"fopen\00"
@.str.2 = [7 x i8] c"malloc\00"
define i32 @sha256_compute(i8* nocapture %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %30
9: ; preds = %2
%10 = call i32 @SHA256_Init(i32* %3)
%11 = call dereferenceable_or_null(32768) i8* @malloc(i32 32768)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %30
15: ; preds = %9
%16 = call i32 @fread(i8* %11, i32 1, i32 16, i32* %5)
%17 = call i32 @SHA256_Update(i32* %3, i8* %11, i32 %16)
%18 = call i32 @fread(i8* %11, i32 1, i32 496, i32* %5)
%19 = call i32 @fread(i8* %11, i32 1, i32 32768, i32* %5)
%20 = icmp eq i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %15, %21
%22 = phi i32 [ %24, %21 ], [ %19, %15 ]
%23 = call i32 @SHA256_Update(i32* %3, i8* %11, i32 %22)
%24 = call i32 @fread(i8* %11, i32 1, i32 32768, i32* %5)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %21
26: ; preds = %21, %15
%27 = call i32 @SHA256_Final(i8* %1, i32* %3)
%28 = call i32 @fclose(i32* %5)
%29 = call i32 @free(i8* %11)
br label %30
30: ; preds = %26, %13, %7
%31 = phi i32 [ -1, %7 ], [ -1, %13 ], [ 0, %26 ]
ret i32 %31
}