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

/* Variables and functions */
int /*<<< orphan*/ debug(char *, size_t, char const *);
int /*<<< orphan*/ die(char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
size_t sc_read_seccomp_filter(const char *filename, char *buf,
                              size_t buf_size) {
  if (buf_size == 0) {
    die("seccomp load buffer cannot be empty");
  }
  FILE *file = fopen(filename, "rb");
  if (file == NULL) {
    die("cannot open seccomp filter %s", filename);
  }
  size_t num_read = fread(buf, 1, buf_size - 1, file);
  buf[num_read] = '\0';
  if (ferror(file) != 0) {
    die("cannot read seccomp profile %s", filename);
  }
  if (feof(file) == 0) {
    die("cannot fit seccomp profile %s to memory buffer", filename);
  }
  fclose(file);
  debug("read %zu bytes from %s", num_read, filename);
  return num_read;
}


x86 assembly (clang, -O0):
<sc_read_seccomp_filter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119d <sc_read_seccomp_filter+0x2d>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1090 <die@plt>
mov    -0x8(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ce <sc_read_seccomp_filter+0x5e>
mov    -0x8(%rbp),%rsi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1090 <die@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     121d <sc_read_seccomp_filter+0xad>
mov    -0x8(%rbp),%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1090 <die@plt>
mov    -0x20(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
jne    1242 <sc_read_seccomp_filter+0xd2>
mov    -0x8(%rbp),%rsi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1090 <die@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0xe39(%rip),%rdi
call   1060 <debug@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sc_read_seccomp_filter>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r14
test   %rdx,%rdx
jne    1195 <sc_read_seccomp_filter+0x25>
lea    0xe72(%rip),%rdi
xor    %eax,%eax
call   1090 <die@plt>
lea    0xe88(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11bd <sc_read_seccomp_filter+0x4d>
lea    0xe74(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1090 <die@plt>
add    $0xffffffffffffffff,%rbx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1040 <fread@plt>
mov    %rax,%r15
movb   $0x0,(%r12,%rax,1)
mov    %r13,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11fa <sc_read_seccomp_filter+0x8a>
lea    0xe55(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1090 <die@plt>
mov    %r13,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    1218 <sc_read_seccomp_filter+0xa8>
lea    0xe56(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1090 <die@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
lea    0xe6c(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <debug@plt>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [36 x i8] c"seccomp load buffer cannot be empty\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [30 x i8] c"cannot open seccomp filter %s\00"
@.str.3 = [31 x i8] c"cannot read seccomp profile %s\00"
@.str.4 = [47 x i8] c"cannot fit seccomp profile %s to memory buffer\00"
@.str.5 = [23 x i8] c"read %zu bytes from %s\00"
define i64 @sc_read_seccomp_filter(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %3
%14 = load i8*, i8** %4
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %7
%16 = load i32*, i32** %7
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %18, %13
%22 = load i8*, i8** %5
%23 = load i64, i64* %6
%24 = sub i64 %23, 1
%25 = load i32*, i32** %7
%26 = call i64 @fread(i8* %22, i32 1, i64 %24, i32* %25)
store i64 %26, i64* %8
%27 = load i8*, i8** %5
%28 = load i64, i64* %8
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8 0, i8* %29
%30 = load i32*, i32** %7
%31 = call i64 @ferror(i32* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %21
%34 = load i8*, i8** %4
%35 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %33, %21
%37 = load i32*, i32** %7
%38 = call i64 @feof(i32* %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i8*, i8** %4
%42 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %40, %36
%44 = load i32*, i32** %7
%45 = call i32 @fclose(i32* %44)
%46 = load i64, i64* %8
%47 = load i8*, i8** %4
%48 = call i32 @debug(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i64 %46, i8* %47)
%49 = load i64, i64* %8
ret i64 %49
}

LLVM IR (-O2):
@.str = [36 x i8] c"seccomp load buffer cannot be empty\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [30 x i8] c"cannot open seccomp filter %s\00"
@.str.3 = [31 x i8] c"cannot read seccomp profile %s\00"
@.str.4 = [47 x i8] c"cannot fit seccomp profile %s to memory buffer\00"
@.str.5 = [23 x i8] c"read %zu bytes from %s\00"
define i64 @sc_read_seccomp_filter(i8* %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %3
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %10, %7
%13 = add i64 %2, -1
%14 = tail call i64 @fread(i8* %1, i32 1, i64 %13, i32* %8)
%15 = getelementptr inbounds i8, i8* %1, i64 %14
store i8 0, i8* %15
%16 = tail call i64 @ferror(i32* %8)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %12
%19 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %20
20: ; preds = %18, %12
%21 = tail call i64 @feof(i32* %8)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %25
25: ; preds = %23, %20
%26 = tail call i32 @fclose(i32* %8)
%27 = tail call i32 @debug(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i64 %14, i8* %0)
ret i64 %14
}