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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *open_proc_stat_file(char const *);
unsigned int const strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, unsigned int const);
char *strtok_r(char *, char const *, char **);
int get_pid_stat_val(const char *proc_pid_dir_name, const int column,
                     const unsigned len_val, char *val) {
  FILE *fproc_pid_stats;
  char buf[512];
  const char *delim = " ";
  size_t n_read;
  char *token, *saveptr;
  int col_idx = 1;
  if (proc_pid_dir_name == NULL || val == NULL)
    return -1;
  fproc_pid_stats = open_proc_stat_file(proc_pid_dir_name);
  if (fproc_pid_stats == NULL)
    return -1;
  memset(buf, 0, sizeof(buf));
  n_read = fread(buf, sizeof(char), sizeof(buf) - 1, fproc_pid_stats);
  fclose(fproc_pid_stats);
  if (n_read == 0)
    return -1;
  token = strtok_r(buf, delim, &saveptr);
  if (token == NULL)
    return -1;
  do {
    if (col_idx == column) {
      if (len_val <= (strlen(token) + 1)) {
        return -1;
      } else {
        strncpy(val, token, len_val);
        return 0;
      }
    }
    col_idx++;
  } while ((token = strtok_r(NULL, delim, &saveptr)) != NULL);
  return -1;
}


