#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_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strdup(char *);
char *strtok(char *, char *);
char *parse_stat(char *pid, int field) {
  char path[32];
  snprintf(path, 32 - 1, "/proc/%s/stat", pid);
  char stat_buffer[4096];
  int stat_fd = open(path, O_RDONLY);
  if (stat_fd < 0)
    return NULL;
  int rbytes = read(stat_fd, stat_buffer, 4096);
  close(stat_fd);
  if (rbytes < 0)
    return NULL;
  char *delimiter = " ";
  char *stat_str = strtok(stat_buffer, delimiter);
  if (stat_str == NULL)
    return NULL;
  for (int i = 1; i < field; i++)
    stat_str = strtok(NULL, delimiter);
  char *ps_stat = strdup(stat_str);
  return ps_stat;
}


x86 assembly (clang, -O0):
<parse_stat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1070,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1f,%esi
lea    0xe7a(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1044(%rbp)
cmpl   $0x0,-0x1044(%rbp)
jge    11bd <parse_stat+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    12a4 <parse_stat+0x144>
mov    -0x1044(%rbp),%edi
lea    -0x1040(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1048(%rbp)
mov    -0x1044(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x1048(%rbp)
jge    11ff <parse_stat+0x9f>
movq   $0x0,-0x8(%rbp)
jmp    12a4 <parse_stat+0x144>
lea    0xe08(%rip),%rax
mov    %rax,-0x1050(%rbp)
lea    -0x1040(%rbp),%rdi
mov    -0x1050(%rbp),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x1058(%rbp)
cmpq   $0x0,-0x1058(%rbp)
jne    1242 <parse_stat+0xe2>
movq   $0x0,-0x8(%rbp)
jmp    12a4 <parse_stat+0x144>
movl   $0x1,-0x105c(%rbp)
mov    -0x105c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1286 <parse_stat+0x126>
mov    -0x1050(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1070 <strtok@plt>
mov    %rax,-0x1058(%rbp)
mov    -0x105c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x105c(%rbp)
jmp    124c <parse_stat+0xec>
mov    -0x1058(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x1068(%rbp)
mov    -0x1068(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1070,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_stat>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1020,%rsp
mov    %esi,%ebx
mov    %rdi,%rcx
lea    0xe89(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x1f,%esi
call   1030 <snprintf@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1209 <parse_stat+0xa9>
mov    %eax,%ebp
lea    0x20(%rsp),%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1040 <close@plt>
test   %r14d,%r14d
js     1209 <parse_stat+0xa9>
lea    0xe49(%rip),%rsi
lea    0x20(%rsp),%rdi
call   1070 <strtok@plt>
test   %rax,%rax
je     1209 <parse_stat+0xa9>
cmp    $0x2,%ebx
jl     11ff <parse_stat+0x9f>
add    $0xffffffff,%ebx
lea    0xe2b(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edi,%edi
mov    %r14,%rsi
call   1070 <strtok@plt>
add    $0xffffffff,%ebx
jne    11f0 <parse_stat+0x90>
mov    %rax,%rdi
call   1080 <strdup@plt>
jmp    120b <parse_stat+0xab>
xor    %eax,%eax
add    $0x1020,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%s/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c" \00"
define i8* @parse_stat(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [32 x i8]
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%15 = load i8*, i8** %4
%16 = call i32 @snprintf(i8* %14, i32 31, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [32 x i8], [32 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* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %2
store i8* null, i8** %3
br label %54
23: ; preds = %2
%24 = load i32, i32* %8
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 4096)
store i32 %26, i32* %9
%27 = load i32, i32* %8
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %9
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %23
store i8* null, i8** %3
br label %54
32: ; preds = %23
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %10
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%34 = load i8*, i8** %10
%35 = call i8* @strtok(i8* %33, i8* %34)
store i8* %35, i8** %11
%36 = load i8*, i8** %11
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %39
38: ; preds = %32
store i8* null, i8** %3
br label %54
39: ; preds = %32
store i32 1, i32* %12
br label %40
40: ; preds = %47, %39
%41 = load i32, i32* %12
%42 = load i32, i32* %5
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %50
44: ; preds = %40
%45 = load i8*, i8** %10
%46 = call i8* @strtok(i8* null, i8* %45)
store i8* %46, i8** %11
br label %47
47: ; preds = %44
%48 = load i32, i32* %12
%49 = add nsw i32 %48, 1
store i32 %49, i32* %12
br label %40
50: ; preds = %40
%51 = load i8*, i8** %11
%52 = call i8* @strdup(i8* %51)
store i8* %52, i8** %13
%53 = load i8*, i8** %13
store i8* %53, i8** %3
br label %54
54: ; preds = %50, %38, %31, %22
%55 = load i8*, i8** %3
ret i8* %55
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%s/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c" \00"
define noalias i8* @parse_stat(i8* %0, i32 %1) {
%3 = alloca [32 x i8]
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 31, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %5, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %28, label %11
11: ; preds = %2
%12 = call i32 @read(i32 %9, i8* %7, i32 4096)
%13 = call i32 @close(i32 %9)
%14 = icmp slt i32 %12, 0
br i1 %14, label %28, label %15
15: ; preds = %11
%16 = call i8* @strtok(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %28, label %18
18: ; preds = %15
%19 = icmp sgt i32 %1, 1
br i1 %19, label %23, label %20
20: ; preds = %23, %18
%21 = phi i8* [ %16, %18 ], [ %25, %23 ]
%22 = call i8* @strdup(i8* %21)
br label %28
23: ; preds = %18, %23
%24 = phi i32 [ %26, %23 ], [ 1, %18 ]
%25 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%26 = add nuw nsw i32 %24, 1
%27 = icmp eq i32 %26, %1
br i1 %27, label %20, label %23
28: ; preds = %11, %15, %20, %2
%29 = phi i8* [ null, %2 ], [ null, %11 ], [ %22, %20 ], [ null, %15 ]
ret i8* %29
}