#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 *, size_t);
int /*<<< orphan*/ _IOFBF;
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
scalar_t__ setvbuf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int sha256sum(FILE *f, unsigned char *md) {
  int err = 0;
  SHA256_CTX c;
  char *buf;
  char *vbuf;
  buf = malloc(32767);
  if (!buf) {
    fprintf(stderr, "could not allocate buffer\n");
    return 1;
  }
  vbuf = malloc(32767);
  if (!vbuf) {
    fprintf(stderr, "could not allocate buffer\n");
    return 1;
  }
  if (setvbuf(f, vbuf, _IOFBF, 32767)) {
    fprintf(stderr, "setvbuf failed\n");
    return 1;
  }
  err = !SHA256_Init(&c);
  if (err) {
    fprintf(stderr, "could not init sha256 context\n");
    return err;
  }
  size_t count;
  while ((count = fread(buf, 1, 32767, f))) {
    if ((err = !SHA256_Update(&c, buf, count))) {
      fprintf(stderr, "could not update sha256\n");
      return err;
    }
  }
  if (ferror(f)) {
    fprintf(stderr, "could not read from file\n");
    return 1;
  }
  err = !SHA256_Final(md, &c);
  if (err) {
    fprintf(stderr, "could not get the hash\n");
  }
  return err;
}


