#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*/ proc_cmdline;
typedef int pid_t;
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ dirpath;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
size_t strlen(char *);
int /*<<< orphan*/ *strstr(char *, char const *);
int check_pid_valid(pid_t pid, const char *prog) {
  FILE *fp;
  DIR *dir;
  char filename[PATH_MAX];
  char dirpath[PATH_MAX];
  char proc_cmdline[64];
  char *s = NULL;
  memset(filename, 0, PATH_MAX);
  memset(dirpath, 0, PATH_MAX);
  snprintf(dirpath, sizeof(dirpath), "/proc/%d", pid);
  if ((dir = opendir(dirpath)) == NULL) {
    return 0;
  }
  closedir(dir);
  snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
  fp = fopen(filename, "r");
  if (fp == NULL) {
    perror("check_pid_valid");
    return 0;
  }
  if (!fgets(proc_cmdline, sizeof(proc_cmdline) - 1, fp)) {
    fclose(fp);
    return 0;
  }
  fclose(fp);
  s = &(proc_cmdline[strlen(proc_cmdline)]);
  if (*s == '\n')
    *s = 0;
  if (strstr(proc_cmdline, prog) == NULL) {
    return 0;
  } else {
    return 1;
  }
}


x86 assembly (clang, -O0):
<check_pid_valid>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%ecx
mov    %rcx,-0xb0(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x28(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0xa8(%rbp)
mov    %rdi,%rsp
mov    %rcx,-0x30(%rbp)
mov    (%rax),%eax
mov    %rax,-0xa0(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x98(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x88(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x98(%rbp),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x98(%rbp),%rdi
mov    -0xa0(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%ecx
lea    0xda4(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x98(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1291 <check_pid_valid+0xf1>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    13b5 <check_pid_valid+0x215>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0xa8(%rbp),%rdi
mov    -0xb0(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%ecx
lea    0xd55(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0xa8(%rbp),%rdi
lea    0xd53(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12fd <check_pid_valid+0x15d>
lea    0xd3a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    13b5 <check_pid_valid+0x215>
lea    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x3f,%esi
call   1090 <fgets@plt>
cmp    $0x0,%eax
jne    1337 <check_pid_valid+0x197>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    13b5 <check_pid_valid+0x215>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
lea    -0x80(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x88(%rbp)
mov    -0x88(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1377 <check_pid_valid+0x1d7>
mov    -0x88(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
jne    13a4 <check_pid_valid+0x204>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    13b5 <check_pid_valid+0x215>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pid_valid>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,-0x30(%rbp)
mov    %edi,%r12d
mov    0x2e31(%rip),%r15
mov    (%r15),%r13d
mov    %rsp,%rbx
mov    %r13,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r13d,%edx
call   1070 <memset@plt>
mov    (%r15),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
lea    0xe00(%rip),%rdx
mov    %r14,%rdi
mov    %r13d,%esi
mov    %r12d,%ecx
call   1060 <snprintf@plt>
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12b1 <check_pid_valid+0x111>
mov    %rax,%rdi
call   1080 <closedir@plt>
lea    0xddb(%rip),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %r12d,%ecx
call   1060 <snprintf@plt>
lea    0xdd7(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12a5 <check_pid_valid+0x105>
mov    %rax,%rbx
lea    -0x70(%rbp),%rdi
mov    $0x3f,%esi
mov    %rax,%rdx
call   1090 <fgets@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %r14d,%r14d
mov    $0x0,%ecx
je     12b3 <check_pid_valid+0x113>
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
cmpb   $0xa,-0x70(%rbp,%rax,1)
jne    128e <check_pid_valid+0xee>
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10c0 <strstr@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
jmp    12b3 <check_pid_valid+0x113>
lea    0xd70(%rip),%rdi
call   10b0 <perror@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [9 x i8] c"/proc/%d\00"
@.str.1 = [17 x i8] c"/proc/%d/cmdline\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [16 x i8] c"check_pid_valid\00"
define i32 @check_pid_valid(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca [64 x i8]
%12 = alloca i8*
%13 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
%18 = load i32, i32* @PATH_MAX
%19 = zext i32 %18 to i64
%20 = alloca i8, i64 %19
store i64 %19, i64* %10
store i8* null, i8** %12
%21 = load i32, i32* @PATH_MAX
%22 = call i32 @memset(i8* %17, i32 0, i32 %21)
%23 = load i32, i32* @PATH_MAX
%24 = call i32 @memset(i8* %20, i32 0, i32 %23)
%25 = trunc i64 %19 to i32
%26 = load i32, i32* %4
%27 = call i32 @snprintf(i8* %20, i32 %25, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = call i32* @opendir(i8* %20)
store i32* %28, i32** %7
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %31
30: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %13
br label %69
31: ; preds = %2
%32 = load i32*, i32** %7
%33 = call i32 @closedir(i32* %32)
%34 = trunc i64 %15 to i32
%35 = load i32, i32* %4
%36 = call i32 @snprintf(i8* %17, i32 %34, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %37, i32** %6
%38 = load i32*, i32** %6
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
store i32 1, i32* %13
br label %69
42: ; preds = %31
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%44 = load i32*, i32** %6
%45 = call i32 @fgets(i8* %43, i32 63, i32* %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %42
%48 = load i32*, i32** %6
%49 = call i32 @fclose(i32* %48)
store i32 0, i32* %3
store i32 1, i32* %13
br label %69
50: ; preds = %42
%51 = load i32*, i32** %6
%52 = call i32 @fclose(i32* %51)
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%54 = call i64 @strlen(i8* %53)
%55 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 %54
store i8* %55, i8** %12
%56 = load i8*, i8** %12
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 10
br i1 %59, label %60, label %62
60: ; preds = %50
%61 = load i8*, i8** %12
store i8 0, i8* %61
br label %62
62: ; preds = %60, %50
%63 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%64 = load i8*, i8** %5
%65 = call i32* @strstr(i8* %63, i8* %64)
%66 = icmp eq i32* %65, null
br i1 %66, label %67, label %68
67: ; preds = %62
store i32 0, i32* %3
store i32 1, i32* %13
br label %69
68: ; preds = %62
store i32 1, i32* %3
store i32 1, i32* %13
br label %69
69: ; preds = %68, %67, %47, %40, %30
%70 = load i8*, i8** %8
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [9 x i8] c"/proc/%d\00"
@.str.1 = [17 x i8] c"/proc/%d/cmdline\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [16 x i8] c"check_pid_valid\00"
define i32 @check_pid_valid(i32 %0, i8* nocapture %1) {
%3 = alloca [64 x i8]
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = alloca i8, i64 %5
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%9 = call i32 @memset(i8* %6, i32 0, i32 %4)
%10 = load i32, i32* @PATH_MAX
%11 = call i32 @memset(i8* %7, i32 0, i32 %10)
%12 = call i32 @snprintf(i8* %7, i32 %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0)
%13 = call i32* @opendir(i8* %7)
%14 = icmp eq i32* %13, null
br i1 %14, label %36, label %15
15: ; preds = %2
%16 = call i32 @closedir(i32* %13)
%17 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%18 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %36
22: ; preds = %15
%23 = call i32 @fgets(i8* %8, i32 63, i32* %18)
%24 = icmp eq i32 %23, 0
%25 = call i32 @fclose(i32* %18)
br i1 %24, label %36, label %26
26: ; preds = %22
%27 = call i64 @strlen(i8* %8)
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 10
br i1 %30, label %31, label %32
31: ; preds = %26
store i8 0, i8* %28
br label %32
32: ; preds = %31, %26
%33 = call i32* @strstr(i8* %8, i8* dereferenceable(1) %1)
%34 = icmp ne i32* %33, null
%35 = zext i1 %34 to i32
br label %36
36: ; preds = %22, %32, %2, %20
%37 = phi i32 [ 0, %20 ], [ 0, %2 ], [ %35, %32 ], [ 0, %22 ]
ret i32 %37
}