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

/* Variables and functions */
scalar_t__ get_file(int, char *, char *);
int is_kernel_thread(int);
int /*<<< orphan*/ sscanf(char *, char *, char *);
char *strrchr(char *, char);
char proc_status(int pid) {
  char buf[512];
  char *s;
  char status;
  int kthread;
  kthread = is_kernel_thread(pid);
  if (kthread < 0)
    return kthread;
  if (kthread > 0)
    return 0;
  if (get_file(pid, "stat", buf) <= 0)
    return -1;
  s = strrchr(buf, ')') + 2;
  sscanf(s, "%c", &status);
  return status;
}


x86 assembly (clang, -O0):
<proc_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <is_kernel_thread@plt>
mov    %eax,-0x220(%rbp)
cmpl   $0x0,-0x220(%rbp)
jge    1177 <proc_status+0x37>
mov    -0x220(%rbp),%eax
mov    %al,-0x1(%rbp)
jmp    11f5 <proc_status+0xb5>
cmpl   $0x0,-0x220(%rbp)
jle    118d <proc_status+0x4d>
movb   $0x0,-0x1(%rbp)
jmp    11f5 <proc_status+0xb5>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rdx
lea    0xe62(%rip),%rsi
call   1050 <get_file@plt>
cmp    $0x0,%rax
jg     11b6 <proc_status+0x76>
movb   $0xff,-0x1(%rbp)
jmp    11f5 <proc_status+0xb5>
lea    -0x210(%rbp),%rdi
mov    $0x29,%esi
call   1040 <strrchr@plt>
add    $0x2,%rax
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rdi
lea    0xe25(%rip),%rsi
lea    -0x219(%rbp),%rdx
call   1060 <sscanf@plt>
mov    -0x219(%rbp),%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_status>:
push   %rbx
sub    $0x210,%rsp
mov    %edi,%ebx
call   1030 <is_kernel_thread@plt>
test   %eax,%eax
js     119d <proc_status+0x5d>
je     1159 <proc_status+0x19>
xor    %eax,%eax
jmp    119d <proc_status+0x5d>
lea    0xea0(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1050 <get_file@plt>
test   %rax,%rax
jle    119b <proc_status+0x5b>
lea    0x10(%rsp),%rdi
mov    $0x29,%esi
call   1040 <strrchr@plt>
lea    0x2(%rax),%rdi
lea    0xe7a(%rip),%rsi
lea    0xf(%rsp),%rdx
call   1060 <sscanf@plt>
mov    0xf(%rsp),%al
jmp    119d <proc_status+0x5d>
mov    $0xff,%al
add    $0x210,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"stat\00"
@.str.1 = [3 x i8] c"%c\00"
define signext i8 @proc_status(i32 %0) {
%2 = alloca i8
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @is_kernel_thread(i32 %8)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* %7
%14 = trunc i32 %13 to i8
store i8 %14, i8* %2
br label %32
15: ; preds = %1
%16 = load i32, i32* %7
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
store i8 0, i8* %2
br label %32
19: ; preds = %15
%20 = load i32, i32* %3
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%22 = call i64 @get_file(i32 %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = icmp sle i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i8 -1, i8* %2
br label %32
25: ; preds = %19
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%27 = call i8* @strrchr(i8* %26, i8 signext 41)
%28 = getelementptr inbounds i8, i8* %27, i64 2
store i8* %28, i8** %5
%29 = load i8*, i8** %5
%30 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%31 = load i8, i8* %6
store i8 %31, i8* %2
br label %32
32: ; preds = %25, %24, %18, %12
%33 = load i8, i8* %2
ret i8 %33
}

LLVM IR (-O2):
@.str = [5 x i8] c"stat\00"
@.str.1 = [3 x i8] c"%c\00"
define signext i8 @proc_status(i32 %0) {
%2 = alloca [512 x i8]
%3 = alloca i8
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @is_kernel_thread(i32 %0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = trunc i32 %5 to i8
br label %19
9: ; preds = %1
%10 = icmp eq i32 %5, 0
br i1 %10, label %11, label %19
11: ; preds = %9
%12 = call i64 @get_file(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %4)
%13 = icmp slt i64 %12, 1
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = call i8* @strrchr(i8* %4, i8 signext 41)
%16 = getelementptr inbounds i8, i8* %15, i64 2
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%18 = load i8, i8* %3
br label %19
19: ; preds = %11, %9, %14, %7
%20 = phi i8 [ %8, %7 ], [ %18, %14 ], [ 0, %9 ], [ -1, %11 ]
ret i8 %20
}