#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(char *, int, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ hash16(char *, char const *, int);
scalar_t__ malloc(int const);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int const strlen(char const *);
bool checkKeyivIntegrity(const char *keyiv, const char *directory) {
  const int hash_length = 2;
  char hash[hash_length];
  hash16(hash, keyiv, 128 / 8 + 128 / 8);
  char *path = (char *)malloc(strlen(directory) + strlen("/"
                                                         ".osir_hash"));
  strcpy(path, directory);
  strcat(path, "/"
               ".osir_hash");
  FILE *fp = fopen(path, "rb");
  if (fp == NULL) {
    fprintf(stderr, "Error: checkKeyivIntegrity: failed to open %s\n", path);
    return 0;
  }
  char *old_hash = (char *)malloc(hash_length);
  fread(old_hash, 1, hash_length, fp);
  fclose(fp);
  for (int i = 0; i < hash_length; i++) {
    if (old_hash[i] != hash[i]) {
      free(old_hash);
      free(path);
      return false;
    }
  }
  free(path);
  free(old_hash);
  return true;
}


x86 assembly (clang, -O0):
<checkKeyivIntegrity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x50(%rbp),%rdi
mov    $0x20,%edx
call   1080 <hash16@plt>
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x54(%rbp)
lea    0xe1d(%rip),%rdi
call   1070 <strlen@plt>
mov    -0x54(%rbp),%edi
add    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
lea    0xdf2(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x30(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    125e <checkKeyivIntegrity+0xbe>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xdc9(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1312 <checkKeyivIntegrity+0x172>
mov    $0x2,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1050 <fread@plt>
mov    -0x38(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x0,-0x4c(%rbp)
cmpl   $0x2,-0x4c(%rbp)
jge    12f2 <checkKeyivIntegrity+0x152>
mov    -0x48(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
movslq -0x4c(%rbp),%rcx
movsbl -0x50(%rbp,%rcx,1),%ecx
cmp    %ecx,%eax
je     12df <checkKeyivIntegrity+0x13f>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1312 <checkKeyivIntegrity+0x172>
jmp    12e4 <checkKeyivIntegrity+0x144>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1293 <checkKeyivIntegrity+0xf3>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkKeyivIntegrity>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rsi
mov    %rsp,%rdi
mov    $0x20,%edx
call   1080 <hash16@plt>
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
lea    0xe34(%rip),%r12
mov    %r12,%rdi
call   1070 <strlen@plt>
add    %eax,%ebx
mov    %ebx,%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   10c0 <strcat@plt>
lea    0xe0f(%rip),%rsi
mov    %r15,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1272 <checkKeyivIntegrity+0xd2>
mov    %rax,%rbx
mov    $0x2,%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    (%r14),%al
cmp    (%rsp),%al
jne    1260 <checkKeyivIntegrity+0xc0>
mov    0x1(%r14),%al
cmp    0x1(%rsp),%al
jne    1260 <checkKeyivIntegrity+0xc0>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
jmp    128c <checkKeyivIntegrity+0xec>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    128a <checkKeyivIntegrity+0xea>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
mov    %r15,%rdx
call   1090 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/.osir_hash\00"
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [47 x i8] c"Error: checkKeyivIntegrity: failed to open %s\0A\00"
define i32 @checkKeyivIntegrity(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 2, i32* %6
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 2
%15 = load i8*, i8** %4
%16 = call i32 @hash16(i8* %14, i8* %15, i32 32)
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
%19 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%20 = add nsw i32 %18, %19
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = load i8*, i8** %5
%25 = call i32 @strcpy(i8* %23, i8* %24)
%26 = load i8*, i8** %8
%27 = call i32 @strcat(i8* %26, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%28 = load i8*, i8** %8
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %29, i32** %9
%30 = load i32*, i32** %9
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %36
32: ; preds = %2
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %8
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i32 0, i32* %3
store i32 1, i32* %10
br label %74
36: ; preds = %2
%37 = call i64 @malloc(i32 2)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %11
%39 = load i8*, i8** %11
%40 = load i32*, i32** %9
%41 = call i32 @fread(i8* %39, i32 1, i32 2, i32* %40)
%42 = load i32*, i32** %9
%43 = call i32 @fclose(i32* %42)
store i32 0, i32* %12
br label %44
44: ; preds = %66, %36
%45 = load i32, i32* %12
%46 = icmp slt i32 %45, 2
br i1 %46, label %47, label %69
47: ; preds = %44
%48 = load i8*, i8** %11
%49 = load i32, i32* %12
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = load i32, i32* %12
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %14, i64 %55
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp ne i32 %53, %58
br i1 %59, label %60, label %65
60: ; preds = %47
%61 = load i8*, i8** %11
%62 = call i32 @free(i8* %61)
%63 = load i8*, i8** %8
%64 = call i32 @free(i8* %63)
store i32 0, i32* %3
store i32 1, i32* %10
br label %74
65: ; preds = %47
br label %66
66: ; preds = %65
%67 = load i32, i32* %12
%68 = add nsw i32 %67, 1
store i32 %68, i32* %12
br label %44
69: ; preds = %44
%70 = load i8*, i8** %8
%71 = call i32 @free(i8* %70)
%72 = load i8*, i8** %11
%73 = call i32 @free(i8* %72)
store i32 1, i32* %3
store i32 1, i32* %10
br label %74
74: ; preds = %69, %60, %32
%75 = load i8*, i8** %7
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@.str = [12 x i8] c"/.osir_hash\00"
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [47 x i8] c"Error: checkKeyivIntegrity: failed to open %s\0A\00"
define i32 @checkKeyivIntegrity(i8* %0, i8* %1) {
%3 = alloca [2 x i8]
%4 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%5 = call i32 @hash16(i8* %4, i8* %0, i32 32)
%6 = call i32 @strlen(i8* %1)
%7 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = add nsw i32 %7, %6
%9 = call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = call i32 @strcpy(i8* %10, i8* %1)
%12 = call i32 @strcat(i8* %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%13 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %38
18: ; preds = %2
%19 = call i64 @malloc(i32 2)
%20 = inttoptr i64 %19 to i8*
%21 = call i32 @fread(i8* %20, i32 1, i32 2, i32* %13)
%22 = call i32 @fclose(i32* %13)
%23 = load i8, i8* %20
%24 = load i8, i8* %4
%25 = icmp eq i8 %23, %24
br i1 %25, label %26, label %35
26: ; preds = %18
%27 = getelementptr inbounds i8, i8* %20, i64 1
%28 = load i8, i8* %27
%29 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
%30 = load i8, i8* %29
%31 = icmp eq i8 %28, %30
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = call i32 @free(i8* %10)
%34 = call i32 @free(i8* %20)
br label %38
35: ; preds = %26, %18
%36 = call i32 @free(i8* %20)
%37 = call i32 @free(i8* %10)
br label %38
38: ; preds = %35, %32, %15
%39 = phi i32 [ 0, %15 ], [ 1, %32 ], [ 0, %35 ]
ret i32 %39
}