#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*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
int clear_pid(void) {
  char buf[10];
  int fd = open("/sys/kernel/ece453/pid", O_WRONLY);
  if (fd < 0) {
    perror("open");
    return -1;
  }
  memset(buf, 0, 10);
  if (write(fd, buf, strlen(buf) + 1) < 0) {
    perror("fwrite");
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<clear_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a2 <clear_pid+0x42>
lea    0xe86(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <clear_pid+0xad>
lea    -0xe(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0xe(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0xe(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11fe <clear_pid+0x9e>
lea    0xe2f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <clear_pid+0xad>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clear_pid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11c8 <clear_pid+0x68>
mov    %eax,%ebp
lea    0x6(%rsp),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
test   %rax,%rax
js     11d1 <clear_pid+0x71>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe48(%rip),%rdi
jmp    11d8 <clear_pid+0x78>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11bc <clear_pid+0x5c>

LLVM IR (-O0):
@.str = [23 x i8] c"/sys/kernel/ece453/pid\00"
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [7 x i8] c"fwrite\00"
define i32 @clear_pid() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca i32
%4 = load i32, i32* @O_WRONLY
%5 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %25
10: ; preds = %0
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 10)
%13 = load i32, i32* %3
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%16 = call i64 @strlen(i8* %15)
%17 = add nsw i64 %16, 1
%18 = call i64 @write(i32 %13, i8* %14, i64 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %25
22: ; preds = %10
%23 = load i32, i32* %3
%24 = call i32 @close(i32 %23)
store i32 0, i32* %1
br label %25
25: ; preds = %22, %20, %8
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/kernel/ece453/pid\00"
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [7 x i8] c"fwrite\00"
define i32 @clear_pid() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %18
8: ; preds = %0
%9 = call i32 @memset(i8* %2, i32 0, i32 10)
%10 = call i64 @strlen(i8* %2)
%11 = add nsw i64 %10, 1
%12 = call i64 @write(i32 %4, i8* %2, i64 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %18
16: ; preds = %8
%17 = call i32 @close(i32 %4)
br label %18
18: ; preds = %16, %14, %6
%19 = phi i32 [ -1, %6 ], [ -1, %14 ], [ 0, %16 ]
ret i32 %19
}