#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 sock_fprog {
  unsigned short len;
  struct sock_filter *filter;
};
struct sock_filter {
  int dummy;
};
typedef scalar_t__ off_t;

/* Variables and functions */
struct sock_filter *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ PR_SET_SECCOMP;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
struct sock_filter *mmap(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/,
                         int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(struct sock_filter *, scalar_t__);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ prctl(int, int, struct sock_fprog *, ...);
int /*<<< orphan*/ stderr;
__attribute__((constructor)) void load_seccomp(void) {
  int fd = open("/run"
                "/firejail"
                "/mnt"
                "/seccomp"
                "/seccomp.postexec",
                O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "Error: cannot open seccomp postexec filter file %s\n",
            "/run"
            "/firejail"
            "/mnt"
            "/seccomp"
            "/seccomp.postexec");
    return;
  }
  off_t size = lseek(fd, 0, SEEK_END);
  if (size <= 0) {
    close(fd);
    return;
  }
  unsigned short entries =
      (unsigned short)size / (unsigned short)sizeof(struct sock_filter);
  struct sock_filter *filter = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
  close(fd);
  if (filter == MAP_FAILED) {
    fprintf(stderr, "Error: cannot map seccomp postexec filter data\n");
    return;
  }
  struct sock_fprog prog = {
      .len = entries,
      .filter = filter,
  };
  prctl(38, 1, 0, 0, 0);
  prctl(PR_SET_SECCOMP, 2, &prog);
  munmap(filter, size);
}


x86 assembly (clang, -O0):
<load_seccomp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11bd <load_seccomp+0x4d>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe81(%rip),%rsi
lea    0xe4f(%rip),%rdx
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12b2 <load_seccomp+0x142>
mov    -0x4(%rbp),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jg     11ec <load_seccomp+0x7c>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
jmp    12b2 <load_seccomp+0x142>
mov    -0x10(%rbp),%rax
movzwl %ax,%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %ax,-0x12(%rbp)
mov    -0x10(%rbp),%rsi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
mov    0x2da5(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
mov    0x2d74(%rip),%rcx
cmp    (%rcx),%rax
jne    1261 <load_seccomp+0xf1>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12b2 <load_seccomp+0x142>
mov    -0x12(%rbp),%ax
mov    %ax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x26,%edi
mov    $0x1,%esi
xor    %eax,%eax
mov    %eax,%edx
xor    %r8d,%r8d
mov    %r8d,%ecx
mov    $0x0,%al
call   1070 <prctl@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
mov    $0x2,%esi
lea    -0x30(%rbp),%rdx
mov    $0x0,%al
call   1070 <prctl@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <munmap@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_seccomp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1245 <load_seccomp+0xd5>
mov    %eax,%ebp
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
jle    126b <load_seccomp+0xfb>
mov    %rax,%rbx
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
mov    0x2df2(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2dc7(%rip),%rax
cmp    (%rax),%r14
je     127a <load_seccomp+0x10a>
movzwl %bx,%eax
shr    $0x2,%eax
mov    %ax,(%rsp)
mov    %r14,0x8(%rsp)
mov    $0x26,%edi
mov    $0x1,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %eax,%eax
call   1070 <prctl@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rdx
mov    $0x2,%esi
xor    %eax,%eax
call   1070 <prctl@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <munmap@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
lea    0xda4(%rip),%rdx
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <fprintf@plt>
mov    %ebp,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <close@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <fprintf@plt>

LLVM IR (-O0):
%struct.sock_filter = type { i32 }
%struct.sock_fprog = type { i16, %struct.sock_filter* }
@.str = [43 x i8] c"/run/firejail/mnt/seccomp/seccomp.postexec\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [52 x i8] c"Error: cannot open seccomp postexec filter file %s\0A\00"
@SEEK_END = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global %struct.sock_filter* null
@.str.2 = [48 x i8] c"Error: cannot map seccomp postexec filter data\0A\00"
@PR_SET_SECCOMP = global i32 0
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @load_seccomp, i8* null }]
define void @load_seccomp() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i16
%4 = alloca %struct.sock_filter*
%5 = alloca %struct.sock_fprog
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %52
13: ; preds = %0
%14 = load i32, i32* %1
%15 = load i32, i32* @SEEK_END
%16 = call i64 @lseek(i32 %14, i32 0, i32 %15)
store i64 %16, i64* %2
%17 = load i64, i64* %2
%18 = icmp sle i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* %1
%21 = call i32 @close(i32 %20)
br label %52
22: ; preds = %13
%23 = load i64, i64* %2
%24 = trunc i64 %23 to i16
%25 = zext i16 %24 to i32
%26 = sdiv i32 %25, 4
%27 = trunc i32 %26 to i16
store i16 %27, i16* %3
%28 = load i64, i64* %2
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @MAP_PRIVATE
%31 = load i32, i32* %1
%32 = call %struct.sock_filter* @mmap(i32* null, i64 %28, i32 %29, i32 %30, i32 %31, i32 0)
store %struct.sock_filter* %32, %struct.sock_filter** %4
%33 = load i32, i32* %1
%34 = call i32 @close(i32 %33)
%35 = load %struct.sock_filter*, %struct.sock_filter** %4
%36 = load %struct.sock_filter*, %struct.sock_filter** @MAP_FAILED
%37 = icmp eq %struct.sock_filter* %35, %36
br i1 %37, label %38, label %41
38: ; preds = %22
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
br label %52
41: ; preds = %22
%42 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %5, i32 0, i32 0
%43 = load i16, i16* %3
store i16 %43, i16* %42
%44 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %5, i32 0, i32 1
%45 = load %struct.sock_filter*, %struct.sock_filter** %4
store %struct.sock_filter* %45, %struct.sock_filter** %44
%46 = call i32 (i32, i32, %struct.sock_fprog*, ...) @prctl(i32 38, i32 1, %struct.sock_fprog* null, i32 0, i32 0)
%47 = load i32, i32* @PR_SET_SECCOMP
%48 = call i32 (i32, i32, %struct.sock_fprog*, ...) @prctl(i32 %47, i32 2, %struct.sock_fprog* %5)
%49 = load %struct.sock_filter*, %struct.sock_filter** %4
%50 = load i64, i64* %2
%51 = call i32 @munmap(%struct.sock_filter* %49, i64 %50)
br label %52
52: ; preds = %41, %38, %19, %10
ret void
}

