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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strtok(char *, char *);
char get_status_of(pid_t pid) {
  char statfile[20];
  char buffer[1024];
  sprintf(statfile, "/proc/%d/stat", pid);
  FILE *fd = fopen(statfile, "r");
  if (fd == NULL)
    return -1;
  if (fgets(buffer, sizeof(buffer), fd) == NULL) {
    fclose(fd);
    return -1;
  }
  fclose(fd);
  char *token = strtok(buffer, " ");
  int i;
  for (i = 0; i < 2; i++)
    token = strtok(NULL, " ");
  return token[0];
}


x86 assembly (clang, -O0):
<get_status_of>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe94(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x20(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    119f <get_status_of+0x4f>
movb   $0xff,-0x1(%rbp)
jmp    124a <get_status_of+0xfa>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11d6 <get_status_of+0x86>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
movb   $0xff,-0x1(%rbp)
jmp    124a <get_status_of+0xfa>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x420(%rbp),%rdi
lea    0xe20(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x430(%rbp)
movl   $0x0,-0x434(%rbp)
cmpl   $0x2,-0x434(%rbp)
jge    123e <get_status_of+0xee>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdf2(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x430(%rbp)
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
jmp    1206 <get_status_of+0xb6>
mov    -0x430(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_status_of>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    %edi,%edx
lea    0xe9c(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xe98(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
mov    $0xff,%bpl
test   %rax,%rax
je     11d6 <get_status_of+0x86>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %r14,%r14
je     11d6 <get_status_of+0x86>
lea    0xe5e(%rip),%rbx
lea    0x20(%rsp),%rdi
mov    %rbx,%rsi
call   1060 <strtok@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
mov    (%rax),%bpl
mov    %ebp,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c" \00"
define signext i8 @get_status_of(i32 %0) {
%2 = alloca i8
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca [1024 x i8]
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %3
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %1
store i8 -1, i8* %2
br label %42
17: ; preds = %1
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%19 = load i32*, i32** %6
%20 = call i32* @fgets(i8* %18, i32 1024, i32* %19)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32*, i32** %6
%24 = call i32 @fclose(i32* %23)
store i8 -1, i8* %2
br label %42
25: ; preds = %17
%26 = load i32*, i32** %6
%27 = call i32 @fclose(i32* %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%29 = call i8* @strtok(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %29, i8** %7
store i32 0, i32* %8
br label %30
30: ; preds = %35, %25
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 2
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %34, i8** %7
br label %35
35: ; preds = %33
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
br label %30
38: ; preds = %30
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i64 0
%41 = load i8, i8* %40
store i8 %41, i8* %2
br label %42
42: ; preds = %38, %22, %16
%43 = load i8, i8* %2
ret i8 %43
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c" \00"
define signext i8 @get_status_of(i32 %0) {
%2 = alloca [20 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %18, label %9
9: ; preds = %1
%10 = call i32* @fgets(i8* %5, i32 1024, i32* %7)
%11 = icmp eq i32* %10, null
%12 = call i32 @fclose(i32* %7)
br i1 %11, label %18, label %13
13: ; preds = %9
%14 = call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = load i8, i8* %16
br label %18
18: ; preds = %9, %1, %13
%19 = phi i8 [ %17, %13 ], [ -1, %1 ], [ -1, %9 ]
ret i8 %19
}