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

/* Variables and functions */
int DT_DIR;
scalar_t__ ERANGE;
long LONG_MAX;
long LONG_MIN;
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, long *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
char *strerror(scalar_t__);
long strtol(char *, char **, int);
int read_processes(char dir_name[]) {
  DIR *cure_dir = opendir(dir_name);
  if (cure_dir == NULL) {
    perror("opendir");
    return -1;
  }
  struct dirent *entry = NULL;
  while ((entry = readdir(cure_dir)) != NULL) {
    if (entry->d_type & DT_DIR != 1)
      continue;
    char *endptr;
    long pid = strtol(entry->d_name, &endptr, 10);
    if ((errno == ERANGE && (pid == LONG_MAX || pid == LONG_MIN)) ||
        (errno != 0 && pid == 0) || endptr == entry->d_name)
      continue;
    char exelink_path[PATH_MAX];
    snprintf(exelink_path, PATH_MAX, "%s/%s/stat", dir_name, entry->d_name);
    FILE *stat = fopen(exelink_path, "r");
    if (!stat) {
      printf("%ld : %s\n", pid, strerror(errno));
      continue;
    }
    char exe_path[PATH_MAX];
    fscanf(stat, "%d %s", &pid, exe_path);
    printf("%ld : %s\n", pid, exe_path);
    fclose(stat);
  }
  closedir(cure_dir);
  return 0;
}


