#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);
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 *, ...);
__attribute__((constructor)) void load_seccomp(void) {
  int fd = open("/run/firejail/mnt/seccomp.postexec", O_RDONLY);
  if (fd == -1)
    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 = MAP_FAILED;
  if (size != 0)
    filter = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
  close(fd);
  if (filter == MAP_FAILED)
    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    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    118f <load_seccomp+0x2f>
jmp    1286 <load_seccomp+0x126>
mov    -0x4(%rbp),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jg     11be <load_seccomp+0x5e>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
jmp    1286 <load_seccomp+0x126>
mov    -0x10(%rbp),%rax
movzwl %ax,%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %ax,-0x12(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1214 <load_seccomp+0xb4>
mov    -0x10(%rbp),%rsi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    0x2dc2(%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    0x2d91(%rip),%rcx
cmp    (%rcx),%rax
jne    1235 <load_seccomp+0xd5>
jmp    1286 <load_seccomp+0x126>
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   1060 <prctl@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    $0x2,%esi
lea    -0x30(%rbp),%rdx
mov    $0x0,%al
call   1060 <prctl@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <munmap@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_seccomp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1228 <load_seccomp+0xc8>
mov    %eax,%ebp
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
jle    1231 <load_seccomp+0xd1>
mov    %rax,%rbx
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    0x2e0a(%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    0x2ddf(%rip),%rax
cmp    (%rax),%r14
je     1228 <load_seccomp+0xc8>
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   1060 <prctl@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rdx
mov    $0x2,%esi
xor    %eax,%eax
call   1060 <prctl@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <munmap@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebp,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <close@plt>

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

LLVM IR (-O2):
%struct.sock_filter = type { i32 }
%struct.sock_fprog = type { i16, %struct.sock_filter* }
@.str = [35 x i8] c"/run/firejail/mnt/seccomp.postexec\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@MAP_FAILED = global %struct.sock_filter* null
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@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 ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %28, label %5
5: ; preds = %0
%6 = load i32, i32* @SEEK_END
%7 = tail call i64 @lseek(i32 %3, i32 0, i32 %6)
%8 = icmp slt i64 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @close(i32 %3)
br label %28
11: ; preds = %5
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @MAP_PRIVATE
%14 = tail call %struct.sock_filter* @mmap(i32* null, i64 %7, i32 %12, i32 %13, i32 %3, i32 0)
%15 = tail call i32 @close(i32 %3)
%16 = load %struct.sock_filter*, %struct.sock_filter** @MAP_FAILED
%17 = icmp eq %struct.sock_filter* %14, %16
br i1 %17, label %28, label %18
18: ; preds = %11
%19 = trunc i64 %7 to i16
%20 = lshr i16 %19, 2
%21 = bitcast %struct.sock_fprog* %1 to i8*
%22 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %1, i64 0, i32 0
store i16 %20, i16* %22
%23 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %1, i64 0, i32 1
store %struct.sock_filter* %14, %struct.sock_filter** %23
%24 = tail call i32 (i32, i32, %struct.sock_fprog*, ...) @prctl(i32 38, i32 1, %struct.sock_fprog* null, i32 0, i32 0)
%25 = load i32, i32* @PR_SET_SECCOMP
%26 = call i32 (i32, i32, %struct.sock_fprog*, ...) @prctl(i32 %25, i32 2, %struct.sock_fprog* %1)
%27 = call i32 @munmap(%struct.sock_filter* %14, i64 %7)
br label %28
28: ; preds = %9, %11, %18, %0
ret void
}