#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 O_APPEND;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ write(int, char const *, int /*<<< orphan*/);
int bpf_detach_kprobe(const char *event_desc) {
  int rc = -1, kfd = -1;
  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;
  }
  rc = 0;
cleanup:
  if (kfd >= 0)
    close(kfd);
  return rc;
}


x86 assembly (clang, -O0):
<bpf_detach_kprobe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    0x2e56(%rip),%rax
or     (%rax),%esi
lea    0xe7d(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11a8 <bpf_detach_kprobe+0x58>
lea    0xe8a(%rip),%rdi
call   1070 <perror@plt>
jmp    11ef <bpf_detach_kprobe+0x9f>
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11e8 <bpf_detach_kprobe+0x98>
lea    0xe5e(%rip),%rdi
call   1070 <perror@plt>
jmp    11ef <bpf_detach_kprobe+0x9f>
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1201 <bpf_detach_kprobe+0xb1>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_detach_kprobe>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2e7a(%rip),%rax
mov    0x2e6b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe90(%rip),%rdi
xor    %r14d,%r14d
xor    %edx,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11a8 <bpf_detach_kprobe+0x58>
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     11bc <bpf_detach_kprobe+0x6c>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe79(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11a0 <bpf_detach_kprobe+0x50>
lea    0xe79(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1199 <bpf_detach_kprobe+0x49>

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"
define i32 @bpf_detach_kprobe(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %3
store i32 -1, i32* %4
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_APPEND
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %7, i32 0)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %23
13: ; preds = %1
%14 = load i32, i32* %4
%15 = load i8*, i8** %2
%16 = load i8*, i8** %2
%17 = call i32 @strlen(i8* %16)
%18 = call i64 @write(i32 %14, i8* %15, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %23
22: ; preds = %13
store i32 0, i32* %3
br label %23
23: ; preds = %22, %20, %11
%24 = load i32, i32* %4
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
br label %29
29: ; preds = %26, %23
%30 = load i32, i32* %3
ret i32 %30
}

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"
define i32 @bpf_detach_kprobe(i8* %0) {
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_APPEND
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %1
%8 = tail call i32 @strlen(i8* %0)
%9 = tail call i64 @write(i32 %5, i8* %0, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %15
13: ; preds = %1
%14 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %18
15: ; preds = %7, %11
%16 = phi i32 [ 0, %7 ], [ -1, %11 ]
%17 = tail call i32 @close(i32 %5)
br label %18
18: ; preds = %13, %15
%19 = phi i32 [ %16, %15 ], [ -1, %13 ]
ret i32 %19
}