#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*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *load_key(const char *keyfile, int keysize) {
  char *key = NULL;
  int key_fd = open(keyfile, O_RDONLY);
  if (key_fd < 0) {
    perror("Unable to open key file");
  } else {
    key = (char *)malloc(keysize);
    if (read(key_fd, key, keysize) != keysize) {
      fprintf(stderr, "Key file size should be at exactly %d bytes\n", keysize);
      free(key);
      key = NULL;
    } else {
      printf("Key loaded from %s, %d bits\n", (char *)&keyfile, keysize * 8);
    }
    close(key_fd);
  }
  return key;
}


x86 assembly (clang, -O0):
<load_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c7 <load_key+0x47>
lea    0xe43(%rip),%rdi
call   10a0 <perror@plt>
jmp    1237 <load_key+0xb7>
mov    -0xc(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1060 <read@plt>
cmp    -0xc(%rbp),%eax
je     1219 <load_key+0x99>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe1a(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
jmp    122f <load_key+0xaf>
lea    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
shl    $0x3,%edx
lea    0xe1b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_key>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,0x8(%rsp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     120c <load_key+0x8c>
mov    %eax,%r14d
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1060 <read@plt>
cmp    %ebp,%eax
jne    11d7 <load_key+0x57>
shl    $0x3,%ebp
lea    0xe7c(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    %ebp,%edx
call   1040 <printf@plt>
jmp    11f8 <load_key+0x78>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
mov    %ebp,%edx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %r14d,%edi
call   1050 <close@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xded(%rip),%rdi
call   10a0 <perror@plt>
xor    %ebx,%ebx
jmp    1200 <load_key+0x80>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [24 x i8] c"Unable to open key file\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"Key file size should be at exactly %d bytes\0A\00"
@.str.2 = [29 x i8] c"Key loaded from %s, %d bits\0A\00"
define i8* @load_key(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i8* null, i8** %5
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %38
14: ; preds = %2
%15 = load i32, i32* %4
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %5
%18 = load i32, i32* %6
%19 = load i8*, i8** %5
%20 = load i32, i32* %4
%21 = call i32 @read(i32 %18, i8* %19, i32 %20)
%22 = load i32, i32* %4
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %30
24: ; preds = %14
%25 = load i32, i32* @stderr
%26 = load i32, i32* %4
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i8*, i8** %5
%29 = call i32 @free(i8* %28)
store i8* null, i8** %5
br label %35
30: ; preds = %14
%31 = bitcast i8** %3 to i8*
%32 = load i32, i32* %4
%33 = mul nsw i32 %32, 8
%34 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %31, i32 %33)
br label %35
35: ; preds = %30, %24
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %35, %12
%39 = load i8*, i8** %5
ret i8* %39
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [24 x i8] c"Unable to open key file\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"Key file size should be at exactly %d bytes\0A\00"
@.str.2 = [29 x i8] c"Key loaded from %s, %d bits\0A\00"
define i8* @load_key(i8* %0, i32 %1) {
%3 = alloca i8*
store i8* %0, i8** %3
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %25
9: ; preds = %2
%10 = tail call i64 @malloc(i32 %1)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @read(i32 %5, i8* %11, i32 %1)
%13 = icmp eq i32 %12, %1
br i1 %13, label %18, label %14
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%17 = tail call i32 @free(i8* %11)
br label %22
18: ; preds = %9
%19 = bitcast i8** %3 to i8*
%20 = shl nsw i32 %1, 3
%21 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %19, i32 %20)
br label %22
22: ; preds = %18, %14
%23 = phi i8* [ null, %14 ], [ %11, %18 ]
%24 = call i32 @close(i32 %5)
br label %25
25: ; preds = %22, %7
%26 = phi i8* [ null, %7 ], [ %23, %22 ]
ret i8* %26
}