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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, long);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stat(int /*<<< orphan*/, struct stat *);
void main() {
  DIR *d;
  struct dirent *info;
  struct stat sb;
  long total = 0;
  d = opendir(".");
  while ((info = readdir(d)) != NULL) {
    stat(info->d_name, &sb);
    total += sb.st_size;
  }
  closedir(d);
  printf("total size = %ld\n", total);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x20(%rbp)
lea    0xe99(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11a7 <main+0x57>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1170 <main+0x20>
mov    -0x8(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x20(%rbp),%rsi
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xea0(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1070 <readdir@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     119c <main+0x4c>
lea    0x8(%rsp),%r15
nopl   0x0(%rax)
mov    (%rax),%edi
mov    %r15,%rsi
call   1060 <stat@plt>
add    0x8(%rsp),%rbx
mov    %r14,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    1180 <main+0x30>
mov    %r14,%rdi
call   1050 <closedir@plt>
lea    0xe57(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i64 }
@.str = [2 x i8] c".\00"
@.str.1 = [18 x i8] c"total size = %ld\0A\00"
define void @main() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca %struct.stat
%4 = alloca i64
store i64 0, i64* %4
%5 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %1
br label %6
6: ; preds = %10, %0
%7 = load i32*, i32** %1
%8 = call %struct.dirent* @readdir(i32* %7)
store %struct.dirent* %8, %struct.dirent** %2
%9 = icmp ne %struct.dirent* %8, null
br i1 %9, label %10, label %19
10: ; preds = %6
%11 = load %struct.dirent*, %struct.dirent** %2
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @stat(i32 %13, %struct.stat* %3)
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%16 = load i64, i64* %15
%17 = load i64, i64* %4
%18 = add nsw i64 %17, %16
store i64 %18, i64* %4
br label %6
19: ; preds = %6
%20 = load i32*, i32** %1
%21 = call i32 @closedir(i32* %20)
%22 = load i64, i64* %4
%23 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %22)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [18 x i8] c"total size = %ld\0A\00"
define void @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = call %struct.dirent* @readdir(i32* %3)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %18, label %6
6: ; preds = %0
%7 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
br label %8
8: ; preds = %6, %8
%9 = phi %struct.dirent* [ %4, %6 ], [ %16, %8 ]
%10 = phi i64 [ 0, %6 ], [ %15, %8 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @stat(i32 %12, %struct.stat* %1)
%14 = load i64, i64* %7
%15 = add nsw i64 %14, %10
%16 = call %struct.dirent* @readdir(i32* %3)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %18, label %8
18: ; preds = %8, %0
%19 = phi i64 [ 0, %0 ], [ %15, %8 ]
%20 = call i32 @closedir(i32* %3)
%21 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %19)
ret void
}