#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;
};

/* Variables and functions */
int ENODEV;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, char *);
int open(char *, int /*<<< orphan*/);
int stat(char *, struct stat *);
int /*<<< orphan*/ write(int, char *, int);
int set_ftrace(int set) {
  struct stat buf;
  char *path = "/proc/sys/kernel/ftrace_enabled";
  int fd;
  char *val = set ? "1" : "0";
  fd = stat(path, &buf);
  if (fd < 0)
    return -ENODEV;
  fd = open(path, O_WRONLY);
  if (fd < 0)
    die("Can't %s ftrace", set ? "enable" : "disable");
  write(fd, val, 1);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<set_ftrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe9e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edx
lea    0xeb2(%rip),%rax
lea    0xea9(%rip),%rcx
cmp    $0x0,%edx
cmovne %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11af <set_ftrace+0x5f>
xor    %eax,%eax
mov    0x2e3b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1212 <set_ftrace+0xc2>
mov    -0x18(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f2 <set_ftrace+0xa2>
mov    -0x8(%rbp),%ecx
lea    0xe63(%rip),%rsi
lea    0xe55(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xe37(%rip),%rdi
call   1070 <die@plt>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_ftrace>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
test   %edi,%edi
lea    0xebe(%rip),%rax
lea    0xeb5(%rip),%rbx
cmove  %rax,%rbx
lea    0xe8a(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %eax,%eax
js     11da <set_ftrace+0x8a>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11c0 <set_ftrace+0x70>
test   %r14d,%r14d
lea    0xe92(%rip),%rax
lea    0xe84(%rip),%rsi
cmove  %rax,%rsi
lea    0xe69(%rip),%rdi
call   1070 <die@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    11e5 <set_ftrace+0x95>
mov    0x2dff(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [32 x i8] c"/proc/sys/kernel/ftrace_enabled\00"
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [2 x i8] c"0\00"
@ENODEV = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [16 x i8] c"Can't %s ftrace\00"
@.str.4 = [7 x i8] c"enable\00"
@.str.5 = [8 x i8] c"disable\00"
define i32 @set_ftrace(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %3
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8** %5
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 0
%10 = zext i1 %9 to i64
%11 = select i1 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
store i8* %11, i8** %7
%12 = load i8*, i8** %5
%13 = call i32 @stat(i8* %12, %struct.stat* %4)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = load i32, i32* @ENODEV
%18 = sub nsw i32 0, %17
store i32 %18, i32* %2
br label %37
19: ; preds = %1
%20 = load i8*, i8** %5
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %19
%26 = load i32, i32* %3
%27 = icmp ne i32 %26, 0
%28 = zext i1 %27 to i64
%29 = select i1 %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0)
%30 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %25, %19
%32 = load i32, i32* %6
%33 = load i8*, i8** %7
%34 = call i32 @write(i32 %32, i8* %33, i32 1)
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
store i32 0, i32* %2
br label %37
37: ; preds = %31, %16
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [32 x i8] c"/proc/sys/kernel/ftrace_enabled\00"
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [2 x i8] c"0\00"
@ENODEV = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [16 x i8] c"Can't %s ftrace\00"
@.str.4 = [7 x i8] c"enable\00"
@.str.5 = [8 x i8] c"disable\00"
define i32 @set_ftrace(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = icmp eq i32 %0, 0
%5 = select i1 %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%6 = call i32 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @ENODEV
%10 = sub nsw i32 0, %9
br label %21
11: ; preds = %1
%12 = load i32, i32* @O_WRONLY
%13 = tail call i32 @open(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = select i1 %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0)
%17 = tail call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %15, %11
%19 = tail call i32 @write(i32 %13, i8* %5, i32 1)
%20 = tail call i32 @close(i32 %13)
br label %21
21: ; preds = %18, %8
%22 = phi i32 [ %10, %8 ], [ 0, %18 ]
ret i32 %22
}