#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 int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EINVAL;
int O_APPEND;
int O_WRONLY;
int bpf_attach_tracing_event(int, char *, int, int /*<<< orphan*/, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ write(int, char const *, int /*<<< orphan*/);
int bpf_attach_kprobe(int progfd, const char *event, const char *event_desc,
                      pid_t pid, int cpu, int group_fd) {
  int rc = -1, kfd = -1;
  char buf[256];
  kfd = open("/sys/kernel/debug/tracing/kprobe_events", O_WRONLY | O_APPEND, 0);
  if (kfd < 0) {
    perror("open(kprobe_events)");
    goto cleanup;
  }
  if (write(kfd, event_desc, strlen(event_desc)) < 0) {
    fprintf(stderr, "write of \"%s\" into kprobe_events failed: %s\n",
            event_desc, strerror(errno));
    if (errno == EINVAL)
      fprintf(stderr, "check dmesg output for possible cause\n");
    goto cleanup;
  }
  snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/kprobes/%s",
           event);
  rc = bpf_attach_tracing_event(progfd, buf, -1, 0, -1);
cleanup:
  if (kfd >= 0)
    close(kfd);
  return rc;
}


x86 assembly (clang, -O0):
<bpf_attach_kprobe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    0x2de9(%rip),%rax
or     (%rax),%esi
lea    0xe28(%rip),%rdi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11fd <bpf_attach_kprobe+0x6d>
lea    0xe35(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ee <bpf_attach_kprobe+0x15e>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x13c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x13c(%rbp),%edi
mov    -0x138(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12b5 <bpf_attach_kprobe+0x125>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x148(%rbp)
mov    0x2d7f(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x14c(%rbp),%edi
mov    -0x148(%rbp),%rdx
mov    %rax,%rcx
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%rax
mov    0x2d40(%rip),%rcx
cmp    (%rcx),%rax
jne    12b0 <bpf_attach_kprobe+0x120>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    12ee <bpf_attach_kprobe+0x15e>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xdc4(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xffffffff,%r8d
xor    %ecx,%ecx
mov    %r8d,%edx
call   1060 <bpf_attach_tracing_event@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     1300 <bpf_attach_kprobe+0x170>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_attach_kprobe>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r14d
mov    0x2e11(%rip),%rax
mov    0x2e02(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe3f(%rip),%rdi
xor    %edx,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1291 <bpf_attach_kprobe+0x101>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1221 <bpf_attach_kprobe+0x91>
lea    0xe9e(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r15,%rcx
call   1050 <snprintf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xffffffff,%edx
xor    %ecx,%ecx
mov    $0xffffffff,%r8d
call   1060 <bpf_attach_tracing_event@plt>
mov    %eax,%ebx
jmp    1278 <bpf_attach_kprobe+0xe8>
mov    0x2db8(%rip),%r15
mov    (%r15),%r14d
mov    0x2da6(%rip),%r12
mov    (%r12),%rdi
call   10b0 <strerror@plt>
lea    0xdfa(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r12),%rax
mov    0x2d73(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
jne    1278 <bpf_attach_kprobe+0xe8>
mov    (%r15),%edi
lea    0xdf8(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd90(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebx
jmp    127f <bpf_attach_kprobe+0xef>

LLVM IR (-O0):
@.str = [40 x i8] c"/sys/kernel/debug/tracing/kprobe_events\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.1 = [20 x i8] c"open(kprobe_events)\00"
@stderr = global i32 0
@.str.2 = [45 x i8] c"write of \22%s\22 into kprobe_events failed: %s\0A\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.3 = [39 x i8] c"check dmesg output for possible cause\0A\00"
@.str.4 = [44 x i8] c"/sys/kernel/debug/tracing/events/kprobes/%s\00"
define i32 @bpf_attach_kprobe(i32 %0, i8* %1, i8* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [256 x i8]
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
store i32 -1, i32* %13
store i32 -1, i32* %14
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_APPEND
%18 = or i32 %16, %17
%19 = call i32 @open(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %18, i32 0)
store i32 %19, i32* %14
%20 = load i32, i32* %14
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %6
%23 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %51
24: ; preds = %6
%25 = load i32, i32* %14
%26 = load i8*, i8** %9
%27 = load i8*, i8** %9
%28 = call i32 @strlen(i8* %27)
%29 = call i64 @write(i32 %25, i8* %26, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %44
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %9
%34 = load i64, i64* @errno
%35 = call i8* @strerror(i64 %34)
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %33, i8* %35)
%37 = load i64, i64* @errno
%38 = load i64, i64* @EINVAL
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %31
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %40, %31
br label %51
44: ; preds = %24
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%46 = load i8*, i8** %8
%47 = call i32 @snprintf(i8* %45, i32 256, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = load i32, i32* %7
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%50 = call i32 @bpf_attach_tracing_event(i32 %48, i8* %49, i32 -1, i32 0, i32 -1)
store i32 %50, i32* %13
br label %51
51: ; preds = %44, %43, %22
%52 = load i32, i32* %14
%53 = icmp sge i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32, i32* %14
%56 = call i32 @close(i32 %55)
br label %57
57: ; preds = %54, %51
%58 = load i32, i32* %13
ret i32 %58
}

LLVM IR (-O2):
@.str = [40 x i8] c"/sys/kernel/debug/tracing/kprobe_events\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.1 = [20 x i8] c"open(kprobe_events)\00"
@stderr = global i32 0
@.str.2 = [45 x i8] c"write of \22%s\22 into kprobe_events failed: %s\0A\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.3 = [39 x i8] c"check dmesg output for possible cause\0A\00"
@.str.4 = [44 x i8] c"/sys/kernel/debug/tracing/events/kprobes/%s\00"
define i32 @bpf_attach_kprobe(i32 %0, i8* %1, i8* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca [256 x i8]
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_APPEND
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %32, label %14
14: ; preds = %6
%15 = tail call i32 @strlen(i8* %2)
%16 = tail call i64 @write(i32 %12, i8* %2, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = load i64, i64* @errno
%21 = tail call i8* @strerror(i64 %20)
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %21)
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINVAL
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %34
26: ; preds = %18
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %34
29: ; preds = %14
%30 = call i32 @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%31 = call i32 @bpf_attach_tracing_event(i32 %0, i8* %8, i32 -1, i32 0, i32 -1)
br label %34
32: ; preds = %6
%33 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %37
34: ; preds = %18, %26, %29
%35 = phi i32 [ %31, %29 ], [ -1, %18 ], [ -1, %26 ]
%36 = call i32 @close(i32 %12)
br label %37
37: ; preds = %32, %34
%38 = phi i32 [ %35, %34 ], [ -1, %32 ]
ret i32 %38
}