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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ PR_SET_NO_NEW_PRIVS;
int atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getppid();
int /*<<< orphan*/ install_filter();
int /*<<< orphan*/ perror(char *);
scalar_t__ prctl(int /*<<< orphan*/, int, int /*<<< orphan*/,
                 int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int j, nloops;
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <num-loops> [x]\n", argv[0]);
    fprintf(stderr, "       (use 'x' to run with BPF filter applied)\n");
    exit(EXIT_FAILURE);
  }
  if (argc > 2) {
    printf("Appling BPF filter\n");
    if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
      do {
        perror("prctl");
        exit(EXIT_FAILURE);
      } while (0);
    install_filter();
  }
  nloops = atoi(argv[1]);
  for (j = 0; j < nloops; j++)
    getppid();
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e3 <main+0x63>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jle    1245 <main+0xc5>
lea    0xe58(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%esi
xor    %r8d,%r8d
mov    %r8d,%edx
mov    %r8d,%ecx
call   1060 <prctl@plt>
cmp    $0x0,%rax
je     123e <main+0xbe>
jmp    1224 <main+0xa4>
lea    0xe35(%rip),%rdi
call   1070 <perror@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1040 <install_filter@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    127d <main+0xfd>
mov    $0x0,%al
call   1090 <getppid@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    125c <main+0xdc>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11ba <main+0x3a>
mov    0x2e4d(%rip),%r14
mov    (%r14),%edi
mov    (%rbx),%rdx
lea    0xe60(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r14),%edi
lea    0xe6a(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1226 <main+0xa6>
cmp    $0x2,%edi
je     11f1 <main+0x71>
lea    0xe8c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <prctl@plt>
test   %rax,%rax
jne    121a <main+0x9a>
xor    %eax,%eax
call   1040 <install_filter@plt>
mov    0x8(%rbx),%rdi
call   1080 <atoi@plt>
test   %eax,%eax
jle    120c <main+0x8c>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <getppid@plt>
add    $0xffffffff,%ebx
jne    1200 <main+0x80>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xe2b(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s <num-loops> [x]\0A\00"
@.str.1 = [49 x i8] c" (use 'x' to run with BPF filter applied)\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [20 x i8] c"Appling BPF filter\0A\00"
@PR_SET_NO_NEW_PRIVS = global i32 0
@.str.3 = [6 x i8] c"prctl\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %20
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 2
br i1 %22, label %23, label %36
23: ; preds = %20
%24 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @PR_SET_NO_NEW_PRIVS
%26 = call i64 @prctl(i32 %25, i32 1, i32 0, i32 0, i32 0)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %23
br label %29
29: ; preds = %28
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; No predecessors!
br label %34
34: ; preds = %33, %23
%35 = call i32 (...) @install_filter()
br label %36
36: ; preds = %34, %20
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i32 @atoi(i8* %39)
store i32 %40, i32* %7
store i32 0, i32* %6
br label %41
41: ; preds = %47, %36
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = call i32 (...) @getppid()
br label %47
47: ; preds = %45
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
store i32 %49, i32* %6
br label %41
50: ; preds = %41
%51 = load i32, i32* @EXIT_SUCCESS
%52 = call i32 @exit(i32 %51)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s <num-loops> [x]\0A\00"
@.str.1 = [49 x i8] c" (use 'x' to run with BPF filter applied)\0A\00"
@EXIT_FAILURE = global i32 0
@PR_SET_NO_NEW_PRIVS = global i32 0
@.str.3 = [6 x i8] c"prctl\00"
@EXIT_SUCCESS = global i32 0
@str = [19 x i8] c"Appling BPF filter\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %12
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = icmp eq i32 %0, 2
br i1 %13, label %25, label %14
14: ; preds = %12
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%16 = load i32, i32* @PR_SET_NO_NEW_PRIVS
%17 = tail call i64 @prctl(i32 %16, i32 1, i32 0, i32 0, i32 0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %14
%24 = tail call i32 (...) @install_filter()
br label %25
25: ; preds = %23, %12
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = tail call i32 @atoi(i8* %27)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %25, %30
%31 = phi i32 [ %33, %30 ], [ 0, %25 ]
%32 = tail call i32 (...) @getppid()
%33 = add nuw nsw i32 %31, 1
%34 = icmp eq i32 %33, %28
br i1 %34, label %35, label %30
35: ; preds = %30, %25
%36 = load i32, i32* @EXIT_SUCCESS
%37 = tail call i32 @exit(i32 %36)
unreachable
}