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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
unsigned char *newkey() {
  char *data = NULL;
  int fd = 0;
  fd = open("/dev/urandom", O_RDONLY);
  if (fd <= 0) {
    printf("[ERROR] Failed to open /dev/urandom");
    exit(1);
  }
  data = malloc(32);
  if (data == NULL) {
    printf("[ERROR] Failed to malloc data\n");
    close(fd);
    exit(1);
  }
  if (read(fd, data, 32) != 32) {
    printf("[ERROR] Failed to read random data\n");
    close(fd);
    free(data);
    exit(1);
  }
  close(fd);
  return data;
}


x86 assembly (clang, -O0):
<newkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movq   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe80(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     11bf <newkey+0x4f>
lea    0xe50(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x20,%edi
call   1070 <malloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11f6 <newkey+0x86>
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x20,%edx
call   1060 <read@plt>
cmp    $0x20,%eax
je     1237 <newkey+0xc7>
lea    0xe2c(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newkey>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
jle    11d5 <newkey+0x55>
mov    %eax,%ebp
mov    $0x20,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11eb <newkey+0x6b>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    $0x20,%edx
call   1070 <read@plt>
cmp    $0x20,%eax
jne    1208 <newkey+0x88>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe24(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe55(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe15(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [36 x i8] c"[ERROR] Failed to open /dev/urandom\00"
@.str.2 = [31 x i8] c"[ERROR] Failed to malloc data\0A\00"
@.str.3 = [36 x i8] c"[ERROR] Failed to read random data\0A\00"
define i8* @newkey() {
%1 = alloca i8*
%2 = alloca i32
store i8* null, i8** %1
store i32 0, i32* %2
%3 = load i32, i32* @O_RDONLY
%4 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp sle i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = call i8* @malloc(i32 32)
store i8* %11, i8** %1
%12 = load i8*, i8** %1
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* %2
%17 = call i32 @close(i32 %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = load i32, i32* %2
%21 = load i8*, i8** %1
%22 = call i32 @read(i32 %20, i8* %21, i32 32)
%23 = icmp ne i32 %22, 32
br i1 %23, label %24, label %31
24: ; preds = %19
%25 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* %2
%27 = call i32 @close(i32 %26)
%28 = load i8*, i8** %1
%29 = call i32 @free(i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %19
%32 = load i32, i32* %2
%33 = call i32 @close(i32 %32)
%34 = load i8*, i8** %1
ret i8* %34
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [36 x i8] c"[ERROR] Failed to open /dev/urandom\00"
@str = [35 x i8] c"[ERROR] Failed to read random data\00"
@str.4 = [30 x i8] c"[ERROR] Failed to malloc data\00"
define noalias i8* @newkey() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 1
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = tail call dereferenceable_or_null(32) i8* @malloc(i32 32)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.4, i64 0, i64 0))
%12 = tail call i32 @close(i32 %2)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %7
%15 = tail call i32 @read(i32 %2, i8* %8, i32 32)
%16 = icmp eq i32 %15, 32
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 @close(i32 %2)
%20 = tail call i32 @free(i8* %8)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = tail call i32 @close(i32 %2)
ret i8* %8
}