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

/* Variables and functions */
int SHA_DIGEST_LENGTH;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ hash_letter(char, char, char *);
int strlen(char *);
bool check_password(char *filename, char *password) {
  FILE *fh;
  int letter;
  char hash[SHA_DIGEST_LENGTH];
  char check[SHA_DIGEST_LENGTH];
  uint32_t salt;
  bool same;
  int pos;
  same = false;
  fh = fopen(filename, "rb");
  if (fh != NULL) {
    same = true;
    letter = 0;
    while (same && !feof(fh) && letter < strlen(password)) {
      fread(&salt, sizeof(uint32_t), 1, fh);
      fread(hash, sizeof(char), SHA_DIGEST_LENGTH, fh);
      hash_letter(password[letter], salt, check);
      for (pos = 0; (pos < SHA_DIGEST_LENGTH) && (hash[pos] == check[pos]);
           pos++)
        ;
      same = (pos == SHA_DIGEST_LENGTH);
      letter++;
    }
    same &= (fread(hash, 1, 1, fh) == 0);
    fclose(fh);
  }
  return same;
}


x86 assembly (clang, -O0):
<check_password>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x28(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x58(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x30(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe34(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1341 <check_password+0x1e1>
movl   $0x1,-0x40(%rbp)
movl   $0x0,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x40(%rbp)
mov    %al,-0x59(%rbp)
je     1232 <check_password+0xd2>
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x59(%rbp)
jne    1232 <check_password+0xd2>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x60(%rbp),%eax
cmp    %ecx,%eax
setl   %al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
test   $0x1,%al
jne    1242 <check_password+0xe2>
jmp    1312 <check_password+0x1b2>
mov    -0x18(%rbp),%rcx
lea    -0x39(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    -0x58(%rbp),%rdi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x50(%rbp),%rdx
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%al
movsbl %al,%edi
movsbl -0x39(%rbp),%esi
call   1070 <hash_letter@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%ecx
xor    %eax,%eax
mov    0x2d49(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x61(%rbp)
jge    12ca <check_password+0x16a>
mov    -0x50(%rbp),%rcx
mov    -0x58(%rbp),%rax
movslq -0x44(%rbp),%rdx
movsbl (%rax,%rdx,1),%eax
movslq -0x44(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
sete   %al
mov    %al,-0x61(%rbp)
mov    -0x61(%rbp),%al
test   $0x1,%al
jne    12da <check_password+0x17a>
jmp    12ed <check_password+0x18d>
jmp    12df <check_password+0x17f>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1293 <check_password+0x133>
mov    -0x44(%rbp),%eax
mov    0x2cf1(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ee <check_password+0x8e>
mov    -0x58(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
and    -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x40(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_password>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe62(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
test   %rax,%rax
je     12a4 <check_password+0x144>
xor    %r13d,%r13d
mov    %r15,-0x40(%rbp)
mov    -0x38(%rbp),%r14
jmp    11d0 <check_password+0x70>
nopl   (%rax)
add    $0x1,%r13
cmp    %eax,%ecx
mov    -0x40(%rbp),%r15
jne    1273 <check_password+0x113>
mov    -0x38(%rbp),%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    126e <check_password+0x10e>
mov    %r15,%rdi
call   1050 <strlen@plt>
cltq
cmp    %rax,%r13
jge    126e <check_password+0x10e>
lea    -0x29(%rbp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1030 <fread@plt>
movsbl (%r15,%r13,1),%edi
movsbl -0x29(%rbp),%esi
mov    %rbx,%rdx
call   1070 <hash_letter@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
xor    %esi,%esi
mov    $0x0,%ecx
test   %eax,%eax
jle    11c0 <check_password+0x60>
xor    %ecx,%ecx
xchg   %ax,%ax
movzbl (%r12,%rcx,1),%edx
cmp    (%rbx,%rcx,1),%dl
jne    11c0 <check_password+0x60>
add    $0x1,%rcx
cmp    %rcx,%rax
jne    1250 <check_password+0xf0>
mov    %eax,%ecx
jmp    11c0 <check_password+0x60>
mov    $0x1,%esi
mov    %r12,%rdi
mov    %esi,%r15d
mov    $0x1,%esi
mov    $0x1,%edx
mov    -0x38(%rbp),%r14
mov    %r14,%rcx
call   1030 <fread@plt>
xor    %ebx,%ebx
test   %rax,%rax
sete   %bl
and    %r15d,%ebx
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    12a6 <check_password+0x146>
xor    %ebx,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SHA_DIGEST_LENGTH = global i32 0
@.str = [3 x i8] c"rb\00"
define i32 @check_password(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%13 = load i32, i32* @SHA_DIGEST_LENGTH
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %7
%16 = alloca i8, i64 %14
store i64 %14, i64* %8
%17 = load i32, i32* @SHA_DIGEST_LENGTH
%18 = zext i32 %17 to i64
%19 = alloca i8, i64 %18
store i64 %18, i64* %9
store i32 0, i32* %11
%20 = load i8*, i8** %3
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %5
%22 = load i32*, i32** %5
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %90
24: ; preds = %2
store i32 1, i32* %11
store i32 0, i32* %6
br label %25
25: ; preds = %74, %24
%26 = load i32, i32* %11
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %25
%29 = load i32*, i32** %5
%30 = call i32 @feof(i32* %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %28
%33 = load i32, i32* %6
%34 = load i8*, i8** %4
%35 = call i32 @strlen(i8* %34)
%36 = icmp slt i32 %33, %35
br label %37
37: ; preds = %32, %28, %25
%38 = phi i1 [ false, %28 ], [ false, %25 ], [ %36, %32 ]
br i1 %38, label %39, label %81
39: ; preds = %37
%40 = load i32*, i32** %5
%41 = call i64 @fread(i8* %10, i32 1, i32 1, i32* %40)
%42 = load i32, i32* @SHA_DIGEST_LENGTH
%43 = load i32*, i32** %5
%44 = call i64 @fread(i8* %16, i32 1, i32 %42, i32* %43)
%45 = load i8*, i8** %4
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i8, i8* %48
%50 = load i8, i8* %10
%51 = call i32 @hash_letter(i8 signext %49, i8 signext %50, i8* %19)
store i32 0, i32* %12
br label %52
52: ; preds = %71, %39
%53 = load i32, i32* %12
%54 = load i32, i32* @SHA_DIGEST_LENGTH
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %68
56: ; preds = %52
%57 = load i32, i32* %12
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %16, i64 %58
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = load i32, i32* %12
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %19, i64 %63
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = icmp eq i32 %61, %66
br label %68
68: ; preds = %56, %52
%69 = phi i1 [ false, %52 ], [ %67, %56 ]
br i1 %69, label %70, label %74
70: ; preds = %68
br label %71
71: ; preds = %70
%72 = load i32, i32* %12
%73 = add nsw i32 %72, 1
store i32 %73, i32* %12
br label %52
74: ; preds = %68
%75 = load i32, i32* %12
%76 = load i32, i32* @SHA_DIGEST_LENGTH
%77 = icmp eq i32 %75, %76
%78 = zext i1 %77 to i32
store i32 %78, i32* %11
%79 = load i32, i32* %6
%80 = add nsw i32 %79, 1
store i32 %80, i32* %6
br label %25
81: ; preds = %37
%82 = load i32*, i32** %5
%83 = call i64 @fread(i8* %16, i32 1, i32 1, i32* %82)
%84 = icmp eq i64 %83, 0
%85 = zext i1 %84 to i32
%86 = load i32, i32* %11
%87 = and i32 %86, %85
store i32 %87, i32* %11
%88 = load i32*, i32** %5
%89 = call i32 @fclose(i32* %88)
br label %90
90: ; preds = %81, %2
%91 = load i32, i32* %11
%92 = load i8*, i8** %7
ret i32 %91
}

LLVM IR (-O2):
@SHA_DIGEST_LENGTH = global i32 0
@.str = [3 x i8] c"rb\00"
define i32 @check_password(i8* nocapture %0, i8* %1) {
%3 = alloca i8
%4 = load i32, i32* @SHA_DIGEST_LENGTH
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = alloca i8, i64 %5
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %53, label %10
10: ; preds = %2, %42
%11 = phi i64 [ %45, %42 ], [ 0, %2 ]
%12 = call i32 @feof(i32* %8)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %46
14: ; preds = %10
%15 = call i32 @strlen(i8* %1)
%16 = sext i32 %15 to i64
%17 = icmp slt i64 %11, %16
br i1 %17, label %18, label %46
18: ; preds = %14
%19 = call i64 @fread(i8* %3, i32 1, i32 1, i32* %8)
%20 = load i32, i32* @SHA_DIGEST_LENGTH
%21 = call i64 @fread(i8* %6, i32 1, i32 %20, i32* %8)
%22 = getelementptr inbounds i8, i8* %1, i64 %11
%23 = load i8, i8* %22
%24 = load i8, i8* %3
%25 = call i32 @hash_letter(i8 signext %23, i8 signext %24, i8* %7)
%26 = load i32, i32* @SHA_DIGEST_LENGTH
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %42
28: ; preds = %18
%29 = zext i32 %26 to i64
br label %30
30: ; preds = %28, %37
%31 = phi i64 [ 0, %28 ], [ %38, %37 ]
%32 = getelementptr inbounds i8, i8* %6, i64 %31
%33 = load i8, i8* %32
%34 = getelementptr inbounds i8, i8* %7, i64 %31
%35 = load i8, i8* %34
%36 = icmp eq i8 %33, %35
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = add nuw nsw i64 %31, 1
%39 = icmp eq i64 %38, %29
br i1 %39, label %42, label %30
40: ; preds = %30
%41 = trunc i64 %31 to i32
br label %42
42: ; preds = %37, %40, %18
%43 = phi i32 [ 0, %18 ], [ %41, %40 ], [ %26, %37 ]
%44 = icmp eq i32 %43, %26
%45 = add nuw nsw i64 %11, 1
br i1 %44, label %10, label %46
46: ; preds = %42, %10, %14
%47 = phi i32 [ 0, %42 ], [ 1, %10 ], [ 1, %14 ]
%48 = call i64 @fread(i8* %6, i32 1, i32 1, i32* %8)
%49 = icmp eq i64 %48, 0
%50 = zext i1 %49 to i32
%51 = and i32 %47, %50
%52 = call i32 @fclose(i32* %8)
br label %53
53: ; preds = %46, %2
%54 = phi i32 [ %51, %46 ], [ 0, %2 ]
ret i32 %54
}