#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_RDWR;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ close(int);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int write(int, char *, int);
int procfile_write(const char *procfile, char *oldval, char newval) {
  int fd;
  if ((fd = open(procfile, O_RDWR)) < 0) {
    return -1;
  }
  if (oldval) {
    if (read(fd, oldval, 1) != 1) {
      return -1;
    }
    if (lseek(fd, 0, SEEK_SET) != 0) {
      return -1;
    }
  }
  if (write(fd, &newval, 1) != 1) {
    return -1;
  }
  if (close(fd) < 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<procfile_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    118f <procfile_write+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <procfile_write+0xe9>
cmpq   $0x0,-0x18(%rbp)
je     11ee <procfile_write+0x9e>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
je     11c0 <procfile_write+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <procfile_write+0xe9>
mov    -0x20(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
je     11e9 <procfile_write+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <procfile_write+0xe9>
jmp    11ee <procfile_write+0x9e>
mov    -0x20(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1214 <procfile_write+0xc4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <procfile_write+0xe9>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    1232 <procfile_write+0xe2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <procfile_write+0xe9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<procfile_write>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %dl,0xf(%rsp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     11cd <procfile_write+0x7d>
mov    %eax,%ebp
test   %rbx,%rbx
je     11a9 <procfile_write+0x59>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
jne    11cd <procfile_write+0x7d>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
jne    11cd <procfile_write+0x7d>
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    11cd <procfile_write+0x7d>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rax,%r14
sar    $0x3f,%r14
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
define i32 @procfile_write(i8* %0, i8* %1, i8 signext %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8 %2, i8* %7
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %41
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %30
17: ; preds = %14
%18 = load i32, i32* %8
%19 = load i8*, i8** %6
%20 = call i32 @read(i32 %18, i8* %19, i32 1)
%21 = icmp ne i32 %20, 1
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 -1, i32* %4
br label %41
23: ; preds = %17
%24 = load i32, i32* %8
%25 = load i32, i32* @SEEK_SET
%26 = call i64 @lseek(i32 %24, i32 0, i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 -1, i32* %4
br label %41
29: ; preds = %23
br label %30
30: ; preds = %29, %14
%31 = load i32, i32* %8
%32 = call i32 @write(i32 %31, i8* %7, i32 1)
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 -1, i32* %4
br label %41
35: ; preds = %30
%36 = load i32, i32* %8
%37 = call i64 @close(i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 -1, i32* %4
br label %41
40: ; preds = %35
store i32 0, i32* %4
br label %41
41: ; preds = %40, %39, %34, %28, %22, %13
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
define i32 @procfile_write(i8* nocapture %0, i8* %1, i8 signext %2) {
%4 = alloca i8
store i8 %2, i8* %4
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %3
%9 = icmp eq i8* %1, null
br i1 %9, label %17, label %10
10: ; preds = %8
%11 = tail call i32 @read(i32 %6, i8* %1, i32 1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %24
13: ; preds = %10
%14 = load i32, i32* @SEEK_SET
%15 = tail call i64 @lseek(i32 %6, i32 0, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %13, %8
%18 = call i32 @write(i32 %6, i8* %4, i32 1)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = tail call i64 @close(i32 %6)
%22 = ashr i64 %21, 63
%23 = trunc i64 %22 to i32
br label %24
24: ; preds = %20, %17, %13, %10, %3
%25 = phi i32 [ -1, %3 ], [ -1, %10 ], [ -1, %13 ], [ -1, %17 ], [ %23, %20 ]
ret i32 %25
}