#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 uint8_t;
typedef int /*<<< orphan*/ keys;
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*/ fwrite(int *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
uint8_t write_keys(uint8_t *keys, char *filename, size_t size) {
  FILE *file;
  if (!(file = fopen(filename, "wb"))) {
    fprintf(stderr, "Can not open file '%s' for writing\n", filename);
    return 1;
  }
  if (!fwrite(keys, sizeof(keys[0]), size, file)) {
    fprintf(stderr, "Can not write the file '%s'\n", filename);
    return 1;
  }
  fclose(file);
  printf("Wrote keys to: %s\n", filename);
  return 0;
}


x86 assembly (clang, -O0):
<write_keys>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a7 <write_keys+0x57>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe6d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    120b <write_keys+0xbb>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x4,%esi
call   1070 <fwrite@plt>
cmp    $0x0,%eax
jne    11eb <write_keys+0x9b>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe4d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    120b <write_keys+0xbb>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rsi
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_keys>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe98(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11aa <write_keys+0x5a>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x4,%esi
mov    %r15,%rdx
mov    %rax,%rcx
call   1070 <fwrite@plt>
test   %eax,%eax
je     11bc <write_keys+0x6c>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xea6(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
jmp    11d9 <write_keys+0x89>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
jmp    11cc <write_keys+0x7c>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"Can not open file '%s' for writing\0A\00"
@.str.2 = [29 x i8] c"Can not write the file '%s'\0A\00"
@.str.3 = [19 x i8] c"Wrote keys to: %s\0A\00"
define i32 @write_keys(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %6
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = icmp ne i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %6
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 1, i32* %4
br label %31
16: ; preds = %3
%17 = load i32*, i32** %5
%18 = load i64, i64* %7
%19 = load i32*, i32** %8
%20 = call i32 @fwrite(i32* %17, i32 4, i64 %18, i32* %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %6
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 1, i32* %4
br label %31
26: ; preds = %16
%27 = load i32*, i32** %8
%28 = call i32 @fclose(i32* %27)
%29 = load i8*, i8** %6
%30 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %29)
store i32 0, i32* %4
br label %31
31: ; preds = %26, %22, %12
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"Can not open file '%s' for writing\0A\00"
@.str.2 = [29 x i8] c"Can not write the file '%s'\0A\00"
@.str.3 = [19 x i8] c"Wrote keys to: %s\0A\00"
define i32 @write_keys(i32* nocapture %0, i8* %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %18
9: ; preds = %3
%10 = tail call i32 @fwrite(i32* %0, i32 4, i64 %2, i32* %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %18
15: ; preds = %9
%16 = tail call i32 @fclose(i32* %4)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %18
18: ; preds = %15, %12, %6
%19 = phi i32 [ 0, %15 ], [ 1, %12 ], [ 1, %6 ]
ret i32 %19
}