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

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ chdir(char const *);
scalar_t__ closedir(int /*<<< orphan*/ *);
scalar_t__ entry_size(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ statfs(char const *, struct statfs *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
unsigned long directory_size(const char *path) {
  struct statfs sfs;
  if (statfs(path, &sfs) != 0) {
    if (errno == EACCES)
      return 0;
    error("directory_size.statfs");
  } else if (sfs.f_type == 0x9fa0)
    return 0;
  if (chdir(path) != 0) {
    if (errno == EACCES)
      return 0;
    error("From directory_size.chdir");
  }
  DIR *dir = opendir(".");
  if (dir == NULL) {
    if (errno == EACCES)
      return 0;
    error("directory_size.opendir");
  }
  struct dirent *ent = NULL;
  unsigned long size = 0;
  while ((ent = readdir(dir)) != NULL) {
    if ((strcmp(ent->d_name, ".") == 0) || (strcmp(ent->d_name, "..") == 0))
      continue;
    size += entry_size(ent->d_name);
  }
  if (chdir("..") != 0)
    error("directory_size.chdir");
  if (closedir(dir) != 0)
    error("directory_size.closedir");
  return size;
}


x86 assembly (clang, -O0):
<directory_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <statfs@plt>
cmp    $0x0,%rax
je     11db <directory_size+0x5b>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
jne    11ca <directory_size+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    1353 <directory_size+0x1d3>
lea    0xe2f(%rip),%rdi
call   10a0 <error@plt>
jmp    11fa <directory_size+0x7a>
cmpl   $0x9fa0,-0x18(%rbp)
jne    11f5 <directory_size+0x75>
movq   $0x0,-0x8(%rbp)
jmp    1353 <directory_size+0x1d3>
jmp    11fa <directory_size+0x7a>
mov    -0x10(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     1240 <directory_size+0xc0>
mov    0x2dc4(%rip),%rax
mov    (%rax),%rax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
jne    1234 <directory_size+0xb4>
movq   $0x0,-0x8(%rbp)
jmp    1353 <directory_size+0x1d3>
lea    0xddb(%rip),%rdi
call   10a0 <error@plt>
lea    0xe01(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    128e <directory_size+0x10e>
mov    0x2d76(%rip),%rax
mov    (%rax),%rax
mov    0x2d7c(%rip),%rcx
cmp    (%rcx),%rax
jne    1282 <directory_size+0x102>
movq   $0x0,-0x8(%rbp)
jmp    1353 <directory_size+0x1d3>
lea    0xda7(%rip),%rdi
call   10a0 <error@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     130a <directory_size+0x18a>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12ed <directory_size+0x16d>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
lea    0xd69(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12f2 <directory_size+0x172>
jmp    129e <directory_size+0x11e>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1030 <entry_size@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    129e <directory_size+0x11e>
lea    0xd36(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     132c <directory_size+0x1ac>
lea    0xcf4(%rip),%rdi
call   10a0 <error@plt>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
cmp    $0x0,%rax
je     134b <directory_size+0x1cb>
lea    0xd04(%rip),%rdi
call   10a0 <error@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<directory_size>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1080 <statfs@plt>
test   %rax,%rax
je     11c3 <directory_size+0x43>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    0x2e38(%rip),%rcx
cmp    (%rcx),%rax
je     1226 <directory_size+0xa6>
lea    0xe44(%rip),%rdi
call   10a0 <error@plt>
jmp    11cd <directory_size+0x4d>
cmpl   $0x9fa0,0x8(%rsp)
je     1226 <directory_size+0xa6>
mov    %rbx,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
je     11fc <directory_size+0x7c>
mov    0x2df7(%rip),%rax
mov    (%rax),%rax
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%rax
je     1226 <directory_size+0xa6>
lea    0xe1f(%rip),%rdi
call   10a0 <error@plt>
lea    0xe45(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1246 <directory_size+0xc6>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rax
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
jne    123a <directory_size+0xba>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdef(%rip),%rdi
call   10a0 <error@plt>
mov    %r13,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12aa <directory_size+0x12a>
mov    %rax,%rbx
xor    %r14d,%r14d
lea    0xde8(%rip),%r15
lea    0xde0(%rip),%r12
jmp    1280 <directory_size+0x100>
nopl   0x0(%rax)
mov    %r13,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12ad <directory_size+0x12d>
mov    (%rbx),%edi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1270 <directory_size+0xf0>
mov    (%rbx),%edi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1270 <directory_size+0xf0>
mov    (%rbx),%edi
call   1030 <entry_size@plt>
add    %rax,%r14
jmp    1270 <directory_size+0xf0>
xor    %r14d,%r14d
lea    0xd93(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
je     12ca <directory_size+0x14a>
lea    0xd56(%rip),%rdi
call   10a0 <error@plt>
mov    %r13,%rdi
call   1060 <closedir@plt>
test   %rax,%rax
je     1229 <directory_size+0xa9>
lea    0xd68(%rip),%rdi
call   10a0 <error@plt>
jmp    1229 <directory_size+0xa9>

LLVM IR (-O0):
%struct.statfs = type { i32 }
%struct.dirent = type { i32 }
@errno = global i64 0
@EACCES = global i64 0
@.str = [22 x i8] c"directory_size.statfs\00"
@.str.1 = [26 x i8] c"From directory_size.chdir\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [23 x i8] c"directory_size.opendir\00"
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [21 x i8] c"directory_size.chdir\00"
@.str.6 = [24 x i8] c"directory_size.closedir\00"
define i64 @directory_size(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca %struct.statfs
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @statfs(i8* %8, %struct.statfs* %4)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EACCES
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %11
store i64 0, i64* %2
br label %84
16: ; preds = %11
%17 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %24
18: ; preds = %1
%19 = getelementptr inbounds %struct.statfs, %struct.statfs* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = icmp eq i32 %20, 40864
br i1 %21, label %22, label %23
22: ; preds = %18
store i64 0, i64* %2
br label %84
23: ; preds = %18
br label %24
24: ; preds = %23, %16
%25 = load i8*, i8** %3
%26 = call i64 @chdir(i8* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @EACCES
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
store i64 0, i64* %2
br label %84
33: ; preds = %28
%34 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %24
%36 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %36, i32** %5
%37 = load i32*, i32** %5
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %46
39: ; preds = %35
%40 = load i64, i64* @errno
%41 = load i64, i64* @EACCES
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %39
store i64 0, i64* %2
br label %84
44: ; preds = %39
%45 = call i32 @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %35
store %struct.dirent* null, %struct.dirent** %6
store i64 0, i64* %7
br label %47
47: ; preds = %64, %63, %46
%48 = load i32*, i32** %5
%49 = call %struct.dirent* @readdir(i32* %48)
store %struct.dirent* %49, %struct.dirent** %6
%50 = icmp ne %struct.dirent* %49, null
br i1 %50, label %51, label %71
51: ; preds = %47
%52 = load %struct.dirent*, %struct.dirent** %6
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i64 @strcmp(i32 %54, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%56 = icmp eq i64 %55, 0
br i1 %56, label %63, label %57
57: ; preds = %51
%58 = load %struct.dirent*, %struct.dirent** %6
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i64 @strcmp(i32 %60, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %57, %51
br label %47
64: ; preds = %57
%65 = load %struct.dirent*, %struct.dirent** %6
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i64 @entry_size(i32 %67)
%69 = load i64, i64* %7
%70 = add i64 %69, %68
store i64 %70, i64* %7
br label %47
71: ; preds = %47
%72 = call i64 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %71
%75 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %76
76: ; preds = %74, %71
%77 = load i32*, i32** %5
%78 = call i64 @closedir(i32* %77)
%79 = icmp ne i64 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %76
%81 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %82
82: ; preds = %80, %76
%83 = load i64, i64* %7
store i64 %83, i64* %2
br label %84
84: ; preds = %82, %43, %32, %22, %15
%85 = load i64, i64* %2
ret i64 %85
}

LLVM IR (-O2):
%struct.statfs = type { i32 }
%struct.dirent = type { i32 }
@errno = global i64 0
@EACCES = global i64 0
@.str = [22 x i8] c"directory_size.statfs\00"
@.str.1 = [26 x i8] c"From directory_size.chdir\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [23 x i8] c"directory_size.opendir\00"
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [21 x i8] c"directory_size.chdir\00"
@.str.6 = [24 x i8] c"directory_size.closedir\00"
define i64 @directory_size(i8* %0) {
%2 = alloca %struct.statfs
%3 = bitcast %struct.statfs* %2 to i8*
%4 = call i64 @statfs(i8* %0, %struct.statfs* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @EACCES
%9 = icmp eq i64 %7, %8
br i1 %9, label %70, label %10
10: ; preds = %6
%11 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %16
12: ; preds = %1
%13 = getelementptr inbounds %struct.statfs, %struct.statfs* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = icmp eq i32 %14, 40864
br i1 %15, label %70, label %16
16: ; preds = %12, %10
%17 = call i64 @chdir(i8* %0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EACCES
%22 = icmp eq i64 %20, %21
br i1 %22, label %70, label %23
23: ; preds = %19
%24 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %16
%26 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @EACCES
%31 = icmp eq i64 %29, %30
br i1 %31, label %70, label %32
32: ; preds = %28
%33 = call i32 @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %25
%35 = call %struct.dirent* @readdir(i32* %26)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %59, label %37
37: ; preds = %34, %53
%38 = phi %struct.dirent* [ %57, %53 ], [ %35, %34 ]
%39 = phi i64 [ %56, %53 ], [ 0, %34 ]
br label %40
40: ; preds = %37, %50
%41 = phi %struct.dirent* [ %38, %37 ], [ %51, %50 ]
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i64 0, i32 0
%43 = load i32, i32* %42
%44 = call i64 @strcmp(i32 %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %40
%47 = load i32, i32* %42
%48 = call i64 @strcmp(i32 %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %46, %40
%51 = call %struct.dirent* @readdir(i32* %26)
%52 = icmp eq %struct.dirent* %51, null
br i1 %52, label %59, label %40
53: ; preds = %46
%54 = load i32, i32* %42
%55 = call i64 @entry_size(i32 %54)
%56 = add i64 %55, %39
%57 = call %struct.dirent* @readdir(i32* %26)
%58 = icmp eq %struct.dirent* %57, null
br i1 %58, label %59, label %37
59: ; preds = %53, %50, %34
%60 = phi i64 [ 0, %34 ], [ %39, %50 ], [ %56, %53 ]
%61 = call i64 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%62 = icmp eq i64 %61, 0
br i1 %62, label %65, label %63
63: ; preds = %59
%64 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %65
65: ; preds = %63, %59
%66 = call i64 @closedir(i32* %26)
%67 = icmp eq i64 %66, 0
br i1 %67, label %70, label %68
68: ; preds = %65
%69 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %70
70: ; preds = %28, %68, %65, %19, %12, %6
%71 = phi i64 [ 0, %6 ], [ 0, %12 ], [ 0, %19 ], [ 0, %28 ], [ %60, %68 ], [ %60, %65 ]
ret i64 %71
}