#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 dummy;
};
struct dirent {
  char *d_name;
  scalar_t__ d_type;
};
typedef int /*<<< orphan*/ sourcefile;
typedef int /*<<< orphan*/ destfile;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_REG;
int checkfilename(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ destExist(char *, char *);
int /*<<< orphan*/ destNotExist(char *, char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int snprintf(char *, int, char *, char const *, char const *, ...);
scalar_t__ strcmp(char *, char *);
void listdir2(const char *filename, const char *destname) {
  struct dirent *pDirent;
  struct stat st;
  DIR *pDir;
  pDir = opendir(filename);
  char destfile[1024];
  char sourcefile[1024];
  if (pDir == NULL) {
    perror("Source error");
    return;
  }
  while ((pDirent = readdir(pDir)) != NULL) {
    int len = snprintf(destfile, sizeof(destfile) - 1, "%s/%s/%s", destname,
                       filename, pDirent->d_name);
    int len2 = snprintf(sourcefile, sizeof(sourcefile) - 1, "%s/%s", filename,
                        pDirent->d_name);
    destfile[len] = 0;
    sourcefile[len2] = 0;
    if (pDirent->d_type == DT_REG) {
      if (checkfilename(sourcefile) == 1) {
        continue;
      }
      FILE *dFile = fopen(destfile, "r");
      if (dFile == NULL)
        destNotExist(sourcefile, destfile);
      else
        destExist(sourcefile, destfile);
    } else {
      if (strcmp(pDirent->d_name, ".") == 0 ||
          strcmp(pDirent->d_name, "..") == 0)
        continue;
      else {
        listdir2(sourcefile, destname);
      }
    }
  }
  closedir(pDir);
}


x86 assembly (clang, -O0):
<listdir2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ec <listdir2+0x3c>
lea    0xe1e(%rip),%rdi
call   10b0 <perror@plt>
jmp    136c <listdir2+0x1bc>
jmp    11f1 <listdir2+0x41>
mov    -0x28(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1363 <listdir2+0x1b3>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%r8
mov    -0x18(%rbp),%rax
mov    (%rax),%r9
mov    $0x3ff,%esi
lea    0xde3(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0x834(%rbp)
lea    -0x830(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0x3ff,%esi
lea    0xdbb(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0x838(%rbp)
movslq -0x834(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
movslq -0x838(%rbp),%rax
movb   $0x0,-0x830(%rbp,%rax,1)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d51(%rip),%rcx
cmp    (%rcx),%rax
jne    130a <listdir2+0x15a>
lea    -0x830(%rbp),%rdi
call   1090 <checkfilename@plt>
cmp    $0x1,%eax
jne    12b2 <listdir2+0x102>
jmp    11f1 <listdir2+0x41>
lea    -0x430(%rbp),%rdi
lea    0xd4b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x840(%rbp)
cmpq   $0x0,-0x840(%rbp)
jne    12f2 <listdir2+0x142>
lea    -0x830(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   10d0 <destNotExist@plt>
jmp    1305 <listdir2+0x155>
lea    -0x830(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1030 <destExist@plt>
jmp    135e <listdir2+0x1ae>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xcff(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1344 <listdir2+0x194>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xce1(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1349 <listdir2+0x199>
jmp    11f1 <listdir2+0x41>
lea    -0x830(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <listdir2@plt>
jmp    135e <listdir2+0x1ae>
jmp    11f1 <listdir2+0x41>
mov    -0x28(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listdir2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r12
call   1040 <opendir@plt>
test   %rax,%rax
je     1323 <listdir2+0x173>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1309 <listdir2+0x159>
mov    %rax,%rbp
lea    0x410(%rsp),%rbx
jmp    1214 <listdir2+0x64>
call   1030 <destExist@plt>
nopl   (%rax)
mov    %r14,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1309 <listdir2+0x159>
mov    0x0(%rbp),%r9
mov    %rbx,%r15
mov    %rbx,%rdi
mov    $0x3ff,%esi
lea    0xde3(%rip),%rdx
mov    0x8(%rsp),%rcx
mov    %r12,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %eax,%r13d
mov    0x0(%rbp),%r8
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x3ff,%esi
lea    0xdbc(%rip),%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
movslq %r13d,%rcx
movb   $0x0,0x410(%rsp,%rcx,1)
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    0x8(%rbp),%rax
mov    0x2d65(%rip),%rcx
cmp    (%rcx),%rax
jne    12c0 <listdir2+0x110>
mov    %rbx,%rdi
call   1090 <checkfilename@plt>
cmp    $0x1,%eax
mov    %r15,%rbx
je     1200 <listdir2+0x50>
mov    %rbx,%rdi
lea    0xd6d(%rip),%rsi
call   10a0 <fopen@plt>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
test   %rax,%rax
jne    11f8 <listdir2+0x48>
call   10d0 <destNotExist@plt>
jmp    1200 <listdir2+0x50>
xchg   %ax,%ax
mov    0x0(%rbp),%rdi
lea    0xd4c(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
mov    %r15,%rbx
je     1200 <listdir2+0x50>
mov    0x0(%rbp),%rdi
lea    0xd2f(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1200 <listdir2+0x50>
lea    0x10(%rsp),%rdi
mov    0x8(%rsp),%rsi
call   10c0 <listdir2@plt>
jmp    1200 <listdir2+0x50>
mov    %r14,%rdi
call   1060 <closedir@plt>
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd6(%rip),%rdi
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <perror@plt>

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
%struct.stat = type { i32 }
@.str = [13 x i8] c"Source error\00"
@.str.1 = [9 x i8] c"%s/%s/%s\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@DT_REG = global i64 0
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [3 x i8] c"..\00"
define void @listdir2(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.dirent*
%6 = alloca %struct.stat
%7 = alloca i32*
%8 = alloca [1024 x i8]
%9 = alloca [1024 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%13 = load i8*, i8** %3
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %89
19: ; preds = %2
br label %20
20: ; preds = %85, %80, %53, %19
%21 = load i32*, i32** %7
%22 = call %struct.dirent* @readdir(i32* %21)
store %struct.dirent* %22, %struct.dirent** %5
%23 = icmp ne %struct.dirent* %22, null
br i1 %23, label %24, label %86
24: ; preds = %20
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%26 = load i8*, i8** %4
%27 = load i8*, i8** %3
%28 = load %struct.dirent*, %struct.dirent** %5
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %25, i32 1023, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* %27, i8* %30)
store i32 %31, i32* %10
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%33 = load i8*, i8** %3
%34 = load %struct.dirent*, %struct.dirent** %5
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %32, i32 1023, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %33, i8* %36)
store i32 %37, i32* %11
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %39
store i8 0, i8* %40
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %42
store i8 0, i8* %43
%44 = load %struct.dirent*, %struct.dirent** %5
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 1
%46 = load i64, i64* %45
%47 = load i64, i64* @DT_REG
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %68
49: ; preds = %24
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%51 = call i32 @checkfilename(i8* %50)
%52 = icmp eq i32 %51, 1
br i1 %52, label %53, label %54
53: ; preds = %49
br label %20
54: ; preds = %49
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%56 = call i32* @fopen(i8* %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %56, i32** %12
%57 = load i32*, i32** %12
%58 = icmp eq i32* %57, null
br i1 %58, label %59, label %63
59: ; preds = %54
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%62 = call i32 @destNotExist(i8* %60, i8* %61)
br label %67
63: ; preds = %54
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%66 = call i32 @destExist(i8* %64, i8* %65)
br label %67
67: ; preds = %63, %59
br label %85
68: ; preds = %24
%69 = load %struct.dirent*, %struct.dirent** %5
%70 = getelementptr inbounds %struct.dirent, %struct.dirent* %69, i32 0, i32 0
%71 = load i8*, i8** %70
%72 = call i64 @strcmp(i8* %71, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%73 = icmp eq i64 %72, 0
br i1 %73, label %80, label %74
74: ; preds = %68
%75 = load %struct.dirent*, %struct.dirent** %5
%76 = getelementptr inbounds %struct.dirent, %struct.dirent* %75, i32 0, i32 0
%77 = load i8*, i8** %76
%78 = call i64 @strcmp(i8* %77, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%79 = icmp eq i64 %78, 0
br i1 %79, label %80, label %81
80: ; preds = %74, %68
br label %20
81: ; preds = %74
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%83 = load i8*, i8** %4
br label %84
84: ; preds = %81
br label %85
85: ; preds = %84, %67
br label %20
86: ; preds = %20
%87 = load i32*, i32** %7
%88 = call i32 @closedir(i32* %87)
br label %89
89: ; preds = %86, %17
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@.str = [13 x i8] c"Source error\00"
@.str.1 = [9 x i8] c"%s/%s/%s\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@DT_REG = global i64 0
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [3 x i8] c"..\00"
define void @listdir2(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = tail call i32* @opendir(i8* %0)
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = icmp eq i32* %5, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = call %struct.dirent* @readdir(i32* %5)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %51, label %14
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %53
14: ; preds = %9, %48
%15 = phi %struct.dirent* [ %49, %48 ], [ %10, %9 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %6, i32 1023, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %0, i8* %17)
%19 = load i8*, i8** %16
%20 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %7, i32 1023, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %19)
%21 = sext i32 %18 to i64
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %21
store i8 0, i8* %22
%23 = sext i32 %20 to i64
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %23
store i8 0, i8* %24
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 1
%26 = load i64, i64* %25
%27 = load i64, i64* @DT_REG
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %39
29: ; preds = %14
%30 = call i32 @checkfilename(i8* %7)
%31 = icmp eq i32 %30, 1
br i1 %31, label %48, label %32
32: ; preds = %29
%33 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @destNotExist(i8* %7, i8* %6)
br label %48
37: ; preds = %32
%38 = call i32 @destExist(i8* %7, i8* %6)
br label %48
39: ; preds = %14
%40 = load i8*, i8** %16
%41 = call i64 @strcmp(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %48, label %43
43: ; preds = %39
%44 = load i8*, i8** %16
%45 = call i64 @strcmp(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%46 = icmp eq i64 %45, 0
br i1 %46, label %48, label %47
47: ; preds = %43
br label %48
48: ; preds = %47, %37, %35, %39, %43, %29
%49 = call %struct.dirent* @readdir(i32* %5)
%50 = icmp eq %struct.dirent* %49, null
br i1 %50, label %51, label %14
51: ; preds = %48, %9
%52 = call i32 @closedir(i32* %5)
br label %53
53: ; preds = %51, %12
ret void
}