#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 */
typedef int /*<<< orphan*/ tmp;
struct stat {
  int st_blocks;
};
struct dirent {
  char const *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ printf(char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewinddir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, ...);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char const *, char *);
char *strerror(int /*<<< orphan*/);
int show_total_size(const char *dirname, DIR *d, int flags) {
  struct dirent *de;
  char tmp[1024];
  struct stat s;
  int sum = 0;
  while ((de = readdir(d)) != 0) {
    if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
      continue;
    if (de->d_name[0] == '.' && (flags & (1 << 1)) == 0)
      continue;
    if (strcmp(dirname, "/") == 0)
      snprintf(tmp, sizeof(tmp), "/%s", de->d_name);
    else
      snprintf(tmp, sizeof(tmp), "%s/%s", dirname, de->d_name);
    if (lstat(tmp, &s) < 0) {
      fprintf(stderr, "stat failed on %s: %s\n", tmp, strerror(errno));
      rewinddir(d);
      return -1;
    }
    sum += s.st_blocks / 2;
  }
  printf("total %d\n", sum);
  rewinddir(d);
  return 0;
}


x86 assembly (clang, -O0):
<show_total_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x43c(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     131a <show_total_size+0x19a>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xe3c(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     11f1 <show_total_size+0x71>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1e(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    11f6 <show_total_size+0x76>
jmp    11a0 <show_total_size+0x20>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    121d <show_total_size+0x9d>
mov    -0x1c(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
jne    121d <show_total_size+0x9d>
jmp    11a0 <show_total_size+0x20>
mov    -0x10(%rbp),%rdi
lea    0xddb(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    125d <show_total_size+0xdd>
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
mov    $0x400,%esi
lea    0xdb6(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    1282 <show_total_size+0x102>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xd8a(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0x430(%rbp),%rdi
lea    -0x438(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
jge    12fb <show_total_size+0x17b>
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44c(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x44c(%rbp),%edi
mov    -0x448(%rbp),%rdx
mov    %rax,%rcx
lea    0xd2a(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <rewinddir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133c <show_total_size+0x1bc>
mov    -0x438(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    -0x43c(%rbp),%eax
mov    %eax,-0x43c(%rbp)
jmp    11a0 <show_total_size+0x20>
mov    -0x43c(%rbp),%esi
lea    0xcfb(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <rewinddir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<show_total_size>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %edx,%r13d
mov    %rsi,%rbx
mov    %rdi,0x8(%rsp)
mov    %rsi,%rdi
call   1090 <readdir@plt>
mov    $0x0,%r12d
test   %rax,%rax
je     1282 <show_total_size+0x102>
mov    %rax,%r14
lea    0xe44(%rip),%rbp
lea    0xe3c(%rip),%r15
xor    %r12d,%r12d
jmp    11e4 <show_total_size+0x64>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1282 <show_total_size+0x102>
mov    (%r14),%rdi
mov    %rbp,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11d0 <show_total_size+0x50>
mov    (%r14),%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11d0 <show_total_size+0x50>
test   $0x2,%r13b
jne    1212 <show_total_size+0x92>
mov    (%r14),%rax
cmpb   $0x2e,(%rax)
je     11d0 <show_total_size+0x50>
mov    0x8(%rsp),%rdi
lea    0xde5(%rip),%rsi
call   1070 <strcmp@plt>
mov    (%r14),%r8
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x400,%esi
test   %rax,%rax
je     124d <show_total_size+0xcd>
lea    0xdc6(%rip),%rdx
mov    0x8(%rsp),%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
jmp    125e <show_total_size+0xde>
lea    0xdb3(%rip),%rdx
mov    %r8,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %r14,%rdi
mov    %rsp,%rsi
call   1060 <lstat@plt>
test   %rax,%rax
js     12af <show_total_size+0x12f>
mov    (%rsp),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
add    %ecx,%r12d
jmp    11d0 <show_total_size+0x50>
lea    0xd99(%rip),%rdi
mov    %r12d,%esi
call   1030 <printf@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1050 <rewinddir@plt>
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d2a(%rip),%rax
mov    (%rax),%ebp
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd3e(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1293 <show_total_size+0x113>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [4 x i8] c"/%s\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.5 = [23 x i8] c"stat failed on %s: %s\0A\00"
@errno = global i32 0
@.str.6 = [10 x i8] c"total %d\0A\00"
define i32 @show_total_size(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca %struct.dirent*
%9 = alloca [1024 x i8]
%10 = alloca %struct.stat
%11 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 0, i32* %11
br label %12
12: ; preds = %71, %41, %28, %3
%13 = load i32*, i32** %6
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %8
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %77
16: ; preds = %12
%17 = load %struct.dirent*, %struct.dirent** %8
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %16
%23 = load %struct.dirent*, %struct.dirent** %8
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22, %16
br label %12
29: ; preds = %22
%30 = load %struct.dirent*, %struct.dirent** %8
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = getelementptr inbounds i8, i8* %32, i64 0
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 46
br i1 %36, label %37, label %42
37: ; preds = %29
%38 = load i32, i32* %7
%39 = and i32 %38, 2
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %37
br label %12
42: ; preds = %37, %29
%43 = load i8*, i8** %5
%44 = call i64 @strcmp(i8* %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 %46, label %52
46: ; preds = %42
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%48 = load %struct.dirent*, %struct.dirent** %8
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %47, i32 1024, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %50)
br label %59
52: ; preds = %42
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%54 = load i8*, i8** %5
%55 = load %struct.dirent*, %struct.dirent** %8
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %53, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %54, i8* %57)
br label %59
59: ; preds = %52, %46
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%61 = call i64 @lstat(i8* %60, %struct.stat* %10)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %71
63: ; preds = %59
%64 = load i32, i32* @stderr
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %65, i8* %67)
%69 = load i32*, i32** %6
%70 = call i32 @rewinddir(i32* %69)
store i32 -1, i32* %4
br label %82
71: ; preds = %59
%72 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%73 = load i32, i32* %72
%74 = sdiv i32 %73, 2
%75 = load i32, i32* %11
%76 = add nsw i32 %75, %74
store i32 %76, i32* %11
br label %12
77: ; preds = %12
%78 = load i32, i32* %11
%79 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %78)
%80 = load i32*, i32** %6
%81 = call i32 @rewinddir(i32* %80)
store i32 0, i32* %4
br label %82
82: ; preds = %77, %63
%83 = load i32, i32* %4
ret i32 %83
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [4 x i8] c"/%s\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.5 = [23 x i8] c"stat failed on %s: %s\0A\00"
@errno = global i32 0
@.str.6 = [10 x i8] c"total %d\0A\00"
define i32 @show_total_size(i8* %0, i32* %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = bitcast %struct.stat* %5 to i8*
%8 = call %struct.dirent* @readdir(i32* %1)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %57, label %10
10: ; preds = %3
%11 = and i32 %2, 2
%12 = icmp eq i32 %11, 0
%13 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %14
14: ; preds = %10, %51
%15 = phi %struct.dirent* [ %8, %10 ], [ %55, %51 ]
%16 = phi i32 [ 0, %10 ], [ %54, %51 ]
br label %17
17: ; preds = %14, %27
%18 = phi %struct.dirent* [ %15, %14 ], [ %28, %27 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %17
%24 = load i8*, i8** %19
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %17, %23, %30
%28 = call %struct.dirent* @readdir(i32* %1)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %57, label %17
30: ; preds = %23
%31 = load i8*, i8** %19
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 46
%34 = and i1 %12, %33
br i1 %34, label %27, label %35
35: ; preds = %30
%36 = call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
%38 = load i8*, i8** %19
br i1 %37, label %39, label %41
39: ; preds = %35
%40 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %38)
br label %43
41: ; preds = %35
%42 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %38)
br label %43
43: ; preds = %41, %39
%44 = call i64 @lstat(i8* %6, %struct.stat* %5)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %43
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %6, i8* %49)
br label %60
51: ; preds = %43
%52 = load i32, i32* %13
%53 = sdiv i32 %52, 2
%54 = add nsw i32 %53, %16
%55 = call %struct.dirent* @readdir(i32* %1)
%56 = icmp eq %struct.dirent* %55, null
br i1 %56, label %57, label %14
57: ; preds = %51, %27, %3
%58 = phi i32 [ 0, %3 ], [ %16, %27 ], [ %54, %51 ]
%59 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %57, %46
%61 = phi i32 [ -1, %46 ], [ 0, %57 ]
%62 = call i32 @rewinddir(i32* %1)
ret i32 %61
}