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

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int /*<<< orphan*/, char *, char *,
                            int /*<<< orphan*/);
char *strdup(char *);
char *strtok(char *, char *);
char *parse_stat(pid_t pid, int field) {
  char *fieldstr = NULL;
  char path[PATH_MAX + 1];
  snprintf(path, PATH_MAX, "%s%d/stat", "/proc/", pid);
  FILE *fh = fopen(path, "r");
  if (!fh)
    return NULL;
  size_t n = 0;
  char *line = NULL;
  if (getline(&line, &n, fh) < 0)
    goto close_fh;
  char *delim = " ";
  fieldstr = strtok(line, delim);
  if (!fieldstr || field == 0)
    goto free_line;
  for (int fieldno = 0; fieldstr && fieldno < field; fieldno++)
    fieldstr = strtok(NULL, delim);
free_line:
  free(line);
close_fh:
  fclose(fh);
  return strdup(fieldstr);
}


x86 assembly (clang, -O0):
<parse_stat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%r8d
lea    0xe37(%rip),%rdx
lea    0xe3a(%rip),%rcx
call   1050 <snprintf@plt>
mov    -0x60(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1208 <parse_stat+0x98>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12eb <parse_stat+0x17b>
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x30(%rbp),%rdx
lea    -0x48(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <getline@plt>
cmp    $0x0,%rax
jge    1238 <parse_stat+0xc8>
jmp    12ce <parse_stat+0x15e>
lea    0xdd4(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1269 <parse_stat+0xf9>
cmpl   $0x0,-0x10(%rbp)
jne    126e <parse_stat+0xfe>
jmp    12c5 <parse_stat+0x155>
movl   $0x0,-0x54(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x61(%rbp)
je     1291 <parse_stat+0x121>
mov    -0x54(%rbp),%eax
cmp    -0x10(%rbp),%eax
setl   %al
mov    %al,-0x61(%rbp)
mov    -0x61(%rbp),%al
test   $0x1,%al
jne    12a1 <parse_stat+0x131>
jmp    12c0 <parse_stat+0x150>
mov    -0x50(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1070 <strtok@plt>
mov    %rax,-0x18(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1275 <parse_stat+0x105>
jmp    12c5 <parse_stat+0x155>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_stat>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %edi,%r8d
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0x1(%rsi),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe55(%rip),%rdx
lea    0xe58(%rip),%rcx
mov    %rbx,%rdi
call   1050 <snprintf@plt>
lea    0xe50(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1247 <parse_stat+0xd7>
mov    %rax,%r14
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    %rax,%rdx
call   1080 <getline@plt>
test   %rax,%rax
js     124b <parse_stat+0xdb>
mov    -0x30(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,%r13
test   %r15d,%r15d
jle    123c <parse_stat+0xcc>
test   %r13,%r13
je     123c <parse_stat+0xcc>
mov    $0x1,%ebx
lea    0xdf4(%rip),%r12
nop
xor    %edi,%edi
mov    %r12,%rsi
call   1070 <strtok@plt>
mov    %rax,%r13
test   %rax,%rax
je     123c <parse_stat+0xcc>
lea    0x1(%rbx),%eax
cmp    %r15d,%ebx
mov    %eax,%ebx
jl     1220 <parse_stat+0xb0>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    124e <parse_stat+0xde>
xor    %eax,%eax
jmp    125e <parse_stat+0xee>
xor    %r13d,%r13d
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r13,%rdi
call   1090 <strdup@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [10 x i8] c"%s%d/stat\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c" \00"
define i8* @parse_stat(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* null, i8** %6
%15 = load i32, i32* @PATH_MAX
%16 = add nsw i32 %15, 1
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %7
%19 = alloca i8, i64 %17
store i64 %17, i64* %8
%20 = load i32, i32* @PATH_MAX
%21 = load i32, i32* %4
%22 = call i32 @snprintf(i8* %19, i32 %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %23, i32** %9
%24 = load i32*, i32** %9
%25 = icmp ne i32* %24, null
br i1 %25, label %27, label %26
26: ; preds = %2
store i8* null, i8** %3
store i32 1, i32* %10
br label %67
27: ; preds = %2
store i64 0, i64* %11
store i8* null, i8** %12
%28 = load i32*, i32** %9
%29 = call i64 @getline(i8** %12, i64* %11, i32* %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
br label %62
32: ; preds = %27
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %13
%33 = load i8*, i8** %12
%34 = load i8*, i8** %13
%35 = call i8* @strtok(i8* %33, i8* %34)
store i8* %35, i8** %6
%36 = load i8*, i8** %6
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i32, i32* %5
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %38, %32
br label %59
42: ; preds = %38
store i32 0, i32* %14
br label %43
43: ; preds = %55, %42
%44 = load i8*, i8** %6
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %50
46: ; preds = %43
%47 = load i32, i32* %14
%48 = load i32, i32* %5
%49 = icmp slt i32 %47, %48
br label %50
50: ; preds = %46, %43
%51 = phi i1 [ false, %43 ], [ %49, %46 ]
br i1 %51, label %52, label %58
52: ; preds = %50
%53 = load i8*, i8** %13
%54 = call i8* @strtok(i8* null, i8* %53)
store i8* %54, i8** %6
br label %55
55: ; preds = %52
%56 = load i32, i32* %14
%57 = add nsw i32 %56, 1
store i32 %57, i32* %14
br label %43
58: ; preds = %50
br label %59
59: ; preds = %58, %41
%60 = load i8*, i8** %12
%61 = call i32 @free(i8* %60)
br label %62
62: ; preds = %59, %31
%63 = load i32*, i32** %9
%64 = call i32 @fclose(i32* %63)
%65 = load i8*, i8** %6
%66 = call i8* @strdup(i8* %65)
store i8* %66, i8** %3
store i32 1, i32* %10
br label %67
67: ; preds = %62, %26
%68 = load i8*, i8** %7
%69 = load i8*, i8** %3
ret i8* %69
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [10 x i8] c"%s%d/stat\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c" \00"
define noalias i8* @parse_stat(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = load i32, i32* @PATH_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = call i32 @snprintf(i8* %8, i32 %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%10 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %38, label %12
12: ; preds = %2
%13 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%14 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%15 = call i64 @getline(i8** %4, i64* %3, i32* %10)
%16 = icmp slt i64 %15, 0
br i1 %16, label %34, label %17
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = call i8* @strtok(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp ne i8* %19, null
%21 = icmp sgt i32 %1, 0
%22 = and i1 %21, %20
br i1 %22, label %23, label %30
23: ; preds = %17, %23
%24 = phi i32 [ %26, %23 ], [ 0, %17 ]
%25 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%26 = add nuw nsw i32 %24, 1
%27 = icmp ne i8* %25, null
%28 = icmp slt i32 %26, %1
%29 = select i1 %27, i1 %28, i1 false
br i1 %29, label %23, label %30
30: ; preds = %23, %17
%31 = phi i8* [ %19, %17 ], [ %25, %23 ]
%32 = load i8*, i8** %4
%33 = call i32 @free(i8* %32)
br label %34
34: ; preds = %12, %30
%35 = phi i8* [ null, %12 ], [ %31, %30 ]
%36 = call i32 @fclose(i32* %10)
%37 = call i8* @strdup(i8* %35)
br label %38
38: ; preds = %2, %34
%39 = phi i8* [ %37, %34 ], [ null, %2 ]
ret i8* %39
}