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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int is_pid_running(int pid) {
  DIR *dp;
  char proc_path[PATH_MAX];
  sprintf(proc_path, "/proc/%d/", pid);
  dp = opendir(proc_path);
  if (dp == NULL) {
    return 0;
  } else {
    closedir(dp);
  }
  return 1;
}


x86 assembly (clang, -O0):
<is_pid_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2eae(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edx
lea    0xe8f(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a1 <is_pid_running+0x71>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    11b8 <is_pid_running+0x88>
mov    -0x10(%rbp),%rdi
call   1040 <closedir@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_pid_running>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    %edi,%edx
mov    0x2eb1(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xea7(%rip),%rsi
mov    %rbx,%rdi
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     117d <is_pid_running+0x4d>
mov    %rax,%rdi
call   1040 <closedir@plt>
mov    $0x1,%eax
jmp    117f <is_pid_running+0x4f>
xor    %eax,%eax
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [10 x i8] c"/proc/%d/\00"
define i32 @is_pid_running(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %5
%11 = alloca i8, i64 %9
store i64 %9, i64* %6
%12 = load i32, i32* %3
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = call i32* @opendir(i8* %11)
store i32* %14, i32** %4
%15 = load i32*, i32** %4
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 0, i32* %2
store i32 1, i32* %7
br label %22
18: ; preds = %1
%19 = load i32*, i32** %4
%20 = call i32 @closedir(i32* %19)
br label %21
21: ; preds = %18
store i32 1, i32* %2
store i32 1, i32* %7
br label %22
22: ; preds = %21, %17
%23 = load i8*, i8** %5
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [10 x i8] c"/proc/%d/\00"
define i32 @is_pid_running(i32 %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @opendir(i8* %4)
%7 = icmp eq i32* %6, null
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = tail call i32 @closedir(i32* %6)
br label %10
10: ; preds = %1, %8
%11 = phi i32 [ 1, %8 ], [ 0, %1 ]
ret i32 %11
}