x86 assembly (clang, -O0):
<read_processes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ec <read_processes+0x3c>
lea    0xe25(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f3 <read_processes+0x243>
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     13e3 <read_processes+0x233>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    0x2da8(%rip),%rcx
cmpl   $0x1,(%rcx)
setne  %cl
and    $0x1,%cl
movzbl %cl,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1234 <read_processes+0x84>
jmp    11f4 <read_processes+0x44>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %rax,-0x30(%rbp)
mov    0x2d63(%rip),%rax
mov    (%rax),%rax
mov    0x2d79(%rip),%rcx
cmp    (%rcx),%rax
jne    1290 <read_processes+0xe0>
mov    -0x30(%rbp),%rax
mov    0x2d55(%rip),%rcx
cmp    (%rcx),%rax
je     12be <read_processes+0x10e>
mov    -0x30(%rbp),%rax
mov    0x2d59(%rip),%rcx
cmp    (%rcx),%rax
je     12be <read_processes+0x10e>
mov    0x2d21(%rip),%rax
cmpq   $0x0,(%rax)
je     12ac <read_processes+0xfc>
cmpq   $0x0,-0x30(%rbp)
je     12be <read_processes+0x10e>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    0x8(%rcx),%rax
jne    12c3 <read_processes+0x113>
jmp    11f4 <read_processes+0x44>
mov    0x2d26(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%r8
lea    0xcfd(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x60(%rbp),%rdi
lea    0xceb(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1365 <read_processes+0x1b5>
mov    -0x30(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2c7a(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x68(%rbp),%rsi
mov    %rax,%rdx
lea    0xcbf(%rip),%rdi
call   1050 <printf@plt>
movl   $0x2,-0x4c(%rbp)
jmp    13c2 <read_processes+0x212>
mov    0x2c84(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
lea    0xc88(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1070 <fscanf@plt>
mov    -0x70(%rbp),%rdx
mov    -0x30(%rbp),%rsi
lea    0xc66(%rip),%rdi
call   1050 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4c(%rbp),%eax
test   %eax,%eax
je     13de <read_processes+0x22e>
jmp    13d9 <read_processes+0x229>
jmp    11f4 <read_processes+0x44>
jmp    11f4 <read_processes+0x44>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_processes>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,-0x48(%rbp)
call   1040 <opendir@plt>
test   %rax,%rax
je     136d <read_processes+0x1bd>
mov    %rax,%r14
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1354 <read_processes+0x1a4>
mov    %rax,%r13
mov    0x2dcf(%rip),%r15
lea    -0x38(%rbp),%rbx
mov    0x2dbc(%rip),%r12
jmp    1274 <read_processes+0xc4>
mov    %rax,%r13
mov    (%rbx),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r13,%rdi
lea    0xdff(%rip),%rsi
lea    -0x30(%rbp),%rdx
mov    %rbx,%rcx
call   1070 <fscanf@plt>
mov    -0x30(%rbp),%rsi
lea    0xdde(%rip),%rdi
mov    %rbx,%rdx
call   1050 <printf@plt>
mov    %r13,%rdi
call   1030 <fclose@plt>
lea    -0x38(%rbp),%rbx
mov    0x2d68(%rip),%r12
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1354 <read_processes+0x1a4>
xor    %eax,%eax
cmpl   $0x1,(%r15)
setne  %al
test   %eax,0x0(%r13)
jne    1260 <read_processes+0xb0>
mov    0x8(%r13),%rdi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %rax,-0x30(%rbp)
mov    (%r12),%rcx
mov    0x2d35(%rip),%rdx
cmp    (%rdx),%rcx
jne    12c0 <read_processes+0x110>
mov    0x2d19(%rip),%rdx
cmp    (%rdx),%rax
je     1260 <read_processes+0xb0>
mov    0x2d25(%rip),%rdx
cmp    (%rdx),%rax
je     1260 <read_processes+0xb0>
test   %rcx,%rcx
je     12ca <read_processes+0x11a>
test   %rax,%rax
je     1260 <read_processes+0xb0>
mov    -0x38(%rbp),%rax
cmp    0x8(%r13),%rax
je     1260 <read_processes+0xb0>
mov    %rsp,-0x40(%rbp)
mov    0x2d11(%rip),%rbx
mov    (%rbx),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    (%rbx),%esi
mov    0x8(%r13),%r8
mov    %r12,%rdi
lea    0xd06(%rip),%rdx
mov    -0x48(%rbp),%rcx
call   1060 <snprintf@plt>
mov    %r12,%rdi
lea    0xcf1(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
jne    11fe <read_processes+0x4e>
mov    -0x30(%rbp),%r12
mov    0x2c8a(%rip),%rbx
mov    (%rbx),%rdi
call   10d0 <strerror@plt>
lea    0xcd6(%rip),%rdi
mov    %r12,%rsi
mov    %rbx,%r12
mov    %rax,%rdx
call   1050 <printf@plt>
lea    -0x38(%rbp),%rbx
jmp    1250 <read_processes+0xa0>
mov    %r14,%rdi
call   1080 <closedir@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc8c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%eax
jmp    135e <read_processes+0x1ae>

LLVM IR (-O0):
%struct.dirent = type { i32, i8* }
@.str = [8 x i8] c"opendir\00"
@DT_DIR = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@PATH_MAX = global i32 0
@.str.1 = [11 x i8] c"%s/%s/stat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [10 x i8] c"%ld : %s\0A\00"
@.str.4 = [6 x i8] c"%d %s\00"
define i32 @read_processes(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i64
store i8* %0, i8** %3
%13 = load i8*, i8** %3
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %4
%15 = load i32*, i32** %4
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %99
19: ; preds = %1
store %struct.dirent* null, %struct.dirent** %5
br label %20
20: ; preds = %95, %92, %62, %33, %19
%21 = load i32*, i32** %4
%22 = call %struct.dirent* @readdir(i32* %21)
store %struct.dirent* %22, %struct.dirent** %5
%23 = icmp ne %struct.dirent* %22, null
br i1 %23, label %24, label %96
24: ; preds = %20
%25 = load %struct.dirent*, %struct.dirent** %5
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* @DT_DIR
%29 = icmp ne i32 %28, 1
%30 = zext i1 %29 to i32
%31 = and i32 %27, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %24
br label %20
34: ; preds = %24
%35 = load %struct.dirent*, %struct.dirent** %5
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 1
%37 = load i8*, i8** %36
%38 = call i64 @strtol(i8* %37, i8** %6, i32 10)
store i64 %38, i64* %7
%39 = load i64, i64* @errno
%40 = load i64, i64* @ERANGE
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %50
42: ; preds = %34
%43 = load i64, i64* %7
%44 = load i64, i64* @LONG_MAX
%45 = icmp eq i64 %43, %44
br i1 %45, label %62, label %46
46: ; preds = %42
%47 = load i64, i64* %7
%48 = load i64, i64* @LONG_MIN
%49 = icmp eq i64 %47, %48
br i1 %49, label %62, label %50
50: ; preds = %46, %34
%51 = load i64, i64* @errno
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i64, i64* %7
%55 = icmp eq i64 %54, 0
br i1 %55, label %62, label %56
56: ; preds = %53, %50
%57 = load i8*, i8** %6
%58 = load %struct.dirent*, %struct.dirent** %5
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 1
%60 = load i8*, i8** %59
%61 = icmp eq i8* %57, %60
br i1 %61, label %62, label %63
62: ; preds = %56, %53, %46, %42
br label %20
63: ; preds = %56
%64 = load i32, i32* @PATH_MAX
%65 = zext i32 %64 to i64
%66 = call i8* @llvm.stacksave()
store i8* %66, i8** %8
%67 = alloca i8, i64 %65
store i64 %65, i64* %9
%68 = load i32, i32* @PATH_MAX
%69 = load i8*, i8** %3
%70 = load %struct.dirent*, %struct.dirent** %5
%71 = getelementptr inbounds %struct.dirent, %struct.dirent* %70, i32 0, i32 1
%72 = load i8*, i8** %71
%73 = call i32 @snprintf(i8* %67, i32 %68, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %69, i8* %72)
%74 = call i32* @fopen(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %74, i32** %10
%75 = load i32*, i32** %10
%76 = icmp ne i32* %75, null
br i1 %76, label %82, label %77
77: ; preds = %63
%78 = load i64, i64* %7
%79 = load i64, i64* @errno
%80 = call i8* @strerror(i64 %79)
%81 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64 %78, i8* %80)
store i32 2, i32* %11
br label %92
82: ; preds = %63
%83 = load i32, i32* @PATH_MAX
%84 = zext i32 %83 to i64
%85 = alloca i8, i64 %84
store i64 %84, i64* %12
%86 = load i32*, i32** %10
%87 = call i32 @fscanf(i32* %86, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64* %7, i8* %85)
%88 = load i64, i64* %7
%89 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64 %88, i8* %85)
%90 = load i32*, i32** %10
%91 = call i32 @fclose(i32* %90)
store i32 0, i32* %11
br label %92
92: ; preds = %82, %77
%93 = load i8*, i8** %8
%94 = load i32, i32* %11
switch i32 %94, label %101 [
i32 0, label %95
i32 2, label %20
]
95: ; preds = %92
br label %20
96: ; preds = %20
%97 = load i32*, i32** %4
%98 = call i32 @closedir(i32* %97)
store i32 0, i32* %2
br label %99
99: ; preds = %96, %17
%100 = load i32, i32* %2
ret i32 %100
101: ; preds = %92
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i32, i8* }
@.str = [8 x i8] c"opendir\00"
@DT_DIR = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@PATH_MAX = global i32 0
@.str.1 = [11 x i8] c"%s/%s/stat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [10 x i8] c"%ld : %s\0A\00"
@.str.4 = [6 x i8] c"%d %s\00"
define i32 @read_processes(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %12, label %6
6: ; preds = %1
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %72, label %9
9: ; preds = %6
%10 = bitcast i8** %2 to i8*
%11 = bitcast i64* %3 to i8*
br label %14
12: ; preds = %1
%13 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %74
14: ; preds = %9, %23
%15 = phi %struct.dirent* [ %7, %9 ], [ %24, %23 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @DT_DIR
%19 = icmp ne i32 %18, 1
%20 = zext i1 %19 to i32
%21 = and i32 %17, %20
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %14, %71
%24 = call %struct.dirent* @readdir(i32* %4)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %72, label %14
26: ; preds = %14
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 1
%28 = load i8*, i8** %27
%29 = call i64 @strtol(i8* %28, i8** %2, i32 10)
store i64 %29, i64* %3
%30 = load i64, i64* @errno
%31 = load i64, i64* @ERANGE
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %39
33: ; preds = %26
%34 = load i64, i64* @LONG_MAX
%35 = icmp eq i64 %29, %34
%36 = load i64, i64* @LONG_MIN
%37 = icmp eq i64 %29, %36
%38 = select i1 %35, i1 true, i1 %37
br i1 %38, label %71, label %39
39: ; preds = %33, %26
%40 = icmp ne i64 %30, 0
%41 = icmp eq i64 %29, 0
%42 = select i1 %40, i1 %41, i1 false
br i1 %42, label %71, label %43
43: ; preds = %39
%44 = load i8*, i8** %2
%45 = load i8*, i8** %27
%46 = icmp eq i8* %44, %45
br i1 %46, label %71, label %47
47: ; preds = %43
%48 = load i32, i32* @PATH_MAX
%49 = zext i32 %48 to i64
%50 = call i8* @llvm.stacksave()
%51 = alloca i8, i64 %49
%52 = load i32, i32* @PATH_MAX
%53 = load i8*, i8** %27
%54 = call i32 @snprintf(i8* %51, i32 %52, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %53)
%55 = call i32* @fopen(i8* %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %62
57: ; preds = %47
%58 = load i64, i64* %3
%59 = load i64, i64* @errno
%60 = call i8* @strerror(i64 %59)
%61 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64 %58, i8* %60)
br label %70
62: ; preds = %47
%63 = load i32, i32* @PATH_MAX
%64 = zext i32 %63 to i64
%65 = alloca i8, i64 %64
%66 = call i32 @fscanf(i32* %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64* %3, i8* %65)
%67 = load i64, i64* %3
%68 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64 %67, i8* %65)
%69 = call i32 @fclose(i32* %55)
br label %70
70: ; preds = %62, %57
br label %71
71: ; preds = %33, %43, %39, %70
br label %23
72: ; preds = %23, %6
%73 = call i32 @closedir(i32* %4)
br label %74
74: ; preds = %72, %12
%75 = phi i32 [ -1, %12 ], [ 0, %72 ]
ret i32 %75
}