#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 i;
  unsigned char *key = (unsigned char *)malloc(8 * 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 < 8; i++) {
    key[i] = rand() % 255;
  }
  fwrite(key, 1, 8, key_file);
  fclose(key_file);
  return key;
}


x86 assembly (clang, -O0):
<generate_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
lea    0xe7b(%rip),%rdi
lea    0xe5c(%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    11e5 <generate_key+0x65>
lea    0xe30(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1246 <generate_key+0xc6>
movl   $0x0,-0xc(%rbp)
cmpl   $0x8,-0xc(%rbp)
jge    121e <generate_key+0x9e>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0xff,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11ec <generate_key+0x6c>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
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   %r14
push   %rbx
push   %rax
mov    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xe80(%rip),%rdi
lea    0xe61(%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     1304 <generate_key+0x184>
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,(%rbx)
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,0x1(%rbx)
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,0x2(%rbx)
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,0x3(%rbx)
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,0x4(%rbx)
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,0x5(%rbx)
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,0x6(%rbx)
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,0x7(%rbx)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    1312 <generate_key+0x192>
lea    0xcf8(%rip),%rdi
call   1040 <printf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
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() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = call i64 @malloc(i32 8)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %3
%8 = 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* %8, i32** %4
%9 = call i64 @time(i32* null)
%10 = trunc i64 %9 to i32
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = call i32 @srand(i32 %11)
%13 = load i32*, i32** %4
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %0
%16 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %1
br label %39
17: ; preds = %0
store i32 0, i32* %2
br label %18
18: ; preds = %29, %17
%19 = load i32, i32* %2
%20 = icmp slt i32 %19, 8
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = call i32 (...) @rand()
%23 = srem i32 %22, 255
%24 = trunc i32 %23 to i8
%25 = load i8*, i8** %3
%26 = load i32, i32* %2
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 %24, i8* %28
br label %29
29: ; preds = %21
%30 = load i32, i32* %2
%31 = add nsw i32 %30, 1
store i32 %31, i32* %2
br label %18
32: ; preds = %18
%33 = load i8*, i8** %3
%34 = load i32*, i32** %4
%35 = call i32 @fwrite(i8* %33, i32 1, i32 8, i32* %34)
%36 = load i32*, i32** %4
%37 = call i32 @fclose(i32* %36)
%38 = load i8*, i8** %3
store i8* %38, i8** %1
br label %39
39: ; preds = %32, %15
%40 = load i8*, i8** %1
ret i8* %40
}

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() {
%1 = tail call i64 @malloc(i32 8)
%2 = inttoptr i64 %1 to i8*
%3 = 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))
%4 = tail call i64 @time(i32* null)
%5 = trunc i64 %4 to i32
%6 = tail call i32 @srand(i32 %5)
%7 = icmp eq i32* %3, null
br i1 %7, label %42, label %8
8: ; preds = %0
%9 = tail call i32 (...) @rand()
%10 = srem i32 %9, 255
%11 = trunc i32 %10 to i8
store i8 %11, i8* %2
%12 = tail call i32 (...) @rand()
%13 = srem i32 %12, 255
%14 = trunc i32 %13 to i8
%15 = getelementptr inbounds i8, i8* %2, i64 1
store i8 %14, i8* %15
%16 = tail call i32 (...) @rand()
%17 = srem i32 %16, 255
%18 = trunc i32 %17 to i8
%19 = getelementptr inbounds i8, i8* %2, i64 2
store i8 %18, i8* %19
%20 = tail call i32 (...) @rand()
%21 = srem i32 %20, 255
%22 = trunc i32 %21 to i8
%23 = getelementptr inbounds i8, i8* %2, i64 3
store i8 %22, i8* %23
%24 = tail call i32 (...) @rand()
%25 = srem i32 %24, 255
%26 = trunc i32 %25 to i8
%27 = getelementptr inbounds i8, i8* %2, i64 4
store i8 %26, i8* %27
%28 = tail call i32 (...) @rand()
%29 = srem i32 %28, 255
%30 = trunc i32 %29 to i8
%31 = getelementptr inbounds i8, i8* %2, i64 5
store i8 %30, i8* %31
%32 = tail call i32 (...) @rand()
%33 = srem i32 %32, 255
%34 = trunc i32 %33 to i8
%35 = getelementptr inbounds i8, i8* %2, i64 6
store i8 %34, i8* %35
%36 = tail call i32 (...) @rand()
%37 = srem i32 %36, 255
%38 = trunc i32 %37 to i8
%39 = getelementptr inbounds i8, i8* %2, i64 7
store i8 %38, i8* %39
%40 = tail call i32 @fwrite(i8* %2, i32 1, i32 8, i32* %3)
%41 = tail call i32 @fclose(i32* %3)
br label %44
42: ; preds = %0
%43 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %8, %42
%45 = phi i8* [ %2, %8 ], [ null, %42 ]
ret i8* %45
}