#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*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int rand();
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/ *);
unsigned char *generate_key(int key_byte_size) {
  int i;
  unsigned char *key = (unsigned char *)malloc(key_byte_size * sizeof(char));
  FILE *key_file = fopen("keyfile", "wb");
  unsigned int seed = (unsigned int)time(NULL);
  srand(seed);
  if (!key_file) {
    printf("Problems opening the keyfile");
    return NULL;
  }
  for (i = 0; i < key_byte_size; i++) {
    key[i] = rand() % 255;
  }
  fwrite(key, 1, key_byte_size, key_file);
  fclose(key_file);
  return key;
}


x86 assembly (clang, -O0):
<generate_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
lea    0xe73(%rip),%rdi
lea    0xe54(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <srand@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11ed <generate_key+0x6d>
lea    0xe28(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    124e <generate_key+0xce>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1228 <generate_key+0xa8>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0xff,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11f4 <generate_key+0x74>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_key>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r15d
call   1070 <malloc@plt>
mov    %rax,%r12
lea    0xe7e(%rip),%rdi
lea    0xe5f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
test   %r14,%r14
je     121c <generate_key+0x9c>
test   %r15d,%r15d
jle    11ff <generate_key+0x7f>
mov    %r15d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <rand@plt>
cltq
imul   $0xffffffff80808081,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
shr    $0x7,%ecx
add    %edx,%ecx
add    %eax,%ecx
mov    %cl,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11d0 <generate_key+0x50>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    122b <generate_key+0xab>
lea    0xde0(%rip),%rdi
call   1040 <printf@plt>
xor    %r12d,%r12d
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"keyfile\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [29 x i8] c"Problems opening the keyfile\00"
define i8* @generate_key(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 1
%11 = trunc i64 %10 to i32
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %5
%14 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %6
%15 = call i64 @time(i32* null)
%16 = trunc i64 %15 to i32
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = call i32 @srand(i32 %17)
%19 = load i32*, i32** %6
%20 = icmp ne i32* %19, null
br i1 %20, label %23, label %21
21: ; preds = %1
%22 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %47
23: ; preds = %1
store i32 0, i32* %4
br label %24
24: ; preds = %36, %23
%25 = load i32, i32* %4
%26 = load i32, i32* %3
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %39
28: ; preds = %24
%29 = call i32 (...) @rand()
%30 = srem i32 %29, 255
%31 = trunc i32 %30 to i8
%32 = load i8*, i8** %5
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 %31, i8* %35
br label %36
36: ; preds = %28
%37 = load i32, i32* %4
%38 = add nsw i32 %37, 1
store i32 %38, i32* %4
br label %24
39: ; preds = %24
%40 = load i8*, i8** %5
%41 = load i32, i32* %3
%42 = load i32*, i32** %6
%43 = call i32 @fwrite(i8* %40, i32 1, i32 %41, i32* %42)
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
%46 = load i8*, i8** %5
store i8* %46, i8** %2
br label %47
47: ; preds = %39, %21
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
@.str = [8 x i8] c"keyfile\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [29 x i8] c"Problems opening the keyfile\00"
define i8* @generate_key(i32 %0) {
%2 = tail call i64 @malloc(i32 %0)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i64 @time(i32* null)
%6 = trunc i64 %5 to i32
%7 = tail call i32 @srand(i32 %6)
%8 = icmp eq i32* %4, null
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = icmp sgt i32 %0, 0
br i1 %10, label %11, label %23
11: ; preds = %9
%12 = zext i32 %0 to i64
br label %15
13: ; preds = %1
%14 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %26
15: ; preds = %11, %15
%16 = phi i64 [ 0, %11 ], [ %21, %15 ]
%17 = tail call i32 (...) @rand()
%18 = srem i32 %17, 255
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds i8, i8* %3, i64 %16
store i8 %19, i8* %20
%21 = add nuw nsw i64 %16, 1
%22 = icmp eq i64 %21, %12
br i1 %22, label %23, label %15
23: ; preds = %15, %9
%24 = tail call i32 @fwrite(i8* %3, i32 1, i32 %0, i32* %4)
%25 = tail call i32 @fclose(i32* %4)
br label %26
26: ; preds = %23, %13
%27 = phi i8* [ %3, %23 ], [ null, %13 ]
ret i8* %27
}