#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 */
int O_APPEND;
int O_WRONLY;
int bpf_attach_tracing_event(int, char *, int, int /*<<< orphan*/, int);
int /*<<< orphan*/ close(int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
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) {
    perror("write(kprobe_events)");
    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    $0x140,%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    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe48(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11dd <bpf_attach_kprobe+0x6d>
lea    0xe55(%rip),%rdi
call   1090 <perror@plt>
jmp    1262 <bpf_attach_kprobe+0xf2>
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    1229 <bpf_attach_kprobe+0xb9>
lea    0xe1d(%rip),%rdi
call   1090 <perror@plt>
jmp    1262 <bpf_attach_kprobe+0xf2>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xe11(%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     1274 <bpf_attach_kprobe+0x104>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_attach_kprobe>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %edi,%r14d
mov    0x2e4b(%rip),%rax
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe61(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1210 <bpf_attach_kprobe+0xa0>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1223 <bpf_attach_kprobe+0xb3>
lea    0xe85(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %r15,%rcx
call   1050 <snprintf@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0xffffffff,%edx
xor    %ecx,%ecx
mov    $0xffffffff,%r8d
call   1060 <bpf_attach_tracing_event@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe11(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1200 <bpf_attach_kprobe+0x90>
lea    0xe12(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11f9 <bpf_attach_kprobe+0x89>

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"
@.str.2 = [21 x i8] c"write(kprobe_events)\00"
@.str.3 = [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 %40
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 %33
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %40
33: ; preds = %24
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%35 = load i8*, i8** %8
%36 = call i32 @snprintf(i8* %34, i32 256, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %7
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%39 = call i32 @bpf_attach_tracing_event(i32 %37, i8* %38, i32 -1, i32 0, i32 -1)
store i32 %39, i32* %13
br label %40
40: ; preds = %33, %31, %22
%41 = load i32, i32* %14
%42 = icmp sge i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* %14
%45 = call i32 @close(i32 %44)
br label %46
46: ; preds = %43, %40
%47 = load i32, i32* %13
ret i32 %47
}

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"
@.str.2 = [21 x i8] c"write(kprobe_events)\00"
@.str.3 = [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 %23, 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 %20
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %25
20: ; preds = %14
%21 = call i32 @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%22 = call i32 @bpf_attach_tracing_event(i32 %0, i8* %8, i32 -1, i32 0, i32 -1)
br label %25
23: ; preds = %6
%24 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %28
25: ; preds = %20, %18
%26 = phi i32 [ %22, %20 ], [ -1, %18 ]
%27 = call i32 @close(i32 %12)
br label %28
28: ; preds = %23, %25
%29 = phi i32 [ %26, %25 ], [ -1, %23 ]
ret i32 %29
}