#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct stat {
  scalar_t__ st_blocks;
  int /*<<< orphan*/ st_mode;
};
typedef scalar_t__ int64_t;
struct TYPE_3__ {
  int gl_pathc;
  char **gl_pathv;
};
typedef TYPE_1__ glob_t;

/* Variables and functions */
int /*<<< orphan*/ GLOB_APPEND;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ glob(char *, int /*<<< orphan*/, int /*<<< orphan*/ *,
                        TYPE_1__ *);
scalar_t__ path_noloop(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int64_t mydu(const char *path) {
  int i;
  struct stat statres;
  int64_t sum = 0;
  char nextpath[1024];
  glob_t globres;
  if (stat(path, &statres) < 0) {
    perror("stat()");
    exit(1);
  }
  if (!S_ISDIR(statres.st_mode))
    return statres.st_blocks;
  strncpy(nextpath, path, 1024);
  strncat(nextpath, "/*", 1024);
  glob(nextpath, 0, NULL, &globres);
  strncpy(nextpath, path, 1024);
  strncat(nextpath, "/.*", 1024);
  glob(nextpath, GLOB_APPEND, NULL, &globres);
  for (i = 0; i < globres.gl_pathc; i++) {
    if (path_noloop(globres.gl_pathv[i]))
      sum += mydu(globres.gl_pathv[i]);
  }
  sum += statres.st_blocks;
  return sum;
}


x86 assembly (clang, -O0):
<mydu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    11d4 <mydu+0x44>
lea    0xe3b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11f2 <mydu+0x62>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12fc <mydu+0x16c>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
lea    -0x430(%rbp),%rdi
lea    0xdf2(%rip),%rsi
mov    $0x400,%edx
call   1060 <strncat@plt>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x440(%rbp),%rcx
call   10a0 <glob@plt>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
lea    -0x430(%rbp),%rdi
lea    0xdaf(%rip),%rsi
mov    $0x400,%edx
call   1060 <strncat@plt>
lea    -0x430(%rbp),%rdi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x440(%rbp),%rcx
call   10a0 <glob@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x440(%rbp),%eax
jge    12e8 <mydu+0x158>
mov    -0x438(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1070 <path_noloop@plt>
cmp    $0x0,%rax
je     12d5 <mydu+0x145>
mov    -0x438(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <mydu@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    12da <mydu+0x14a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    128c <mydu+0xfc>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mydu>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x420,%rsp
mov    %rdi,%r14
lea    0x10(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
js     12a3 <mydu+0x113>
mov    0x18(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     128a <mydu+0xfa>
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
lea    0xe24(%rip),%rsi
mov    %r15,%rdi
mov    $0x400,%edx
call   1060 <strncat@plt>
xor    %r13d,%r13d
mov    %rsp,%r12
mov    %r15,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %r12,%rcx
call   10a0 <glob@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
lea    0xdee(%rip),%rsi
mov    %r15,%rdi
mov    $0x400,%edx
call   1060 <strncat@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
xor    %edx,%edx
mov    %r12,%rcx
call   10a0 <glob@plt>
cmpl   $0x0,(%rsp)
jle    1283 <mydu+0xf3>
xor    %ebx,%ebx
xor    %r13d,%r13d
jmp    125d <mydu+0xcd>
nopl   0x0(%rax)
add    $0x1,%rbx
movslq (%rsp),%rax
cmp    %rax,%rbx
jge    1283 <mydu+0xf3>
mov    0x8(%rsp),%rax
mov    (%rax,%rbx,8),%rdi
call   1070 <path_noloop@plt>
test   %rax,%rax
je     1250 <mydu+0xc0>
mov    0x8(%rsp),%rax
mov    (%rax,%rbx,8),%rdi
call   1040 <mydu@plt>
add    %rax,%r13
jmp    1250 <mydu+0xc0>
add    0x10(%rsp),%r13
jmp    128f <mydu+0xff>
mov    0x10(%rsp),%r13
mov    %r13,%rax
add    $0x420,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd56(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
%struct.TYPE_3__ = type { i32, i8** }
@.str = [7 x i8] c"stat()\00"
@.str.1 = [3 x i8] c"/*\00"
@.str.2 = [4 x i8] c"/.*\00"
@GLOB_APPEND = global i32 0
define i64 @mydu(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i64
%7 = alloca [1024 x i8]
%8 = alloca %struct.TYPE_3__
store i8* %0, i8** %3
store i64 0, i64* %6
%9 = load i8*, i8** %3
%10 = call i64 @stat(i8* %9, %struct.stat* %5)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%17 = load i32, i32* %16
%18 = call i32 @S_ISDIR(i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%22 = load i64, i64* %21
store i64 %22, i64* %2
br label %73
23: ; preds = %15
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%25 = load i8*, i8** %3
%26 = call i32 @strncpy(i8* %24, i8* %25, i32 1024)
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%28 = call i32 @strncat(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 1024)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%30 = call i32 @glob(i8* %29, i32 0, i32* null, %struct.TYPE_3__* %8)
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%32 = load i8*, i8** %3
%33 = call i32 @strncpy(i8* %31, i8* %32, i32 1024)
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%35 = call i32 @strncat(i8* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 1024)
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* @GLOB_APPEND
%38 = call i32 @glob(i8* %36, i32 %37, i32* null, %struct.TYPE_3__* %8)
store i32 0, i32* %4
br label %39
39: ; preds = %64, %23
%40 = load i32, i32* %4
%41 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i32 0, i32 0
%42 = load i32, i32* %41
%43 = icmp slt i32 %40, %42
br i1 %43, label %44, label %67
44: ; preds = %39
%45 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i32 0, i32 1
%46 = load i8**, i8*** %45
%47 = load i32, i32* %4
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8*, i8** %46, i64 %48
%50 = load i8*, i8** %49
%51 = call i64 @path_noloop(i8* %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %63
53: ; preds = %44
%54 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i32 0, i32 1
%55 = load i8**, i8*** %54
%56 = load i32, i32* %4
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8*, i8** %55, i64 %57
%59 = load i8*, i8** %58
%60 = call i64 @mydu(i8* %59)
%61 = load i64, i64* %6
%62 = add nsw i64 %61, %60
store i64 %62, i64* %6
br label %63
63: ; preds = %53, %44
br label %64
64: ; preds = %63
%65 = load i32, i32* %4
%66 = add nsw i32 %65, 1
store i32 %66, i32* %4
br label %39
67: ; preds = %39
%68 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%69 = load i64, i64* %68
%70 = load i64, i64* %6
%71 = add nsw i64 %70, %69
store i64 %71, i64* %6
%72 = load i64, i64* %6
store i64 %72, i64* %2
br label %73
73: ; preds = %67, %20
%74 = load i64, i64* %2
ret i64 %74
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
%struct.TYPE_3__ = type { i32, i8** }
@.str = [7 x i8] c"stat()\00"
@.str.1 = [3 x i8] c"/*\00"
@.str.2 = [4 x i8] c"/.*\00"
@GLOB_APPEND = global i32 0
define i64 @mydu(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [1024 x i8]
%4 = alloca %struct.TYPE_3__
%5 = bitcast %struct.stat* %2 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.TYPE_3__* %4 to i8*
%8 = call i64 @stat(i8* %0, %struct.stat* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%15 = load i32, i32* %14
%16 = call i32 @S_ISDIR(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%20 = load i64, i64* %19
br label %59
21: ; preds = %13
%22 = call i32 @strncpy(i8* %6, i8* %0, i32 1024)
%23 = call i32 @strncat(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 1024)
%24 = call i32 @glob(i8* %6, i32 0, i32* null, %struct.TYPE_3__* %4)
%25 = call i32 @strncpy(i8* %6, i8* %0, i32 1024)
%26 = call i32 @strncat(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 1024)
%27 = load i32, i32* @GLOB_APPEND
%28 = call i32 @glob(i8* %6, i32 %27, i32* null, %struct.TYPE_3__* %4)
%29 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i64 0, i32 0
%30 = load i32, i32* %29
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %54
32: ; preds = %21
%33 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i64 0, i32 1
br label %34
34: ; preds = %32, %48
%35 = phi i64 [ 0, %32 ], [ %50, %48 ]
%36 = phi i64 [ 0, %32 ], [ %49, %48 ]
%37 = load i8**, i8*** %33
%38 = getelementptr inbounds i8*, i8** %37, i64 %35
%39 = load i8*, i8** %38
%40 = call i64 @path_noloop(i8* %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %48, label %42
42: ; preds = %34
%43 = load i8**, i8*** %33
%44 = getelementptr inbounds i8*, i8** %43, i64 %35
%45 = load i8*, i8** %44
%46 = call i64 @mydu(i8* %45)
%47 = add nsw i64 %46, %36
br label %48
48: ; preds = %34, %42
%49 = phi i64 [ %47, %42 ], [ %36, %34 ]
%50 = add nuw nsw i64 %35, 1
%51 = load i32, i32* %29
%52 = sext i32 %51 to i64
%53 = icmp slt i64 %50, %52
br i1 %53, label %34, label %54
54: ; preds = %48, %21
%55 = phi i64 [ 0, %21 ], [ %49, %48 ]
%56 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%57 = load i64, i64* %56
%58 = add nsw i64 %57, %55
br label %59
59: ; preds = %54, %18
%60 = phi i64 [ %58, %54 ], [ %20, %18 ]
ret i64 %60
}