x86 assembly (clang, -O0):
<sha256sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x7fff,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d1 <sha256sum+0x51>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138a <sha256sum+0x20a>
mov    $0x7fff,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    120b <sha256sum+0x8b>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138a <sha256sum+0x20a>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
mov    $0x7fff,%ecx
call   1080 <setvbuf@plt>
cmp    $0x0,%rax
je     1251 <sha256sum+0xd1>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138a <sha256sum+0x20a>
lea    -0x20(%rbp),%rdi
call   1050 <SHA256_Init@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1294 <sha256sum+0x114>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xda7(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    138a <sha256sum+0x20a>
jmp    1299 <sha256sum+0x119>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x7fff,%edx
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1314 <sha256sum+0x194>
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    -0x20(%rbp),%rdi
call   10a0 <SHA256_Update@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x1c(%rbp)
test   $0x1,%al
jne    12ef <sha256sum+0x16f>
jmp    130f <sha256sum+0x18f>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
lea    0xd4b(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    138a <sha256sum+0x20a>
jmp    1299 <sha256sum+0x119>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1348 <sha256sum+0x1c8>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
lea    0xd2c(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138a <sha256sum+0x20a>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <SHA256_Final@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1384 <sha256sum+0x204>
mov    0x2c6a(%rip),%rax
mov    (%rax),%edi
lea    0xcfe(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha256sum>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x7fff,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11df <sha256sum+0x5f>
mov    %rax,%rbx
mov    $0x7fff,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11df <sha256sum+0x5f>
mov    0x2e22(%rip),%rcx
mov    (%rcx),%edx
mov    %r15,%rdi
mov    %rax,%rsi
mov    $0x7fff,%ecx
call   1080 <setvbuf@plt>
test   %rax,%rax
je     1205 <sha256sum+0x85>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
jmp    11ef <sha256sum+0x6f>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0x4(%rsp),%rdi
call   1050 <SHA256_Init@plt>
test   %eax,%eax
je     125e <sha256sum+0xde>
lea    0x4(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x7fff,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1273 <sha256sum+0xf3>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   10a0 <SHA256_Update@plt>
test   %eax,%eax
jne    1220 <sha256sum+0xa0>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
jmp    11ef <sha256sum+0x6f>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
jmp    11ef <sha256sum+0x6f>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1295 <sha256sum+0x115>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
jmp    11ef <sha256sum+0x6f>
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
call   1090 <SHA256_Final@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    11f9 <sha256sum+0x79>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
jmp    11ef <sha256sum+0x6f>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"could not allocate buffer\0A\00"
@_IOFBF = global i32 0
@.str.1 = [16 x i8] c"setvbuf failed\0A\00"
@.str.2 = [31 x i8] c"could not init sha256 context\0A\00"
@.str.3 = [25 x i8] c"could not update sha256\0A\00"
@.str.4 = [26 x i8] c"could not read from file\0A\00"
@.str.5 = [24 x i8] c"could not get the hash\0A\00"
define i32 @sha256sum(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 0, i32* %6
%11 = call i8* @malloc(i32 32767)
store i8* %11, i8** %8
%12 = load i8*, i8** %8
%13 = icmp ne i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %82
17: ; preds = %2
%18 = call i8* @malloc(i32 32767)
store i8* %18, i8** %9
%19 = load i8*, i8** %9
%20 = icmp ne i8* %19, null
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %82
24: ; preds = %17
%25 = load i32*, i32** %4
%26 = load i8*, i8** %9
%27 = load i32, i32* @_IOFBF
%28 = call i64 @setvbuf(i32* %25, i8* %26, i32 %27, i32 32767)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %82
33: ; preds = %24
%34 = call i32 @SHA256_Init(i32* %7)
%35 = icmp ne i32 %34, 0
%36 = xor i1 %35, true
%37 = zext i1 %36 to i32
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* %6
store i32 %43, i32* %3
br label %82
44: ; preds = %33
br label %45
45: ; preds = %61, %44
%46 = load i8*, i8** %8
%47 = load i32*, i32** %4
%48 = call i64 @fread(i8* %46, i32 1, i32 32767, i32* %47)
store i64 %48, i64* %10
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %62
50: ; preds = %45
%51 = load i8*, i8** %8
%52 = load i64, i64* %10
%53 = call i32 @SHA256_Update(i32* %7, i8* %51, i64 %52)
%54 = icmp ne i32 %53, 0
%55 = xor i1 %54, true
%56 = zext i1 %55 to i32
store i32 %56, i32* %6
br i1 %55, label %57, label %61
57: ; preds = %50
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%60 = load i32, i32* %6
store i32 %60, i32* %3
br label %82
61: ; preds = %50
br label %45
62: ; preds = %45
%63 = load i32*, i32** %4
%64 = call i64 @ferror(i32* %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = load i32, i32* @stderr
%68 = call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %82
69: ; preds = %62
%70 = load i8*, i8** %5
%71 = call i32 @SHA256_Final(i8* %70, i32* %7)
%72 = icmp ne i32 %71, 0
%73 = xor i1 %72, true
%74 = zext i1 %73 to i32
store i32 %74, i32* %6
%75 = load i32, i32* %6
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %69
%78 = load i32, i32* @stderr
%79 = call i32 @fprintf(i32 %78, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %80
80: ; preds = %77, %69
%81 = load i32, i32* %6
store i32 %81, i32* %3
br label %82
82: ; preds = %80, %66, %57, %40, %30, %21, %14
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"could not allocate buffer\0A\00"
@_IOFBF = global i32 0
@.str.1 = [16 x i8] c"setvbuf failed\0A\00"
@.str.2 = [31 x i8] c"could not init sha256 context\0A\00"
@.str.3 = [25 x i8] c"could not update sha256\0A\00"
@.str.4 = [26 x i8] c"could not read from file\0A\00"
@.str.5 = [24 x i8] c"could not get the hash\0A\00"
define i32 @sha256sum(i32* nocapture %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call dereferenceable_or_null(32767) i8* @malloc(i32 32767)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %50
10: ; preds = %2
%11 = tail call dereferenceable_or_null(32767) i8* @malloc(i32 32767)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %50
16: ; preds = %10
%17 = load i32, i32* @_IOFBF
%18 = tail call i64 @setvbuf(i32* %0, i8* %11, i32 %17, i32 32767)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %50
23: ; preds = %16
%24 = call i32 @SHA256_Init(i32* %3)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %50
29: ; preds = %23, %32
%30 = call i64 @fread(i8* %5, i32 1, i32 32767, i32* %0)
%31 = icmp eq i64 %30, 0
br i1 %31, label %38, label %32
32: ; preds = %29
%33 = call i32 @SHA256_Update(i32* %3, i8* %5, i64 %30)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %29
35: ; preds = %32
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %50
38: ; preds = %29
%39 = call i64 @ferror(i32* %0)
%40 = icmp eq i64 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %38
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %50
44: ; preds = %38
%45 = call i32 @SHA256_Final(i8* %1, i32* %3)
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %50
50: ; preds = %35, %41, %47, %44, %26, %20, %13, %7
%51 = phi i32 [ 1, %20 ], [ 1, %26 ], [ 1, %13 ], [ 1, %7 ], [ 1, %35 ], [ 1, %41 ], [ 1, %47 ], [ 0, %44 ]
ret i32 %51
}