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

/* Variables and functions */
scalar_t__ malloc(int);
int rand();
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/ *);
unsigned char *generate_key(int key_length) {
  time_t t;
  srand((unsigned)time(&t));
  unsigned char *key = (unsigned char *)malloc(key_length);
  for (int i = 0; i < key_length; i++) {
    key[i] = rand() % 256;
  }
  return key;
}


x86 assembly (clang, -O0):
<generate_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1040 <time@plt>
mov    %eax,%edi
call   1030 <srand@plt>
mov    -0x4(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11a2 <generate_key+0x62>
mov    $0x0,%al
call   1060 <rand@plt>
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116e <generate_key+0x2e>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_key>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
lea    0xc(%rsp),%rdi
call   1040 <time@plt>
mov    %eax,%edi
call   1030 <srand@plt>
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %ebp,%ebp
jle    1184 <generate_key+0x44>
mov    %ebp,%ebp
xor    %ebx,%ebx
nopl   (%rax)
xor    %eax,%eax
call   1060 <rand@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <generate_key+0x30>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @generate_key(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
%6 = call i64 @time(i32* %3)
%7 = trunc i64 %6 to i32
%8 = call i32 @srand(i32 %7)
%9 = load i32, i32* %2
%10 = call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %4
store i32 0, i32* %5
br label %12
12: ; preds = %24, %1
%13 = load i32, i32* %5
%14 = load i32, i32* %2
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %27
16: ; preds = %12
%17 = call i32 (...) @rand()
%18 = srem i32 %17, 256
%19 = trunc i32 %18 to i8
%20 = load i8*, i8** %4
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 %19, i8* %23
br label %24
24: ; preds = %16
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
br label %12
27: ; preds = %12
%28 = load i8*, i8** %4
ret i8* %28
}

LLVM IR (-O2):
define i8* @generate_key(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i64 @time(i32* %2)
%5 = trunc i64 %4 to i32
%6 = call i32 @srand(i32 %5)
%7 = call i64 @malloc(i32 %0)
%8 = inttoptr i64 %7 to i8*
%9 = icmp sgt i32 %0, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = zext i32 %0 to i64
br label %13
12: ; preds = %13, %1
ret i8* %8
13: ; preds = %10, %13
%14 = phi i64 [ 0, %10 ], [ %18, %13 ]
%15 = call i32 (...) @rand()
%16 = trunc i32 %15 to i8
%17 = getelementptr inbounds i8, i8* %8, i64 %14
store i8 %16, i8* %17
%18 = add nuw nsw i64 %14, 1
%19 = icmp eq i64 %18, %11
br i1 %19, label %12, label %13
}