#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*/ *, unsigned char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int sha_file(const char *filename, unsigned char *md) {
  FILE *inFile;
  SHA256_CTX shaContext;
  int bytes;
  unsigned char data[256];
  if ((filename == NULL) || (md == NULL)) {
    ;
    return 0;
  }
  inFile = fopen(filename, "rb");
  if (inFile == NULL) {
    ;
    return 0;
  }
  SHA256_Init(&shaContext);
  while ((bytes = fread(data, 1, 256, inFile)) != 0) {
    SHA256_Update(&shaContext, data, bytes);
  }
  SHA256_Final(md, &shaContext);
  fclose(inFile);
  return 1;
}


x86 assembly (clang, -O0):
<sha_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1189 <sha_file+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    1195 <sha_file+0x35>
movl   $0x0,-0x4(%rbp)
jmp    1224 <sha_file+0xc4>
mov    -0x10(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c0 <sha_file+0x60>
movl   $0x0,-0x4(%rbp)
jmp    1224 <sha_file+0xc4>
lea    -0x24(%rbp),%rdi
call   1050 <SHA256_Init@plt>
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x100,%edx
call   1030 <fread@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     1207 <sha_file+0xa7>
lea    -0x130(%rbp),%rsi
mov    -0x28(%rbp),%edx
lea    -0x24(%rbp),%rdi
call   1080 <SHA256_Update@plt>
jmp    11c9 <sha_file+0x69>
mov    -0x18(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1060 <SHA256_Final@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
xor    %r15d,%r15d
test   %rdi,%rdi
je     120b <sha_file+0xab>
mov    %rsi,%r14
test   %rsi,%rsi
je     120b <sha_file+0xab>
lea    0xe72(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     120b <sha_file+0xab>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   1050 <SHA256_Init@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%r15d
mov    $0x1,%esi
mov    $0x100,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     11f6 <sha_file+0x96>
lea    0xc(%rsp),%r12
lea    0x10(%rsp),%rbp
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1080 <SHA256_Update@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x100,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jne    11d0 <sha_file+0x70>
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
call   1060 <SHA256_Final@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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