#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 open(char const *, int /*<<< orphan*/);
int stat(char const *, struct stat *);
int write(int, char *, int);
int set_ftrace_enable(const char *path, int set) {
  struct stat st;
  int fd;
  char *val = set ? "1" : "0";
  int ret;
  fd = stat(path, &st);
  if (fd < 0)
    return -ENODEV;
  ret = -1;
  fd = open(path, O_WRONLY);
  if (fd < 0)
    goto out;
  ret = write(fd, val, 1);
  close(fd);
out:
  return ret < 0 ? ret : 0;
}


x86 assembly (clang, -O0):
<set_ftrace_enable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edx
lea    0xea9(%rip),%rax
lea    0xea0(%rip),%rcx
cmp    $0x0,%edx
cmovne %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1198 <set_ftrace_enable+0x58>
xor    %eax,%eax
mov    0x2e52(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <set_ftrace_enable+0xc4>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c3 <set_ftrace_enable+0x83>
jmp    11df <set_ftrace_enable+0x9f>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x2c(%rbp)
jge    11f4 <set_ftrace_enable+0xb4>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    11fe <set_ftrace_enable+0xbe>
xor    %eax,%eax
mov    %eax,-0x30(%rbp)
jmp    11fe <set_ftrace_enable+0xbe>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_ftrace_enable>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
test   %esi,%esi
lea    0xeae(%rip),%rax
lea    0xea5(%rip),%rbx
cmove  %rax,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %eax,%eax
js     11a6 <set_ftrace_enable+0x66>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11b3 <set_ftrace_enable+0x73>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
test   %ebx,%ebx
jns    11b8 <set_ftrace_enable+0x78>
mov    %ebx,%eax
jmp    11b8 <set_ftrace_enable+0x78>
mov    0x2e33(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11b8 <set_ftrace_enable+0x78>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"1\00"
@.str.1 = [2 x i8] c"0\00"
@ENODEV = global i32 0
@O_WRONLY = global i32 0
define i32 @set_ftrace_enable(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %1, 0
%6 = select i1 %5, 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, i64 0, i64 0)
%7 = call i32 @stat(i8* %0, %struct.stat* %3)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @ENODEV
%11 = sub nsw i32 0, %10
br label %22
12: ; preds = %2
%13 = load i32, i32* @O_WRONLY
%14 = tail call i32 @open(i8* %0, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = tail call i32 @write(i32 %14, i8* %6, i32 1)
%18 = tail call i32 @close(i32 %14)
%19 = icmp slt i32 %17, 0
br i1 %19, label %20, label %22
20: ; preds = %12, %16
%21 = phi i32 [ %17, %16 ], [ -1, %12 ]
br label %22
22: ; preds = %20, %16, %9
%23 = phi i32 [ %11, %9 ], [ %21, %20 ], [ 0, %16 ]
ret i32 %23
}