#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, char *);
int open(char *, int /*<<< orphan*/);
char read_proc();
int write(int, char *, int);
void start_stop_trace(char val) {
  char buf[1];
  int fd;
  int n;
  buf[0] = read_proc();
  if (buf[0] == val)
    return;
  fd = open("/proc/sys/kernel/stack_tracer_enabled", O_WRONLY);
  if (fd < 0)
    die("writing %s", "/proc/sys/kernel/stack_tracer_enabled");
  buf[0] = val;
  n = write(fd, buf, 1);
  if (n < 0)
    die("writing into %s", "/proc/sys/kernel/stack_tracer_enabled");
  close(fd);
}


x86 assembly (clang, -O0):
<start_stop_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    $0x0,%al
call   1070 <read_proc@plt>
mov    %al,-0x2(%rbp)
movsbl -0x2(%rbp),%eax
movsbl -0x1(%rbp),%ecx
cmp    %ecx,%eax
jne    117d <start_stop_trace+0x2d>
jmp    11f1 <start_stop_trace+0xa1>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b2 <start_stop_trace+0x62>
lea    0xe80(%rip),%rdi
lea    0xe53(%rip),%rsi
call   1060 <die@plt>
mov    -0x1(%rbp),%al
mov    %al,-0x2(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x2(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e9 <start_stop_trace+0x99>
lea    0xe54(%rip),%rdi
lea    0xe1c(%rip),%rsi
call   1060 <die@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_stop_trace>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <read_proc@plt>
cmp    %bpl,%al
jne    1168 <start_stop_trace+0x18>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1196 <start_stop_trace+0x46>
lea    0xe9c(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1060 <die@plt>
mov    %bpl,0x7(%rsp)
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    11c3 <start_stop_trace+0x73>
lea    0xe7a(%rip),%rdi
lea    0xe42(%rip),%rsi
call   1060 <die@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <close@plt>

LLVM IR (-O0):
@.str = [38 x i8] c"/proc/sys/kernel/stack_tracer_enabled\00"
@O_WRONLY = global i32 0
@.str.1 = [11 x i8] c"writing %s\00"
@.str.2 = [16 x i8] c"writing into %s\00"
define void @start_stop_trace(i8 signext %0) {
%2 = alloca i8
%3 = alloca [1 x i8]
%4 = alloca i32
%5 = alloca i32
store i8 %0, i8* %2
%6 = call signext i8 (...) @read_proc()
%7 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
store i8 %6, i8* %7
%8 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = load i8, i8* %2
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %10, %12
br i1 %13, label %14, label %15
14: ; preds = %1
br label %35
15: ; preds = %1
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @open(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = load i8, i8* %2
%24 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
store i8 %23, i8* %24
%25 = load i32, i32* %4
%26 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
%27 = call i32 @write(i32 %25, i8* %26, i32 1)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %30, %22
%33 = load i32, i32* %4
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %32, %14
ret void
}

LLVM IR (-O2):
@.str = [38 x i8] c"/proc/sys/kernel/stack_tracer_enabled\00"
@O_WRONLY = global i32 0
@.str.1 = [11 x i8] c"writing %s\00"
@.str.2 = [16 x i8] c"writing into %s\00"
define void @start_stop_trace(i8 signext %0) {
%2 = alloca [1 x i8]
%3 = getelementptr inbounds [1 x i8], [1 x i8]* %2, i64 0, i64 0
%4 = tail call signext i8 (...) @read_proc()
%5 = icmp eq i8 %4, %0
br i1 %5, label %19, label %6
6: ; preds = %1
%7 = load i32, i32* @O_WRONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
store i8 %0, i8* %3
%13 = call i32 @write(i32 %8, i8* %3, i32 1)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = tail call i32 @close(i32 %8)
br label %19
19: ; preds = %1, %17
ret void
}