#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*/ st_mode;
};
typedef int pid_t;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
int pid_present(pid_t pid) {
  struct stat sb;
  char buffer[128];
  sprintf(buffer, "/proc/%d", pid);
  return ((stat(buffer, &sb) == 0) && S_ISDIR(sb.st_mode));
}


x86 assembly (clang, -O0):
<pid_present>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xeb1(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x90(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x91(%rbp)
jne    118e <pid_present+0x5e>
mov    -0x8(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x91(%rbp)
mov    -0x91(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_present>:
push   %rbx
sub    $0x90,%rsp
mov    %edi,%edx
lea    0xebf(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1179 <pid_present+0x49>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
add    $0x90,%rsp
pop    %rbx
ret

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

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