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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int chdir(char *);
int closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ getFileSize(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  DIR *directory;
  struct dirent *info;
  int directorySize;
  if (argc != 2) {
    printf("Usage: %s dirname\n", argv[0]);
    exit(EXIT_SUCCESS);
  }
  if (chdir(argv[1]) == -1) {
    do {
      perror("chdir");
      exit(EXIT_FAILURE);
    } while (0);
  }
  directory = opendir(".");
  directorySize = 0;
  while ((info = readdir(directory)) != NULL) {
    directorySize += getFileSize(info->d_name);
  }
  printf("Total size of directory = %d\n", directorySize);
  if (closedir(directory) == -1) {
    do {
      perror("closedir");
      exit(EXIT_FAILURE);
    } while (0);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c3 <main+0x43>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11f8 <main+0x78>
jmp    11de <main+0x5e>
lea    0xe2e(%rip),%rdi
call   1090 <perror@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xe1a(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1243 <main+0xc3>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1060 <getFileSize@plt>
mov    %rax,%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
jmp    120f <main+0x8f>
mov    -0x24(%rbp),%esi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
cmp    $0xffffffff,%eax
jne    1285 <main+0x105>
jmp    126b <main+0xeb>
lea    0xdc7(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1203 <main+0x83>
mov    0x8(%rsi),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
je     1222 <main+0xa2>
lea    0xe78(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11d6 <main+0x56>
nopl   0x0(%rax,%rax,1)
mov    (%rax),%edi
call   1060 <getFileSize@plt>
add    %eax,%ebp
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    11c0 <main+0x40>
lea    0xe3e(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <closedir@plt>
cmp    $0xffffffff,%eax
je     122b <main+0xab>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    (%rsi),%rsi
lea    0xdf3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdea(%rip),%rdi
jmp    1232 <main+0xb2>
lea    0xe07(%rip),%rdi
call   1090 <perror@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [19 x i8] c"Usage: %s dirname\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [6 x i8] c"chdir\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] c".\00"
@.str.3 = [30 x i8] c"Total size of directory = %d\0A\00"
@.str.4 = [9 x i8] c"closedir\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_SUCCESS
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @chdir(i8* %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %18
br label %25
25: ; preds = %24
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; No predecessors!
br label %30
30: ; preds = %29, %18
%31 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %6
store i32 0, i32* %8
br label %32
32: ; preds = %36, %30
%33 = load i32*, i32** %6
%34 = call %struct.dirent* @readdir(i32* %33)
store %struct.dirent* %34, %struct.dirent** %7
%35 = icmp ne %struct.dirent* %34, null
br i1 %35, label %36, label %45
36: ; preds = %32
%37 = load %struct.dirent*, %struct.dirent** %7
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i64 @getFileSize(i32 %39)
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = add nsw i64 %42, %40
%44 = trunc i64 %43 to i32
store i32 %44, i32* %8
br label %32
45: ; preds = %32
%46 = load i32, i32* %8
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = load i32*, i32** %6
%49 = call i32 @closedir(i32* %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %57
51: ; preds = %45
br label %52
52: ; preds = %51
%53 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; No predecessors!
br label %57
57: ; preds = %56, %45
%58 = load i32, i32* @EXIT_SUCCESS
ret i32 %58
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [19 x i8] c"Usage: %s dirname\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [6 x i8] c"chdir\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] c".\00"
@.str.3 = [30 x i8] c"Total size of directory = %d\0A\00"
@.str.4 = [9 x i8] c"closedir\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i32, i32* @EXIT_SUCCESS
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @chdir(i8* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %9
%19 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call %struct.dirent* @readdir(i32* %19)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %32, label %22
22: ; preds = %18, %22
%23 = phi %struct.dirent* [ %30, %22 ], [ %20, %18 ]
%24 = phi i32 [ %29, %22 ], [ 0, %18 ]
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i64 0, i32 0
%26 = load i32, i32* %25
%27 = tail call i64 @getFileSize(i32 %26)
%28 = trunc i64 %27 to i32
%29 = add i32 %24, %28
%30 = tail call %struct.dirent* @readdir(i32* %19)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %32, label %22
32: ; preds = %22, %18
%33 = phi i32 [ 0, %18 ], [ %29, %22 ]
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = tail call i32 @closedir(i32* %19)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = tail call i32 @exit(i32 %39)
unreachable
41: ; preds = %32
%42 = load i32, i32* @EXIT_SUCCESS
ret i32 %42
}