#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int readpid(void) {
  int fd;
  int pid = -1;
  char buf[9] = {0};
  if ((fd = open("/var/run/luci-bwc.pid", O_RDONLY)) > -1) {
    if (read(fd, buf, sizeof(buf))) {
      buf[8] = 0;
      pid = atoi(buf);
    }
    close(fd);
  }
  return pid;
}


x86 assembly (clang, -O0):
<readpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0xffffffff,-0x8(%rbp)
lea    -0x11(%rbp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
call   1030 <memset@plt>
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jle    11c3 <readpid+0x73>
mov    -0x4(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x9,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
je     11bb <readpid+0x6b>
movb   $0x0,-0x9(%rbp)
lea    -0x11(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readpid>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movb   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1198 <readpid+0x58>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x9,%edx
call   1040 <read@plt>
test   %rax,%rax
je     119f <readpid+0x5f>
movb   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebp
jmp    11a4 <readpid+0x64>
mov    $0xffffffff,%ebp
jmp    11ab <readpid+0x6b>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"/var/run/luci-bwc.pid\00"
@O_RDONLY = global i32 0
define i32 @readpid() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [9 x i8]
store i32 -1, i32* %2
%4 = bitcast [9 x i8]* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %20
8: ; preds = %0
%9 = load i32, i32* %1
%10 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%11 = call i64 @read(i32 %9, i8* %10, i32 9)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 8
store i8 0, i8* %14
%15 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %2
br label %17
17: ; preds = %13, %8
%18 = load i32, i32* %1
%19 = call i32 @close(i32 %18)
br label %20
20: ; preds = %17, %0
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
@.str = [22 x i8] c"/var/run/luci-bwc.pid\00"
@O_RDONLY = global i32 0
define i32 @readpid() {
%1 = alloca [9 x i8]
%2 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %15
6: ; preds = %0
%7 = call i64 @read(i32 %4, i8* %2, i32 9)
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 8
store i8 0, i8* %10
%11 = call i32 @atoi(i8* %2)
br label %12
12: ; preds = %9, %6
%13 = phi i32 [ %11, %9 ], [ -1, %6 ]
%14 = tail call i32 @close(i32 %4)
br label %15
15: ; preds = %12, %0
%16 = phi i32 [ %13, %12 ], [ -1, %0 ]
ret i32 %16
}