#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 */
struct stat {
  size_t st_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char const *, int /*<<< orphan*/);
size_t read(int, void *, size_t);
int /*<<< orphan*/ stderr;
bool read_file(const char *filename, void *buffer, size_t len) {
  int fd;
  size_t file_len;
  struct stat file_info;
  if ((fd = open(filename, O_RDONLY)) == -1) {
    fprintf(stderr,
            "[-] "
            "could not open private key file %s"
            "\n",
            filename);
    return false;
  }
  fstat(fd, &file_info);
  if (!S_ISREG(file_info.st_mode)) {
    close(fd);
    fprintf(stderr,
            "[-] "
            "private key %s is not a file"
            "\n",
            filename);
    return false;
  }
  file_len = file_info.st_size;
  if (file_len < len) {
    close(fd);
    fprintf(stderr,
            "[-] "
            "private key %s contains less than %zu bytes"
            "\n",
            filename, len);
    return false;
  }
  if (read(fd, buffer, len) != len) {
    close(fd);
    fprintf(stderr,
            "[-] "
            "not able to read %zu bytes from private key file"
            "\n",
            len);
    return false;
  }
  close(fd);
  return true;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11b9 <read_file+0x59>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a6 <read_file+0x146>
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x38(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1205 <read_file+0xa5>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a6 <read_file+0x146>
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    124e <read_file+0xee>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a6 <read_file+0x146>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <read@plt>
cmp    -0x20(%rbp),%rax
je     1297 <read_file+0x137>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a6 <read_file+0x146>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11d0 <read_file+0x70>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
mov    0x10(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     11e2 <read_file+0x82>
cmp    %r14,0x8(%rsp)
jae    1212 <read_file+0xb2>
mov    %ebp,%edi
call   1030 <close@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe8b(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1205 <read_file+0xa5>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
jmp    11f9 <read_file+0x99>
mov    %ebp,%edi
call   1030 <close@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1030 <close@plt>
mov    $0x1,%ebp
cmp    %r14,%rbx
je     1205 <read_file+0xa5>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
xor    %ebp,%ebp
mov    %r14,%rdx
jmp    11fe <read_file+0x9e>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"[-] could not open private key file %s\0A\00"
@.str.1 = [34 x i8] c"[-] private key %s is not a file\0A\00"
@.str.2 = [49 x i8] c"[-] private key %s contains less than %zu bytes\0A\00"
@.str.3 = [54 x i8] c"[-] not able to read %zu bytes from private key file\0A\00"
define i32 @read_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca %struct.stat
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %5
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 0, i32* %4
br label %61
19: ; preds = %3
%20 = load i32, i32* %8
%21 = call i32 @fstat(i32 %20, %struct.stat* %10)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%23 = load i32, i32* %22
%24 = call i32 @S_ISREG(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %19
%27 = load i32, i32* %8
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %5
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %30)
store i32 0, i32* %4
br label %61
32: ; preds = %19
%33 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%34 = load i64, i64* %33
store i64 %34, i64* %9
%35 = load i64, i64* %9
%36 = load i64, i64* %7
%37 = icmp ult i64 %35, %36
br i1 %37, label %38, label %45
38: ; preds = %32
%39 = load i32, i32* %8
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %5
%43 = load i64, i64* %7
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i8* %42, i64 %43)
store i32 0, i32* %4
br label %61
45: ; preds = %32
%46 = load i32, i32* %8
%47 = load i8*, i8** %6
%48 = load i64, i64* %7
%49 = call i64 @read(i32 %46, i8* %47, i64 %48)
%50 = load i64, i64* %7
%51 = icmp ne i64 %49, %50
br i1 %51, label %52, label %58
52: ; preds = %45
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* @stderr
%56 = load i64, i64* %7
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %56)
store i32 0, i32* %4
br label %61
58: ; preds = %45
%59 = load i32, i32* %8
%60 = call i32 @close(i32 %59)
store i32 1, i32* %4
br label %61
61: ; preds = %58, %52, %38, %26, %15
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"[-] could not open private key file %s\0A\00"
@.str.1 = [34 x i8] c"[-] private key %s is not a file\0A\00"
@.str.2 = [49 x i8] c"[-] private key %s contains less than %zu bytes\0A\00"
@.str.3 = [54 x i8] c"[-] not able to read %zu bytes from private key file\0A\00"
define i32 @read_file(i8* %0, i8* nocapture %1, i64 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %37
12: ; preds = %3
%13 = call i32 @fstat(i32 %7, %struct.stat* %4)
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%15 = load i32, i32* %14
%16 = tail call i32 @S_ISREG(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = tail call i32 @close(i32 %7)
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %37
22: ; preds = %12
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%24 = load i64, i64* %23
%25 = icmp ult i64 %24, %2
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = tail call i32 @close(i32 %7)
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i8* %0, i64 %2)
br label %37
30: ; preds = %22
%31 = tail call i64 @read(i32 %7, i8* %1, i64 %2)
%32 = icmp eq i64 %31, %2
%33 = tail call i32 @close(i32 %7)
br i1 %32, label %37, label %34
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %2)
br label %37
37: ; preds = %30, %34, %26, %18, %9
%38 = phi i32 [ 0, %9 ], [ 0, %26 ], [ 0, %34 ], [ 0, %18 ], [ 1, %30 ]
ret i32 %38
}