#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);
  perror(files);
  if (fd < 0)
    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    -0x248(%rbp),%rdi
mov    %eax,-0x23c(%rbp)
call   10b0 <perror@plt>
cmpl   $0x0,-0x23c(%rbp)
jge    1308 <setup_hr_tick+0x158>
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    1226 <setup_hr_tick+0x76>
movb   $0x1,0x2e81(%rip)
cmpb   $0x0,(%r14)
je     1223 <setup_hr_tick+0x73>
lea    0xdf8(%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
jns    1236 <setup_hr_tick+0x86>
xor    %r15d,%r15d
mov    %r15d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   10b0 <perror@plt>
test   %r14d,%r14d
js     1223 <setup_hr_tick+0x73>
lea    -0x220(%rbp),%r12
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x1f4,%edx
call   1080 <read@plt>
test   %eax,%eax
js     12ed <setup_hr_tick+0x13d>
cmp    $0x1f3,%eax
mov    $0x1f3,%ecx
cmovb  %eax,%ecx
movb   $0x0,-0x220(%rbp,%rcx,1)
lea    0xd87(%rip),%rsi
mov    %r12,%rdi
call   10d0 <strstr@plt>
lea    0x3(%rax),%rcx
mov    $0x1,%r15d
cmp    %r12,%rcx
jb     12e0 <setup_hr_tick+0x130>
add    $0xfffffffffffffffd,%rax
lea    0xd62(%rip),%rsi
mov    %rax,%rdi
mov    $0x9,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    12e0 <setup_hr_tick+0x130>
lea    0xd4c(%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    1226 <setup_hr_tick+0x76>
mov    %rbx,%rdi
call   10b0 <perror@plt>
mov    %r14d,%edi
call   1060 <close@plt>
jmp    1223 <setup_hr_tick+0x73>

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 = call i32 @perror(i8* %19)
%39 = load i32, i32* %10
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %35
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, %41, %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 %47, 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 %47, 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 %47, label %17
17: ; preds = %13
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %6, i32 %18)
%20 = call i32 @perror(i8* %6)
%21 = icmp slt i32 %19, 0
br i1 %21, label %47, label %22
22: ; preds = %17
%23 = call i32 @read(i32 %19, i8* %7, i32 500)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @perror(i8* %6)
%27 = tail call i32 @close(i32 %19)
br label %47
28: ; preds = %22
%29 = icmp ult i32 %23, 499
%30 = select i1 %29, i32 %23, i32 499
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 %31
store i8 0, i8* %32
%33 = call i8* @strstr(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%34 = getelementptr inbounds i8, i8* %33, i64 3
%35 = icmp ult i8* %34, %7
br i1 %35, label %44, label %36
36: ; preds = %28
%37 = getelementptr inbounds i8, i8* %33, i64 -3
%38 = call i64 @strncmp(i8* %37, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 9)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = call i32 @write(i32 %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%42 = icmp eq i32 %41, 6
%43 = zext i1 %42 to i32
br label %44
44: ; preds = %40, %36, %28
%45 = phi i32 [ 1, %36 ], [ 1, %28 ], [ %43, %40 ]
%46 = call i32 @close(i32 %19)
br label %47
47: ; preds = %17, %13, %10, %0, %44, %25
%48 = phi i32 [ 0, %25 ], [ %45, %44 ], [ 1, %0 ], [ 0, %10 ], [ 0, %13 ], [ 0, %17 ]
ret i32 %48
}