#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 /*<<< orphan*/ O_RDONLY;
int S_IRUSR;
int S_IWUSR;
int S_IXUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int creat(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int getDirLen(char *, int /*<<< orphan*/);
char *getFullFileName(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ oops(char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int ac, char *av[]) {
  int outFile, inFile, len, nChars;
  char buf[4096];
  struct dirent *pDirent;
  DIR *sourceDir;
  DIR *targetDir;
  if (ac < 3) {
    fprintf(stderr, "usage: %s source destination\n", *av);
    exit(1);
  }
  sourceDir = opendir(av[1]);
  if (sourceDir == NULL) {
    printf("Cannot Open Directory\n");
    exit(1);
  }
  targetDir = opendir(av[2]);
  if (targetDir == NULL) {
    printf("Creating Directory %s\n", av[2]);
    mkdir(av[2], S_IRUSR | S_IWUSR | S_IXUSR);
  }
  printf("Copying files from %s to %s\n", av[1], av[2]);
  while ((pDirent = readdir(sourceDir)) != NULL) {
    int sourceDirLen = getDirLen(av[1], pDirent->d_name);
    char *sourceFile = getFullFileName(sourceDirLen, av[1], pDirent->d_name);
    inFile = open(sourceFile, O_RDONLY);
    int destDirLen = getDirLen(av[2], pDirent->d_name);
    char *destFile = getFullFileName(destDirLen, av[2], pDirent->d_name);
    outFile = creat(destFile, 0644);
    while ((nChars = read(inFile, buf, 4096)) > 0) {
      if (write(outFile, buf, nChars) != nChars) {
        oops("Write error to ", destFile);
      }
    }
  }
  close(inFile);
  close(outFile);
  closedir(sourceDir);
  closedir(targetDir);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1239 <main+0x49>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdd6(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <opendir@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
jne    1273 <main+0x83>
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <opendir@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
jne    12d3 <main+0xe3>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d06(%rip),%rax
mov    (%rax),%esi
mov    0x2d15(%rip),%rax
or     (%rax),%esi
mov    0x2d1c(%rip),%rax
or     (%rax),%esi
call   1030 <mkdir@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x1030(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
je     1410 <main+0x220>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x1028(%rbp),%rax
mov    (%rax),%esi
call   1050 <getDirLen@plt>
mov    %eax,-0x103c(%rbp)
mov    -0x103c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x1028(%rbp),%rax
mov    (%rax),%edx
call   1110 <getFullFileName@plt>
mov    %rax,-0x1048(%rbp)
mov    -0x1048(%rbp),%rdi
mov    0x2c85(%rip),%rax
mov    (%rax),%esi
call   10e0 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x1028(%rbp),%rax
mov    (%rax),%esi
call   1050 <getDirLen@plt>
mov    %eax,-0x104c(%rbp)
mov    -0x104c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x1028(%rbp),%rax
mov    (%rax),%edx
call   1110 <getFullFileName@plt>
mov    %rax,-0x1058(%rbp)
mov    -0x1058(%rbp),%rdi
mov    $0x1a4,%esi
call   10f0 <creat@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   10b0 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    140b <main+0x21b>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <write@plt>
cmp    -0x20(%rbp),%eax
je     1406 <main+0x216>
mov    -0x1058(%rbp),%rsi
lea    0xc68(%rip),%rdi
call   1060 <oops@plt>
jmp    13b8 <main+0x1c8>
jmp    12f1 <main+0x101>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    -0x1030(%rbp),%rdi
call   10a0 <closedir@plt>
mov    -0x1038(%rbp),%rdi
call   10a0 <closedir@plt>
xor    %eax,%eax
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    %rsi,%r12
cmp    $0x2,%edi
jle    13d1 <main+0x1d1>
mov    0x8(%r12),%rdi
call   1080 <opendir@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     13f4 <main+0x1f4>
mov    0x10(%r12),%rdi
call   1080 <opendir@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
jne    1281 <main+0x81>
mov    0x10(%r12),%rsi
lea    0xdc9(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x2d5d(%rip),%rax
mov    0x2d6e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%r12),%rdi
mov    0x2d6e(%rip),%rax
or     (%rax),%esi
call   1030 <mkdir@plt>
mov    0x8(%r12),%rsi
mov    0x10(%r12),%rdx
lea    0xda3(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x8(%rsp),%rdi
call   10e0 <readdir@plt>
mov    %rax,%r14
lea    0x20(%rsp),%rbx
lea    0xda0(%rip),%r13
mov    %r12,0x18(%rsp)
jmp    12db <main+0xdb>
nopl   0x0(%rax)
mov    0x8(%rsp),%rdi
call   10e0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
mov    0x18(%rsp),%r12
je     139a <main+0x19a>
mov    0x8(%r12),%rdi
mov    (%r14),%esi
call   1060 <getDirLen@plt>
mov    0x8(%r12),%rsi
mov    (%r14),%edx
mov    %eax,%edi
call   1120 <getFullFileName@plt>
mov    0x2ce2(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   10f0 <open@plt>
mov    %eax,%ebp
mov    0x10(%r12),%rdi
mov    (%r14),%esi
call   1060 <getDirLen@plt>
mov    0x10(%r12),%rsi
mov    (%r14),%edx
mov    %eax,%edi
call   1120 <getFullFileName@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1a4,%esi
call   1100 <creat@plt>
mov    %eax,%r15d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   10c0 <read@plt>
test   %eax,%eax
jle    12c0 <main+0xc0>
mov    %eax,%r12d
jmp    137a <main+0x17a>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   10c0 <read@plt>
mov    %eax,%r12d
test   %eax,%eax
jle    12c0 <main+0xc0>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1050 <write@plt>
cmp    %r12d,%eax
je     1360 <main+0x160>
mov    %r13,%rdi
mov    %r14,%rsi
call   1070 <oops@plt>
jmp    1360 <main+0x160>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    %r15d,%edi
call   10a0 <close@plt>
mov    0x8(%rsp),%rdi
call   10b0 <closedir@plt>
mov    0x10(%rsp),%rdi
call   10b0 <closedir@plt>
xor    %eax,%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2bf0(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%rdx
lea    0xc1b(%rip),%rsi
call   10d0 <fprintf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
lea    0xc67(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [30 x i8] c"usage: %s source destination\0A\00"
@.str.1 = [23 x i8] c"Cannot Open Directory\0A\00"
@.str.2 = [23 x i8] c"Creating Directory %s\0A\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
@.str.3 = [29 x i8] c"Copying files from %s to %s\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [16 x i8] c"Write error to \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [4096 x i8]
%11 = alloca %struct.dirent*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 3
br i1 %19, label %20, label %26
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = load i8*, i8** %22
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32* @opendir(i8* %29)
store i32* %30, i32** %12
%31 = load i32*, i32** %12
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %26
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
%40 = call i32* @opendir(i8* %39)
store i32* %40, i32** %13
%41 = load i32*, i32** %13
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %57
43: ; preds = %36
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 2
%50 = load i8*, i8** %49
%51 = load i32, i32* @S_IRUSR
%52 = load i32, i32* @S_IWUSR
%53 = or i32 %51, %52
%54 = load i32, i32* @S_IXUSR
%55 = or i32 %53, %54
%56 = call i32 @mkdir(i8* %50, i32 %55)
br label %57
57: ; preds = %43, %36
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 1
%60 = load i8*, i8** %59
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 2
%63 = load i8*, i8** %62
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %60, i8* %63)
br label %65
65: ; preds = %121, %57
%66 = load i32*, i32** %12
%67 = call %struct.dirent* @readdir(i32* %66)
store %struct.dirent* %67, %struct.dirent** %11
%68 = icmp ne %struct.dirent* %67, null
br i1 %68, label %69, label %122
69: ; preds = %65
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 1
%72 = load i8*, i8** %71
%73 = load %struct.dirent*, %struct.dirent** %11
%74 = getelementptr inbounds %struct.dirent, %struct.dirent* %73, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i32 @getDirLen(i8* %72, i32 %75)
store i32 %76, i32* %14
%77 = load i32, i32* %14
%78 = load i8**, i8*** %5
%79 = getelementptr inbounds i8*, i8** %78, i64 1
%80 = load i8*, i8** %79
%81 = load %struct.dirent*, %struct.dirent** %11
%82 = getelementptr inbounds %struct.dirent, %struct.dirent* %81, i32 0, i32 0
%83 = load i32, i32* %82
%84 = call i8* @getFullFileName(i32 %77, i8* %80, i32 %83)
store i8* %84, i8** %15
%85 = load i8*, i8** %15
%86 = load i32, i32* @O_RDONLY
%87 = call i32 @open(i8* %85, i32 %86)
store i32 %87, i32* %7
%88 = load i8**, i8*** %5
%89 = getelementptr inbounds i8*, i8** %88, i64 2
%90 = load i8*, i8** %89
%91 = load %struct.dirent*, %struct.dirent** %11
%92 = getelementptr inbounds %struct.dirent, %struct.dirent* %91, i32 0, i32 0
%93 = load i32, i32* %92
%94 = call i32 @getDirLen(i8* %90, i32 %93)
store i32 %94, i32* %16
%95 = load i32, i32* %16
%96 = load i8**, i8*** %5
%97 = getelementptr inbounds i8*, i8** %96, i64 2
%98 = load i8*, i8** %97
%99 = load %struct.dirent*, %struct.dirent** %11
%100 = getelementptr inbounds %struct.dirent, %struct.dirent* %99, i32 0, i32 0
%101 = load i32, i32* %100
%102 = call i8* @getFullFileName(i32 %95, i8* %98, i32 %101)
store i8* %102, i8** %17
%103 = load i8*, i8** %17
%104 = call i32 @creat(i8* %103, i32 420)
store i32 %104, i32* %6
br label %105
105: ; preds = %120, %69
%106 = load i32, i32* %7
%107 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%108 = call i32 @read(i32 %106, i8* %107, i32 4096)
store i32 %108, i32* %9
%109 = icmp sgt i32 %108, 0
br i1 %109, label %110, label %121
110: ; preds = %105
%111 = load i32, i32* %6
%112 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%113 = load i32, i32* %9
%114 = call i32 @write(i32 %111, i8* %112, i32 %113)
%115 = load i32, i32* %9
%116 = icmp ne i32 %114, %115
br i1 %116, label %117, label %120
117: ; preds = %110
%118 = load i8*, i8** %17
%119 = call i32 @oops(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %118)
br label %120
120: ; preds = %117, %110
br label %105
121: ; preds = %105
br label %65
122: ; preds = %65
%123 = load i32, i32* %7
%124 = call i32 @close(i32 %123)
%125 = load i32, i32* %6
%126 = call i32 @close(i32 %125)
%127 = load i32*, i32** %12
%128 = call i32 @closedir(i32* %127)
%129 = load i32*, i32** %13
%130 = call i32 @closedir(i32* %129)
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [30 x i8] c"usage: %s source destination\0A\00"
@.str.2 = [23 x i8] c"Creating Directory %s\0A\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
@.str.3 = [29 x i8] c"Copying files from %s to %s\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [16 x i8] c"Write error to \00"
@str = [22 x i8] c"Cannot Open Directory\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @opendir(i8* %13)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i32* @opendir(i8* %21)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %34
24: ; preds = %19
%25 = load i8*, i8** %20
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %20
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IWUSR
%30 = or i32 %29, %28
%31 = load i32, i32* @S_IXUSR
%32 = or i32 %30, %31
%33 = tail call i32 @mkdir(i8* %27, i32 %32)
br label %34
34: ; preds = %24, %19
%35 = load i8*, i8** %12
%36 = load i8*, i8** %20
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %35, i8* %36)
%38 = tail call %struct.dirent* @readdir(i32* %14)
%39 = icmp ne %struct.dirent* %38, null
br label %43
40: ; preds = %69, %43
%41 = tail call %struct.dirent* @readdir(i32* %14)
%42 = icmp eq %struct.dirent* %41, null
br i1 %42, label %72, label %43
43: ; preds = %34, %40
%44 = phi %struct.dirent* [ %41, %40 ], [ %38, %34 ]
%45 = load i8*, i8** %12
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i64 0, i32 0
%47 = load i32, i32* %46
%48 = tail call i32 @getDirLen(i8* %45, i32 %47)
%49 = load i8*, i8** %12
%50 = load i32, i32* %46
%51 = tail call i8* @getFullFileName(i32 %48, i8* %49, i32 %50)
%52 = load i32, i32* @O_RDONLY
%53 = tail call i32 @open(i8* %51, i32 %52)
%54 = load i8*, i8** %20
%55 = load i32, i32* %46
%56 = tail call i32 @getDirLen(i8* %54, i32 %55)
%57 = load i8*, i8** %20
%58 = load i32, i32* %46
%59 = tail call i8* @getFullFileName(i32 %56, i8* %57, i32 %58)
%60 = tail call i32 @creat(i8* %59, i32 420)
%61 = call i32 @read(i32 %53, i8* %4, i32 4096)
%62 = icmp sgt i32 %61, 0
br i1 %62, label %63, label %40
63: ; preds = %43, %69
%64 = phi i32 [ %70, %69 ], [ %61, %43 ]
%65 = call i32 @write(i32 %60, i8* %4, i32 %64)
%66 = icmp eq i32 %65, %64
br i1 %66, label %69, label %67
67: ; preds = %63
%68 = tail call i32 @oops(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %59)
br label %69
69: ; preds = %67, %63
%70 = call i32 @read(i32 %53, i8* %4, i32 4096)
%71 = icmp sgt i32 %70, 0
br i1 %71, label %63, label %40
72: ; preds = %40
%73 = tail call i32 @close(i32 %53)
%74 = tail call i32 @close(i32 %60)
%75 = tail call i32 @closedir(i32* %14)
%76 = tail call i32 @closedir(i32* %22)
ret i32 0
}