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

/* Variables and functions */
int chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *getcwd(char *, int);
scalar_t__ isdigit(int /*<<< orphan*/);
int /*<<< orphan*/ mcleanstop(int, char *, ...);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int sscanf(char *, char *, unsigned long *);
unsigned long counttasks(void) {
  unsigned long nr = 0;
  char linebuf[256];
  FILE *fp;
  DIR *dirp;
  struct dirent *entp;
  char origdir[1024];
  if ((fp = fopen("/proc/loadavg", "r")) != NULL) {
    if (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
      if (sscanf(linebuf, "%*f %*f %*f %*d/%lu", &nr) < 1)
        mcleanstop(53, "wrong /proc/loadavg\n");
    } else
      mcleanstop(53, "unreadable /proc/loadavg\n");
    fclose(fp);
  } else
    mcleanstop(53, "can not open /proc/loadavg\n");
  if (getcwd(origdir, sizeof origdir) == NULL)
    mcleanstop(53, "cannot determine cwd\n");
  if (chdir("/proc") == -1)
    mcleanstop(53, "cannot change to /proc\n");
  dirp = opendir(".");
  while ((entp = readdir(dirp))) {
    if (isdigit(entp->d_name[0]))
      nr++;
  }
  closedir(dirp);
  if (chdir(origdir) == -1)
    mcleanstop(53, "cannot change to %s\n", origdir);
  return nr;
}


