#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 enum bpf_attach_type {
  ____Placeholder_bpf_attach_type
} bpf_attach_type;

/* Variables and functions */
int BPF_CGROUP_INET_EGRESS;
int BPF_CGROUP_INET_INGRESS;
int EXIT_FAILURE;
int O_DIRECTORY;
int O_RDONLY;
int attach_filter(int, int, int);
int bpf_prog_detach(int, int);
int errno;
int getopt(int, char **, char *);
int open(char *, int);
int optind;
int /*<<< orphan*/ printf(char *, char *, ...);
scalar_t__ strcmp(char *, char *);
char *strerror(int);
int usage(char *);
int main(int argc, char **argv) {
  int detach_only = 0, verdict = 1;
  enum bpf_attach_type type;
  int opt, cg_fd, ret;
  while ((opt = getopt(argc, argv, "Dd")) != -1) {
    switch (opt) {
    case 'd':
      verdict = 0;
      break;
    case 'D':
      detach_only = 1;
      break;
    default:
      return usage(argv[0]);
    }
  }
  if (argc - optind < 2)
    return usage(argv[0]);
  if (strcmp(argv[optind + 1], "ingress") == 0)
    type = BPF_CGROUP_INET_INGRESS;
  else if (strcmp(argv[optind + 1], "egress") == 0)
    type = BPF_CGROUP_INET_EGRESS;
  else
    return usage(argv[0]);
  cg_fd = open(argv[optind], O_DIRECTORY | O_RDONLY);
  if (cg_fd < 0) {
    printf("Failed to open cgroup path: '%s'\n", strerror(errno));
    return EXIT_FAILURE;
  }
  if (detach_only) {
    ret = bpf_prog_detach(cg_fd, type);
    printf("bpf_prog_detach() returned '%s' (%d)\n", strerror(errno), errno);
  } else
    ret = attach_filter(cg_fd, type, verdict);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe4e(%rip),%rdx
call   1090 <getopt@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
je     1219 <main+0x99>
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x44,%eax
je     11f4 <main+0x74>
jmp    11d7 <main+0x57>
mov    -0x2c(%rbp),%eax
sub    $0x64,%eax
jne    1200 <main+0x80>
jmp    11e8 <main+0x68>
movl   $0x0,-0x18(%rbp)
jmp    1214 <main+0x94>
movl   $0x1,-0x14(%rbp)
jmp    1214 <main+0x94>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
mov    %eax,-0x4(%rbp)
jmp    1399 <main+0x219>
jmp    11a4 <main+0x24>
mov    -0x8(%rbp),%eax
mov    0x2dbd(%rip),%rcx
sub    (%rcx),%eax
cmp    $0x2,%eax
jge    1242 <main+0xc2>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
mov    %eax,-0x4(%rbp)
jmp    1399 <main+0x219>
mov    -0x10(%rbp),%rax
mov    0x2d93(%rip),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xda3(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1280 <main+0x100>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    12d7 <main+0x157>
mov    -0x10(%rbp),%rax
mov    0x2d55(%rip),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xd6d(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12be <main+0x13e>
mov    0x2d0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    12d2 <main+0x152>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
mov    %eax,-0x4(%rbp)
jmp    1399 <main+0x219>
jmp    12d7 <main+0x157>
mov    -0x10(%rbp),%rax
mov    0x2cfe(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2cd8(%rip),%rax
mov    (%rax),%esi
mov    0x2cef(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    133d <main+0x1bd>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xced(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1399 <main+0x219>
cmpl   $0x0,-0x14(%rbp)
je     1382 <main+0x202>
mov    -0x24(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1040 <bpf_prog_detach@plt>
mov    %eax,-0x28(%rbp)
mov    0x2c5c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
mov    0x2c4b(%rip),%rax
mov    (%rax),%edx
lea    0xcbe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1393 <main+0x213>
mov    -0x24(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <attach_filter@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %edi,%ebp
xor    %ebx,%ebx
mov    $0x1,%r14d
lea    0xe64(%rip),%r15
mov    %ebx,%ecx
xchg   %ax,%ax
mov    %ecx,%ebx
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r15,%rdx
call   1090 <getopt@plt>
mov    $0x1,%ecx
cmp    $0x44,%eax
je     11a0 <main+0x20>
cmp    $0xffffffff,%eax
je     11c8 <main+0x48>
cmp    $0x64,%eax
jne    1208 <main+0x88>
xor    %r14d,%r14d
jmp    119c <main+0x1c>
mov    0x2e11(%rip),%r15
movslq (%r15),%rax
sub    %eax,%ebp
cmp    $0x1,%ebp
jle    1208 <main+0x88>
mov    0x8(%r12,%rax,8),%rdi
lea    0xe1e(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1219 <main+0x99>
movslq (%r15),%rax
mov    0x8(%r12,%rax,8),%rdi
lea    0xe0d(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1222 <main+0xa2>
mov    (%r12),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <usage@plt>
mov    0x2db8(%rip),%rax
jmp    1229 <main+0xa9>
mov    0x2d97(%rip),%rax
mov    (%rax),%ebp
movslq (%r15),%rax
mov    (%r12,%rax,8),%rdi
mov    0x2d8f(%rip),%rax
mov    0x2da8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1283 <main+0x103>
mov    %eax,%edi
mov    %ebp,%esi
test   %ebx,%ebx
je     12b4 <main+0x134>
call   1040 <bpf_prog_detach@plt>
mov    %eax,%ebx
mov    0x2d55(%rip),%rbp
mov    0x0(%rbp),%edi
call   10a0 <strerror@plt>
mov    0x0(%rbp),%edx
lea    0xdbf(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
jmp    12ab <main+0x12b>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd7a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14d,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <attach_filter@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"Dd\00"
@optind = global i32 0
@.str.1 = [8 x i8] c"ingress\00"
@BPF_CGROUP_INET_INGRESS = global i32 0
@.str.2 = [7 x i8] c"egress\00"
@BPF_CGROUP_INET_EGRESS = global i32 0
@O_DIRECTORY = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [34 x i8] c"Failed to open cgroup path: '%s'\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.4 = [38 x i8] c"bpf_prog_detach() returned '%s' (%d)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 1, i32* %7
br label %12
12: ; preds = %26, %2
%13 = load i32, i32* %4
%14 = load i8**, i8*** %5
%15 = call i32 @getopt(i32 %13, i8** %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %15, i32* %9
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %27
17: ; preds = %12
%18 = load i32, i32* %9
switch i32 %18, label %21 [
i32 100, label %19
i32 68, label %20
]
19: ; preds = %17
store i32 0, i32* %7
br label %26
20: ; preds = %17
store i32 1, i32* %6
br label %26
21: ; preds = %17
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 @usage(i8* %24)
store i32 %25, i32* %3
br label %100
26: ; preds = %20, %19
br label %12
27: ; preds = %12
%28 = load i32, i32* %4
%29 = load i32, i32* @optind
%30 = sub nsw i32 %28, %29
%31 = icmp slt i32 %30, 2
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = call i32 @usage(i8* %35)
store i32 %36, i32* %3
br label %100
37: ; preds = %27
%38 = load i8**, i8*** %5
%39 = load i32, i32* @optind
%40 = add nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8*, i8** %38, i64 %41
%43 = load i8*, i8** %42
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %37
%47 = load i32, i32* @BPF_CGROUP_INET_INGRESS
store i32 %47, i32* %8
br label %65
48: ; preds = %37
%49 = load i8**, i8*** %5
%50 = load i32, i32* @optind
%51 = add nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8*, i8** %49, i64 %52
%54 = load i8*, i8** %53
%55 = call i64 @strcmp(i8* %54, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %48
%58 = load i32, i32* @BPF_CGROUP_INET_EGRESS
store i32 %58, i32* %8
br label %64
59: ; preds = %48
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 0
%62 = load i8*, i8** %61
%63 = call i32 @usage(i8* %62)
store i32 %63, i32* %3
br label %100
64: ; preds = %57
br label %65
65: ; preds = %64, %46
%66 = load i8**, i8*** %5
%67 = load i32, i32* @optind
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8*, i8** %66, i64 %68
%70 = load i8*, i8** %69
%71 = load i32, i32* @O_DIRECTORY
%72 = load i32, i32* @O_RDONLY
%73 = or i32 %71, %72
%74 = call i32 @open(i8* %70, i32 %73)
store i32 %74, i32* %10
%75 = load i32, i32* %10
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %82
77: ; preds = %65
%78 = load i32, i32* @errno
%79 = call i8* @strerror(i32 %78)
%80 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %79)
%81 = load i32, i32* @EXIT_FAILURE
store i32 %81, i32* %3
br label %100
82: ; preds = %65
%83 = load i32, i32* %6
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %93
85: ; preds = %82
%86 = load i32, i32* %10
%87 = load i32, i32* %8
%88 = call i32 @bpf_prog_detach(i32 %86, i32 %87)
store i32 %88, i32* %11
%89 = load i32, i32* @errno
%90 = call i8* @strerror(i32 %89)
%91 = load i32, i32* @errno
%92 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i8* %90, i32 %91)
br label %98
93: ; preds = %82
%94 = load i32, i32* %10
%95 = load i32, i32* %8
%96 = load i32, i32* %7
%97 = call i32 @attach_filter(i32 %94, i32 %95, i32 %96)
store i32 %97, i32* %11
br label %98
98: ; preds = %93, %85
%99 = load i32, i32* %11
store i32 %99, i32* %3
br label %100
100: ; preds = %98, %77, %59, %32, %21
%101 = load i32, i32* %3
ret i32 %101
}

LLVM IR (-O2):
@.str = [3 x i8] c"Dd\00"
@optind = global i32 0
@.str.1 = [8 x i8] c"ingress\00"
@BPF_CGROUP_INET_INGRESS = global i32 0
@.str.2 = [7 x i8] c"egress\00"
@BPF_CGROUP_INET_EGRESS = global i32 0
@O_DIRECTORY = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [34 x i8] c"Failed to open cgroup path: '%s'\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.4 = [38 x i8] c"bpf_prog_detach() returned '%s' (%d)\0A\00"
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %6, %2
%4 = phi i32 [ 1, %2 ], [ 0, %6 ]
%5 = phi i32 [ 0, %2 ], [ %7, %6 ]
br label %6
6: ; preds = %6, %3
%7 = phi i32 [ %5, %3 ], [ 1, %6 ]
%8 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
switch i32 %8, label %9 [
i32 -1, label %12
i32 100, label %3
i32 68, label %6
]
9: ; preds = %6
%10 = load i8*, i8** %1
%11 = tail call i32 @usage(i8* %10)
br label %64
12: ; preds = %6
%13 = load i32, i32* @optind
%14 = sub nsw i32 %0, %13
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i8*, i8** %1
%18 = tail call i32 @usage(i8* %17)
br label %64
19: ; preds = %12
%20 = add nsw i32 %13, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8*, i8** %1, i64 %21
%23 = load i8*, i8** %22
%24 = tail call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %37, label %26
26: ; preds = %19
%27 = load i32, i32* @optind
%28 = add nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %1, i64 %29
%31 = load i8*, i8** %30
%32 = tail call i64 @strcmp(i8* %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %26
%35 = load i8*, i8** %1
%36 = tail call i32 @usage(i8* %35)
br label %64
37: ; preds = %26, %19
%38 = phi i32* [ @BPF_CGROUP_INET_INGRESS, %19 ], [ @BPF_CGROUP_INET_EGRESS, %26 ]
%39 = load i32, i32* %38
%40 = load i32, i32* @optind
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8*, i8** %1, i64 %41
%43 = load i8*, i8** %42
%44 = load i32, i32* @O_DIRECTORY
%45 = load i32, i32* @O_RDONLY
%46 = or i32 %45, %44
%47 = tail call i32 @open(i8* %43, i32 %46)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %37
%50 = load i32, i32* @errno
%51 = tail call i8* @strerror(i32 %50)
%52 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %51)
%53 = load i32, i32* @EXIT_FAILURE
br label %64
54: ; preds = %37
%55 = icmp eq i32 %7, 0
br i1 %55, label %62, label %56
56: ; preds = %54
%57 = tail call i32 @bpf_prog_detach(i32 %47, i32 %39)
%58 = load i32, i32* @errno
%59 = tail call i8* @strerror(i32 %58)
%60 = load i32, i32* @errno
%61 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i8* %59, i32 %60)
br label %64
62: ; preds = %54
%63 = tail call i32 @attach_filter(i32 %47, i32 %39, i32 %4)
br label %64
64: ; preds = %56, %62, %49, %34, %16, %9
%65 = phi i32 [ %11, %9 ], [ %18, %16 ], [ %53, %49 ], [ %36, %34 ], [ %57, %56 ], [ %63, %62 ]
ret i32 %65
}