#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 */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strstr(char *, char *);
int validpid(pid_t proc) {
  FILE *sysout;
  char temp[512];
  char valid = 0;
  sprintf(temp, "ps -A | grep %d", proc);
  sysout = popen(temp, "r");
  if (sysout) {
    if (fgets(temp, 512, sysout))
      if (!strstr(temp, "<defunct>"))
        valid = 1;
    pclose(sysout);
  }
  return valid;
}


x86 assembly (clang, -O0):
<validpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x4(%rbp)
movb   $0x0,-0x211(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe8a(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x210(%rbp),%rdi
lea    0xe87(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ed <validpid+0x9d>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x200,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11e4 <validpid+0x94>
lea    -0x210(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%eax
jne    11df <validpid+0x8f>
movb   $0x1,-0x211(%rbp)
jmp    11e4 <validpid+0x94>
mov    -0x10(%rbp),%rdi
call   1030 <pclose@plt>
movsbl -0x211(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validpid>:
push   %rbp
push   %rbx
sub    $0x208,%rsp
mov    %edi,%edx
lea    0xe9e(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
lea    0xe9c(%rip),%rsi
mov    %rbx,%rdi
call   1050 <popen@plt>
test   %rax,%rax
je     11b2 <validpid+0x62>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11b6 <validpid+0x66>
lea    0xe72(%rip),%rsi
mov    %rsp,%rdi
call   1070 <strstr@plt>
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
jmp    11b8 <validpid+0x68>
xor    %ebp,%ebp
jmp    11c0 <validpid+0x70>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <pclose@plt>
mov    %ebp,%eax
add    $0x208,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"ps -A | grep %d\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"<defunct>\00"
define i32 @validpid(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [512 x i8]
%5 = alloca i8
store i32 %0, i32* %2
store i8 0, i8* %5
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* %2
%8 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%10 = call i32* @popen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %27
13: ; preds = %1
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%15 = load i32*, i32** %3
%16 = call i64 @fgets(i8* %14, i32 512, i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %13
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%20 = call i32 @strstr(i8* %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %18
store i8 1, i8* %5
br label %23
23: ; preds = %22, %18
br label %24
24: ; preds = %23, %13
%25 = load i32*, i32** %3
%26 = call i32 @pclose(i32* %25)
br label %27
27: ; preds = %24, %1
%28 = load i8, i8* %5
%29 = sext i8 %28 to i32
ret i32 %29
}

LLVM IR (-O2):
@.str = [16 x i8] c"ps -A | grep %d\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"<defunct>\00"
define i32 @validpid(i32 %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32* @popen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %17, label %7
7: ; preds = %1
%8 = call i64 @fgets(i8* %3, i32 512, i32* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %7
%11 = call i32 @strstr(i8* %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32 %11, 0
%13 = zext i1 %12 to i32
br label %14
14: ; preds = %10, %7
%15 = phi i32 [ 0, %7 ], [ %13, %10 ]
%16 = call i32 @pclose(i32* %5)
br label %17
17: ; preds = %14, %1
%18 = phi i32 [ %15, %14 ], [ 0, %1 ]
ret i32 %18
}