x86 assembly (clang, -O0):
<counttasks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x530,%rsp
movq   $0x0,-0x8(%rbp)
lea    0xe36(%rip),%rdi
lea    0xe3d(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
je     1265 <counttasks+0xb5>
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rdx
mov    $0x100,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%rax
je     1241 <counttasks+0x91>
lea    -0x110(%rbp),%rdi
lea    0xdf9(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   10c0 <sscanf@plt>
cmp    $0x1,%eax
jge    123c <counttasks+0x8c>
mov    $0x35,%edi
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1030 <mcleanstop@plt>
jmp    1254 <counttasks+0xa4>
mov    $0x35,%edi
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1030 <mcleanstop@plt>
mov    -0x118(%rbp),%rdi
call   1050 <fclose@plt>
jmp    1278 <counttasks+0xc8>
mov    $0x35,%edi
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1030 <mcleanstop@plt>
lea    -0x530(%rbp),%rdi
mov    $0x400,%esi
call   1080 <getcwd@plt>
cmp    $0x0,%rax
jne    12a6 <counttasks+0xf6>
mov    $0x35,%edi
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1030 <mcleanstop@plt>
lea    0xdd8(%rip),%rdi
call   1070 <chdir@plt>
cmp    $0xffffffff,%eax
jne    12ce <counttasks+0x11e>
mov    $0x35,%edi
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   1030 <mcleanstop@plt>
lea    0xdce(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
je     132a <counttasks+0x17a>
mov    -0x128(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
je     1325 <counttasks+0x175>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    12e1 <counttasks+0x131>
mov    -0x120(%rbp),%rdi
call   1090 <closedir@plt>
lea    -0x530(%rbp),%rdi
call   1070 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1365 <counttasks+0x1b5>
lea    -0x530(%rbp),%rdx
mov    $0x35,%edi
lea    0xd47(%rip),%rsi
mov    $0x0,%al
call   1030 <mcleanstop@plt>
mov    -0x8(%rbp),%rax
add    $0x530,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<counttasks>:
push   %rbx
sub    $0x510,%rsp
movq   $0x0,0x8(%rsp)
lea    0xe38(%rip),%rdi
lea    0xe3f(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1216 <counttasks+0x66>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     122b <counttasks+0x7b>
lea    0xe16(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rdx
call   10c0 <sscanf@plt>
test   %eax,%eax
jg     123e <counttasks+0x8e>
lea    0xe10(%rip),%rsi
jmp    1232 <counttasks+0x82>
lea    0xe36(%rip),%rsi
mov    $0x35,%edi
xor    %eax,%eax
call   1030 <mcleanstop@plt>
jmp    1246 <counttasks+0x96>
lea    0xe07(%rip),%rsi
mov    $0x35,%edi
xor    %eax,%eax
call   1030 <mcleanstop@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0x110(%rsp),%rdi
mov    $0x400,%esi
call   1080 <getcwd@plt>
test   %rax,%rax
jne    1270 <counttasks+0xc0>
lea    0xe0b(%rip),%rsi
mov    $0x35,%edi
xor    %eax,%eax
call   1030 <mcleanstop@plt>
lea    0xe0e(%rip),%rdi
call   1070 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1294 <counttasks+0xe4>
lea    0xe03(%rip),%rsi
mov    $0x35,%edi
xor    %eax,%eax
call   1030 <mcleanstop@plt>
lea    0xe08(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
jmp    12b3 <counttasks+0x103>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12d4 <counttasks+0x124>
mov    (%rax),%rax
mov    (%rax),%edi
call   1040 <isdigit@plt>
test   %rax,%rax
je     12b0 <counttasks+0x100>
addq   $0x1,0x8(%rsp)
jmp    12b0 <counttasks+0x100>
mov    %rbx,%rdi
call   1090 <closedir@plt>
lea    0x110(%rsp),%rdi
call   1070 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1309 <counttasks+0x159>
lea    0xdb0(%rip),%rsi
lea    0x110(%rsp),%rdx
mov    $0x35,%edi
xor    %eax,%eax
call   1030 <mcleanstop@plt>
mov    0x8(%rsp),%rax
add    $0x510,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.dirent = type { i32* }
@.str = [14 x i8] c"/proc/loadavg\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"%*f %*f %*f %*d/%lu\00"
@.str.3 = [21 x i8] c"wrong /proc/loadavg\0A\00"
@.str.4 = [26 x i8] c"unreadable /proc/loadavg\0A\00"
@.str.5 = [28 x i8] c"can not open /proc/loadavg\0A\00"
@.str.6 = [22 x i8] c"cannot determine cwd\0A\00"
@.str.7 = [6 x i8] c"/proc\00"
@.str.8 = [24 x i8] c"cannot change to /proc\0A\00"
@.str.9 = [2 x i8] c".\00"
@.str.10 = [21 x i8] c"cannot change to %s\0A\00"
define i64 @counttasks() {
%1 = alloca i64
%2 = alloca [256 x i8]
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca [1024 x i8]
store i64 0, i64* %1
%7 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %26
9: ; preds = %0
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%11 = load i32*, i32** %3
%12 = call i32* @fgets(i8* %10, i32 256, i32* %11)
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %21
14: ; preds = %9
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%16 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 (i32, i8*, ...) @mcleanstop(i32 53, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
br label %23
21: ; preds = %9
%22 = call i32 (i32, i8*, ...) @mcleanstop(i32 53, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %23
23: ; preds = %21, %20
%24 = load i32*, i32** %3
%25 = call i32 @fclose(i32* %24)
br label %28
26: ; preds = %0
%27 = call i32 (i32, i8*, ...) @mcleanstop(i32 53, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%30 = call i32* @getcwd(i8* %29, i32 1024)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 (i32, i8*, ...) @mcleanstop(i32 53, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = call i32 @chdir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 (i32, i8*, ...) @mcleanstop(i32 53, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
store i32* %40, i32** %4
br label %41
41: ; preds = %56, %39
%42 = load i32*, i32** %4
%43 = call %struct.dirent* @readdir(i32* %42)
store %struct.dirent* %43, %struct.dirent** %5
%44 = icmp ne %struct.dirent* %43, null
br i1 %44, label %45, label %57
45: ; preds = %41
%46 = load %struct.dirent*, %struct.dirent** %5
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i32*, i32** %47
%49 = getelementptr inbounds i32, i32* %48, i64 0
%50 = load i32, i32* %49
%51 = call i64 @isdigit(i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %45
%54 = load i64, i64* %1
%55 = add i64 %54, 1
store i64 %55, i64* %1
br label %56
56: ; preds = %53, %45
br label %41
57: ; preds = %41
%58 = load i32*, i32** %4
%59 = call i32 @closedir(i32* %58)
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%61 = call i32 @chdir(i8* %60)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %66
63: ; preds = %57
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%65 = call i32 (i32, i8*, ...) @mcleanstop(i32 53, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0), i8* %64)
br label %66
66: ; preds = %63, %57
%67 = load i64, i64* %1
ret i64 %67
}

LLVM IR (-O2):
%struct.dirent = type { i32* }
@.str = [14 x i8] c"/proc/loadavg\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"%*f %*f %*f %*d/%lu\00"
@.str.3 = [21 x i8] c"wrong /proc/loadavg\0A\00"
@.str.4 = [26 x i8] c"unreadable /proc/loadavg\0A\00"
@.str.5 = [28 x i8] c"can not open /proc/loadavg\0A\00"
@.str.6 = [22 x i8] c"cannot determine cwd\0A\00"
@.str.7 = [6 x i8] c"/proc\00"
@.str.8 = [24 x i8] c"cannot change to /proc\0A\00"
@.str.9 = [2 x i8] c".\00"
@.str.10 = [21 x i8] c"cannot change to %s\0A\00"
define i64 @counttasks() {
%1 = alloca i64
%2 = alloca [256 x i8]
%3 = alloca [1024 x i8]
%4 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %20, label %9
9: ; preds = %0
%10 = call i32* @fgets(i8* %5, i32 256, i32* %7)
%11 = icmp eq i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %18
15: ; preds = %9, %12
%16 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), %12 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), %9 ]
%17 = call i32 (i32, i8*, ...) @mcleanstop(i32 53, i8* %16)
br label %18
18: ; preds = %15, %12
%19 = call i32 @fclose(i32* %7)
br label %22
20: ; preds = %0
%21 = tail call i32 (i32, i8*, ...) @mcleanstop(i32 53, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %22
22: ; preds = %20, %18
%23 = call i32* @getcwd(i8* %6, i32 1024)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 (i32, i8*, ...) @mcleanstop(i32 53, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = call i32 @chdir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 (i32, i8*, ...) @mcleanstop(i32 53, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%34 = call %struct.dirent* @readdir(i32* %33)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %49, label %36
36: ; preds = %32, %46
%37 = phi %struct.dirent* [ %47, %46 ], [ %34, %32 ]
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i64 0, i32 0
%39 = load i32*, i32** %38
%40 = load i32, i32* %39
%41 = call i64 @isdigit(i32 %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %36
%44 = load i64, i64* %1
%45 = add i64 %44, 1
store i64 %45, i64* %1
br label %46
46: ; preds = %43, %36
%47 = call %struct.dirent* @readdir(i32* %33)
%48 = icmp eq %struct.dirent* %47, null
br i1 %48, label %49, label %36
49: ; preds = %46, %32
%50 = call i32 @closedir(i32* %33)
%51 = call i32 @chdir(i8* %6)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 (i32, i8*, ...) @mcleanstop(i32 53, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0), i8* %6)
br label %55
55: ; preds = %53, %49
%56 = load i64, i64* %1
ret i64 %56
}