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


x86 assembly (clang, -O0):
<set_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b2 <set_pid+0x42>
lea    0xe76(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <set_pid+0xbe>
lea    -0xe(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe4e(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0xe(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0xe(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    121f <set_pid+0xaf>
lea    0xe11(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <set_pid+0xbe>
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):
<set_pid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11e1 <set_pid+0x71>
mov    %eax,%ebp
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe78(%rip),%rsi
lea    0x6(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
test   %rax,%rax
js     11ea <set_pid+0x7a>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe2f(%rip),%rdi
jmp    11f1 <set_pid+0x81>
lea    0xe2e(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11d5 <set_pid+0x65>

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 = [3 x i8] c"%i\00"
@.str.3 = [7 x i8] c"fwrite\00"
define i32 @set_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 %26
10: ; preds = %0
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%12 = call i32 (...) @getpid()
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %3
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %17, 1
%19 = call i64 @write(i32 %14, i8* %15, i64 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %26
23: ; preds = %10
%24 = load i32, i32* %3
%25 = call i32 @close(i32 %24)
store i32 0, i32* %1
br label %26
26: ; preds = %23, %21, %8
%27 = load i32, i32* %1
ret i32 %27
}

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 = [3 x i8] c"%i\00"
@.str.3 = [7 x i8] c"fwrite\00"
define i32 @set_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 %19
8: ; preds = %0
%9 = tail call i32 (...) @getpid()
%10 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = call i64 @strlen(i8* %2)
%12 = add nsw i64 %11, 1
%13 = call i64 @write(i32 %4, i8* %2, i64 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %19
17: ; preds = %8
%18 = tail call i32 @close(i32 %4)
br label %19
19: ; preds = %17, %15, %6
%20 = phi i32 [ -1, %6 ], [ -1, %15 ], [ 0, %17 ]
ret i32 %20
}