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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strchr(char *, char);
int strlen(char *);
char *get_proc_stat_field(const char *statfile, int idx) {
  char buffer[1024];
  FILE *fd = fopen(statfile, "r");
  if (fd == NULL) {
    perror("open process stat file failed");
    return NULL;
  }
  if (fgets(buffer, sizeof(buffer), fd) == NULL) {
    perror("read process stat file failed");
    fclose(fd);
    return NULL;
  }
  fclose(fd);
  char *p = buffer;
  int slen = strlen(buffer);
  int sp = idx;
  while (sp-- && p != NULL && (p + 1) < (buffer + slen))
    p = strchr(p + 1, ' ');
  if (p == NULL || (p + 1) >= (buffer + slen)) {
    printf("process stat file format error");
    return NULL;
  }
  char *q = strchr(p + 1, ' ');
  if (q == NULL) {
    printf("process stat file format error");
    return NULL;
  }
  *q = '\0';
  char *item = (char *)malloc(strlen(p + 1) + 1);
  if (item != NULL) {
    snprintf(item, strlen(p + 1) + 1, "%s", p + 1);
  }
  return item;
}


x86 assembly (clang, -O0):
<get_proc_stat_field>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeac(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    11e0 <get_proc_stat_field+0x50>
lea    0xe32(%rip),%rdi
call   10b0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1404 <get_proc_stat_field+0x274>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    1227 <get_proc_stat_field+0x97>
lea    0xe15(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1404 <get_proc_stat_field+0x274>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x430(%rbp)
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x434(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x438(%rbp)
mov    -0x438(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x438(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x449(%rbp)
je     12bc <get_proc_stat_field+0x12c>
xor    %eax,%eax
cmpq   $0x0,-0x430(%rbp)
mov    %al,-0x449(%rbp)
je     12bc <get_proc_stat_field+0x12c>
mov    -0x430(%rbp),%rax
add    $0x1,%rax
lea    -0x420(%rbp),%rcx
movslq -0x434(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x449(%rbp)
mov    -0x449(%rbp),%al
test   $0x1,%al
jne    12cf <get_proc_stat_field+0x13f>
jmp    12f0 <get_proc_stat_field+0x160>
mov    -0x430(%rbp),%rdi
add    $0x1,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x430(%rbp)
jmp    125c <get_proc_stat_field+0xcc>
cmpq   $0x0,-0x430(%rbp)
je     1323 <get_proc_stat_field+0x193>
mov    -0x430(%rbp),%rax
add    $0x1,%rax
lea    -0x420(%rbp),%rcx
movslq -0x434(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jb     133c <get_proc_stat_field+0x1ac>
lea    0xd12(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1404 <get_proc_stat_field+0x274>
mov    -0x430(%rbp),%rdi
add    $0x1,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x440(%rbp)
cmpq   $0x0,-0x440(%rbp)
jne    137f <get_proc_stat_field+0x1ef>
lea    0xccf(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1404 <get_proc_stat_field+0x274>
mov    -0x440(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x430(%rbp),%rdi
add    $0x1,%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x448(%rbp)
je     13f9 <get_proc_stat_field+0x269>
mov    -0x448(%rbp),%rax
mov    %rax,-0x458(%rbp)
mov    -0x430(%rbp),%rdi
add    $0x1,%rdi
call   1040 <strlen@plt>
mov    -0x458(%rbp),%rdi
mov    %eax,%esi
add    $0x1,%esi
mov    -0x430(%rbp),%rcx
add    $0x1,%rcx
lea    0xc67(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x448(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_proc_stat_field>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%ebp
lea    0xeb3(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     129d <get_proc_stat_field+0x10d>
mov    %rax,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12ab <get_proc_stat_field+0x11b>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
movslq %eax,%r14
test   %ebp,%ebp
je     1225 <get_proc_stat_field+0x95>
mov    %rsp,%rbx
lea    (%rsp,%r14,1),%r15
add    $0xffffffff,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0x1(%rbx),%rdi
cmp    %r15,%rdi
jae    1225 <get_proc_stat_field+0x95>
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
add    $0xffffffff,%ebp
jae    1220 <get_proc_stat_field+0x90>
test   %rbx,%rbx
jne    1200 <get_proc_stat_field+0x70>
test   %rbx,%rbx
je     127e <get_proc_stat_field+0xee>
add    $0x1,%rbx
lea    (%rsp,%r14,1),%rax
cmp    %rax,%rbx
jae    127e <get_proc_stat_field+0xee>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     127e <get_proc_stat_field+0xee>
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     128c <get_proc_stat_field+0xfc>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%esi
lea    0xdea(%rip),%rdx
mov    %rbp,%rdi
mov    %rbx,%rcx
call   1070 <snprintf@plt>
jmp    128c <get_proc_stat_field+0xfc>
lea    0xdb7(%rip),%rdi
call   1060 <printf@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5c(%rip),%rdi
call   10b0 <perror@plt>
jmp    128a <get_proc_stat_field+0xfa>
lea    0xd6c(%rip),%rdi
call   10b0 <perror@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    128a <get_proc_stat_field+0xfa>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [30 x i8] c"open process stat file failed\00"
@.str.2 = [30 x i8] c"read process stat file failed\00"
@.str.3 = [31 x i8] c"process stat file format error\00"
@.str.4 = [3 x i8] c"%s\00"
define i8* @get_proc_stat_field(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%13 = load i8*, i8** %4
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %98
19: ; preds = %2
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%21 = load i32*, i32** %7
%22 = call i32* @fgets(i8* %20, i32 1024, i32* %21)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32*, i32** %7
%27 = call i32 @fclose(i32* %26)
store i8* null, i8** %3
br label %98
28: ; preds = %19
%29 = load i32*, i32** %7
%30 = call i32 @fclose(i32* %29)
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
store i8* %31, i8** %8
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
store i32 %33, i32* %9
%34 = load i32, i32* %5
store i32 %34, i32* %10
br label %35
35: ; preds = %52, %28
%36 = load i32, i32* %10
%37 = add nsw i32 %36, -1
store i32 %37, i32* %10
%38 = icmp ne i32 %36, 0
br i1 %38, label %39, label %50
39: ; preds = %35
%40 = load i8*, i8** %8
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %50
42: ; preds = %39
%43 = load i8*, i8** %8
%44 = getelementptr inbounds i8, i8* %43, i64 1
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = icmp ult i8* %44, %48
br label %50
50: ; preds = %42, %39, %35
%51 = phi i1 [ false, %39 ], [ false, %35 ], [ %49, %42 ]
br i1 %51, label %52, label %56
52: ; preds = %50
%53 = load i8*, i8** %8
%54 = getelementptr inbounds i8, i8* %53, i64 1
%55 = call i8* @strchr(i8* %54, i8 signext 32)
store i8* %55, i8** %8
br label %35
56: ; preds = %50
%57 = load i8*, i8** %8
%58 = icmp eq i8* %57, null
br i1 %58, label %67, label %59
59: ; preds = %56
%60 = load i8*, i8** %8
%61 = getelementptr inbounds i8, i8* %60, i64 1
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%63 = load i32, i32* %9
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
%66 = icmp uge i8* %61, %65
br i1 %66, label %67, label %69
67: ; preds = %59, %56
%68 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %3
br label %98
69: ; preds = %59
%70 = load i8*, i8** %8
%71 = getelementptr inbounds i8, i8* %70, i64 1
%72 = call i8* @strchr(i8* %71, i8 signext 32)
store i8* %72, i8** %11
%73 = load i8*, i8** %11
%74 = icmp eq i8* %73, null
br i1 %74, label %75, label %77
75: ; preds = %69
%76 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %3
br label %98
77: ; preds = %69
%78 = load i8*, i8** %11
store i8 0, i8* %78
%79 = load i8*, i8** %8
%80 = getelementptr inbounds i8, i8* %79, i64 1
%81 = call i32 @strlen(i8* %80)
%82 = add nsw i32 %81, 1
%83 = call i64 @malloc(i32 %82)
%84 = inttoptr i64 %83 to i8*
store i8* %84, i8** %12
%85 = load i8*, i8** %12
%86 = icmp ne i8* %85, null
br i1 %86, label %87, label %96
87: ; preds = %77
%88 = load i8*, i8** %12
%89 = load i8*, i8** %8
%90 = getelementptr inbounds i8, i8* %89, i64 1
%91 = call i32 @strlen(i8* %90)
%92 = add nsw i32 %91, 1
%93 = load i8*, i8** %8
%94 = getelementptr inbounds i8, i8* %93, i64 1
%95 = call i32 @snprintf(i8* %88, i32 %92, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %94)
br label %96
96: ; preds = %87, %77
%97 = load i8*, i8** %12
store i8* %97, i8** %3
br label %98
98: ; preds = %96, %75, %67, %24, %17
%99 = load i8*, i8** %3
ret i8* %99
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [30 x i8] c"open process stat file failed\00"
@.str.2 = [30 x i8] c"read process stat file failed\00"
@.str.3 = [31 x i8] c"process stat file format error\00"
@.str.4 = [3 x i8] c"%s\00"
define i8* @get_proc_stat_field(i8* nocapture %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %61
9: ; preds = %2
%10 = call i32* @fgets(i8* %4, i32 1024, i32* %5)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @fclose(i32* %5)
br label %61
15: ; preds = %9
%16 = call i32 @fclose(i32* %5)
%17 = call i32 @strlen(i8* %4)
%18 = icmp eq i32 %1, 0
br i1 %18, label %35, label %19
19: ; preds = %15
%20 = sext i32 %17 to i64
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %20
br label %22
22: ; preds = %19, %28
%23 = phi i32 [ %1, %19 ], [ %25, %28 ]
%24 = phi i8* [ %4, %19 ], [ %29, %28 ]
%25 = add nsw i32 %23, -1
%26 = getelementptr inbounds i8, i8* %24, i64 1
%27 = icmp ult i8* %26, %21
br i1 %27, label %28, label %38
28: ; preds = %22
%29 = call i8* @strchr(i8* %26, i8 signext 32)
%30 = icmp ne i32 %25, 0
%31 = icmp ne i8* %29, null
%32 = select i1 %30, i1 %31, i1 false
br i1 %32, label %22, label %33
33: ; preds = %28
%34 = icmp eq i8* %29, null
br i1 %34, label %44, label %35
35: ; preds = %15, %33
%36 = phi i8* [ %29, %33 ], [ %4, %15 ]
%37 = sext i32 %17 to i64
br label %38
38: ; preds = %22, %35
%39 = phi i64 [ %37, %35 ], [ %20, %22 ]
%40 = phi i8* [ %36, %35 ], [ %24, %22 ]
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %39
%43 = icmp ult i8* %41, %42
br i1 %43, label %46, label %44
44: ; preds = %38, %33
%45 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %61
46: ; preds = %38
%47 = call i8* @strchr(i8* %41, i8 signext 32)
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %61
51: ; preds = %46
store i8 0, i8* %47
%52 = call i32 @strlen(i8* %41)
%53 = add nsw i32 %52, 1
%54 = call i64 @malloc(i32 %53)
%55 = inttoptr i64 %54 to i8*
%56 = icmp eq i64 %54, 0
br i1 %56, label %61, label %57
57: ; preds = %51
%58 = call i32 @strlen(i8* %41)
%59 = add nsw i32 %58, 1
%60 = call i32 @snprintf(i8* %55, i32 %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %41)
br label %61
61: ; preds = %44, %51, %57, %49, %12, %7
%62 = phi i8* [ null, %7 ], [ null, %12 ], [ null, %44 ], [ null, %49 ], [ %55, %57 ], [ %55, %51 ]
ret i8* %62
}