#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;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
int generate_key() {
  int i;
  int j;
  int fd;
  int len;
  uint8_t key[64];
  if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
    perror("open()");
    return (1);
  };
  if ((len = read(fd, key, 64)) == -1) {
    perror("read()");
    return (1);
  };
  printf("512 bit key:");
  for (i = 0; i < 64; i++)
    if (i % 32)
      printf("%02X", key[i]);
    else
      printf("\n%02X", key[i]);
  printf("\n------ \n");
  return (0);
}


x86 assembly (clang, -O0):
<generate_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
lea    0xea5(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1184 <generate_key+0x44>
lea    0xe9a(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124e <generate_key+0x10e>
mov    -0x10(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x40,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11bc <generate_key+0x7c>
lea    0xe69(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124e <generate_key+0x10e>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x40,-0x8(%rbp)
jge    1239 <generate_key+0xf9>
mov    -0x8(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     120d <generate_key+0xcd>
movslq -0x8(%rbp),%rax
mov    -0x120(%rbp,%rax,4),%esi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1226 <generate_key+0xe6>
movslq -0x8(%rbp),%rax
mov    -0x120(%rbp,%rax,4),%esi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122b <generate_key+0xeb>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11d1 <generate_key+0x91>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_key>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e9 <generate_key+0x99>
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x40,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11f2 <generate_key+0xa2>
lea    0xe8a(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe89(%rip),%r14
lea    0xe83(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
test   $0x1f,%bl
mov    (%rsp,%rbx,4),%esi
mov    %r15,%rdi
cmove  %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    $0x40,%rbx
jne    11b0 <generate_key+0x60>
lea    0xe59(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe1d(%rip),%rdi
jmp    11f9 <generate_key+0xa9>
lea    0xe1b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    11dc <generate_key+0x8c>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [7 x i8] c"read()\00"
@.str.3 = [13 x i8] c"512 bit key:\00"
@.str.4 = [5 x i8] c"%02X\00"
@.str.5 = [6 x i8] c"\0A%02X\00"
@.str.6 = [10 x i8] c"\0A------ \0A\00"
define i32 @generate_key() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [64 x i32]
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %46
12: ; preds = %0
%13 = load i32, i32* %4
%14 = getelementptr inbounds [64 x i32], [64 x i32]* %6, i64 0, i64 0
%15 = call i32 @read(i32 %13, i32* %14, i32 64)
store i32 %15, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %46
19: ; preds = %12
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %21
21: ; preds = %41, %19
%22 = load i32, i32* %2
%23 = icmp slt i32 %22, 64
br i1 %23, label %24, label %44
24: ; preds = %21
%25 = load i32, i32* %2
%26 = srem i32 %25, 32
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = load i32, i32* %2
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [64 x i32], [64 x i32]* %6, i64 0, i64 %30
%32 = load i32, i32* %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %32)
br label %40
34: ; preds = %24
%35 = load i32, i32* %2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [64 x i32], [64 x i32]* %6, i64 0, i64 %36
%38 = load i32, i32* %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %34, %28
br label %41
41: ; preds = %40
%42 = load i32, i32* %2
%43 = add nsw i32 %42, 1
store i32 %43, i32* %2
br label %21
44: ; preds = %21
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %46
46: ; preds = %44, %17, %10
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [7 x i8] c"read()\00"
@.str.3 = [13 x i8] c"512 bit key:\00"
@.str.4 = [5 x i8] c"%02X\00"
@.str.5 = [6 x i8] c"\0A%02X\00"
@str = [9 x i8] c"\0A------ \00"
define i32 @generate_key() {
%1 = alloca [64 x i32]
%2 = bitcast [64 x i32]* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %28
8: ; preds = %0
%9 = getelementptr inbounds [64 x i32], [64 x i32]* %1, i64 0, i64 0
%10 = call i32 @read(i32 %4, i32* %9, i32 64)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %28
14: ; preds = %8
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ 0, %14 ], [ %24, %16 ]
%18 = and i64 %17, 31
%19 = icmp eq i64 %18, 0
%20 = getelementptr inbounds [64 x i32], [64 x i32]* %1, i64 0, i64 %17
%21 = load i32, i32* %20
%22 = select i1 %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %22, i32 %21)
%24 = add nuw nsw i64 %17, 1
%25 = icmp eq i64 %24, 64
br i1 %25, label %26, label %16
26: ; preds = %16
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %26, %12, %6
%29 = phi i32 [ 1, %6 ], [ 1, %12 ], [ 0, %26 ]
ret i32 %29
}