#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 *);
char *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) {
    closedir(dir);
    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 ((s = 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    129a <check_pid_valid+0xfa>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    13c5 <check_pid_valid+0x225>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0xa8(%rbp),%rdi
mov    -0xb0(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%ecx
lea    0xd4c(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0xa8(%rbp),%rdi
lea    0xd4a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1306 <check_pid_valid+0x166>
lea    0xd31(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    13c5 <check_pid_valid+0x225>
lea    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x3f,%esi
call   1090 <fgets@plt>
cmp    $0x0,%eax
jne    1340 <check_pid_valid+0x1a0>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    13c5 <check_pid_valid+0x225>
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    1380 <check_pid_valid+0x1e0>
mov    -0x88(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <strstr@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
jne    13b4 <check_pid_valid+0x214>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    13c5 <check_pid_valid+0x225>
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,%r15d
mov    0x2e31(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
xor    %r13d,%r13d
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1070 <memset@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
lea    0xdf7(%rip),%rdx
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r15d,%ecx
call   1060 <snprintf@plt>
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12ab <check_pid_valid+0x10b>
mov    %rax,%rdi
call   1080 <closedir@plt>
lea    0xdd2(%rip),%rdx
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r15d,%ecx
call   1060 <snprintf@plt>
lea    0xdce(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12b4 <check_pid_valid+0x114>
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
je     12c0 <check_pid_valid+0x120>
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
cmpb   $0xa,-0x70(%rbp,%rax,1)
jne    1292 <check_pid_valid+0xf2>
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10c0 <strstr@plt>
xor    %r13d,%r13d
test   %rax,%rax
setne  %r13b
jmp    12c3 <check_pid_valid+0x123>
xor    %edi,%edi
call   1080 <closedir@plt>
jmp    12c3 <check_pid_valid+0x123>
lea    0xd61(%rip),%rdi
call   10b0 <perror@plt>
xor    %r13d,%r13d
mov    %r13d,%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 %33
30: ; preds = %2
%31 = load i32*, i32** %7
%32 = call i32 @closedir(i32* %31)
store i32 0, i32* %3
store i32 1, i32* %13
br label %71
33: ; preds = %2
%34 = load i32*, i32** %7
%35 = call i32 @closedir(i32* %34)
%36 = trunc i64 %15 to i32
%37 = load i32, i32* %4
%38 = call i32 @snprintf(i8* %17, i32 %36, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %39, i32** %6
%40 = load i32*, i32** %6
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %44
42: ; preds = %33
%43 = 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 %71
44: ; preds = %33
%45 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%46 = load i32*, i32** %6
%47 = call i32 @fgets(i8* %45, i32 63, i32* %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %44
%50 = load i32*, i32** %6
%51 = call i32 @fclose(i32* %50)
store i32 0, i32* %3
store i32 1, i32* %13
br label %71
52: ; preds = %44
%53 = load i32*, i32** %6
%54 = call i32 @fclose(i32* %53)
%55 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%56 = call i64 @strlen(i8* %55)
%57 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 %56
store i8* %57, i8** %12
%58 = load i8*, i8** %12
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 10
br i1 %61, label %62, label %64
62: ; preds = %52
%63 = load i8*, i8** %12
store i8 0, i8* %63
br label %64
64: ; preds = %62, %52
%65 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%66 = load i8*, i8** %5
%67 = call i8* @strstr(i8* %65, i8* %66)
store i8* %67, i8** %12
%68 = icmp eq i8* %67, null
br i1 %68, label %69, label %70
69: ; preds = %64
store i32 0, i32* %3
store i32 1, i32* %13
br label %71
70: ; preds = %64
store i32 1, i32* %3
store i32 1, i32* %13
br label %71
71: ; preds = %70, %69, %49, %42, %30
%72 = load i8*, i8** %8
%73 = load i32, i32* %3
ret i32 %73
}

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 %15, label %17
15: ; preds = %2
%16 = call i32 @closedir(i32* null)
br label %38
17: ; preds = %2
%18 = call i32 @closedir(i32* %13)
%19 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%20 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %38
24: ; preds = %17
%25 = call i32 @fgets(i8* %8, i32 63, i32* %20)
%26 = icmp eq i32 %25, 0
%27 = call i32 @fclose(i32* %20)
br i1 %26, label %38, label %28
28: ; preds = %24
%29 = call i64 @strlen(i8* %8)
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 10
br i1 %32, label %33, label %34
33: ; preds = %28
store i8 0, i8* %30
br label %34
34: ; preds = %33, %28
%35 = call i8* @strstr(i8* %8, i8* dereferenceable(1) %1)
%36 = icmp ne i8* %35, null
%37 = zext i1 %36 to i32
br label %38
38: ; preds = %24, %34, %22, %15
%39 = phi i32 [ 0, %15 ], [ 0, %22 ], [ %37, %34 ], [ 0, %24 ]
ret i32 %39
}