#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 dirent {
  scalar_t__ d_type;
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ F_OK;
int PATH_MAX;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int only_one_process(void) {
  DIR *dir;
  struct dirent *dent;
  int proc_num = 0;
  char comm_file[PATH_MAX];
  int is_one_process = 1;
  dir = opendir("/proc");
  if (dir == NULL) {
    return -1;
  }
  while ((dent = readdir(dir)) != NULL) {
    if (dent->d_type != DT_DIR) {
      continue;
    }
    snprintf(comm_file, PATH_MAX, "/proc/%s/comm", dent->d_name);
    if (access(comm_file, F_OK) != 0) {
      continue;
    }
    proc_num++;
    if (proc_num > 1) {
      is_one_process = 0;
      break;
    }
  }
  closedir(dir);
  return is_one_process;
}


x86 assembly (clang, -O0):
<only_one_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x1c(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
lea    0xe67(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c0 <only_one_process+0x70>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    1274 <only_one_process+0x124>
jmp    11c5 <only_one_process+0x75>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     125e <only_one_process+0x10e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%rax
je     11f8 <only_one_process+0xa8>
jmp    11c5 <only_one_process+0x75>
mov    -0x40(%rbp),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xdf2(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x40(%rbp),%rdi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
je     123a <only_one_process+0xea>
jmp    11c5 <only_one_process+0x75>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jle    1259 <only_one_process+0x109>
movl   $0x0,-0x34(%rbp)
jmp    125e <only_one_process+0x10e>
jmp    11c5 <only_one_process+0x75>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<only_one_process>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe81(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1263 <only_one_process+0x113>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <readdir@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     1271 <only_one_process+0x121>
mov    0x2e2a(%rip),%r14
lea    0xe51(%rip),%r12
mov    0x2e0c(%rip),%r13
jmp    11d1 <only_one_process+0x81>
xchg   %ax,%ax
mov    %r15,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     126b <only_one_process+0x11b>
mov    (%rax),%rcx
cmp    (%r14),%rcx
jne    11c0 <only_one_process+0x70>
mov    0x2e10(%rip),%rcx
mov    (%rcx),%esi
mov    0x8(%rax),%rcx
mov    %rbx,%rdi
mov    %r12,%rdx
call   1040 <snprintf@plt>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %rax,%rax
jne    11c0 <only_one_process+0x70>
mov    %r15,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     126b <only_one_process+0x11b>
lea    0xdf0(%rip),%r12
jmp    122d <only_one_process+0xdd>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     126b <only_one_process+0x11b>
mov    (%rax),%rcx
cmp    (%r14),%rcx
jne    1220 <only_one_process+0xd0>
mov    0x2db4(%rip),%rcx
mov    (%rcx),%esi
mov    0x8(%rax),%rcx
mov    %rbx,%rdi
mov    %r12,%rdx
call   1040 <snprintf@plt>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %rax,%rax
jne    1220 <only_one_process+0xd0>
xor    %r14d,%r14d
jmp    1271 <only_one_process+0x121>
mov    $0xffffffff,%r14d
jmp    1279 <only_one_process+0x129>
mov    $0x1,%r14d
mov    %r15,%rdi
call   1050 <closedir@plt>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"/proc\00"
@DT_DIR = global i64 0
@.str.1 = [14 x i8] c"/proc/%s/comm\00"
@F_OK = global i32 0
define i32 @only_one_process() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %4
%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
store i32 1, i32* %7
%13 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %2
%14 = load i32*, i32** %2
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %0
store i32 -1, i32* %1
store i32 1, i32* %8
br label %50
17: ; preds = %0
br label %18
18: ; preds = %45, %38, %28, %17
%19 = load i32*, i32** %2
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %3
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %46
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %3
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @DT_DIR
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %22
br label %18
29: ; preds = %22
%30 = load i32, i32* @PATH_MAX
%31 = load %struct.dirent*, %struct.dirent** %3
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 1
%33 = load i8*, i8** %32
%34 = call i32 @snprintf(i8* %12, i32 %30, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = load i32, i32* @F_OK
%36 = call i64 @access(i8* %12, i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %29
br label %18
39: ; preds = %29
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
%42 = load i32, i32* %4
%43 = icmp sgt i32 %42, 1
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 0, i32* %7
br label %46
45: ; preds = %39
br label %18
46: ; preds = %44, %18
%47 = load i32*, i32** %2
%48 = call i32 @closedir(i32* %47)
%49 = load i32, i32* %7
store i32 %49, i32* %1
store i32 1, i32* %8
br label %50
50: ; preds = %46, %16
%51 = load i8*, i8** %5
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"/proc\00"
@DT_DIR = global i64 0
@.str.1 = [14 x i8] c"/proc/%s/comm\00"
@F_OK = global i32 0
define i32 @only_one_process() {
%1 = load i32, i32* @PATH_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %49, label %6
6: ; preds = %0
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %46, label %9
9: ; preds = %6, %15
%10 = phi %struct.dirent* [ %16, %15 ], [ %7, %6 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @DT_DIR
%14 = icmp eq i64 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %9, %18
%16 = call %struct.dirent* @readdir(i32* %4)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %46, label %9
18: ; preds = %9
%19 = load i32, i32* @PATH_MAX
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 1
%21 = load i8*, i8** %20
%22 = call i32 @snprintf(i8* %3, i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @F_OK
%24 = call i64 @access(i8* %3, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %15
26: ; preds = %18
%27 = call %struct.dirent* @readdir(i32* %4)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %46, label %29
29: ; preds = %26, %43
%30 = phi %struct.dirent* [ %44, %43 ], [ %27, %26 ]
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i64 0, i32 0
%32 = load i64, i64* %31
%33 = load i64, i64* @DT_DIR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %43
35: ; preds = %29
%36 = load i32, i32* @PATH_MAX
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i64 0, i32 1
%38 = load i8*, i8** %37
%39 = call i32 @snprintf(i8* %3, i32 %36, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @F_OK
%41 = call i64 @access(i8* %3, i32 %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %35, %29
%44 = call %struct.dirent* @readdir(i32* %4)
%45 = icmp eq %struct.dirent* %44, null
br i1 %45, label %46, label %29
46: ; preds = %15, %35, %43, %6, %26
%47 = phi i32 [ 1, %6 ], [ 1, %26 ], [ 0, %35 ], [ 1, %43 ], [ 1, %15 ]
%48 = call i32 @closedir(i32* %4)
br label %49
49: ; preds = %0, %46
%50 = phi i32 [ %47, %46 ], [ -1, %0 ]
ret i32 %50
}