LLVM IR (-O2):
%struct.sock_filter = type { i32 }
%struct.sock_fprog = type { i16, %struct.sock_filter* }
@.str = [43 x i8] c"/run/firejail/mnt/seccomp/seccomp.postexec\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [52 x i8] c"Error: cannot open seccomp postexec filter file %s\0A\00"
@SEEK_END = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global %struct.sock_filter* null
@.str.2 = [48 x i8] c"Error: cannot map seccomp postexec filter data\0A\00"
@PR_SET_SECCOMP = global i32 0
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @load_seccomp, i8* null }]
define void @load_seccomp() {
%1 = alloca %struct.sock_fprog
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %34
8: ; preds = %0
%9 = load i32, i32* @SEEK_END
%10 = tail call i64 @lseek(i32 %3, i32 0, i32 %9)
%11 = icmp slt i64 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @close(i32 %3)
br label %34
14: ; preds = %8
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @MAP_PRIVATE
%17 = tail call %struct.sock_filter* @mmap(i32* null, i64 %10, i32 %15, i32 %16, i32 %3, i32 0)
%18 = tail call i32 @close(i32 %3)
%19 = load %struct.sock_filter*, %struct.sock_filter** @MAP_FAILED
%20 = icmp eq %struct.sock_filter* %17, %19
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
br label %34
24: ; preds = %14
%25 = trunc i64 %10 to i16
%26 = lshr i16 %25, 2
%27 = bitcast %struct.sock_fprog* %1 to i8*
%28 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %1, i64 0, i32 0
store i16 %26, i16* %28
%29 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %1, i64 0, i32 1
store %struct.sock_filter* %17, %struct.sock_filter** %29
%30 = tail call i32 (i32, i32, %struct.sock_fprog*, ...) @prctl(i32 38, i32 1, %struct.sock_fprog* null, i32 0, i32 0)
%31 = load i32, i32* @PR_SET_SECCOMP
%32 = call i32 (i32, i32, %struct.sock_fprog*, ...) @prctl(i32 %31, i32 2, %struct.sock_fprog* %1)
%33 = call i32 @munmap(%struct.sock_filter* %17, i64 %10)
br label %34
34: ; preds = %12, %24, %21, %5
ret void
}