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

/* Variables and functions */
int O_APPEND;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ write(int, char const *, int /*<<< orphan*/);
int bpf_detach_probe(const char *event_desc, const char *event_type) {
  int kfd;
  char buf[256];
  snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/%s_events", event_type);
  kfd = open(buf, O_WRONLY | O_APPEND, 0);
  if (kfd < 0) {
    fprintf(stderr, "open(%s): %s\n", buf, strerror(errno));
    return -1;
  }
  if (write(kfd, event_desc, strlen(event_desc)) < 0) {
    fprintf(stderr, "write(%s): %s\n", buf, strerror(errno));
    close(kfd);
    return -1;
  }
  close(kfd);
  return 0;
}


x86 assembly (clang, -O0):
<bpf_detach_probe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x100,%esi
lea    0xe66(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    121f <bpf_detach_probe+0xaf>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x12c(%rbp),%edi
mov    -0x128(%rbp),%rdx
mov    %rax,%rcx
lea    0xe16(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <bpf_detach_probe+0x154>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x13c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    -0x10(%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_detach_probe+0x145>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14c(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x148(%rbp)
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x14c(%rbp),%edi
mov    -0x148(%rbp),%rdx
mov    %rax,%rcx
lea    0xd96(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <bpf_detach_probe+0x154>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_detach_probe>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%rcx
mov    %rdi,%r14
lea    0xe78(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1050 <snprintf@plt>
mov    0x2e29(%rip),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1080 <open@plt>
test   %eax,%eax
js     11de <bpf_detach_probe+0x6e>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     120b <bpf_detach_probe+0x9b>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    1242 <bpf_detach_probe+0xd2>
mov    0x2dfb(%rip),%rax
mov    (%rax),%ebx
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe28(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1070 <fprintf@plt>
jmp    123d <bpf_detach_probe+0xcd>
mov    0x2dce(%rip),%rax
mov    (%rax),%ebp
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe09(%rip),%rsi
mov    %rsp,%rdx
mov    %ebp,%edi
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"/sys/kernel/debug/tracing/%s_events\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@stderr = global i32 0
@.str.1 = [14 x i8] c"open(%s): %s\0A\00"
@errno = global i32 0
@.str.2 = [15 x i8] c"write(%s): %s\0A\00"
define i32 @bpf_detach_probe(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [256 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%9 = load i8*, i8** %5
%10 = call i32 @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_APPEND
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %11, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* %22)
store i32 -1, i32* %3
br label %42
24: ; preds = %2
%25 = load i32, i32* %6
%26 = load i8*, i8** %4
%27 = load i8*, i8** %4
%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 %39
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %33, i8* %35)
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %3
br label %42
39: ; preds = %24
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
store i32 0, i32* %3
br label %42
42: ; preds = %39, %31, %18
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@.str = [36 x i8] c"/sys/kernel/debug/tracing/%s_events\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@stderr = global i32 0
@.str.1 = [14 x i8] c"open(%s): %s\0A\00"
@errno = global i32 0
@.str.2 = [15 x i8] c"write(%s): %s\0A\00"
define i32 @bpf_detach_probe(i8* %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %1)
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_APPEND
%8 = or i32 %7, %6
%9 = call i32 @open(i8* %4, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %14)
br label %28
16: ; preds = %2
%17 = call i32 @strlen(i8* %0)
%18 = call i64 @write(i32 %9, i8* %0, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %4, i8* %23)
%25 = call i32 @close(i32 %9)
br label %28
26: ; preds = %16
%27 = call i32 @close(i32 %9)
br label %28
28: ; preds = %26, %20, %11
%29 = phi i32 [ -1, %11 ], [ -1, %20 ], [ 0, %26 ]
ret i32 %29
}