#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 scalar_t__ uid_t;
struct stat {
  scalar_t__ st_uid;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ snprintf(char *, int, char *, long);
scalar_t__ stat(char *, struct stat *);
int pid_is_user(pid_t pid, uid_t uid) {
  struct stat sb;
  char buf[PATH_MAX];
  snprintf(buf, sizeof(buf), "/proc/%ld", (long)pid);
  if (stat(buf, &sb) != 0)
    return 0;
  return (sb.st_uid == uid);
}


x86 assembly (clang, -O0):
<pid_is_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe97(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     1198 <pid_is_user+0x78>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    11b2 <pid_is_user+0x92>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_is_user>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rcx
mov    %rsp,%r15
mov    0x2eb6(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xea9(%rip),%rdx
mov    %rbx,%rdi
call   1030 <snprintf@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
xor    -0x20(%rbp),%r14
xor    %ecx,%ecx
or     %rax,%r14
sete   %cl
mov    %r15,%rsp
mov    %ecx,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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