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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int const);
int sscanf(char *, char *, int *, char *, char *, int *);
int read_proc_info(const pid_t pid, pid_t *ppid, char *name, int max_size) {
  char path[64], info[max_size], c;
  int fd, xpid, r = 0;
  snprintf(path, sizeof(path), "/proc/%u/stat", pid);
  if ((fd = open(path, O_RDONLY)) < 0)
    return 0;
  if (read(fd, info, max_size) > 0 &&
      sscanf(info, "%d %s %c %d", &xpid, name, &c, ppid) == 4)
    r = 1;
  if (!r) {
    *ppid = 0;
    name[0] = '\0';
  }
  close(fd);
  return r;
}


x86 assembly (clang, -O0):
<read_proc_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x90(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x70(%rbp)
movl   $0x0,-0x80(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe54(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x78(%rbp)
cmp    $0x0,%eax
jge    11e5 <read_proc_info+0x95>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x84(%rbp)
jmp    1267 <read_proc_info+0x117>
mov    -0x90(%rbp),%rsi
mov    -0x78(%rbp),%edi
mov    -0x1c(%rbp),%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jle    1234 <read_proc_info+0xe4>
mov    -0x90(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r9
lea    0xdf7(%rip),%rsi
lea    -0x7c(%rbp),%rdx
lea    -0x71(%rbp),%r8
call   1060 <sscanf@plt>
cmp    $0x4,%eax
jne    1234 <read_proc_info+0xe4>
movl   $0x1,-0x80(%rbp)
cmpl   $0x0,-0x80(%rbp)
jne    124f <read_proc_info+0xff>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x78(%rbp),%edi
call   1040 <close@plt>
mov    -0x80(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x84(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_proc_info>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ecx
mov    %rsp,-0x38(%rbp)
mov    %r12d,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe75(%rip),%rdx
lea    -0x80(%rbp),%r13
mov    %r13,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1204 <read_proc_info+0xb4>
mov    %eax,%r13d
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11ed <read_proc_info+0x9d>
lea    0xe41(%rip),%rsi
lea    -0x3c(%rbp),%rdx
lea    -0x29(%rbp),%r8
mov    %rbx,%rdi
mov    %r14,%rcx
mov    %r15,%r9
call   1060 <sscanf@plt>
mov    $0x1,%ebx
cmp    $0x4,%eax
je     11fa <read_proc_info+0xaa>
movl   $0x0,(%r15)
movb   $0x0,(%r14)
xor    %ebx,%ebx
mov    %r13d,%edi
call   1040 <close@plt>
jmp    1206 <read_proc_info+0xb6>
xor    %ebx,%ebx
mov    -0x38(%rbp),%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%u/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"%d %s %c %d\00"
define i32 @read_proc_info(i32 %0, i32* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [64 x i8]
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i32 %0, i32* %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%18 = load i32, i32* %9
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %11
%21 = alloca i8, i64 %19
store i64 %19, i64* %12
store i32 0, i32* %16
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%23 = load i32, i32* %6
%24 = call i32 @snprintf(i8* %22, i32 64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %14
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %4
store i32 0, i32* %5
store i32 1, i32* %17
br label %52
30: ; preds = %4
%31 = load i32, i32* %14
%32 = load i32, i32* %9
%33 = call i64 @read(i32 %31, i8* %21, i32 %32)
%34 = icmp sgt i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = load i8*, i8** %8
%37 = load i32*, i32** %7
%38 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %15, i8* %36, i8* %13, i32* %37)
%39 = icmp eq i32 %38, 4
br i1 %39, label %40, label %41
40: ; preds = %35
store i32 1, i32* %16
br label %41
41: ; preds = %40, %35, %30
%42 = load i32, i32* %16
%43 = icmp ne i32 %42, 0
br i1 %43, label %48, label %44
44: ; preds = %41
%45 = load i32*, i32** %7
store i32 0, i32* %45
%46 = load i8*, i8** %8
%47 = getelementptr inbounds i8, i8* %46, i64 0
store i8 0, i8* %47
br label %48
48: ; preds = %44, %41
%49 = load i32, i32* %14
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %16
store i32 %51, i32* %5
store i32 1, i32* %17
br label %52
52: ; preds = %48, %29
%53 = load i8*, i8** %11
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%u/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"%d %s %c %d\00"
define i32 @read_proc_info(i32 %0, i32* %1, i8* %2, i32 %3) {
%5 = alloca [64 x i8]
%6 = alloca i8
%7 = alloca i32
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%9 = zext i32 %3 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = bitcast i32* %7 to i8*
%13 = call i32 @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %8, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %4
%18 = call i64 @read(i32 %15, i8* %11, i32 %3)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %7, i8* %2, i8* %6, i32* %1)
%22 = icmp eq i32 %21, 4
br i1 %22, label %24, label %23
23: ; preds = %17, %20
store i32 0, i32* %1
store i8 0, i8* %2
br label %24
24: ; preds = %23, %20
%25 = phi i32 [ 0, %23 ], [ 1, %20 ]
%26 = call i32 @close(i32 %15)
br label %27
27: ; preds = %4, %24
%28 = phi i32 [ %25, %24 ], [ 0, %4 ]
ret i32 %28
}