x86 assembly (clang, -O0):
<get_pid_stat_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    0xe70(%rip),%rax
mov    %rax,-0x238(%rbp)
movl   $0x1,-0x254(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b7 <get_pid_stat_val+0x47>
cmpq   $0x0,-0x20(%rbp)
jne    11c3 <get_pid_stat_val+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    131e <get_pid_stat_val+0x1ae>
mov    -0x10(%rbp),%rdi
call   1040 <open_proc_stat_file@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e7 <get_pid_stat_val+0x77>
movl   $0xffffffff,-0x4(%rbp)
jmp    131e <get_pid_stat_val+0x1ae>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1080 <memset@plt>
lea    -0x230(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1ff,%edx
call   1050 <fread@plt>
mov    %rax,-0x240(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
cmpq   $0x0,-0x240(%rbp)
jne    123e <get_pid_stat_val+0xce>
movl   $0xffffffff,-0x4(%rbp)
jmp    131e <get_pid_stat_val+0x1ae>
lea    -0x230(%rbp),%rdi
mov    -0x238(%rbp),%rsi
lea    -0x250(%rbp),%rdx
call   1090 <strtok_r@plt>
mov    %rax,-0x248(%rbp)
cmpq   $0x0,-0x248(%rbp)
jne    1279 <get_pid_stat_val+0x109>
movl   $0xffffffff,-0x4(%rbp)
jmp    131e <get_pid_stat_val+0x1ae>
jmp    127e <get_pid_stat_val+0x10e>
mov    -0x254(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    12e0 <get_pid_stat_val+0x170>
mov    -0x18(%rbp),%eax
mov    %eax,-0x258(%rbp)
mov    -0x248(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x258(%rbp),%eax
add    $0x1,%ecx
cmp    %ecx,%eax
ja     12c1 <get_pid_stat_val+0x151>
movl   $0xffffffff,-0x4(%rbp)
jmp    131e <get_pid_stat_val+0x1ae>
mov    -0x20(%rbp),%rdi
mov    -0x248(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    131e <get_pid_stat_val+0x1ae>
mov    -0x254(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x254(%rbp)
mov    -0x238(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x250(%rbp),%rdx
call   1090 <strtok_r@plt>
mov    %rax,-0x248(%rbp)
cmp    $0x0,%rax
jne    127e <get_pid_stat_val+0x10e>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pid_stat_val>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    $0xffffffff,%r12d
test   %rdi,%rdi
je     1270 <get_pid_stat_val+0x100>
mov    %rcx,%r15
test   %rcx,%rcx
je     1270 <get_pid_stat_val+0x100>
mov    %edx,%r13d
mov    %esi,%ebp
call   1040 <open_proc_stat_file@plt>
test   %rax,%rax
je     1270 <get_pid_stat_val+0x100>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1080 <memset@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1ff,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <fclose@plt>
test   %r14,%r14
je     1270 <get_pid_stat_val+0x100>
lea    0xe0a(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rdx
call   1090 <strtok_r@plt>
test   %rax,%rax
je     1270 <get_pid_stat_val+0x100>
mov    %rax,%rbx
cmp    $0x1,%ebp
je     124f <get_pid_stat_val+0xdf>
mov    %r13d,0x4(%rsp)
add    $0xffffffff,%ebp
lea    0xddf(%rip),%r13
lea    0x8(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r13,%rsi
mov    %r14,%rdx
call   1090 <strtok_r@plt>
test   %rax,%rax
je     1270 <get_pid_stat_val+0x100>
mov    %rax,%rbx
add    $0xffffffff,%ebp
jne    1230 <get_pid_stat_val+0xc0>
mov    0x4(%rsp),%r13d
mov    %rbx,%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
cmp    %r13d,%eax
jae    1270 <get_pid_stat_val+0x100>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1030 <strncpy@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
define i32 @get_pid_stat_val(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca [512 x i8]
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %12
store i32 1, i32* %16
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %22, label %19
19: ; preds = %4
%20 = load i8*, i8** %9
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %19, %4
store i32 -1, i32* %5
br label %72
23: ; preds = %19
%24 = load i8*, i8** %6
%25 = call i32* @open_proc_stat_file(i8* %24)
store i32* %25, i32** %10
%26 = load i32*, i32** %10
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 -1, i32* %5
br label %72
29: ; preds = %23
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i32 0, i32 512)
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%33 = load i32*, i32** %10
%34 = call i64 @fread(i8* %32, i32 1, i32 511, i32* %33)
store i64 %34, i64* %13
%35 = load i32*, i32** %10
%36 = call i32 @fclose(i32* %35)
%37 = load i64, i64* %13
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %29
store i32 -1, i32* %5
br label %72
40: ; preds = %29
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%42 = load i8*, i8** %12
%43 = call i8* @strtok_r(i8* %41, i8* %42, i8** %15)
store i8* %43, i8** %14
%44 = load i8*, i8** %14
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %47
46: ; preds = %40
store i32 -1, i32* %5
br label %72
47: ; preds = %40
br label %48
48: ; preds = %67, %47
%49 = load i32, i32* %16
%50 = load i32, i32* %7
%51 = icmp eq i32 %49, %50
br i1 %51, label %52, label %64
52: ; preds = %48
%53 = load i32, i32* %8
%54 = load i8*, i8** %14
%55 = call i32 @strlen(i8* %54)
%56 = add i32 %55, 1
%57 = icmp ule i32 %53, %56
br i1 %57, label %58, label %59
58: ; preds = %52
store i32 -1, i32* %5
br label %72
59: ; preds = %52
%60 = load i8*, i8** %9
%61 = load i8*, i8** %14
%62 = load i32, i32* %8
%63 = call i32 @strncpy(i8* %60, i8* %61, i32 %62)
store i32 0, i32* %5
br label %72
64: ; preds = %48
%65 = load i32, i32* %16
%66 = add nsw i32 %65, 1
store i32 %66, i32* %16
br label %67
67: ; preds = %64
%68 = load i8*, i8** %12
%69 = call i8* @strtok_r(i8* null, i8* %68, i8** %15)
store i8* %69, i8** %14
%70 = icmp ne i8* %69, null
br i1 %70, label %48, label %71
71: ; preds = %67
store i32 -1, i32* %5
br label %72
72: ; preds = %71, %59, %58, %46, %39, %28, %22
%73 = load i32, i32* %5
ret i32 %73
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define i32 @get_pid_stat_val(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca [512 x i8]
%6 = alloca i8*
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%8 = bitcast i8** %6 to i8*
%9 = icmp eq i8* %0, null
%10 = icmp eq i8* %3, null
%11 = or i1 %9, %10
br i1 %11, label %39, label %12
12: ; preds = %4
%13 = tail call i32* @open_proc_stat_file(i8* %0)
%14 = icmp eq i32* %13, null
br i1 %14, label %39, label %15
15: ; preds = %12
%16 = call i32 @memset(i8* %7, i32 0, i32 512)
%17 = call i64 @fread(i8* %7, i32 1, i32 511, i32* %13)
%18 = call i32 @fclose(i32* %13)
%19 = icmp eq i64 %17, 0
br i1 %19, label %39, label %20
20: ; preds = %15
%21 = call i8* @strtok_r(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6)
%22 = icmp eq i8* %21, null
br i1 %22, label %39, label %23
23: ; preds = %20
%24 = icmp eq i32 %1, 1
br i1 %24, label %27, label %34
25: ; preds = %34
%26 = icmp eq i32 %36, %1
br i1 %26, label %27, label %34
27: ; preds = %25, %23
%28 = phi i8* [ %21, %23 ], [ %37, %25 ]
%29 = call i32 @strlen(i8* %28)
%30 = add i32 %29, 1
%31 = icmp ult i32 %30, %2
br i1 %31, label %32, label %39
32: ; preds = %27
%33 = call i32 @strncpy(i8* %3, i8* %28, i32 %2)
br label %39
34: ; preds = %23, %25
%35 = phi i32 [ %36, %25 ], [ 1, %23 ]
%36 = add nuw nsw i32 %35, 1
%37 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6)
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %25
39: ; preds = %34, %27, %20, %15, %12, %4, %32
%40 = phi i32 [ 0, %32 ], [ -1, %4 ], [ -1, %12 ], [ -1, %15 ], [ -1, %20 ], [ -1, %27 ], [ -1, %34 ]
ret i32 %40
}