#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 proc_stat {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(char *, char *, char *);
int open(char *, int /*<<< orphan*/);
int parse_proc_stat(int, struct proc_stat *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(int /*<<< orphan*/);
int read_proc_stat(pid_t pid, struct proc_stat *out) {
  char stat_path[PATH_MAX];
  int statfd;
  int err;
  snprintf(stat_path, sizeof stat_path, "/proc/%d/stat", pid);
  statfd = open(stat_path, O_RDONLY);
  if (statfd < 0) {
    error("Unable to open %s: %s", stat_path, strerror(errno));
    return -statfd;
  }
  err = parse_proc_stat(statfd, out);
  close(statfd);
  return err;
}


x86 assembly (clang, -O0):
<read_proc_stat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%ecx
lea    0xe59(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1200 <read_proc_stat+0xa0>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe27(%rip),%rdi
call   1060 <error@plt>
xor    %eax,%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1224 <read_proc_stat+0xc4>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <parse_proc_stat@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_proc_stat>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ecx
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe6d(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
js     11cd <read_proc_stat+0x6d>
mov    %r15d,%edi
mov    %r14,%rsi
call   1030 <parse_proc_stat@plt>
mov    %eax,%ebx
mov    %r15d,%edi
call   1050 <close@plt>
mov    %ebx,%r15d
jmp    11f0 <read_proc_stat+0x90>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe2c(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1060 <error@plt>
neg    %r15d
mov    %r15d,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.proc_stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/proc/%d/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Unable to open %s: %s\00"
@errno = global i32 0
define i32 @read_proc_stat(i32 %0, %struct.proc_stat* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.proc_stat*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store %struct.proc_stat* %1, %struct.proc_stat** %5
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = trunc i64 %12 to i32
%16 = load i32, i32* %4
%17 = call i32 @snprintf(i8* %14, i32 %15, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %14, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %2
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %24)
%26 = load i32, i32* %8
%27 = sub nsw i32 0, %26
store i32 %27, i32* %3
store i32 1, i32* %10
br label %35
28: ; preds = %2
%29 = load i32, i32* %8
%30 = load %struct.proc_stat*, %struct.proc_stat** %5
%31 = call i32 @parse_proc_stat(i32 %29, %struct.proc_stat* %30)
store i32 %31, i32* %9
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %9
store i32 %34, i32* %3
store i32 1, i32* %10
br label %35
35: ; preds = %28, %22
%36 = load i8*, i8** %6
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.proc_stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/proc/%d/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Unable to open %s: %s\00"
@errno = global i32 0
define i32 @read_proc_stat(i32 %0, %struct.proc_stat* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* %12)
%14 = sub nsw i32 0, %8
br label %18
15: ; preds = %2
%16 = call i32 @parse_proc_stat(i32 %8, %struct.proc_stat* %1)
%17 = call i32 @close(i32 %8)
br label %18
18: ; preds = %15, %10
%19 = phi i32 [ %14, %10 ], [ %16, %15 ]
ret i32 %19
}