#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 *, ...);
int /*<<< orphan*/ free(char *);
unsigned int const fwrite(char *, int, unsigned int const,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ hash16(char *, char const *, int);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
bool makeHashFile(const char *keyiv, const char *directory) {
  const unsigned 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, "wb");
  if (fp == NULL) {
    fprintf(stderr, "Error: makeHashFile: failed to open %s\n", path);
    return 0;
  }
  if (fwrite(hash, 1, hash_length, fp) != hash_length) {
    fprintf(stderr, "Error: makeHashFile: failed to write to file\n");
    return 0;
  }
  fclose(fp);
  free(path);
  return 1;
}


x86 assembly (clang, -O0):
<makeHashFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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    -0x40(%rbp),%rdi
mov    $0x20,%edx
call   1070 <hash16@plt>
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x48(%rbp)
lea    0xe1c(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    %rax,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x30(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1263 <makeHashFile+0xc3>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xdc6(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12cd <makeHashFile+0x12d>
mov    -0x38(%rbp),%rcx
lea    -0x40(%rbp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
call   10c0 <fwrite@plt>
cmp    $0x2,%eax
je     12ad <makeHashFile+0x10d>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12cd <makeHashFile+0x12d>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%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    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makeHashFile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
mov    %rdi,%rsi
mov    %rsp,%rdi
mov    $0x20,%edx
call   1070 <hash16@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0xb(%rax),%rdi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
lea    0xe1f(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strcat@plt>
lea    0xe1c(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1232 <makeHashFile+0x92>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x1,%r14d
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
cmp    $0x2,%eax
jne    1251 <makeHashFile+0xb1>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    126b <makeHashFile+0xcb>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
xor    %r14d,%r14d
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    126b <makeHashFile+0xcb>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/.osir_hash\00"
@.str.1 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.2 = [40 x i8] c"Error: makeHashFile: failed to open %s\0A\00"
@.str.3 = [46 x i8] c"Error: makeHashFile: failed to write to file\0A\00"
define i32 @makeHashFile(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
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 2, i32* %6
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %7
%12 = alloca i8, i64 2
%13 = load i8*, i8** %4
%14 = call i32 @hash16(i8* %12, i8* %13, i32 32)
%15 = load i8*, i8** %5
%16 = call i64 @strlen(i8* %15)
%17 = call i64 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%18 = add nsw i64 %16, %17
%19 = call i64 @malloc(i64 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %8
%21 = load i8*, i8** %8
%22 = load i8*, i8** %5
%23 = call i32 @strcpy(i8* %21, i8* %22)
%24 = load i8*, i8** %8
%25 = call i32 @strcat(i8* %24, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%26 = load i8*, i8** %8
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %9
%28 = load i32*, i32** %9
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %34
30: ; preds = %2
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %8
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 0, i32* %3
store i32 1, i32* %10
br label %46
34: ; preds = %2
%35 = load i32*, i32** %9
%36 = call i32 @fwrite(i8* %12, i32 1, i32 2, i32* %35)
%37 = icmp ne i32 %36, 2
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
store i32 1, i32* %10
br label %46
41: ; preds = %34
%42 = load i32*, i32** %9
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %8
%45 = call i32 @free(i8* %44)
store i32 1, i32* %3
store i32 1, i32* %10
br label %46
46: ; preds = %41, %38, %30
%47 = load i8*, i8** %7
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [12 x i8] c"/.osir_hash\00"
@.str.1 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.2 = [40 x i8] c"Error: makeHashFile: failed to open %s\0A\00"
@.str.3 = [46 x i8] c"Error: makeHashFile: failed to write to file\0A\00"
define i32 @makeHashFile(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 i64 @strlen(i8* dereferenceable(1) %1)
%7 = add nsw i64 %6, 11
%8 = call i64 @malloc(i64 %7)
%9 = inttoptr i64 %8 to i8*
%10 = call i32 @strcpy(i8* %9, i8* %1)
%11 = call i32 @strcat(i8* %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%12 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %9)
br label %26
17: ; preds = %2
%18 = call i32 @fwrite(i8* %4, i32 1, i32 2, i32* %12)
%19 = icmp eq i32 %18, 2
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0))
br label %26
23: ; preds = %17
%24 = call i32 @fclose(i32* %12)
%25 = call i32 @free(i8* %9)
br label %26
26: ; preds = %23, %20, %14
%27 = phi i32 [ 0, %14 ], [ 0, %20 ], [ 1, %23 ]
ret i32 %27
}