#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
char *find_debugfs();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int stat(char *, struct stat *);
scalar_t__ strlen(char const *);
scalar_t__ strncmp(char *, char *, int);
char *strstr(char *, char *);
int write(int, char *, int);
int setup_hr_tick(void) {
  const char *debugfs = find_debugfs();
  char files[strlen(debugfs) + strlen("/sched_features") + 1];
  char buf[500];
  struct stat st;
  static int set = 0;
  char *p;
  int ret;
  int len;
  int fd;
  if (set)
    return 1;
  set = 1;
  if (strlen(debugfs) == 0)
    return 0;
  sprintf(files, "%s/sched_features", debugfs);
  ret = stat(files, &st);
  if (ret < 0)
    return 0;
  fd = open(files, O_RDWR);
  if (fd < 0) {
    perror(files);
    return 0;
  }
  len = sizeof(buf);
  ret = read(fd, buf, len);
  if (ret < 0) {
    perror(files);
    close(fd);
    return 0;
  }
  if (ret >= len)
    ret = len - 1;
  buf[ret] = 0;
  ret = 1;
  p = strstr(buf, "HRTICK");
  if (p + 3 >= buf) {
    p -= 3;
    if (strncmp(p, "NO_HRTICK", 9) == 0) {
      ret = write(fd, "HRTICK", 6);
      if (ret != 6)
        ret = 0;
      else
        ret = 1;
    }
  }
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<setup_hr_tick>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
xor    %eax,%eax
call   1070 <find_debugfs@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x250(%rbp)
lea    0xe25(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x250(%rbp),%rcx
mov    %rax,%rdx
lea    0x1(%rcx,%rdx,1),%rax
mov    %rsp,%rsi
mov    %rsi,-0x18(%rbp)
lea    0x10(%rcx,%rdx,1),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x248(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
cmpl   $0x0,0x2e60(%rip)
je     1238 <setup_hr_tick+0x88>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x240(%rbp)
jmp    1478 <setup_hr_tick+0x2c8>
movl   $0x1,0x2e3a(%rip)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
jne    126b <setup_hr_tick+0xbb>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x240(%rbp)
jmp    1478 <setup_hr_tick+0x2c8>
mov    -0x248(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xd83(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x248(%rbp),%rdi
lea    -0x228(%rbp),%rsi
call   1090 <stat@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
jge    12be <setup_hr_tick+0x10e>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x240(%rbp)
jmp    1478 <setup_hr_tick+0x2c8>
mov    -0x248(%rbp),%rdi
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x23c(%rbp)
cmpl   $0x0,-0x23c(%rbp)
jge    1308 <setup_hr_tick+0x158>
mov    -0x248(%rbp),%rdi
call   10b0 <perror@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x240(%rbp)
jmp    1478 <setup_hr_tick+0x2c8>
movl   $0x1f4,-0x238(%rbp)
mov    -0x23c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x238(%rbp),%edx
call   1080 <read@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
jge    136a <setup_hr_tick+0x1ba>
mov    -0x248(%rbp),%rdi
call   10b0 <perror@plt>
mov    -0x23c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x240(%rbp)
jmp    1478 <setup_hr_tick+0x2c8>
mov    -0x234(%rbp),%eax
cmp    -0x238(%rbp),%eax
jl     138b <setup_hr_tick+0x1db>
mov    -0x238(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x234(%rbp)
movslq -0x234(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
movl   $0x1,-0x234(%rbp)
lea    -0x220(%rbp),%rdi
lea    0xc63(%rip),%rsi
call   10d0 <strstr@plt>
mov    %rax,-0x230(%rbp)
mov    -0x230(%rbp),%rax
add    $0x3,%rax
lea    -0x220(%rbp),%rcx
cmp    %rcx,%rax
jb     145a <setup_hr_tick+0x2aa>
mov    -0x230(%rbp),%rax
add    $0xfffffffffffffffd,%rax
mov    %rax,-0x230(%rbp)
mov    -0x230(%rbp),%rdi
lea    0xc19(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1455 <setup_hr_tick+0x2a5>
mov    -0x23c(%rbp),%edi
lea    0xbfb(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x6,-0x234(%rbp)
je     1446 <setup_hr_tick+0x296>
movl   $0x0,-0x234(%rbp)
jmp    1450 <setup_hr_tick+0x2a0>
movl   $0x1,-0x234(%rbp)
jmp    1455 <setup_hr_tick+0x2a5>
jmp    145a <setup_hr_tick+0x2aa>
mov    -0x23c(%rbp),%edi
call   1060 <close@plt>
mov    -0x234(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x240(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_hr_tick>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
xor    %eax,%eax
call   1070 <find_debugfs@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %rsp,%rbx
add    $0x1f,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    $0x1,%r15d
cmpb   $0x0,0x2e8a(%rip)
jne    12fe <setup_hr_tick+0x14e>
movb   $0x1,0x2e7d(%rip)
cmpb   $0x0,(%r14)
je     12fb <setup_hr_tick+0x14b>
lea    0xdf0(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   10c0 <sprintf@plt>
lea    -0x28(%rbp),%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
test   %eax,%eax
js     12fb <setup_hr_tick+0x14b>
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12e1 <setup_hr_tick+0x131>
mov    %eax,%r14d
lea    -0x220(%rbp),%r12
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x1f4,%edx
call   1080 <read@plt>
test   %eax,%eax
js     12eb <setup_hr_tick+0x13b>
cmp    $0x1f3,%eax
mov    $0x1f3,%ecx
cmovb  %eax,%ecx
movb   $0x0,-0x220(%rbp,%rcx,1)
lea    0xd90(%rip),%rsi
mov    %r12,%rdi
call   10d0 <strstr@plt>
lea    0x3(%rax),%rcx
mov    $0x1,%r15d
cmp    %r12,%rcx
jb     12d7 <setup_hr_tick+0x127>
add    $0xfffffffffffffffd,%rax
lea    0xd6b(%rip),%rsi
mov    %rax,%rdi
mov    $0x9,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    12d7 <setup_hr_tick+0x127>
lea    0xd55(%rip),%rsi
mov    %r14d,%edi
mov    $0x6,%edx
call   1040 <write@plt>
xor    %r15d,%r15d
cmp    $0x6,%eax
sete   %r15b
mov    %r14d,%edi
call   1060 <close@plt>
jmp    12fe <setup_hr_tick+0x14e>
mov    %rbx,%rdi
call   10b0 <perror@plt>
jmp    12fb <setup_hr_tick+0x14b>
mov    %rbx,%rdi
call   10b0 <perror@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [16 x i8] c"/sched_features\00"
@setup_hr_tick.set = internal global i32 0
@.str.1 = [18 x i8] c"%s/sched_features\00"
@O_RDWR = global i32 0
@.str.2 = [7 x i8] c"HRTICK\00"
@.str.3 = [10 x i8] c"NO_HRTICK\00"
define i32 @setup_hr_tick() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [500 x i8]
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = call i8* (...) @find_debugfs()
store i8* %12, i8** %2
%13 = load i8*, i8** %2
%14 = call i64 @strlen(i8* %13)
%15 = call i64 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%16 = add nsw i64 %14, %15
%17 = add nsw i64 %16, 1
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %3
%19 = alloca i8, i64 %17
store i64 %17, i64* %4
%20 = load i32, i32* @setup_hr_tick.set
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %0
store i32 1, i32* %1
store i32 1, i32* %11
br label %89
23: ; preds = %0
store i32 1, i32* @setup_hr_tick.set
%24 = load i8*, i8** %2
%25 = call i64 @strlen(i8* %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 0, i32* %1
store i32 1, i32* %11
br label %89
28: ; preds = %23
%29 = load i8*, i8** %2
%30 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = call i32 @stat(i8* %19, %struct.stat* %6)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %28
store i32 0, i32* %1
store i32 1, i32* %11
br label %89
35: ; preds = %28
%36 = load i32, i32* @O_RDWR
%37 = call i32 @open(i8* %19, i32 %36)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @perror(i8* %19)
store i32 0, i32* %1
store i32 1, i32* %11
br label %89
42: ; preds = %35
store i32 500, i32* %9
%43 = load i32, i32* %10
%44 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%45 = load i32, i32* %9
%46 = call i32 @read(i32 %43, i8* %44, i32 %45)
store i32 %46, i32* %8
%47 = load i32, i32* %8
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %42
%50 = call i32 @perror(i8* %19)
%51 = load i32, i32* %10
%52 = call i32 @close(i32 %51)
store i32 0, i32* %1
store i32 1, i32* %11
br label %89
53: ; preds = %42
%54 = load i32, i32* %8
%55 = load i32, i32* %9
%56 = icmp sge i32 %54, %55
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = load i32, i32* %9
%59 = sub nsw i32 %58, 1
store i32 %59, i32* %8
br label %60
60: ; preds = %57, %53
%61 = load i32, i32* %8
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 %62
store i8 0, i8* %63
store i32 1, i32* %8
%64 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%65 = call i8* @strstr(i8* %64, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* %65, i8** %7
%66 = load i8*, i8** %7
%67 = getelementptr inbounds i8, i8* %66, i64 3
%68 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%69 = icmp uge i8* %67, %68
br i1 %69, label %70, label %85
70: ; preds = %60
%71 = load i8*, i8** %7
%72 = getelementptr inbounds i8, i8* %71, i64 -3
store i8* %72, i8** %7
%73 = load i8*, i8** %7
%74 = call i64 @strncmp(i8* %73, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 9)
%75 = icmp eq i64 %74, 0
br i1 %75, label %76, label %84
76: ; preds = %70
%77 = load i32, i32* %10
%78 = call i32 @write(i32 %77, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
store i32 %78, i32* %8
%79 = load i32, i32* %8
%80 = icmp ne i32 %79, 6
br i1 %80, label %81, label %82
81: ; preds = %76
store i32 0, i32* %8
br label %83
82: ; preds = %76
store i32 1, i32* %8
br label %83
83: ; preds = %82, %81
br label %84
84: ; preds = %83, %70
br label %85
85: ; preds = %84, %60
%86 = load i32, i32* %10
%87 = call i32 @close(i32 %86)
%88 = load i32, i32* %8
store i32 %88, i32* %1
store i32 1, i32* %11
br label %89
89: ; preds = %85, %49, %40, %34, %27, %22
%90 = load i8*, i8** %3
%91 = load i32, i32* %1
ret i32 %91
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@setup_hr_tick.set = internal unnamed_addr global i1 false
@.str.1 = [18 x i8] c"%s/sched_features\00"
@O_RDWR = global i32 0
@.str.2 = [7 x i8] c"HRTICK\00"
@.str.3 = [10 x i8] c"NO_HRTICK\00"
define i32 @setup_hr_tick() {
%1 = alloca [500 x i8]
%2 = alloca %struct.stat
%3 = tail call i8* (...) @find_debugfs()
%4 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%5 = add nsw i64 %4, 16
%6 = alloca i8, i64 %5
%7 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 0
%8 = bitcast %struct.stat* %2 to i8*
%9 = load i1, i1* @setup_hr_tick.set
br i1 %9, label %48, label %10
10: ; preds = %0
store i1 true, i1* @setup_hr_tick.set
%11 = load i8, i8* %3
%12 = icmp eq i8 %11, 0
br i1 %12, label %48, label %13
13: ; preds = %10
%14 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%15 = call i32 @stat(i8* %6, %struct.stat* %2)
%16 = icmp slt i32 %15, 0
br i1 %16, label %48, label %17
17: ; preds = %13
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %6, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* %6)
br label %48
23: ; preds = %17
%24 = call i32 @read(i32 %19, i8* %7, i32 500)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @perror(i8* %6)
%28 = tail call i32 @close(i32 %19)
br label %48
29: ; preds = %23
%30 = icmp ult i32 %24, 499
%31 = select i1 %30, i32 %24, i32 499
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 %32
store i8 0, i8* %33
%34 = call i8* @strstr(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%35 = getelementptr inbounds i8, i8* %34, i64 3
%36 = icmp ult i8* %35, %7
br i1 %36, label %45, label %37
37: ; preds = %29
%38 = getelementptr inbounds i8, i8* %34, i64 -3
%39 = call i64 @strncmp(i8* %38, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 9)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = call i32 @write(i32 %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%43 = icmp eq i32 %42, 6
%44 = zext i1 %43 to i32
br label %45
45: ; preds = %41, %37, %29
%46 = phi i32 [ 1, %37 ], [ 1, %29 ], [ %44, %41 ]
%47 = call i32 @close(i32 %19)
br label %48
48: ; preds = %13, %10, %0, %45, %26, %21
%49 = phi i32 [ 0, %21 ], [ 0, %26 ], [ %46, %45 ], [ 1, %0 ], [ 0, %10 ], [ 0, %13 ]
ret i32 %49
}