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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ getch();
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    printf("Usage: %s, directory, logFile \nEG: %s C:\\Program Files, "
           "C:\\Log\1.txt",
           argv[0], argv[0]);
    getch();
    return 4;
  } else {
    DIR *dir;
    if ((dir = opendir(argv[1])) != NULL) {
      struct dirent *ent;
      FILE *fp;
      fp = fopen(argv[2], "w+");
      while ((ent = readdir(dir)) != NULL) {
        printf("%s\n", ent->d_name);
        fprintf(fp, "%s\n", ent->d_name);
      }
      fclose(fp);
      closedir(dir);
      printf("Saved to: %s", argv[2]);
      return 0;
    } else if (dir == NULL) {
      printf("You either typed the path incorrectly or the directory does not "
             "exist.");
      closedir(dir);
      return 1;
    } else {
      printf("Could not open directory, try running your command line with "
             "administrative privilidges.");
      closedir(dir);
      return 2;
    }
  }
}


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   $0x3,-0x8(%rbp)
je     11cf <main+0x4f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1090 <getch@plt>
movl   $0x4,-0x4(%rbp)
jmp    12ca <main+0x14a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     127e <main+0xfe>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe4b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     124a <main+0xca>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xe07(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1202 <main+0x82>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ca <main+0x14a>
cmpq   $0x0,-0x18(%rbp)
jne    12ac <main+0x12c>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ca <main+0x14a>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
cmp    $0x3,%edi
jne    123c <main+0xac>
mov    0x8(%r14),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     125e <main+0xce>
mov    %rax,%r15
mov    0x10(%r14),%rdi
lea    0xe7f(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1216 <main+0x86>
mov    %rax,%rbx
lea    0xe63(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rbx),%rdi
call   1030 <puts@plt>
mov    (%rbx),%rdx
mov    %r13,%rdi
mov    %r12,%rsi
call   1080 <fprintf@plt>
mov    %r15,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f0 <main+0x60>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1070 <closedir@plt>
mov    0x10(%r14),%rsi
lea    0xe1a(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1278 <main+0xe8>
mov    (%r14),%rdx
lea    0xdba(%rip),%rdi
mov    %rdx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   10a0 <getch@plt>
mov    $0x4,%ebx
jmp    1278 <main+0xe8>
lea    0xdf3(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   1070 <closedir@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [68 x i8] c"Usage: %s, directory, logFile \0AEG: %s C:\\Program Files, C:\\Log\01.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [13 x i8] c"Saved to: %s\00"
@.str.4 = [71 x i8] c"You either typed the path incorrectly or the directory does not exist.\00"
@.str.5 = [89 x i8] c"Could not open directory, try running your command line with administrative privilidges.\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, 3
br i1 %10, label %11, label %20
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %17)
%19 = call i32 (...) @getch()
store i32 4, i32* %3
br label %65
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32* @opendir(i8* %23)
store i32* %24, i32** %6
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %54
26: ; preds = %20
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %30, i32** %8
br label %31
31: ; preds = %35, %26
%32 = load i32*, i32** %6
%33 = call %struct.dirent* @readdir(i32* %32)
store %struct.dirent* %33, %struct.dirent** %7
%34 = icmp ne %struct.dirent* %33, null
br i1 %34, label %35, label %45
35: ; preds = %31
%36 = load %struct.dirent*, %struct.dirent** %7
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = load i32*, i32** %8
%41 = load %struct.dirent*, %struct.dirent** %7
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = call i32 @fprintf(i32* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %43)
br label %31
45: ; preds = %31
%46 = load i32*, i32** %8
%47 = call i32 @fclose(i32* %46)
%48 = load i32*, i32** %6
%49 = call i32 @closedir(i32* %48)
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 2
%52 = load i8*, i8** %51
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %52)
store i32 0, i32* %3
br label %65
54: ; preds = %20
%55 = load i32*, i32** %6
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.4, i64 0, i64 0))
%59 = load i32*, i32** %6
%60 = call i32 @closedir(i32* %59)
store i32 1, i32* %3
br label %65
61: ; preds = %54
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([89 x i8], [89 x i8]* @.str.5, i64 0, i64 0))
%63 = load i32*, i32** %6
%64 = call i32 @closedir(i32* %63)
store i32 2, i32* %3
br label %65
65: ; preds = %61, %57, %45, %11
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [68 x i8] c"Usage: %s, directory, logFile \0AEG: %s C:\\Program Files, C:\\Log\01.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [13 x i8] c"Saved to: %s\00"
@.str.4 = [71 x i8] c"You either typed the path incorrectly or the directory does not exist.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0), i8* %5, i8* %5)
%7 = tail call i32 (...) @getch()
br label %36
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @opendir(i8* %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %33, label %13
13: ; preds = %8
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call %struct.dirent* @readdir(i32* %11)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %28, label %19
19: ; preds = %13, %19
%20 = phi %struct.dirent* [ %26, %19 ], [ %17, %13 ]
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = tail call i32 @puts(i8* dereferenceable(1) %22)
%24 = load i8*, i8** %21
%25 = tail call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = tail call %struct.dirent* @readdir(i32* %11)
%27 = icmp eq %struct.dirent* %26, null
br i1 %27, label %28, label %19
28: ; preds = %19, %13
%29 = tail call i32 @fclose(i32* %16)
%30 = tail call i32 @closedir(i32* %11)
%31 = load i8*, i8** %14
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %31)
br label %36
33: ; preds = %8
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([71 x i8], [71 x i8]* @.str.4, i64 0, i64 0))
%35 = tail call i32 @closedir(i32* null)
br label %36
36: ; preds = %28, %33, %4
%37 = phi i32 [ 4, %4 ], [ 0, %28 ], [ 1, %33 ]
ret i32 %37
}