#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 myproc {
  int dummy;
};
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct myproc *machine_proc_new(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ proc_addto(struct myproc **, struct myproc *);
int /*<<< orphan*/ proc_get(struct myproc **, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int sscanf(int /*<<< orphan*/, char *, int *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
void machine_proc_get_more(struct myproc **procs) {
  DIR *d = opendir("/proc");
  struct dirent *ent;
  if (!d) {
    perror("opendir()");
    exit(1);
  }
  while ((errno = 0, ent = readdir(d))) {
    int pid;
    if (sscanf(ent->d_name, "%d", &pid) == 1 && !proc_get(procs, pid)) {
      struct myproc *p = machine_proc_new(pid);
      if (p) {
        proc_addto(procs, p);
      }
    }
  }
  if (errno) {
    fprintf(stderr, "readdir(): %s\n", strerror(errno));
    exit(1);
  }
  closedir(d);
}


x86 assembly (clang, -O0):
<machine_proc_get_more>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe3d(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ed <machine_proc_get_more+0x3d>
lea    0xe28(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    11f2 <machine_proc_get_more+0x42>
mov    0x2ddf(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1279 <machine_proc_get_more+0xc9>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1090 <sscanf@plt>
cmp    $0x1,%eax
jne    1274 <machine_proc_get_more+0xc4>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <proc_get@plt>
cmp    $0x0,%eax
jne    1274 <machine_proc_get_more+0xc4>
mov    -0x1c(%rbp),%edi
call   10c0 <machine_proc_new@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     126f <machine_proc_get_more+0xbf>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <proc_addto@plt>
jmp    1274 <machine_proc_get_more+0xc4>
jmp    11f2 <machine_proc_get_more+0x42>
mov    0x2d58(%rip),%rax
cmpq   $0x0,(%rax)
je     12c1 <machine_proc_get_more+0x111>
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d3b(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xd61(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<machine_proc_get_more>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xe39(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1281 <machine_proc_get_more+0xd1>
mov    %rax,%rbx
mov    0x2df9(%rip),%r13
movq   $0x0,0x0(%r13)
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1262 <machine_proc_get_more+0xb2>
lea    0xe15(%rip),%r15
lea    0xc(%rsp),%r12
jmp    1225 <machine_proc_get_more+0x75>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movq   $0x0,0x0(%r13)
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1262 <machine_proc_get_more+0xb2>
mov    (%rax),%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1090 <sscanf@plt>
cmp    $0x1,%eax
jne    1210 <machine_proc_get_more+0x60>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1030 <proc_get@plt>
test   %eax,%eax
jne    1210 <machine_proc_get_more+0x60>
mov    0xc(%rsp),%edi
call   10c0 <machine_proc_new@plt>
test   %rax,%rax
je     1210 <machine_proc_get_more+0x60>
mov    %r14,%rdi
mov    %rax,%rsi
call   1080 <proc_addto@plt>
jmp    1210 <machine_proc_get_more+0x60>
mov    0x0(%r13),%rdi
test   %rdi,%rdi
jne    1297 <machine_proc_get_more+0xe7>
mov    %rbx,%rdi
call   1050 <closedir@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd7e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%ebx
call   10d0 <strerror@plt>
lea    0xd67(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.myproc = type { i32 }
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [10 x i8] c"opendir()\00"
@errno = global i64 0
@.str.2 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.3 = [15 x i8] c"readdir(): %s\0A\00"
define void @machine_proc_get_more(%struct.myproc** %0) {
%2 = alloca %struct.myproc**
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i32
%6 = alloca %struct.myproc*
store %struct.myproc** %0, %struct.myproc*** %2
%7 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
br label %14
14: ; preds = %39, %13
store i64 0, i64* @errno
%15 = load i32*, i32** %3
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %4
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %40
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %4
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @sscanf(i32 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %39
24: ; preds = %18
%25 = load %struct.myproc**, %struct.myproc*** %2
%26 = load i32, i32* %5
%27 = call i32 @proc_get(%struct.myproc** %25, i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %39, label %29
29: ; preds = %24
%30 = load i32, i32* %5
%31 = call %struct.myproc* @machine_proc_new(i32 %30)
store %struct.myproc* %31, %struct.myproc** %6
%32 = load %struct.myproc*, %struct.myproc** %6
%33 = icmp ne %struct.myproc* %32, null
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load %struct.myproc**, %struct.myproc*** %2
%36 = load %struct.myproc*, %struct.myproc** %6
%37 = call i32 @proc_addto(%struct.myproc** %35, %struct.myproc* %36)
br label %38
38: ; preds = %34, %29
br label %39
39: ; preds = %38, %24, %18
br label %14
40: ; preds = %14
%41 = load i64, i64* @errno
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = load i64, i64* @errno
%46 = call i8* @strerror(i64 %45)
%47 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %40
%50 = load i32*, i32** %3
%51 = call i32 @closedir(i32* %50)
ret void
}

LLVM IR (-O2):
%struct.myproc = type { i32 }
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [10 x i8] c"opendir()\00"
@errno = global i64 0
@.str.2 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.3 = [15 x i8] c"readdir(): %s\0A\00"
define void @machine_proc_get_more(%struct.myproc** %0) {
%2 = alloca i32
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %10, label %5
5: ; preds = %1
store i64 0, i64* @errno
%6 = call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %32, label %8
8: ; preds = %5
%9 = bitcast i32* %2 to i8*
br label %13
10: ; preds = %1
%11 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %8, %29
%14 = phi %struct.dirent* [ %6, %8 ], [ %30, %29 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @sscanf(i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %29
19: ; preds = %13
%20 = load i32, i32* %2
%21 = call i32 @proc_get(%struct.myproc** %0, i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i32, i32* %2
%25 = call %struct.myproc* @machine_proc_new(i32 %24)
%26 = icmp eq %struct.myproc* %25, null
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = call i32 @proc_addto(%struct.myproc** %0, %struct.myproc* %25)
br label %29
29: ; preds = %23, %27, %19, %13
store i64 0, i64* @errno
%30 = call %struct.dirent* @readdir(i32* %3)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %32, label %13
32: ; preds = %29, %5
%33 = load i64, i64* @errno
%34 = icmp eq i64 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %32
%36 = load i32, i32* @stderr
%37 = call i8* @strerror(i64 %33)
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %32
%41 = call i32 @closedir(i32* %3)
ret void
}