#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 */
struct stat {
  int /*<<< orphan*/ member_0;
};
typedef int pid_t;
typedef int /*<<< orphan*/ pathbuf;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ stat(char *, struct stat *);
int check_pid_exists(pid_t pid) {
  struct stat st = {0};
  char pathbuf[PATH_MAX];
  if (!pid) {
    return 0;
  }
  snprintf(pathbuf, sizeof(pathbuf), "/proc/%u", pid);
  if (stat(pathbuf, &st) == 0) {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_pid_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2eb7(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    117f <check_pid_exists+0x5f>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    11d0 <check_pid_exists+0xb0>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%ecx
lea    0xe6d(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x30(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jne    11c2 <check_pid_exists+0xa2>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    11d0 <check_pid_exists+0xb0>
movl   $0x0,-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):
<check_pid_exists>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsp,%r14
movl   $0x0,-0x18(%rbp)
mov    0x2eb4(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %edi,%edi
je     117d <check_pid_exists+0x5d>
mov    %edi,%ecx
lea    0xea4(%rip),%rdx
mov    %rbx,%rdi
call   1030 <snprintf@plt>
lea    -0x18(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
sete   %al
jmp    117f <check_pid_exists+0x5f>
xor    %eax,%eax
mov    %r14,%rsp
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [9 x i8] c"/proc/%u\00"
define i32 @check_pid_exists(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
store i32 0, i32* %4
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = icmp eq i32 %0, 0
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = call i32 @snprintf(i8* %8, i32 %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0)
%12 = call i64 @stat(i8* %8, %struct.stat* %2)
%13 = icmp eq i64 %12, 0
%14 = zext i1 %13 to i32
br label %15
15: ; preds = %10, %1
%16 = phi i32 [ 0, %1 ], [ %14, %10 ]
ret i32 %16
}