#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct buffer_instance {
  int dummy;
};
struct TYPE_4__ {
  int gl_pathc;
  char **gl_pathv;
  scalar_t__ gl_offs;
};
typedef TYPE_1__ glob_t;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, char *);
char *get_instance_file(struct buffer_instance *, char *);
int glob(char *, int /*<<< orphan*/, int /*<<< orphan*/ *, TYPE_1__ *);
int /*<<< orphan*/ globfree(TYPE_1__ *);
int /*<<< orphan*/ is_top_instance(struct buffer_instance *);
int /*<<< orphan*/ old_update_events(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ tracecmd_put_tracing_file(char *);
scalar_t__ use_old_event_method();
int write(int, char *, int);
void reset_events_instance(struct buffer_instance *instance) {
  glob_t globbuf;
  char *path;
  char c;
  int fd;
  int i;
  int ret;
  if (use_old_event_method()) {
    if (!is_top_instance(instance))
      return;
    old_update_events("all", '0');
    return;
  }
  c = '0';
  path = get_instance_file(instance, "events/enable");
  fd = open(path, O_WRONLY);
  if (fd < 0)
    die("opening to '%s'", path);
  ret = write(fd, &c, 1);
  close(fd);
  tracecmd_put_tracing_file(path);
  path = get_instance_file(instance, "events/*/filter");
  globbuf.gl_offs = 0;
  ret = glob(path, 0, NULL, &globbuf);
  tracecmd_put_tracing_file(path);
  if (ret < 0)
    return;
  for (i = 0; i < globbuf.gl_pathc; i++) {
    path = globbuf.gl_pathv[i];
    fd = open(path, O_WRONLY);
    if (fd < 0)
      die("opening to '%s'", path);
    ret = write(fd, &c, 1);
    close(fd);
  }
  globfree(&globbuf);
}


x86 assembly (clang, -O0):
<reset_events_instance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <use_old_event_method@plt>
cmp    $0x0,%rax
je     11fa <reset_events_instance+0x4a>
mov    -0x8(%rbp),%rdi
call   10d0 <is_top_instance@plt>
cmp    $0x0,%eax
jne    11e4 <reset_events_instance+0x34>
jmp    1335 <reset_events_instance+0x185>
lea    0xe15(%rip),%rdi
mov    $0x30,%esi
call   1060 <old_update_events@plt>
jmp    1335 <reset_events_instance+0x185>
movb   $0x30,-0x29(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xdfb(%rip),%rsi
call   1070 <get_instance_file@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1241 <reset_events_instance+0x91>
mov    -0x28(%rbp),%rsi
lea    0xdd6(%rip),%rdi
call   10b0 <die@plt>
mov    -0x30(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1080 <write@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%edi
call   1090 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <tracecmd_put_tracing_file@plt>
mov    -0x8(%rbp),%rdi
lea    0xdb1(%rip),%rsi
call   1070 <get_instance_file@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x20(%rbp),%rcx
call   10c0 <glob@plt>
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <tracecmd_put_tracing_file@plt>
cmpl   $0x0,-0x38(%rbp)
jge    12b0 <reset_events_instance+0x100>
jmp    1335 <reset_events_instance+0x185>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    132c <reset_events_instance+0x17c>
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1302 <reset_events_instance+0x152>
mov    -0x28(%rbp),%rsi
lea    0xd15(%rip),%rdi
call   10b0 <die@plt>
mov    -0x30(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1080 <write@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%edi
call   1090 <close@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12b7 <reset_events_instance+0x107>
lea    -0x20(%rbp),%rdi
call   1050 <globfree@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reset_events_instance>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <use_old_event_method@plt>
test   %rax,%rax
je     11fc <reset_events_instance+0x4c>
mov    %rbx,%rdi
call   10d0 <is_top_instance@plt>
test   %eax,%eax
je     1306 <reset_events_instance+0x156>
lea    0xe1c(%rip),%rdi
mov    $0x30,%esi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <old_update_events@plt>
movb   $0x30,0xf(%rsp)
lea    0xdfc(%rip),%rsi
mov    %rbx,%rdi
call   1070 <get_instance_file@plt>
mov    %rax,%r14
mov    0x2dbe(%rip),%r12
mov    (%r12),%esi
mov    %rax,%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    123b <reset_events_instance+0x8b>
lea    0xddf(%rip),%rdi
mov    %r14,%rsi
call   10b0 <die@plt>
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1080 <write@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %r14,%rdi
call   1030 <tracecmd_put_tracing_file@plt>
lea    0xdc0(%rip),%rsi
mov    %rbx,%rdi
call   1070 <get_instance_file@plt>
mov    %rax,%rbx
movq   $0x0,0x20(%rsp)
lea    0x10(%rsp),%rcx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <glob@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <tracecmd_put_tracing_file@plt>
test   %ebp,%ebp
js     1306 <reset_events_instance+0x156>
cmpl   $0x0,0x10(%rsp)
jle    12fc <reset_events_instance+0x14c>
xor    %ebx,%ebx
lea    0xd6d(%rip),%r14
lea    0xf(%rsp),%r15
jmp    12d4 <reset_events_instance+0x124>
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1080 <write@plt>
mov    %ebp,%edi
call   1090 <close@plt>
add    $0x1,%rbx
movslq 0x10(%rsp),%rax
cmp    %rax,%rbx
jge    12fc <reset_events_instance+0x14c>
mov    0x18(%rsp),%rax
mov    (%rax,%rbx,8),%r13
mov    (%r12),%esi
mov    %r13,%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12b0 <reset_events_instance+0x100>
mov    %r14,%rdi
mov    %r13,%rsi
call   10b0 <die@plt>
jmp    12b0 <reset_events_instance+0x100>
lea    0x10(%rsp),%rdi
call   1050 <globfree@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.buffer_instance = type { i32 }
%struct.TYPE_4__ = type { i32, i8**, i64 }
@.str = [4 x i8] c"all\00"
@.str.1 = [14 x i8] c"events/enable\00"
@O_WRONLY = global i32 0
@.str.2 = [16 x i8] c"opening to '%s'\00"
@.str.3 = [16 x i8] c"events/*/filter\00"
define void @reset_events_instance(%struct.buffer_instance* %0) {
%2 = alloca %struct.buffer_instance*
%3 = alloca %struct.TYPE_4__
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store %struct.buffer_instance* %0, %struct.buffer_instance** %2
%9 = call i64 (...) @use_old_event_method()
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %1
%12 = load %struct.buffer_instance*, %struct.buffer_instance** %2
%13 = call i32 @is_top_instance(%struct.buffer_instance* %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %11
br label %77
16: ; preds = %11
%17 = call i32 @old_update_events(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 48)
br label %77
18: ; preds = %1
store i8 48, i8* %5
%19 = load %struct.buffer_instance*, %struct.buffer_instance** %2
%20 = call i8* @get_instance_file(%struct.buffer_instance* %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = load i32, i32* @O_WRONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = load i8*, i8** %4
%28 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26, %18
%30 = load i32, i32* %6
%31 = call i32 @write(i32 %30, i8* %5, i32 1)
store i32 %31, i32* %8
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
%34 = load i8*, i8** %4
%35 = call i32 @tracecmd_put_tracing_file(i8* %34)
%36 = load %struct.buffer_instance*, %struct.buffer_instance** %2
%37 = call i8* @get_instance_file(%struct.buffer_instance* %36, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i8* %37, i8** %4
%38 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i32 0, i32 2
store i64 0, i64* %38
%39 = load i8*, i8** %4
%40 = call i32 @glob(i8* %39, i32 0, i32* null, %struct.TYPE_4__* %3)
store i32 %40, i32* %8
%41 = load i8*, i8** %4
%42 = call i32 @tracecmd_put_tracing_file(i8* %41)
%43 = load i32, i32* %8
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %29
br label %77
46: ; preds = %29
store i32 0, i32* %7
br label %47
47: ; preds = %72, %46
%48 = load i32, i32* %7
%49 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i32 0, i32 0
%50 = load i32, i32* %49
%51 = icmp slt i32 %48, %50
br i1 %51, label %52, label %75
52: ; preds = %47
%53 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i32 0, i32 1
%54 = load i8**, i8*** %53
%55 = load i32, i32* %7
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8*, i8** %54, i64 %56
%58 = load i8*, i8** %57
store i8* %58, i8** %4
%59 = load i8*, i8** %4
%60 = load i32, i32* @O_WRONLY
%61 = call i32 @open(i8* %59, i32 %60)
store i32 %61, i32* %6
%62 = load i32, i32* %6
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %52
%65 = load i8*, i8** %4
%66 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %64, %52
%68 = load i32, i32* %6
%69 = call i32 @write(i32 %68, i8* %5, i32 1)
store i32 %69, i32* %8
%70 = load i32, i32* %6
%71 = call i32 @close(i32 %70)
br label %72
72: ; preds = %67
%73 = load i32, i32* %7
%74 = add nsw i32 %73, 1
store i32 %74, i32* %7
br label %47
75: ; preds = %47
%76 = call i32 @globfree(%struct.TYPE_4__* %3)
br label %77
77: ; preds = %75, %45, %16, %15
ret void
}

LLVM IR (-O2):
%struct.buffer_instance = type { i32 }
%struct.TYPE_4__ = type { i32, i8**, i64 }
@.str = [4 x i8] c"all\00"
@.str.1 = [14 x i8] c"events/enable\00"
@O_WRONLY = global i32 0
@.str.2 = [16 x i8] c"opening to '%s'\00"
@.str.3 = [16 x i8] c"events/*/filter\00"
define void @reset_events_instance(%struct.buffer_instance* %0) {
%2 = alloca %struct.TYPE_4__
%3 = alloca i8
%4 = bitcast %struct.TYPE_4__* %2 to i8*
%5 = tail call i64 (...) @use_old_event_method()
%6 = icmp eq i64 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %1
%8 = tail call i32 @is_top_instance(%struct.buffer_instance* %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %53, label %10
10: ; preds = %7
%11 = tail call i32 @old_update_events(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 48)
br label %53
12: ; preds = %1
store i8 48, i8* %3
%13 = tail call i8* @get_instance_file(%struct.buffer_instance* %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @O_WRONLY
%15 = tail call i32 @open(i8* %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %19
19: ; preds = %17, %12
%20 = call i32 @write(i32 %15, i8* %3, i32 1)
%21 = tail call i32 @close(i32 %15)
%22 = tail call i32 @tracecmd_put_tracing_file(i8* %13)
%23 = tail call i8* @get_instance_file(%struct.buffer_instance* %0, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%24 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 2
store i64 0, i64* %24
%25 = call i32 @glob(i8* %23, i32 0, i32* null, %struct.TYPE_4__* %2)
%26 = call i32 @tracecmd_put_tracing_file(i8* %23)
%27 = icmp slt i32 %25, 0
br i1 %27, label %53, label %28
28: ; preds = %19
%29 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 0
%30 = load i32, i32* %29
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %51
32: ; preds = %28
%33 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 1
br label %34
34: ; preds = %32, %44
%35 = phi i64 [ 0, %32 ], [ %47, %44 ]
%36 = load i8**, i8*** %33
%37 = getelementptr inbounds i8*, i8** %36, i64 %35
%38 = load i8*, i8** %37
%39 = load i32, i32* @O_WRONLY
%40 = call i32 @open(i8* %38, i32 %39)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %34
%43 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %38)
br label %44
44: ; preds = %42, %34
%45 = call i32 @write(i32 %40, i8* %3, i32 1)
%46 = call i32 @close(i32 %40)
%47 = add nuw nsw i64 %35, 1
%48 = load i32, i32* %29
%49 = sext i32 %48 to i64
%50 = icmp slt i64 %47, %49
br i1 %50, label %34, label %51
51: ; preds = %44, %28
%52 = call i32 @globfree(%struct.TYPE_4__* %2)
br label %53
53: ; preds = %19, %7, %51, %10
ret void
}