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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
scalar_t__ S_ISDIR(int);
scalar_t__ S_ISLNK(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fchmod(int, int);
int /*<<< orphan*/ lstat(char *, struct stat *);
int /*<<< orphan*/ mkdir(char *, int);
int open(char *, int, ...);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *, char *, ...);
int read(int, char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
char *strerror(scalar_t__);
scalar_t__ symlink(char *, char *);
int write(int, char *, int);
int copyDirectory(char *from, char *to) {
  DIR *dir;
  struct dirent *ent;
  int fd, outfd;
  char buf[4096];
  int i;
  struct stat sb;
  char filespec[256];
  char filespec2[256];
  char link[1024];
  int ret;
  mkdir(to, 0755);
  if (!(dir = opendir(from))) {
    printf("Failed to read directory %s: %s", from, strerror(errno));
    return 1;
  }
  errno = 0;
  while ((ent = readdir(dir))) {
    if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
      continue;
    sprintf(filespec, "%s/%s", from, ent->d_name);
    sprintf(filespec2, "%s/%s", to, ent->d_name);
    lstat(filespec, &sb);
    if (S_ISDIR(sb.st_mode)) {
      if (copyDirectory(filespec, filespec2))
        return 1;
    } else if (S_ISLNK(sb.st_mode)) {
      i = readlink(filespec, link, sizeof(link) - 1);
      link[i] = '\0';
      if (symlink(link, filespec2)) {
        printf("failed to symlink %s to %s: %s", filespec2, link,
               strerror(errno));
      }
    } else {
      fd = open(filespec, O_RDONLY);
      if (fd == -1) {
        printf("failed to open %s: %s", filespec, strerror(errno));
        return 1;
      }
      outfd = open(filespec2, O_RDWR | O_TRUNC | O_CREAT, 0644);
      if (outfd == -1) {
        printf("failed to create %s: %s", filespec2, strerror(errno));
      } else {
        fchmod(outfd, sb.st_mode & 07777);
        while ((i = read(fd, buf, sizeof(buf))) > 0)
          ret = write(outfd, buf, i);
        close(outfd);
      }
      close(fd);
    }
    errno = 0;
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<copyDirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1670,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    -0x10(%rbp),%rdi
call   1070 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    12a6 <copyDirectory+0x76>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1650(%rbp)
mov    0x2d46(%rip),%rax
mov    (%rax),%rdi
call   1150 <strerror@plt>
mov    -0x1650(%rbp),%rsi
mov    %rax,%rdx
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b0 <copyDirectory+0x380>
mov    0x2d13(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1100 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     15a0 <copyDirectory+0x370>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd48(%rip),%rsi
call   10f0 <strcmp@plt>
cmp    $0x0,%eax
je     1303 <copyDirectory+0xd3>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd2b(%rip),%rsi
call   10f0 <strcmp@plt>
cmp    $0x0,%eax
jne    1308 <copyDirectory+0xd8>
jmp    12b4 <copyDirectory+0x84>
lea    -0x1140(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xd02(%rip),%rsi
call   1140 <sprintf@plt>
lea    -0x1240(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xce4(%rip),%rsi
call   1140 <sprintf@plt>
lea    -0x1140(%rbp),%rdi
lea    -0x1038(%rbp),%rsi
call   10e0 <lstat@plt>
mov    -0x1038(%rbp),%edi
call   1090 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1399 <copyDirectory+0x169>
lea    -0x1140(%rbp),%rdi
lea    -0x1240(%rbp),%rsi
call   1110 <copyDirectory@plt>
cmp    $0x0,%eax
je     1394 <copyDirectory+0x164>
movl   $0x1,-0x4(%rbp)
jmp    15b0 <copyDirectory+0x380>
jmp    158d <copyDirectory+0x35d>
mov    -0x1038(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     1447 <copyDirectory+0x217>
lea    -0x1140(%rbp),%rdi
lea    -0x1640(%rbp),%rsi
mov    $0x3ff,%edx
call   1050 <readlink@plt>
mov    %eax,-0x1034(%rbp)
movslq -0x1034(%rbp),%rax
movb   $0x0,-0x1640(%rbp,%rax,1)
lea    -0x1640(%rbp),%rdi
lea    -0x1240(%rbp),%rsi
call   10a0 <symlink@plt>
cmp    $0x0,%rax
je     1442 <copyDirectory+0x212>
lea    -0x1240(%rbp),%rax
mov    %rax,-0x1660(%rbp)
lea    -0x1640(%rbp),%rax
mov    %rax,-0x1658(%rbp)
mov    0x2ba5(%rip),%rax
mov    (%rax),%rdi
call   1150 <strerror@plt>
mov    -0x1660(%rbp),%rsi
mov    -0x1658(%rbp),%rdx
mov    %rax,%rcx
lea    0xbee(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1588 <copyDirectory+0x358>
lea    -0x1140(%rbp),%rdi
mov    0x2b7b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1130 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    14ac <copyDirectory+0x27c>
lea    -0x1140(%rbp),%rax
mov    %rax,-0x1668(%rbp)
mov    0x2b40(%rip),%rax
mov    (%rax),%rdi
call   1150 <strerror@plt>
mov    -0x1668(%rbp),%rsi
mov    %rax,%rdx
lea    0xbaf(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b0 <copyDirectory+0x380>
lea    -0x1240(%rbp),%rdi
mov    0x2b1e(%rip),%rax
mov    (%rax),%esi
mov    0x2b2d(%rip),%rax
or     (%rax),%esi
mov    0x2b2c(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1130 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1521 <copyDirectory+0x2f1>
lea    -0x1240(%rbp),%rax
mov    %rax,-0x1670(%rbp)
mov    0x2ac4(%rip),%rax
mov    (%rax),%rdi
call   1150 <strerror@plt>
mov    -0x1670(%rbp),%rsi
mov    %rax,%rdx
lea    0xb49(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1580 <copyDirectory+0x350>
mov    -0x30(%rbp),%edi
mov    -0x1038(%rbp),%esi
and    $0xfff,%esi
call   1120 <fchmod@plt>
mov    -0x2c(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   10d0 <read@plt>
mov    %eax,-0x1034(%rbp)
cmp    $0x0,%eax
jle    1578 <copyDirectory+0x348>
mov    -0x30(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x1034(%rbp),%edx
call   1060 <write@plt>
mov    %eax,-0x1644(%rbp)
jmp    1535 <copyDirectory+0x305>
mov    -0x30(%rbp),%edi
call   10b0 <close@plt>
mov    -0x2c(%rbp),%edi
call   10b0 <close@plt>
jmp    158d <copyDirectory+0x35d>
mov    0x2a2c(%rip),%rax
movq   $0x0,(%rax)
jmp    12b4 <copyDirectory+0x84>
mov    -0x20(%rbp),%rdi
call   10c0 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1670,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyDirectory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1618,%rsp
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    %rbx,%rdi
call   1070 <opendir@plt>
test   %rax,%rax
je     14ae <copyDirectory+0x27e>
mov    %rax,%r13
mov    %rbx,0x8(%rsp)
mov    0x2d4c(%rip),%rax
movq   $0x0,(%rax)
mov    %r13,%rdi
call   1100 <readdir@plt>
test   %rax,%rax
je     14a2 <copyDirectory+0x272>
mov    %rax,%rbp
lea    0x110(%rsp),%r14
jmp    12d5 <copyDirectory+0xa5>
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1110 <copyDirectory@plt>
test   %eax,%eax
jne    14d1 <copyDirectory+0x2a1>
mov    0x2d06(%rip),%rax
movq   $0x0,(%rax)
mov    %r13,%rdi
call   1100 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     14a2 <copyDirectory+0x272>
mov    0x0(%rbp),%rbx
mov    %rbx,%rdi
lea    0xd3e(%rip),%rsi
call   10f0 <strcmp@plt>
test   %eax,%eax
je     12c1 <copyDirectory+0x91>
mov    %rbx,%rdi
lea    0xd2a(%rip),%rsi
call   10f0 <strcmp@plt>
test   %eax,%eax
je     12c1 <copyDirectory+0x91>
mov    %r14,%rdi
mov    %r14,%r15
lea    0xd17(%rip),%r14
mov    %r14,%rsi
mov    0x8(%rsp),%rdx
mov    %rbx,%rcx
call   1140 <sprintf@plt>
mov    0x0(%rbp),%rcx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%r14
mov    %r12,%rdx
call   1140 <sprintf@plt>
mov    %r15,%rdi
mov    %rsp,%rsi
call   10e0 <lstat@plt>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1090 <S_ISDIR@plt>
test   %rax,%rax
jne    12a0 <copyDirectory+0x70>
mov    %ebp,%edi
call   1030 <S_ISLNK@plt>
test   %rax,%rax
je     13c6 <copyDirectory+0x196>
mov    %r14,%rdi
lea    0x210(%rsp),%rbp
mov    %rbp,%rsi
mov    $0x3ff,%edx
call   1050 <readlink@plt>
cltq
movb   $0x0,0x210(%rsp,%rax,1)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   10a0 <symlink@plt>
test   %rax,%rax
je     12b3 <copyDirectory+0x83>
mov    0x2c23(%rip),%rax
mov    (%rax),%rdi
call   1150 <strerror@plt>
lea    0xc7d(%rip),%rdi
mov    %rbx,%rsi
lea    0x210(%rsp),%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <printf@plt>
jmp    12b3 <copyDirectory+0x83>
mov    %r12,%rbx
mov    0x2c00(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1130 <open@plt>
cmp    $0xffffffff,%eax
je     14e8 <copyDirectory+0x2b8>
mov    %eax,%r12d
mov    0x2c01(%rip),%rax
mov    (%rax),%esi
mov    0x2be0(%rip),%rax
or     (%rax),%esi
mov    0x2bf7(%rip),%rax
or     (%rax),%esi
lea    0x10(%rsp),%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1130 <open@plt>
cmp    $0xffffffff,%eax
je     146d <copyDirectory+0x23d>
mov    %eax,%r15d
and    $0xfff,%ebp
mov    %eax,%edi
mov    %ebp,%esi
call   1120 <fchmod@plt>
mov    %r12d,%edi
lea    0x610(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rsi
mov    $0x1000,%edx
call   10d0 <read@plt>
test   %eax,%eax
jle    1463 <copyDirectory+0x233>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1060 <write@plt>
mov    %r12d,%edi
jmp    1440 <copyDirectory+0x210>
mov    %r15d,%edi
call   10b0 <close@plt>
jmp    1492 <copyDirectory+0x262>
mov    0x2b4c(%rip),%rax
mov    (%rax),%rdi
call   1150 <strerror@plt>
lea    0xbdb(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r12d,%edi
call   10b0 <close@plt>
mov    %rbx,%r12
jmp    12b3 <copyDirectory+0x83>
mov    %r13,%rdi
call   10c0 <closedir@plt>
xor    %eax,%eax
jmp    14d6 <copyDirectory+0x2a6>
mov    0x2b0b(%rip),%rax
mov    (%rax),%rdi
call   1150 <strerror@plt>
lea    0xb3c(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%eax
add    $0x1618,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ad1(%rip),%rax
mov    (%rax),%rdi
call   1150 <strerror@plt>
lea    0xb4a(%rip),%rdi
lea    0x110(%rsp),%rsi
jmp    14c7 <copyDirectory+0x297>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [32 x i8] c"Failed to read directory %s: %s\00"
@errno = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [31 x i8] c"failed to symlink %s to %s: %s\00"
@O_RDONLY = global i32 0
@.str.5 = [22 x i8] c"failed to open %s: %s\00"
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.6 = [24 x i8] c"failed to create %s: %s\00"
define i32 @copyDirectory(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [4096 x i8]
%11 = alloca i32
%12 = alloca %struct.stat
%13 = alloca [256 x i8]
%14 = alloca [256 x i8]
%15 = alloca [1024 x i8]
%16 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%17 = load i8*, i8** %5
%18 = call i32 @mkdir(i8* %17, i32 493)
%19 = load i8*, i8** %4
%20 = call i32* @opendir(i8* %19)
store i32* %20, i32** %6
%21 = icmp ne i32* %20, null
br i1 %21, label %27, label %22
22: ; preds = %2
%23 = load i8*, i8** %4
%24 = load i64, i64* @errno
%25 = call i8* @strerror(i64 %24)
%26 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %23, i8* %25)
store i32 1, i32* %3
br label %147
27: ; preds = %2
store i64 0, i64* @errno
br label %28
28: ; preds = %143, %44, %27
%29 = load i32*, i32** %6
%30 = call %struct.dirent* @readdir(i32* %29)
store %struct.dirent* %30, %struct.dirent** %7
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %32, label %144
32: ; preds = %28
%33 = load %struct.dirent*, %struct.dirent** %7
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @strcmp(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %32
%39 = load %struct.dirent*, %struct.dirent** %7
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @strcmp(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %38, %32
br label %28
45: ; preds = %38
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%47 = load i8*, i8** %4
%48 = load %struct.dirent*, %struct.dirent** %7
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %47, i8* %50)
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %14, i64 0, i64 0
%53 = load i8*, i8** %5
%54 = load %struct.dirent*, %struct.dirent** %7
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i32 0, i32 0
%56 = load i8*, i8** %55
%57 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %53, i8* %56)
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%59 = call i32 @lstat(i8* %58, %struct.stat* %12)
%60 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i64 @S_ISDIR(i32 %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %71
64: ; preds = %45
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%66 = getelementptr inbounds [256 x i8], [256 x i8]* %14, i64 0, i64 0
%67 = call i32 @copyDirectory(i8* %65, i8* %66)
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %64
store i32 1, i32* %3
br label %147
70: ; preds = %64
br label %143
71: ; preds = %45
%72 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%73 = load i32, i32* %72
%74 = call i64 @S_ISLNK(i32 %73)
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %94
76: ; preds = %71
%77 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%79 = call i32 @readlink(i8* %77, i8* %78, i32 1023)
store i32 %79, i32* %11
%80 = load i32, i32* %11
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 %81
store i8 0, i8* %82
%83 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%84 = getelementptr inbounds [256 x i8], [256 x i8]* %14, i64 0, i64 0
%85 = call i64 @symlink(i8* %83, i8* %84)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %93
87: ; preds = %76
%88 = getelementptr inbounds [256 x i8], [256 x i8]* %14, i64 0, i64 0
%89 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%90 = load i64, i64* @errno
%91 = call i8* @strerror(i64 %90)
%92 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %88, i8* %89, i8* %91)
br label %93
93: ; preds = %87, %76
br label %142
94: ; preds = %71
%95 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%96 = load i32, i32* @O_RDONLY
%97 = call i32 (i8*, i32, ...) @open(i8* %95, i32 %96)
store i32 %97, i32* %8
%98 = load i32, i32* %8
%99 = icmp eq i32 %98, -1
br i1 %99, label %100, label %105
100: ; preds = %94
%101 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%102 = load i64, i64* @errno
%103 = call i8* @strerror(i64 %102)
%104 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %101, i8* %103)
store i32 1, i32* %3
br label %147
105: ; preds = %94
%106 = getelementptr inbounds [256 x i8], [256 x i8]* %14, i64 0, i64 0
%107 = load i32, i32* @O_RDWR
%108 = load i32, i32* @O_TRUNC
%109 = or i32 %107, %108
%110 = load i32, i32* @O_CREAT
%111 = or i32 %109, %110
%112 = call i32 (i8*, i32, ...) @open(i8* %106, i32 %111, i32 420)
store i32 %112, i32* %9
%113 = load i32, i32* %9
%114 = icmp eq i32 %113, -1
br i1 %114, label %115, label %120
115: ; preds = %105
%116 = getelementptr inbounds [256 x i8], [256 x i8]* %14, i64 0, i64 0
%117 = load i64, i64* @errno
%118 = call i8* @strerror(i64 %117)
%119 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %116, i8* %118)
br label %139
120: ; preds = %105
%121 = load i32, i32* %9
%122 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%123 = load i32, i32* %122
%124 = and i32 %123, 4095
%125 = call i32 @fchmod(i32 %121, i32 %124)
br label %126
126: ; preds = %131, %120
%127 = load i32, i32* %8
%128 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%129 = call i32 @read(i32 %127, i8* %128, i32 4096)
store i32 %129, i32* %11
%130 = icmp sgt i32 %129, 0
br i1 %130, label %131, label %136
131: ; preds = %126
%132 = load i32, i32* %9
%133 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%134 = load i32, i32* %11
%135 = call i32 @write(i32 %132, i8* %133, i32 %134)
store i32 %135, i32* %16
br label %126
136: ; preds = %126
%137 = load i32, i32* %9
%138 = call i32 @close(i32 %137)
br label %139
139: ; preds = %136, %115
%140 = load i32, i32* %8
%141 = call i32 @close(i32 %140)
br label %142
142: ; preds = %139, %93
br label %143
143: ; preds = %142, %70
store i64 0, i64* @errno
br label %28
144: ; preds = %28
%145 = load i32*, i32** %6
%146 = call i32 @closedir(i32* %145)
store i32 0, i32* %3
br label %147
147: ; preds = %144, %100, %69, %22
%148 = load i32, i32* %3
ret i32 %148
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [32 x i8] c"Failed to read directory %s: %s\00"
@errno = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [31 x i8] c"failed to symlink %s to %s: %s\00"
@O_RDONLY = global i32 0
@.str.5 = [22 x i8] c"failed to open %s: %s\00"
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.6 = [24 x i8] c"failed to create %s: %s\00"
define i32 @copyDirectory(i8* %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = alloca %struct.stat
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = alloca [1024 x i8]
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%9 = bitcast %struct.stat* %4 to i8*
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%13 = tail call i32 @mkdir(i8* %1, i32 493)
%14 = tail call i32* @opendir(i8* %0)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i64, i64* @errno
%18 = tail call i8* @strerror(i64 %17)
%19 = tail call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %18)
br label %98
20: ; preds = %2
store i64 0, i64* @errno
%21 = call %struct.dirent* @readdir(i32* %14)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %96, label %23
23: ; preds = %20
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %25
25: ; preds = %23, %34
%26 = phi %struct.dirent* [ %21, %23 ], [ %35, %34 ]
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i64 0, i32 0
%28 = load i8*, i8** %27
%29 = call i32 @strcmp(i8* dereferenceable(1) %28, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %25
%32 = call i32 @strcmp(i8* dereferenceable(1) %28, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %25, %31, %95
%35 = call %struct.dirent* @readdir(i32* %14)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %96, label %25
37: ; preds = %31
%38 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %28)
%39 = load i8*, i8** %27
%40 = call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %39)
%41 = call i32 @lstat(i8* %10, %struct.stat* %4)
%42 = load i32, i32* %24
%43 = call i64 @S_ISDIR(i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %37
%46 = call i32 @copyDirectory(i8* %10, i8* %11)
%47 = icmp eq i32 %46, 0
br i1 %47, label %95, label %98
48: ; preds = %37
%49 = call i64 @S_ISLNK(i32 %42)
%50 = icmp eq i64 %49, 0
br i1 %50, label %61, label %51
51: ; preds = %48
%52 = call i32 @readlink(i8* %10, i8* %12, i32 1023)
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %53
store i8 0, i8* %54
%55 = call i64 @symlink(i8* %12, i8* %11)
%56 = icmp eq i64 %55, 0
br i1 %56, label %95, label %57
57: ; preds = %51
%58 = load i64, i64* @errno
%59 = call i8* @strerror(i64 %58)
%60 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %11, i8* %12, i8* %59)
br label %95
61: ; preds = %48
%62 = load i32, i32* @O_RDONLY
%63 = call i32 (i8*, i32, ...) @open(i8* %10, i32 %62)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %69
65: ; preds = %61
%66 = load i64, i64* @errno
%67 = call i8* @strerror(i64 %66)
%68 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %10, i8* %67)
br label %98
69: ; preds = %61
%70 = load i32, i32* @O_RDWR
%71 = load i32, i32* @O_TRUNC
%72 = or i32 %71, %70
%73 = load i32, i32* @O_CREAT
%74 = or i32 %72, %73
%75 = call i32 (i8*, i32, ...) @open(i8* %11, i32 %74, i32 420)
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %81
77: ; preds = %69
%78 = load i64, i64* @errno
%79 = call i8* @strerror(i64 %78)
%80 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %11, i8* %79)
br label %93
81: ; preds = %69
%82 = and i32 %42, 4095
%83 = call i32 @fchmod(i32 %75, i32 %82)
%84 = call i32 @read(i32 %63, i8* %8, i32 4096)
%85 = icmp sgt i32 %84, 0
br i1 %85, label %86, label %91
86: ; preds = %81, %86
%87 = phi i32 [ %89, %86 ], [ %84, %81 ]
%88 = call i32 @write(i32 %75, i8* %8, i32 %87)
%89 = call i32 @read(i32 %63, i8* %8, i32 4096)
%90 = icmp sgt i32 %89, 0
br i1 %90, label %86, label %91
91: ; preds = %86, %81
%92 = call i32 @close(i32 %75)
br label %93
93: ; preds = %91, %77
%94 = call i32 @close(i32 %63)
br label %95
95: ; preds = %93, %57, %51, %45
store i64 0, i64* @errno
br label %34
96: ; preds = %34, %20
%97 = call i32 @closedir(i32* %14)
br label %98
98: ; preds = %45, %96, %65, %16
%99 = phi i32 [ 1, %65 ], [ 0, %96 ], [ 1, %16 ], [ 1, %45 ]
ret i32 %99
}