#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 dummy;
};

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ sprintf(char *, char *, long);
int stat(char *, struct stat *);
int pid_is_alive(long pid) {
  char path[256];
  struct stat sts;
  sprintf(path, "/proc/%li", pid);
  if (stat(path, &sts) == -1 && errno == ENOENT) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<pid_is_alive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xebf(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x118(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
jne    1188 <pid_is_alive+0x68>
mov    0x2e77(%rip),%rax
mov    (%rax),%rax
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%rax
jne    1188 <pid_is_alive+0x68>
movl   $0x0,-0x4(%rbp)
jmp    118f <pid_is_alive+0x6f>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_is_alive>:
push   %rbx
sub    $0x110,%rsp
mov    %rdi,%rdx
lea    0xece(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
jne    1169 <pid_is_alive+0x49>
mov    0x2e88(%rip),%rax
mov    (%rax),%rcx
mov    0x2e6e(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     116e <pid_is_alive+0x4e>
mov    $0x1,%eax
add    $0x110,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [10 x i8] c"/proc/%li\00"
@errno = global i64 0
@ENOENT = global i64 0
define i32 @pid_is_alive(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca [256 x i8]
%5 = alloca %struct.stat
store i64 %0, i64* %3
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = load i64, i64* %3
%8 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i64 %7)
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%10 = call i32 @stat(i8* %9, %struct.stat* %5)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOENT
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 0, i32* %2
br label %18
17: ; preds = %12, %1
store i32 1, i32* %2
br label %18
18: ; preds = %17, %16
%19 = load i32, i32* %2
ret i32 %19
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [10 x i8] c"/proc/%li\00"
@errno = global i64 0
@ENOENT = global i64 0
define i32 @pid_is_alive(i64 %0) {
%2 = alloca [256 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i64 %0)
%7 = call i32 @stat(i8* %4, %struct.stat* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp eq i64 %10, %11
br i1 %12, label %14, label %13
13: ; preds = %9, %1
br label %14
14: ; preds = %9, %13
%15 = phi i32 [ 1, %13 ], [ 0, %9 ]
ret i32 %15
}