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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
char *strchr(char *, char);
scalar_t__ strglobmatch(char *, char *);
void process_available_tracepoints(const char *sys, const char *event,
                                   int (*process)(const char *sys,
                                                  const char *event)) {
  char *line = NULL;
  FILE *file;
  char str[128] = {0};
  snprintf(str, 64, "%s:%s\n", sys, event);
  file = fopen("/sys/kernel/debug/tracing/available_events", "r");
  if (file == NULL)
    do {
      perror("open "
             "/sys/kernel/debug/tracing/available_events"
             " failed");
      exit(-1);
    } while (0);
  while (!feof(file)) {
    int line_len;
    size_t n;
    line_len = getline(&line, &n, file);
    if (line_len < 0 || !line)
      break;
    if (strglobmatch(line, str)) {
      char match_sys[64] = {0};
      char match_event[64] = {0};
      char *sep;
      sep = strchr(line, ':');
      memcpy(match_sys, line, sep - line);
      memcpy(match_event, sep + 1, line_len - (sep - line) - 2);
      if (process(match_sys, match_event))
        break;
    }
  }
  free(line);
  fclose(file);
}


x86 assembly (clang, -O0):
<process_available_tracepoints>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0xb0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
lea    -0xb0(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x40,%esi
lea    0xdf3(%rip),%rdx
call   1060 <snprintf@plt>
lea    0xdee(%rip),%rdi
lea    0xe12(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    124f <process_available_tracepoints+0x8f>
jmp    1239 <process_available_tracepoints+0x79>
lea    0xdf4(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
jmp    1254 <process_available_tracepoints+0x94>
mov    -0x28(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1272 <process_available_tracepoints+0xb2>
jmp    137f <process_available_tracepoints+0x1bf>
mov    -0x28(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0xc0(%rbp),%rsi
call   10d0 <getline@plt>
mov    %eax,-0xb4(%rbp)
cmpl   $0x0,-0xb4(%rbp)
jl     12a4 <process_available_tracepoints+0xe4>
cmpq   $0x0,-0x20(%rbp)
jne    12a9 <process_available_tracepoints+0xe9>
jmp    137f <process_available_tracepoints+0x1bf>
mov    -0x20(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
call   10a0 <strglobmatch@plt>
cmp    $0x0,%rax
je     137a <process_available_tracepoints+0x1ba>
lea    -0x100(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
lea    -0x140(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x148(%rbp)
lea    -0x100(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x148(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1090 <memcpy@plt>
lea    -0x140(%rbp),%rdi
mov    -0x148(%rbp),%rsi
add    $0x1,%rsi
movslq -0xb4(%rbp),%rax
mov    -0x148(%rbp),%rcx
mov    -0x20(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
sub    $0x2,%rax
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    -0x18(%rbp),%rax
lea    -0x100(%rbp),%rdi
lea    -0x140(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
je     1375 <process_available_tracepoints+0x1b5>
jmp    137f <process_available_tracepoints+0x1bf>
jmp    137a <process_available_tracepoints+0x1ba>
jmp    1254 <process_available_tracepoints+0x94>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_available_tracepoints>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %rdx,0x10(%rsp)
mov    %rsi,%r8
mov    %rdi,%rcx
movq   $0x0,0x8(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
lea    0xde1(%rip),%rdx
lea    0xa0(%rsp),%rdi
mov    $0x40,%esi
call   1060 <snprintf@plt>
lea    0xdcf(%rip),%rdi
lea    0xdf3(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     135c <process_available_tracepoints+0x1ac>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     1280 <process_available_tracepoints+0xd0>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x60(%rsp),%rbp
lea    0x20(%rsp),%r13
jmp    129c <process_available_tracepoints+0xec>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    125c <process_available_tracepoints+0xac>
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
mov    %rbx,%rdx
call   10c0 <getline@plt>
test   %eax,%eax
js     125c <process_available_tracepoints+0xac>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     125c <process_available_tracepoints+0xac>
mov    %eax,%r15d
lea    0xa0(%rsp),%rsi
call   1090 <strglobmatch@plt>
test   %rax,%rax
je     1290 <process_available_tracepoints+0xe0>
xorps  %xmm0,%xmm0
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
mov    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,%r14
mov    %r14d,%edx
sub    %r12d,%edx
mov    %rbp,%rdi
mov    %r12,%rsi
call   1080 <memcpy@plt>
mov    %r14,%rsi
add    $0x1,%rsi
mov    0x8(%rsp),%eax
sub    %r14d,%eax
lea    (%r15,%rax,1),%edx
add    $0xfffffffe,%edx
mov    %r13,%rdi
call   1080 <memcpy@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   *0x10(%rsp)
test   %eax,%eax
je     1290 <process_available_tracepoints+0xe0>
jmp    125c <process_available_tracepoints+0xac>
lea    0xcd1(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"%s:%s\0A\00"
@.str.1 = [43 x i8] c"/sys/kernel/debug/tracing/available_events\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [55 x i8] c"open /sys/kernel/debug/tracing/available_events failed\00"
define void @process_available_tracepoints(i8* %0, i8* %1, i32 (i8*, i8*)* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32 (i8*, i8*)*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [128 x i8]
%10 = alloca i32
%11 = alloca i64
%12 = alloca [64 x i8]
%13 = alloca [64 x i8]
%14 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 (i8*, i8*)* %2, i32 (i8*, i8*)** %6
store i8* null, i8** %7
%15 = bitcast [128 x i8]* %9 to i8*
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%17 = load i8*, i8** %4
%18 = load i8*, i8** %5
%19 = call i32 @snprintf(i8* %16, i32 64, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %18)
%20 = call i32* @fopen(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %8
%21 = load i32*, i32** %8
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %28
23: ; preds = %3
br label %24
24: ; preds = %23
%25 = call i32 @perror(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 -1)
unreachable
27: ; No predecessors!
br label %28
28: ; preds = %27, %3
br label %29
29: ; preds = %83, %28
%30 = load i32*, i32** %8
%31 = call i32 @feof(i32* %30)
%32 = icmp ne i32 %31, 0
%33 = xor i1 %32, true
br i1 %33, label %34, label %84
34: ; preds = %29
%35 = load i32*, i32** %8
%36 = call i32 @getline(i8** %7, i64* %11, i32* %35)
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = icmp slt i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %34
%40 = load i8*, i8** %7
%41 = icmp ne i8* %40, null
br i1 %41, label %43, label %42
42: ; preds = %39, %34
br label %84
43: ; preds = %39
%44 = load i8*, i8** %7
%45 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%46 = call i64 @strglobmatch(i8* %44, i8* %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %83
48: ; preds = %43
%49 = bitcast [64 x i8]* %12 to i8*
%50 = bitcast [64 x i8]* %13 to i8*
%51 = load i8*, i8** %7
%52 = call i8* @strchr(i8* %51, i8 signext 58)
store i8* %52, i8** %14
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%54 = load i8*, i8** %7
%55 = load i8*, i8** %14
%56 = load i8*, i8** %7
%57 = ptrtoint i8* %55 to i64
%58 = ptrtoint i8* %56 to i64
%59 = sub i64 %57, %58
%60 = trunc i64 %59 to i32
%61 = call i32 @memcpy(i8* %53, i8* %54, i32 %60)
%62 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%63 = load i8*, i8** %14
%64 = getelementptr inbounds i8, i8* %63, i64 1
%65 = load i32, i32* %10
%66 = sext i32 %65 to i64
%67 = load i8*, i8** %14
%68 = load i8*, i8** %7
%69 = ptrtoint i8* %67 to i64
%70 = ptrtoint i8* %68 to i64
%71 = sub i64 %69, %70
%72 = sub nsw i64 %66, %71
%73 = sub nsw i64 %72, 2
%74 = trunc i64 %73 to i32
%75 = call i32 @memcpy(i8* %62, i8* %64, i32 %74)
%76 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %6
%77 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%78 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%79 = call i32 %76(i8* %77, i8* %78)
%80 = icmp ne i32 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %48
br label %84
82: ; preds = %48
br label %83
83: ; preds = %82, %43
br label %29
84: ; preds = %81, %42, %29
%85 = load i8*, i8** %7
%86 = call i32 @free(i8* %85)
%87 = load i32*, i32** %8
%88 = call i32 @fclose(i32* %87)
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s:%s\0A\00"
@.str.1 = [43 x i8] c"/sys/kernel/debug/tracing/available_events\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [55 x i8] c"open /sys/kernel/debug/tracing/available_events failed\00"
define void @process_available_tracepoints(i8* %0, i8* %1, i32 (i8*, i8*)* nocapture %2) {
%4 = alloca i8*
%5 = alloca [128 x i8]
%6 = alloca i64
%7 = alloca [64 x i8]
%8 = alloca [64 x i8]
%9 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%11 = call i32 @snprintf(i8* %10, i32 64, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%12 = call i32* @fopen(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %21, label %14
14: ; preds = %3
%15 = call i32 @feof(i32* %12)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %56
17: ; preds = %14
%18 = bitcast i64* %6 to i8*
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
br label %24
21: ; preds = %3
%22 = call i32 @perror(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %17, %52
%25 = call i32 @getline(i8** %4, i64* %6, i32* %12)
%26 = icmp sgt i32 %25, -1
%27 = load i8*, i8** %4
%28 = icmp ne i8* %27, null
%29 = select i1 %26, i1 %28, i1 false
br i1 %29, label %31, label %30
30: ; preds = %24
br label %56
31: ; preds = %24
%32 = call i64 @strglobmatch(i8* %27, i8* %10)
%33 = icmp eq i64 %32, 0
br i1 %33, label %52, label %34
34: ; preds = %31
%35 = load i8*, i8** %4
%36 = call i8* @strchr(i8* dereferenceable(1) %35, i8 signext 58)
%37 = ptrtoint i8* %36 to i64
%38 = ptrtoint i8* %35 to i64
%39 = sub i64 %37, %38
%40 = trunc i64 %39 to i32
%41 = call i32 @memcpy(i8* %19, i8* %35, i32 %40)
%42 = getelementptr inbounds i8, i8* %36, i64 1
%43 = load i8*, i8** %4
%44 = ptrtoint i8* %43 to i64
%45 = sub i64 %44, %37
%46 = trunc i64 %45 to i32
%47 = add nsw i32 %25, -2
%48 = add i32 %47, %46
%49 = call i32 @memcpy(i8* %20, i8* %42, i32 %48)
%50 = call i32 %2(i8* %19, i8* %20)
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %34, %31
%53 = call i32 @feof(i32* %12)
%54 = icmp eq i32 %53, 0
br i1 %54, label %24, label %56
55: ; preds = %34
br label %56
56: ; preds = %52, %14, %55, %30
%57 = load i8*, i8** %4
%58 = call i32 @free(i8* %57)
%59 = call i32 @fclose(i32* %12)
ret void
}