#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 SHA256_DIGEST_LENGTH;
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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fread(unsigned char **, int, int, int /*<<< orphan*/ *);
char *sha2string(unsigned char *);
int /*<<< orphan*/ stderr;
char *calculate_sha(char *filename) {
  unsigned char sha[SHA256_DIGEST_LENGTH];
  FILE *fd;
  SHA256_CTX sha256;
  unsigned char *buf[1024];
  int rbytes;
  fd = fopen(filename, "rb");
  if (!fd) {
    fprintf(stderr,
            "turnin: Failed to open turned in file '%s' for sha-digest.\n"
            "        Please notify the Instructor or a TA.\n",
            filename);
    exit(1);
  }
  SHA256_Init(&sha256);
  while ((rbytes = fread(buf, 1, 1024, fd))) {
    SHA256_Update(&sha256, buf, rbytes);
  }
  SHA256_Final(sha, &sha256);
  fclose(fd);
  return sha2string(sha);
}


x86 assembly (clang, -O0):
<calculate_sha>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x2040(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe27(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1210 <calculate_sha+0x80>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe02(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x24(%rbp),%rdi
call   1050 <SHA256_Init@plt>
lea    -0x2030(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x2034(%rbp)
cmp    $0x0,%eax
je     125d <calculate_sha+0xcd>
lea    -0x2030(%rbp),%rsi
mov    -0x2034(%rbp),%edx
lea    -0x24(%rbp),%rdi
call   10a0 <SHA256_Update@plt>
jmp    1219 <calculate_sha+0x89>
mov    -0x2040(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1070 <SHA256_Final@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2040(%rbp),%rdi
call   1080 <sha2string@plt>
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_sha>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2010,%rsp
mov    %rdi,%r15
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe3a(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     125f <calculate_sha+0xcf>
mov    %rax,%r12
lea    -0x24(%rbp),%rdi
call   1050 <SHA256_Init@plt>
lea    -0x2030(%rbp),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     1236 <calculate_sha+0xa6>
lea    -0x24(%rbp),%r15
lea    -0x2030(%rbp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   10a0 <SHA256_Update@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jne    1210 <calculate_sha+0x80>
lea    -0x24(%rbp),%rsi
mov    %r14,%rdi
call   1070 <SHA256_Final@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1080 <sha2string@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@SHA256_DIGEST_LENGTH = global i32 0
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [106 x i8] c"turnin: Failed to open turned in file '%s' for sha-digest.\0A Please notify the Instructor or a TA.\0A\00"
define i8* @calculate_sha(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [1024 x i8*]
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i32, i32* @SHA256_DIGEST_LENGTH
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i8, i64 %10
store i64 %10, i64* %4
%13 = load i8*, i8** %2
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %5
%15 = load i32*, i32** %5
%16 = icmp ne i32* %15, null
br i1 %16, label %22, label %17
17: ; preds = %1
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %2
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([106 x i8], [106 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %1
%23 = call i32 @SHA256_Init(i32* %6)
br label %24
24: ; preds = %29, %22
%25 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %7, i64 0, i64 0
%26 = load i32*, i32** %5
%27 = call i32 @fread(i8** %25, i32 1, i32 1024, 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 [1024 x i8*], [1024 x i8*]* %7, i64 0, i64 0
%31 = load i32, i32* %8
%32 = call i32 @SHA256_Update(i32* %6, i8** %30, i32 %31)
br label %24
33: ; preds = %24
%34 = call i32 @SHA256_Final(i8* %12, i32* %6)
%35 = load i32*, i32** %5
%36 = call i32 @fclose(i32* %35)
%37 = call i8* @sha2string(i8* %12)
%38 = load i8*, i8** %3
ret i8* %37
}

LLVM IR (-O2):
@SHA256_DIGEST_LENGTH = global i32 0
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [106 x i8] c"turnin: Failed to open turned in file '%s' for sha-digest.\0A Please notify the Instructor or a TA.\0A\00"
define i8* @calculate_sha(i8* %0) {
%2 = alloca i32
%3 = alloca [1024 x i8*]
%4 = load i32, i32* @SHA256_DIGEST_LENGTH
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast i32* %2 to i8*
%8 = bitcast [1024 x i8*]* %3 to i8*
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([106 x i8], [106 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = call i32 @SHA256_Init(i32* %2)
%17 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %3, i64 0, i64 0
%18 = call i32 @fread(i8** %17, i32 1, i32 1024, i32* %9)
%19 = icmp eq i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %15, %20
%21 = phi i32 [ %23, %20 ], [ %18, %15 ]
%22 = call i32 @SHA256_Update(i32* %2, i8** %17, i32 %21)
%23 = call i32 @fread(i8** %17, i32 1, i32 1024, i32* %9)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %20
25: ; preds = %20, %15
%26 = call i32 @SHA256_Final(i8* %6, i32* %2)
%27 = call i32 @fclose(i32* %9)
%28 = call i8* @sha2string(i8* %6)
ret i8* %28
}