#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 int /*<<< orphan*/ filter;

/* Variables and functions */
int BPF_K;
int BPF_RET;
struct sock_filter BPF_STMT(int, int /*<<< orphan*/);
int /*<<< orphan*/ CLOCK_PROCESS_CPUTIME_ID;
int /*<<< orphan*/ PR_SET_NO_NEW_PRIVS;
int /*<<< orphan*/ PR_SET_SECCOMP;
int /*<<< orphan*/ SECCOMP_MODE_FILTER;
int /*<<< orphan*/ SECCOMP_RET_ALLOW;
int /*<<< orphan*/ assert(int);
unsigned long long calibrate();
long prctl(int /*<<< orphan*/, int, struct sock_fprog *, ...);
int /*<<< orphan*/ printf(char *, ...);
unsigned long long strtoull(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
unsigned long long timing(int /*<<< orphan*/, unsigned long long);
int main(int argc, char *argv[]) {
  struct sock_filter filter[] = {
      BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
  };
  struct sock_fprog prog = {
      .len = (unsigned short)(sizeof(filter) / sizeof(filter[0])),
      .filter = filter,
  };
  long ret;
  unsigned long long samples;
  unsigned long long native, filtered;
  if (argc > 1)
    samples = strtoull(argv[1], NULL, 0);
  else
    samples = calibrate();
  printf("Benchmarking %llu samples...\n", samples);
  native = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
  printf("getpid native: %llu ns\n", native);
  ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
  assert(ret == 0);
  ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
  assert(ret == 0);
  filtered = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
  printf("getpid RET_ALLOW: %llu ns\n", filtered);
  printf("Estimated seccomp overhead per syscall: %llu ns\n",
         filtered - native);
  if (filtered == native)
    printf("Trying running again with more samples.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e52(%rip),%rax
or     (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
call   1090 <BPF_STMT@plt>
mov    %eax,-0x18(%rbp)
movw   $0x1,-0x28(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11dd <main+0x6d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <strtoull@plt>
mov    %rax,-0x38(%rbp)
jmp    11e8 <main+0x78>
mov    $0x0,%al
call   1070 <calibrate@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
call   1030 <timing@plt>
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x38(%rbp)
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%esi
xor    %eax,%eax
mov    %eax,%edx
xor    %r8d,%r8d
mov    %r8d,%ecx
mov    $0x0,%al
call   1080 <prctl@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
mov    $0x0,%al
call   1080 <prctl@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
call   1030 <timing@plt>
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x38(%rbp)
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rsi
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x48(%rbp),%rsi
sub    -0x40(%rbp),%rsi
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x48(%rbp),%rax
cmp    -0x40(%rbp),%rax
jne    12f4 <main+0x184>
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e4c(%rip),%rax
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
call   10a0 <BPF_STMT@plt>
mov    %eax,0x8(%rsp)
movw   $0x1,0x10(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x18(%rsp)
cmp    $0x2,%ebp
jl     11d6 <main+0x56>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtoull@plt>
jmp    11dd <main+0x5d>
xor    %eax,%eax
call   1080 <calibrate@plt>
mov    %rax,%rbx
lea    0xe19(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dd8(%rip),%rbp
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
call   1040 <timing@plt>
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     1219 <main+0x99>
xor    %edx,%edx
div    %rbx
mov    %rax,%r14
jmp    1220 <main+0xa0>
xor    %edx,%edx
div    %ebx
mov    %eax,%r14d
lea    0xdf7(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %eax,%eax
call   1090 <prctl@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1090 <prctl@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
call   1040 <timing@plt>
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     12a8 <main+0x128>
xor    %edx,%edx
div    %rbx
mov    %rax,%rbx
jmp    12ae <main+0x12e>
xor    %edx,%edx
div    %ebx
mov    %eax,%ebx
lea    0xd81(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd8b(%rip),%rdi
mov    %rbx,%rsi
sub    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    %r14,%rbx
jne    12e4 <main+0x164>
lea    0xda3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sock_filter = type { i32 }
%struct.sock_fprog = type { i16, %struct.sock_filter* }
@BPF_RET = global i32 0
@BPF_K = global i32 0
@SECCOMP_RET_ALLOW = global i32 0
@.str = [30 x i8] c"Benchmarking %llu samples...\0A\00"
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
@.str.1 = [24 x i8] c"getpid native: %llu ns\0A\00"
@PR_SET_NO_NEW_PRIVS = global i32 0
@PR_SET_SECCOMP = global i32 0
@SECCOMP_MODE_FILTER = global i32 0
@.str.2 = [27 x i8] c"getpid RET_ALLOW: %llu ns\0A\00"
@.str.3 = [49 x i8] c"Estimated seccomp overhead per syscall: %llu ns\0A\00"
@.str.4 = [41 x i8] c"Trying running again with more samples.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1 x %struct.sock_filter]
%7 = alloca %struct.sock_fprog
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = getelementptr inbounds [1 x %struct.sock_filter], [1 x %struct.sock_filter]* %6, i64 0, i64 0
%13 = load i32, i32* @BPF_RET
%14 = load i32, i32* @BPF_K
%15 = or i32 %13, %14
%16 = load i32, i32* @SECCOMP_RET_ALLOW
%17 = call i32 @BPF_STMT(i32 %15, i32 %16)
%18 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %12, i32 0, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %7, i32 0, i32 0
store i16 1, i16* %19
%20 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %7, i32 0, i32 1
%21 = getelementptr inbounds [1 x %struct.sock_filter], [1 x %struct.sock_filter]* %6, i64 0, i64 0
store %struct.sock_filter* %21, %struct.sock_filter** %20
%22 = load i32, i32* %4
%23 = icmp sgt i32 %22, 1
br i1 %23, label %24, label %29
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i64 @strtoull(i8* %27, i32* null, i32 0)
store i64 %28, i64* %9
br label %31
29: ; preds = %2
%30 = call i64 (...) @calibrate()
store i64 %30, i64* %9
br label %31
31: ; preds = %29, %24
%32 = load i64, i64* %9
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %32)
%34 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%35 = load i64, i64* %9
%36 = call i64 @timing(i32 %34, i64 %35)
%37 = load i64, i64* %9
%38 = udiv i64 %36, %37
store i64 %38, i64* %10
%39 = load i64, i64* %10
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i64 %39)
%41 = load i32, i32* @PR_SET_NO_NEW_PRIVS
%42 = call i64 (i32, i32, %struct.sock_fprog*, ...) @prctl(i32 %41, i32 1, %struct.sock_fprog* null, i32 0, i32 0)
store i64 %42, i64* %8
%43 = load i64, i64* %8
%44 = icmp eq i64 %43, 0
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
%47 = load i32, i32* @PR_SET_SECCOMP
%48 = load i32, i32* @SECCOMP_MODE_FILTER
%49 = call i64 (i32, i32, %struct.sock_fprog*, ...) @prctl(i32 %47, i32 %48, %struct.sock_fprog* %7)
store i64 %49, i64* %8
%50 = load i64, i64* %8
%51 = icmp eq i64 %50, 0
%52 = zext i1 %51 to i32
%53 = call i32 @assert(i32 %52)
%54 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%55 = load i64, i64* %9
%56 = call i64 @timing(i32 %54, i64 %55)
%57 = load i64, i64* %9
%58 = udiv i64 %56, %57
store i64 %58, i64* %11
%59 = load i64, i64* %11
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i64 %59)
%61 = load i64, i64* %11
%62 = load i64, i64* %10
%63 = sub i64 %61, %62
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i64 %63)
%65 = load i64, i64* %11
%66 = load i64, i64* %10
%67 = icmp eq i64 %65, %66
br i1 %67, label %68, label %70
68: ; preds = %31
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %70
70: ; preds = %68, %31
ret i32 0
}

LLVM IR (-O2):
%struct.sock_filter = type { i32 }
%struct.sock_fprog = type { i16, %struct.sock_filter* }
@BPF_RET = global i32 0
@BPF_K = global i32 0
@SECCOMP_RET_ALLOW = global i32 0
@.str = [30 x i8] c"Benchmarking %llu samples...\0A\00"
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
@.str.1 = [24 x i8] c"getpid native: %llu ns\0A\00"
@PR_SET_NO_NEW_PRIVS = global i32 0
@PR_SET_SECCOMP = global i32 0
@SECCOMP_MODE_FILTER = global i32 0
@.str.2 = [27 x i8] c"getpid RET_ALLOW: %llu ns\0A\00"
@.str.3 = [49 x i8] c"Estimated seccomp overhead per syscall: %llu ns\0A\00"
@str = [40 x i8] c"Trying running again with more samples.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1 x %struct.sock_filter]
%4 = alloca %struct.sock_fprog
%5 = bitcast [1 x %struct.sock_filter]* %3 to i8*
%6 = getelementptr inbounds [1 x %struct.sock_filter], [1 x %struct.sock_filter]* %3, i64 0, i64 0
%7 = load i32, i32* @BPF_RET
%8 = load i32, i32* @BPF_K
%9 = or i32 %8, %7
%10 = load i32, i32* @SECCOMP_RET_ALLOW
%11 = tail call i32 @BPF_STMT(i32 %9, i32 %10)
%12 = getelementptr inbounds [1 x %struct.sock_filter], [1 x %struct.sock_filter]* %3, i64 0, i64 0, i32 0
store i32 %11, i32* %12
%13 = bitcast %struct.sock_fprog* %4 to i8*
%14 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %4, i64 0, i32 0
store i16 1, i16* %14
%15 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %4, i64 0, i32 1
store %struct.sock_filter* %6, %struct.sock_filter** %15
%16 = icmp sgt i32 %0, 1
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = call i64 @strtoull(i8* nocapture %19, i32* null, i32 0)
br label %23
21: ; preds = %2
%22 = call i64 (...) @calibrate()
br label %23
23: ; preds = %21, %17
%24 = phi i64 [ %20, %17 ], [ %22, %21 ]
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %24)
%26 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%27 = call i64 @timing(i32 %26, i64 %24)
%28 = udiv i64 %27, %24
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i64 %28)
%30 = load i32, i32* @PR_SET_NO_NEW_PRIVS
%31 = call i64 (i32, i32, %struct.sock_fprog*, ...) @prctl(i32 %30, i32 1, %struct.sock_fprog* null, i32 0, i32 0)
%32 = icmp eq i64 %31, 0
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i32, i32* @PR_SET_SECCOMP
%36 = load i32, i32* @SECCOMP_MODE_FILTER
%37 = call i64 (i32, i32, %struct.sock_fprog*, ...) @prctl(i32 %35, i32 %36, %struct.sock_fprog* %4)
%38 = icmp eq i64 %37, 0
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%42 = call i64 @timing(i32 %41, i64 %24)
%43 = udiv i64 %42, %24
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i64 %43)
%45 = sub i64 %43, %28
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i64 %45)
%47 = icmp eq i64 %43, %28
br i1 %47, label %48, label %50
48: ; preds = %23
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
br label %50
50: ; preds = %48, %23
ret i32 0
}