#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 */
struct procstat {
  int dummy;
};
typedef size_t ssize_t;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int procstat_parse(char *, struct procstat *);
size_t read(int, char *, int);
int snprintf(char *, int, char *, unsigned long);
bool procstat_get(pid_t pid, struct procstat *ps) {
  char path[64];
  if (snprintf(path, sizeof path, "/proc/%lu/stat", (unsigned long)pid) >=
      sizeof path)
    return false;
  int fd;
  char buf[1024];
  ssize_t n;
  fd = open(path, O_RDONLY);
  if (fd < 0)
    return false;
  n = read(fd, buf, sizeof(buf) - 1);
  (void)close(fd);
  if (n <= 0)
    return false;
  buf[n] = '\0';
  return procstat_parse(buf, ps);
}


x86 assembly (clang, -O0):
<procstat_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x40,%esi
lea    0xe89(%rip),%rdx
call   1030 <snprintf@plt>
cltq
cmp    $0x40,%rax
jb     1194 <procstat_get+0x44>
movl   $0x0,-0x4(%rbp)
jmp    121e <procstat_get+0xce>
lea    -0x60(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    11bf <procstat_get+0x6f>
movl   $0x0,-0x4(%rbp)
jmp    121e <procstat_get+0xce>
mov    -0x64(%rbp),%edi
lea    -0x470(%rbp),%rsi
mov    $0x3ff,%edx
call   1050 <read@plt>
mov    %rax,-0x478(%rbp)
mov    -0x64(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x478(%rbp)
ja     11fc <procstat_get+0xac>
movl   $0x0,-0x4(%rbp)
jmp    121e <procstat_get+0xce>
mov    -0x478(%rbp),%rax
movb   $0x0,-0x470(%rbp,%rax,1)
lea    -0x470(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <procstat_parse@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<procstat_get>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x448,%rsp
mov    %rsi,%r14
mov    %rdi,%rcx
lea    0xe96(%rip),%rdx
mov    %rsp,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
xor    %ebp,%ebp
cmp    $0x3f,%eax
ja     11cc <procstat_get+0x7c>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1070 <open@plt>
xor    %ebp,%ebp
test   %eax,%eax
js     11cc <procstat_get+0x7c>
mov    %eax,%ebx
lea    0x40(%rsp),%rsi
mov    %eax,%edi
mov    $0x3ff,%edx
call   1050 <read@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1040 <close@plt>
test   %r15,%r15
je     11cc <procstat_get+0x7c>
movb   $0x0,0x40(%rsp,%r15,1)
lea    0x40(%rsp),%rdi
mov    %r14,%rsi
call   1060 <procstat_parse@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x448,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.procstat = type { i32 }
@.str = [15 x i8] c"/proc/%lu/stat\00"
@O_RDONLY = global i32 0
define i32 @procstat_get(i64 %0, %struct.procstat* %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca %struct.procstat*
%6 = alloca [64 x i8]
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca i64
store i64 %0, i64* %4
store %struct.procstat* %1, %struct.procstat** %5
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%11 = load i64, i64* %4
%12 = call i32 @snprintf(i8* %10, i32 64, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i64 %11)
%13 = sext i32 %12 to i64
%14 = icmp uge i64 %13, 64
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %38
16: ; preds = %2
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 0, i32* %3
br label %38
23: ; preds = %16
%24 = load i32, i32* %7
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%26 = call i64 @read(i32 %24, i8* %25, i32 1023)
store i64 %26, i64* %9
%27 = load i32, i32* %7
%28 = call i32 @close(i32 %27)
%29 = load i64, i64* %9
%30 = icmp ule i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %23
store i32 0, i32* %3
br label %38
32: ; preds = %23
%33 = load i64, i64* %9
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %33
store i8 0, i8* %34
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%36 = load %struct.procstat*, %struct.procstat** %5
%37 = call i32 @procstat_parse(i8* %35, %struct.procstat* %36)
store i32 %37, i32* %3
br label %38
38: ; preds = %32, %31, %22, %15
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.procstat = type { i32 }
@.str = [15 x i8] c"/proc/%lu/stat\00"
@O_RDONLY = global i32 0
define i32 @procstat_get(i64 %0, %struct.procstat* %1) {
%3 = alloca [64 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 64, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i64 %0)
%7 = icmp ugt i32 %6, 63
br i1 %7, label %22, label %8
8: ; preds = %2
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %5, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %8
%14 = call i64 @read(i32 %11, i8* %9, i32 1023)
%15 = call i32 @close(i32 %11)
%16 = icmp eq i64 %14, 0
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %14
store i8 0, i8* %18
%19 = call i32 @procstat_parse(i8* %9, %struct.procstat* %1)
br label %20
20: ; preds = %13, %8, %17
%21 = phi i32 [ %19, %17 ], [ 0, %8 ], [ 0, %13 ]
br label %22
22: ; preds = %2, %20
%23 = phi i32 [ %21, %20 ], [ 0, %2 ]
ret i32 %23
}