#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 */

/* Variables and functions */
int O_CLOEXEC;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, char *);
int open(char *, int);
int strlen(char const *);
char *tracecmd_get_tracing_file(char *);
int /*<<< orphan*/ tracecmd_put_tracing_file(char *);
int write(int, char const *, int);
void update_ftrace_pid(const char *pid, int reset) {
  static char *path;
  int ret;
  static int fd = -1;
  if (!pid) {
    if (fd >= 0)
      close(fd);
    if (path)
      tracecmd_put_tracing_file(path);
    fd = -1;
    path = NULL;
    return;
  }
  if (reset && fd >= 0) {
    close(fd);
    fd = -1;
  }
  if (fd < 0) {
    if (!path)
      path = tracecmd_get_tracing_file("set_ftrace_pid");
    if (!path)
      return;
    fd = open(path, O_WRONLY | O_CLOEXEC | (reset ? O_TRUNC : 0));
    if (fd < 0)
      return;
  }
  ret = write(fd, pid, strlen(pid));
  if (ret < 0 && !strlen(pid))
    ret = write(fd, "-1", 2);
  if (ret < 0)
    die("error writing to %s", path);
  write(fd, " ", 1);
}


x86 assembly (clang, -O0):
<update_ftrace_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11d6 <update_ftrace_pid+0x66>
cmpl   $0x0,0x2ec7(%rip)
jl     11a2 <update_ftrace_pid+0x32>
mov    0x2ebb(%rip),%edi
call   1060 <close@plt>
cmpq   $0x0,0x2ebe(%rip)
je     11bc <update_ftrace_pid+0x4c>
mov    0x2eb1(%rip),%rdi
call   1030 <tracecmd_put_tracing_file@plt>
movl   $0xffffffff,0x2e92(%rip)
ff ff ff
movq   $0x0,0x2e97(%rip)
jmp    134b <update_ftrace_pid+0x1db>
cmpl   $0x0,-0xc(%rbp)
je     1202 <update_ftrace_pid+0x92>
cmpl   $0x0,0x2e71(%rip)
jl     1202 <update_ftrace_pid+0x92>
mov    0x2e65(%rip),%edi
call   1060 <close@plt>
movl   $0xffffffff,0x2e56(%rip)
ff ff ff
cmpl   $0x0,0x2e4f(%rip)
jge    12b6 <update_ftrace_pid+0x146>
cmpq   $0x0,0x2e51(%rip)
jne    1230 <update_ftrace_pid+0xc0>
lea    0xddc(%rip),%rdi
call   1070 <tracecmd_get_tracing_file@plt>
mov    %rax,0x2e38(%rip)
cmpq   $0x0,0x2e30(%rip)
jne    1243 <update_ftrace_pid+0xd3>
jmp    134b <update_ftrace_pid+0x1db>
mov    0x2e1e(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    0x2d6a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     127e <update_ftrace_pid+0x10e>
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1288 <update_ftrace_pid+0x118>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    1288 <update_ftrace_pid+0x118>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%eax
or     %eax,%esi
call   1080 <open@plt>
mov    %eax,0x2db9(%rip)
cmpl   $0x0,0x2db2(%rip)
jge    12b1 <update_ftrace_pid+0x141>
jmp    134b <update_ftrace_pid+0x1db>
jmp    12b6 <update_ftrace_pid+0x146>
mov    0x2d9c(%rip),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1317 <update_ftrace_pid+0x1a7>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%eax
jne    1317 <update_ftrace_pid+0x1a7>
mov    0x2d55(%rip),%edi
lea    0xd05(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1334 <update_ftrace_pid+0x1c4>
mov    0x2d40(%rip),%rsi
lea    0xce3(%rip),%rdi
call   1090 <die@plt>
mov    0x2d1e(%rip),%edi
lea    0xce5(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_ftrace_pid>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     119f <update_ftrace_pid+0x2f>
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2ed4(%rip),%ebx
test   %esi,%esi
je     11d9 <update_ftrace_pid+0x69>
test   %ebx,%ebx
js     11d9 <update_ftrace_pid+0x69>
mov    %ebx,%edi
call   1060 <close@plt>
movl   $0xffffffff,0x2ebb(%rip)
ff ff ff
jmp    11dd <update_ftrace_pid+0x6d>
mov    0x2eb3(%rip),%edi
test   %edi,%edi
js     11ae <update_ftrace_pid+0x3e>
call   1060 <close@plt>
mov    0x2eb3(%rip),%rdi
test   %rdi,%rdi
je     11bf <update_ftrace_pid+0x4f>
call   1030 <tracecmd_put_tracing_file@plt>
movl   $0xffffffff,0x2e8f(%rip)
ff ff ff
movq   $0x0,0x2e94(%rip)
jmp    12a7 <update_ftrace_pid+0x137>
test   %ebx,%ebx
jns    123a <update_ftrace_pid+0xca>
mov    0x2e84(%rip),%rdi
test   %rdi,%rdi
jne    1208 <update_ftrace_pid+0x98>
lea    0xe10(%rip),%rdi
call   1070 <tracecmd_get_tracing_file@plt>
mov    %rax,%rdi
mov    %rax,0x2e69(%rip)
test   %rax,%rax
je     12a7 <update_ftrace_pid+0x137>
mov    0x2dc1(%rip),%rax
mov    0x2db2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
test   %ebp,%ebp
je     1227 <update_ftrace_pid+0xb7>
mov    0x2dd3(%rip),%rax
mov    (%rax),%ebp
or     %ebp,%esi
call   1080 <open@plt>
mov    %eax,%ebx
mov    %eax,0x2e22(%rip)
test   %eax,%eax
js     12a7 <update_ftrace_pid+0x137>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    128c <update_ftrace_pid+0x11c>
mov    %r14,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jne    1279 <update_ftrace_pid+0x109>
mov    0x2df4(%rip),%edi
lea    0xda4(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    128c <update_ftrace_pid+0x11c>
mov    0x2de8(%rip),%rsi
lea    0xd8b(%rip),%rdi
call   1090 <die@plt>
mov    0x2dc6(%rip),%edi
lea    0xd8d(%rip),%rsi
mov    $0x1,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <write@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@update_ftrace_pid.path = internal global i8* null
@update_ftrace_pid.fd = internal global i32 -1
@.str = [15 x i8] c"set_ftrace_pid\00"
@O_WRONLY = global i32 0
@O_CLOEXEC = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [3 x i8] c"-1\00"
@.str.2 = [20 x i8] c"error writing to %s\00"
@.str.3 = [2 x i8] c" \00"
define void @update_ftrace_pid(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %21, label %8
8: ; preds = %2
%9 = load i32, i32* @update_ftrace_pid.fd
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @update_ftrace_pid.fd
%13 = call i32 @close(i32 %12)
br label %14
14: ; preds = %11, %8
%15 = load i8*, i8** @update_ftrace_pid.path
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i8*, i8** @update_ftrace_pid.path
%19 = call i32 @tracecmd_put_tracing_file(i8* %18)
br label %20
20: ; preds = %17, %14
store i32 -1, i32* @update_ftrace_pid.fd
store i8* null, i8** @update_ftrace_pid.path
br label %84
21: ; preds = %2
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = load i32, i32* @update_ftrace_pid.fd
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @update_ftrace_pid.fd
%29 = call i32 @close(i32 %28)
store i32 -1, i32* @update_ftrace_pid.fd
br label %30
30: ; preds = %27, %24, %21
%31 = load i32, i32* @update_ftrace_pid.fd
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %60
33: ; preds = %30
%34 = load i8*, i8** @update_ftrace_pid.path
%35 = icmp ne i8* %34, null
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = call i8* @tracecmd_get_tracing_file(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* %37, i8** @update_ftrace_pid.path
br label %38
38: ; preds = %36, %33
%39 = load i8*, i8** @update_ftrace_pid.path
%40 = icmp ne i8* %39, null
br i1 %40, label %42, label %41
41: ; preds = %38
br label %84
42: ; preds = %38
%43 = load i8*, i8** @update_ftrace_pid.path
%44 = load i32, i32* @O_WRONLY
%45 = load i32, i32* @O_CLOEXEC
%46 = or i32 %44, %45
%47 = load i32, i32* %4
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %42
%50 = load i32, i32* @O_TRUNC
br label %52
51: ; preds = %42
br label %52
52: ; preds = %51, %49
%53 = phi i32 [ %50, %49 ], [ 0, %51 ]
%54 = or i32 %46, %53
%55 = call i32 @open(i8* %43, i32 %54)
store i32 %55, i32* @update_ftrace_pid.fd
%56 = load i32, i32* @update_ftrace_pid.fd
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %52
br label %84
59: ; preds = %52
br label %60
60: ; preds = %59, %30
%61 = load i32, i32* @update_ftrace_pid.fd
%62 = load i8*, i8** %3
%63 = load i8*, i8** %3
%64 = call i32 @strlen(i8* %63)
%65 = call i32 @write(i32 %61, i8* %62, i32 %64)
store i32 %65, i32* %5
%66 = load i32, i32* %5
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %75
68: ; preds = %60
%69 = load i8*, i8** %3
%70 = call i32 @strlen(i8* %69)
%71 = icmp ne i32 %70, 0
br i1 %71, label %75, label %72
72: ; preds = %68
%73 = load i32, i32* @update_ftrace_pid.fd
%74 = call i32 @write(i32 %73, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
store i32 %74, i32* %5
br label %75
75: ; preds = %72, %68, %60
%76 = load i32, i32* %5
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i8*, i8** @update_ftrace_pid.path
%80 = call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %79)
br label %81
81: ; preds = %78, %75
%82 = load i32, i32* @update_ftrace_pid.fd
%83 = call i32 @write(i32 %82, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %84
84: ; preds = %81, %58, %41, %20
ret void
}

LLVM IR (-O2):
@update_ftrace_pid.path = internal unnamed_addr global i8* null
@update_ftrace_pid.fd = internal unnamed_addr global i32 -1
@.str = [15 x i8] c"set_ftrace_pid\00"
@O_WRONLY = global i32 0
@O_CLOEXEC = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [3 x i8] c"-1\00"
@.str.2 = [20 x i8] c"error writing to %s\00"
@.str.3 = [2 x i8] c" \00"
define void @update_ftrace_pid(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %15
4: ; preds = %2
%5 = load i32, i32* @update_ftrace_pid.fd
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @close(i32 %5)
br label %9
9: ; preds = %7, %4
%10 = load i8*, i8** @update_ftrace_pid.path
%11 = icmp eq i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @tracecmd_put_tracing_file(i8* %10)
br label %14
14: ; preds = %12, %9
store i32 -1, i32* @update_ftrace_pid.fd
store i8* null, i8** @update_ftrace_pid.path
br label %58
15: ; preds = %2
%16 = icmp ne i32 %1, 0
%17 = load i32, i32* @update_ftrace_pid.fd
%18 = icmp sgt i32 %17, -1
%19 = select i1 %16, i1 %18, i1 false
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @close(i32 %17)
store i32 -1, i32* @update_ftrace_pid.fd
br label %24
22: ; preds = %15
%23 = icmp slt i32 %17, 0
br i1 %23, label %24, label %40
24: ; preds = %20, %22
%25 = load i8*, i8** @update_ftrace_pid.path
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = tail call i8* @tracecmd_get_tracing_file(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* %28, i8** @update_ftrace_pid.path
%29 = icmp eq i8* %28, null
br i1 %29, label %58, label %30
30: ; preds = %24, %27
%31 = phi i8* [ %28, %27 ], [ %25, %24 ]
%32 = load i32, i32* @O_WRONLY
%33 = load i32, i32* @O_CLOEXEC
%34 = or i32 %33, %32
%35 = load i32, i32* @O_TRUNC
%36 = select i1 %16, i32 %35, i32 0
%37 = or i32 %34, %36
%38 = tail call i32 @open(i8* %31, i32 %37)
store i32 %38, i32* @update_ftrace_pid.fd
%39 = icmp slt i32 %38, 0
br i1 %39, label %58, label %40
40: ; preds = %30, %22
%41 = phi i32 [ %38, %30 ], [ %17, %22 ]
%42 = tail call i32 @strlen(i8* %0)
%43 = tail call i32 @write(i32 %41, i8* %0, i32 %42)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %40
%46 = tail call i32 @strlen(i8* %0)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = load i32, i32* @update_ftrace_pid.fd
%50 = tail call i32 @write(i32 %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %45, %48
%53 = load i8*, i8** @update_ftrace_pid.path
%54 = tail call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %40, %52, %48
%56 = load i32, i32* @update_ftrace_pid.fd
%57 = tail call i32 @write(i32 %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %58
58: ; preds = %30, %27, %55, %14
ret void
}