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

/* Variables and functions */
#define DT_DIR 128
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ chdir(char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int fchdir(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
char *getcwd(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
scalar_t__ readdir_r(int /*<<< orphan*/ *, struct dirent *, struct dirent **);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
off_t totaldiratpath(const char *path) {
  off_t size = 0;
  DIR *stream = opendir(path);
  if (!stream) {
    perror("Couldn't open directory");
    return -1;
  }
  int olddirfd = open(".", O_RDONLY);
  chdir(path);
  static unsigned depth = 0;
  struct dirent entry;
  struct dirent *result = NULL;
  while ((readdir_r(stream, &entry, &result) == 0) && result) {
    if (strcmp(entry.d_name, ".") == 0)
      continue;
    if (strcmp(entry.d_name, "..") == 0)
      continue;
    ;
    switch (entry.d_type) {
    case DT_DIR:
      ++depth;
      ;
      size += totaldiratpath(entry.d_name);
      ;
      --depth;
      break;
    default: {
      struct stat sb;
      int stat_result = stat(entry.d_name, &sb);
      if (stat_result == 0) {
        size += sb.st_size;
      } else {
        perror("Could not stat");
        char cwd[PATH_MAX];
        fprintf(stderr, "%s/%s\n", getcwd(cwd, PATH_MAX), entry.d_name);
      }
    } break;
    }
  }
  int fchdir_result = fchdir(olddirfd);
  if (fchdir_result != 0)
    perror("Could not restore current directory");
  closedir(stream);
  return size;
}


x86 assembly (clang, -O0):
<totaldiratpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1203 <totaldiratpath+0x43>
lea    0xe0e(%rip),%rdi
call   10e0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ba <totaldiratpath+0x1fa>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
lea    0xe06(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <chdir@plt>
movq   $0x0,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1040 <readdir_r@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x65(%rbp)
jne    125a <totaldiratpath+0x9a>
cmpq   $0x0,-0x40(%rbp)
setne  %al
mov    %al,-0x65(%rbp)
mov    -0x65(%rbp),%al
test   $0x1,%al
jne    126a <totaldiratpath+0xaa>
jmp    138a <totaldiratpath+0x1ca>
mov    -0x38(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1289 <totaldiratpath+0xc9>
jmp    122c <totaldiratpath+0x6c>
mov    -0x38(%rbp),%rdi
lea    0xd84(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    12a8 <totaldiratpath+0xe8>
jmp    122c <totaldiratpath+0x6c>
mov    -0x30(%rbp),%eax
sub    $0x80,%eax
jne    12ed <totaldiratpath+0x12d>
jmp    12bb <totaldiratpath+0xfb>
mov    0x2dc7(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2dbe(%rip)
mov    -0x38(%rbp),%rdi
call   1030 <totaldiratpath@plt>
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2da9(%rip),%eax
add    $0xffffffff,%eax
mov    %eax,0x2da0(%rip)
jmp    1385 <totaldiratpath+0x1c5>
mov    -0x38(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   10b0 <stat@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jne    1315 <totaldiratpath+0x155>
mov    -0x48(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1380 <totaldiratpath+0x1c0>
lea    0xcff(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,%rsp
mov    %rax,-0x60(%rbp)
mov    0x2c80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    0x2c94(%rip),%rax
mov    (%rax),%esi
call   1070 <getcwd@plt>
mov    -0x6c(%rbp),%edi
mov    %rax,%rdx
mov    -0x38(%rbp),%rcx
lea    0xcb6(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
jmp    1385 <totaldiratpath+0x1c5>
jmp    122c <totaldiratpath+0x6c>
mov    -0x24(%rbp),%edi
call   10c0 <fchdir@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
je     13ab <totaldiratpath+0x1eb>
lea    0xc8b(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<totaldiratpath>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%r14
call   1050 <opendir@plt>
test   %rax,%rax
je     136f <totaldiratpath+0x1af>
mov    %rax,%rbx
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0xe24(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1060 <chdir@plt>
movq   $0x0,-0x38(%rbp)
lea    -0x50(%rbp),%rsi
lea    -0x38(%rbp),%rdx
mov    %rbx,%rdi
call   1040 <readdir_r@plt>
test   %rax,%rax
mov    %r15d,-0x58(%rbp)
jne    1342 <totaldiratpath+0x182>
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1349 <totaldiratpath+0x189>
movl   $0x0,-0x2c(%rbp)
lea    0xdcf(%rip),%r12
lea    -0x50(%rbp),%r13
lea    -0x38(%rbp),%r15
lea    0xdbf(%rip),%r14
nopl   0x0(%rax)
mov    -0x50(%rbp),%rdi
mov    %r12,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1282 <totaldiratpath+0xc2>
mov    -0x50(%rbp),%rdi
mov    %r14,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    12a5 <totaldiratpath+0xe5>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   1040 <readdir_r@plt>
test   %rax,%rax
jne    1349 <totaldiratpath+0x189>
cmpq   $0x0,-0x38(%rbp)
jne    1260 <totaldiratpath+0xa0>
jmp    1349 <totaldiratpath+0x189>
cmpl   $0x80,-0x48(%rbp)
jne    12ca <totaldiratpath+0x10a>
addl   $0x1,0x2dd3(%rip)
mov    -0x50(%rbp),%rdi
call   1030 <totaldiratpath@plt>
add    %eax,-0x2c(%rbp)
addl   $0xffffffff,0x2dc0(%rip)
jmp    1282 <totaldiratpath+0xc2>
mov    -0x50(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   10b0 <stat@plt>
test   %eax,%eax
jne    12e6 <totaldiratpath+0x126>
mov    -0x2c(%rbp),%eax
add    -0x40(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    1282 <totaldiratpath+0xc2>
lea    0xd2e(%rip),%rdi
call   10e0 <perror@plt>
mov    %rsp,-0x60(%rbp)
mov    0x2cf3(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%rdi
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rdi
mov    %rdi,%rsp
mov    0x2cb9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    (%rcx),%esi
call   1070 <getcwd@plt>
mov    -0x50(%rbp),%rcx
mov    -0x54(%rbp),%edi
lea    0xcf9(%rip),%rsi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
mov    -0x60(%rbp),%rsp
jmp    1282 <totaldiratpath+0xc2>
movl   $0x0,-0x2c(%rbp)
mov    -0x58(%rbp),%edi
call   10c0 <fchdir@plt>
test   %eax,%eax
jne    1382 <totaldiratpath+0x1c2>
mov    %rbx,%rdi
call   1080 <closedir@plt>
mov    -0x2c(%rbp),%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc8a(%rip),%rdi
call   10e0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1360 <totaldiratpath+0x1a0>
lea    0xca8(%rip),%rdi
call   10e0 <perror@plt>
jmp    1355 <totaldiratpath+0x195>

LLVM IR (-O0):
%struct.dirent = type { i8*, i32 }
%struct.stat = type { i32 }
@.str = [24 x i8] c"Couldn't open directory\00"
@.str.1 = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@totaldiratpath.depth = internal global i32 0
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [15 x i8] c"Could not stat\00"
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str.4 = [7 x i8] c"%s/%s\0A\00"
@.str.5 = [36 x i8] c"Could not restore current directory\00"
define i32 @totaldiratpath(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.dirent
%8 = alloca %struct.dirent*
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%14 = load i8*, i8** %3
%15 = call i32* @opendir(i8* %14)
store i32* %15, i32** %5
%16 = load i32*, i32** %5
%17 = icmp ne i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %1
%19 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %96
20: ; preds = %1
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %21)
store i32 %22, i32* %6
%23 = load i8*, i8** %3
%24 = call i32 @chdir(i8* %23)
store %struct.dirent* null, %struct.dirent** %8
br label %25
25: ; preds = %84, %45, %39, %20
%26 = load i32*, i32** %5
%27 = call i64 @readdir_r(i32* %26, %struct.dirent* %7, %struct.dirent** %8)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load %struct.dirent*, %struct.dirent** %8
%31 = icmp ne %struct.dirent* %30, null
br label %32
32: ; preds = %29, %25
%33 = phi i1 [ false, %25 ], [ %31, %29 ]
br i1 %33, label %34, label %85
34: ; preds = %32
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
br label %25
40: ; preds = %34
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %40
br label %25
46: ; preds = %40
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i32 0, i32 1
%48 = load i32, i32* %47
switch i32 %48, label %59 [
i32 128, label %49
]
49: ; preds = %46
%50 = load i32, i32* @totaldiratpath.depth
%51 = add i32 %50, 1
store i32 %51, i32* @totaldiratpath.depth
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i32 0, i32 0
%53 = load i8*, i8** %52
%54 = call i32 @totaldiratpath(i8* %53)
%55 = load i32, i32* %4
%56 = add nsw i32 %55, %54
store i32 %56, i32* %4
%57 = load i32, i32* @totaldiratpath.depth
%58 = add i32 %57, -1
store i32 %58, i32* @totaldiratpath.depth
br label %84
59: ; preds = %46
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i32 @stat(i8* %61, %struct.stat* %9)
store i32 %62, i32* %10
%63 = load i32, i32* %10
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %70
65: ; preds = %59
%66 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%67 = load i32, i32* %66
%68 = load i32, i32* %4
%69 = add nsw i32 %68, %67
store i32 %69, i32* %4
br label %83
70: ; preds = %59
%71 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%72 = load i32, i32* @PATH_MAX
%73 = zext i32 %72 to i64
%74 = call i8* @llvm.stacksave()
store i8* %74, i8** %11
%75 = alloca i8, i64 %73
store i64 %73, i64* %12
%76 = load i32, i32* @stderr
%77 = load i32, i32* @PATH_MAX
%78 = call i8* @getcwd(i8* %75, i32 %77)
%79 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i32 0, i32 0
%80 = load i8*, i8** %79
%81 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %78, i8* %80)
%82 = load i8*, i8** %11
br label %83
83: ; preds = %70, %65
br label %84
84: ; preds = %83, %49
br label %25
85: ; preds = %32
%86 = load i32, i32* %6
%87 = call i32 @fchdir(i32 %86)
store i32 %87, i32* %13
%88 = load i32, i32* %13
%89 = icmp ne i32 %88, 0
br i1 %89, label %90, label %92
90: ; preds = %85
%91 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %92
92: ; preds = %90, %85
%93 = load i32*, i32** %5
%94 = call i32 @closedir(i32* %93)
%95 = load i32, i32* %4
store i32 %95, i32* %2
br label %96
96: ; preds = %92, %18
%97 = load i32, i32* %2
ret i32 %97
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i32 }
%struct.stat = type { i32 }
@.str = [24 x i8] c"Couldn't open directory\00"
@.str.1 = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@totaldiratpath.depth = internal unnamed_addr global i32 0
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [15 x i8] c"Could not stat\00"
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str.4 = [7 x i8] c"%s/%s\0A\00"
@.str.5 = [36 x i8] c"Could not restore current directory\00"
define i32 @totaldiratpath(i8* %0) {
%2 = alloca %struct.dirent
%3 = alloca %struct.dirent*
%4 = alloca %struct.stat
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %87
9: ; preds = %1
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = tail call i32 @chdir(i8* %0)
%13 = bitcast %struct.dirent* %2 to i8*
%14 = bitcast %struct.dirent** %3 to i8*
store %struct.dirent* null, %struct.dirent** %3
%15 = call i64 @readdir_r(i32* %5, %struct.dirent* %2, %struct.dirent** %3)
%16 = icmp eq i64 %15, 0
%17 = load %struct.dirent*, %struct.dirent** %3
%18 = icmp ne %struct.dirent* %17, null
%19 = select i1 %16, i1 %18, i1 false
br i1 %19, label %20, label %79
20: ; preds = %9
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %2, i64 0, i32 0
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %2, i64 0, i32 1
%23 = bitcast %struct.stat* %4 to i8*
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %25
25: ; preds = %20, %72
%26 = phi i32 [ 0, %20 ], [ %73, %72 ]
br label %27
27: ; preds = %25, %31
%28 = load i8*, i8** %21
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %27, %37
%32 = call i64 @readdir_r(i32* %5, %struct.dirent* %2, %struct.dirent** %3)
%33 = icmp eq i64 %32, 0
%34 = load %struct.dirent*, %struct.dirent** %3
%35 = icmp ne %struct.dirent* %34, null
%36 = select i1 %33, i1 %35, i1 false
br i1 %36, label %27, label %79
37: ; preds = %27
%38 = load i8*, i8** %21
%39 = call i64 @strcmp(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %31, label %41
41: ; preds = %37
%42 = load i32, i32* %22
%43 = icmp eq i32 %42, 128
br i1 %43, label %44, label %52
44: ; preds = %41
%45 = load i32, i32* @totaldiratpath.depth
%46 = add i32 %45, 1
store i32 %46, i32* @totaldiratpath.depth
%47 = load i8*, i8** %21
%48 = call i32 @totaldiratpath(i8* %47)
%49 = add nsw i32 %48, %26
%50 = load i32, i32* @totaldiratpath.depth
%51 = add i32 %50, -1
store i32 %51, i32* @totaldiratpath.depth
br label %72
52: ; preds = %41
%53 = load i8*, i8** %21
%54 = call i32 @stat(i8* %53, %struct.stat* %4)
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = load i32, i32* %24
%58 = add nsw i32 %57, %26
br label %70
59: ; preds = %52
%60 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%61 = load i32, i32* @PATH_MAX
%62 = zext i32 %61 to i64
%63 = call i8* @llvm.stacksave()
%64 = alloca i8, i64 %62
%65 = load i32, i32* @stderr
%66 = load i32, i32* @PATH_MAX
%67 = call i8* @getcwd(i8* %64, i32 %66)
%68 = load i8*, i8** %21
%69 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %67, i8* %68)
br label %70
70: ; preds = %59, %56
%71 = phi i32 [ %58, %56 ], [ %26, %59 ]
br label %72
72: ; preds = %70, %44
%73 = phi i32 [ %49, %44 ], [ %71, %70 ]
%74 = call i64 @readdir_r(i32* %5, %struct.dirent* %2, %struct.dirent** %3)
%75 = icmp eq i64 %74, 0
%76 = load %struct.dirent*, %struct.dirent** %3
%77 = icmp ne %struct.dirent* %76, null
%78 = select i1 %75, i1 %77, i1 false
br i1 %78, label %25, label %79
79: ; preds = %72, %31, %9
%80 = phi i32 [ 0, %9 ], [ %26, %31 ], [ %73, %72 ]
%81 = call i32 @fchdir(i32 %11)
%82 = icmp eq i32 %81, 0
br i1 %82, label %85, label %83
83: ; preds = %79
%84 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %85
85: ; preds = %83, %79
%86 = call i32 @closedir(i32* %5)
br label %87
87: ; preds = %85, %7
%88 = phi i32 [ %80, %85 ], [ -1, %7 ]
ret i32 %88
}