#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
void parse_proc_status_val_by_tag(pid_t pid, char *tag, char *result_val) {
  FILE *fp;
  int flag = 0;
  char buf[512];
  char path[512];
  char *p_buf;
  sprintf(path, "/proc/%d/status", pid);
  fp = fopen(path, "r");
  while (fgets(buf, 512, fp) > 0) {
    if (strncmp(buf, tag, strlen(tag)) == 0) {
      buf[strlen(buf) - 1] = 0;
      flag = 1;
      p_buf = buf;
      while (!(*p_buf >= '0' && *p_buf <= '9'))
        p_buf++;
      strcpy(result_val, p_buf);
      break;
    }
  }
  fclose(fp);
  if (!flag) {
    strcpy(result_val, "error_result");
  }
}


x86 assembly (clang, -O0):
<parse_proc_status_val_by_tag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe62(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x430(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x200,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
jle    12bf <parse_proc_status_val_by_tag+0x14f>
lea    -0x230(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x448(%rbp),%rdi
mov    -0x440(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12ba <parse_proc_status_val_by_tag+0x14a>
lea    -0x230(%rbp),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x230(%rbp,%rax,1)
movl   $0x1,-0x24(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x438(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x30,%ecx
mov    %al,-0x449(%rbp)
jl     1279 <parse_proc_status_val_by_tag+0x109>
mov    -0x438(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
setle  %al
mov    %al,-0x449(%rbp)
mov    -0x449(%rbp),%al
xor    $0xff,%al
test   $0x1,%al
jne    128e <parse_proc_status_val_by_tag+0x11e>
jmp    12a5 <parse_proc_status_val_by_tag+0x135>
mov    -0x438(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x438(%rbp)
jmp    1248 <parse_proc_status_val_by_tag+0xd8>
mov    -0x18(%rbp),%rdi
mov    -0x438(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    12bf <parse_proc_status_val_by_tag+0x14f>
jmp    11ba <parse_proc_status_val_by_tag+0x4a>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x24(%rbp)
jne    12e2 <parse_proc_status_val_by_tag+0x172>
mov    -0x18(%rbp),%rdi
lea    0xd35(%rip),%rsi
call   1040 <strcpy@plt>
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_proc_status_val_by_tag>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%edx
lea    0xe73(%rip),%rsi
lea    0x200(%rsp),%r15
mov    %r15,%rdi
call   1090 <sprintf@plt>
lea    0xe6c(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
mov    %rax,%r15
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jle    1230 <parse_proc_status_val_by_tag+0xc0>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11c0 <parse_proc_status_val_by_tag+0x50>
mov    %r12,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r12,1)
lea    -0x1(%rsp),%rsi
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl 0x1(%rsi),%eax
add    $0x1,%rsi
add    $0xd0,%al
cmp    $0x9,%al
ja     1210 <parse_proc_status_val_by_tag+0xa0>
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
jmp    1247 <parse_proc_status_val_by_tag+0xd7>
mov    %r15,%rdi
call   1050 <fclose@plt>
lea    0xdd3(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/%d/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"error_result\00"
define void @parse_proc_status_val_by_tag(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [512 x i8]
%10 = alloca [512 x i8]
%11 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %8
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%13 = load i32, i32* %4
%14 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %7
br label %17
17: ; preds = %56, %3
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%19 = load i32*, i32** %7
%20 = call i64 @fgets(i8* %18, i32 512, i32* %19)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %57
22: ; preds = %17
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%24 = load i8*, i8** %5
%25 = load i8*, i8** %5
%26 = call i32 @strlen(i8* %25)
%27 = call i64 @strncmp(i8* %23, i8* %24, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %56
29: ; preds = %22
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 %33
store i8 0, i8* %34
store i32 1, i32* %8
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
store i8* %35, i8** %11
br label %36
36: ; preds = %49, %29
%37 = load i8*, i8** %11
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp sge i32 %39, 48
br i1 %40, label %41, label %46
41: ; preds = %36
%42 = load i8*, i8** %11
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp sle i32 %44, 57
br label %46
46: ; preds = %41, %36
%47 = phi i1 [ false, %36 ], [ %45, %41 ]
%48 = xor i1 %47, true
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i8*, i8** %11
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %11
br label %36
52: ; preds = %46
%53 = load i8*, i8** %6
%54 = load i8*, i8** %11
%55 = call i32 @strcpy(i8* %53, i8* %54)
br label %57
56: ; preds = %22
br label %17
57: ; preds = %52, %17
%58 = load i32*, i32** %7
%59 = call i32 @fclose(i32* %58)
%60 = load i32, i32* %8
%61 = icmp ne i32 %60, 0
br i1 %61, label %65, label %62
62: ; preds = %57
%63 = load i8*, i8** %6
%64 = call i32 @strcpy(i8* %63, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %65
65: ; preds = %62, %57
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/%d/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"error_result\00"
define void @parse_proc_status_val_by_tag(i32 %0, i8* %1, i8* %2) {
%4 = alloca [512 x i8]
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%8 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %13, %3
%11 = call i64 @fgets(i8* %6, i32 512, i32* %9)
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %31
13: ; preds = %10
%14 = call i32 @strlen(i8* %1)
%15 = call i64 @strncmp(i8* %6, i8* %1, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %10
17: ; preds = %13
%18 = call i32 @strlen(i8* %6)
%19 = add nsw i32 %18, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %22, %17
%23 = phi i8* [ %6, %17 ], [ %27, %22 ]
%24 = load i8, i8* %23
%25 = add i8 %24, -48
%26 = icmp ult i8 %25, 10
%27 = getelementptr inbounds i8, i8* %23, i64 1
br i1 %26, label %28, label %22
28: ; preds = %22
%29 = call i32 @strcpy(i8* %2, i8* %23)
%30 = call i32 @fclose(i32* %9)
br label %34
31: ; preds = %10
%32 = call i32 @fclose(i32* %9)
%33 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %28, %31
ret void
}