#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*/ DIR;

/* Variables and functions */
scalar_t__ DT_REG;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int snprintf(char *, int, char *, char const *, char const *, ...);
int stat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
void listdir(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) {
      continue;
    } else {
      if (strcmp(pDirent->d_name, ".") == 0 ||
          strcmp(pDirent->d_name, "..") == 0)
        continue;
      else {
        if (stat(destfile, &st) == -1) {
          mkdir(destfile, 0700);
        }
        listdir(sourcefile, destname);
      }
    }
  }
  closedir(pDir);
}


x86 assembly (clang, -O0):
<listdir>:
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    11cc <listdir+0x3c>
lea    0xe3e(%rip),%rdi
call   10b0 <perror@plt>
jmp    1309 <listdir+0x179>
jmp    11d1 <listdir+0x41>
mov    -0x28(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1300 <listdir+0x170>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%r8
mov    -0x18(%rbp),%rax
mov    (%rax),%r9
mov    $0x3ff,%esi
lea    0xe03(%rip),%rdx
mov    $0x0,%al
call   1060 <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    0xddb(%rip),%rdx
mov    $0x0,%al
call   1060 <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    0x2d71(%rip),%rcx
cmp    (%rcx),%rax
jne    127d <listdir+0xed>
jmp    11d1 <listdir+0x41>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xd8c(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12b7 <listdir+0x127>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xd6e(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12bc <listdir+0x12c>
jmp    11d1 <listdir+0x41>
lea    -0x430(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
jne    12e6 <listdir+0x156>
lea    -0x430(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
lea    -0x830(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <listdir@plt>
jmp    12fb <listdir+0x16b>
jmp    11d1 <listdir+0x41>
mov    -0x28(%rbp),%rdi
call   1070 <closedir@plt>
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listdir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <opendir@plt>
test   %rax,%rax
je     12dc <listdir+0x14c>
mov    %rax,%r15
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12c2 <listdir+0x132>
mov    %rax,%rbp
lea    0x410(%rsp),%r13
jmp    1204 <listdir+0x74>
lea    0x10(%rsp),%rdi
mov    %r14,%rsi
call   1050 <listdir@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12c2 <listdir+0x132>
mov    0x0(%rbp),%r9
mov    %r13,%rdi
mov    $0x3ff,%esi
lea    0xdf6(%rip),%rdx
mov    %r14,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %eax,%r12d
mov    0x0(%rbp),%r8
lea    0x10(%rsp),%rdi
mov    $0x3ff,%esi
lea    0xdd4(%rip),%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1060 <snprintf@plt>
movslq %r12d,%rcx
movb   $0x0,0x410(%rsp,%rcx,1)
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    0x8(%rbp),%rax
mov    0x2d7d(%rip),%rcx
cmp    (%rcx),%rax
je     11f0 <listdir+0x60>
mov    0x0(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11f0 <listdir+0x60>
mov    0x0(%rbp),%rdi
lea    0xd8a(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11f0 <listdir+0x60>
mov    %r13,%rdi
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
jne    11d6 <listdir+0x46>
mov    %r13,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
jmp    11d6 <listdir+0x46>
mov    %r15,%rdi
call   1070 <closedir@plt>
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd1d(%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".\00"
@.str.4 = [3 x i8] c"..\00"
define void @listdir(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
store i8* %0, i8** %3
store i8* %1, i8** %4
%12 = load i8*, i8** %3
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %77
18: ; preds = %2
br label %19
19: ; preds = %73, %61, %48, %18
%20 = load i32*, i32** %7
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %5
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %74
23: ; preds = %19
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%25 = load i8*, i8** %4
%26 = load i8*, i8** %3
%27 = load %struct.dirent*, %struct.dirent** %5
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %24, i32 1023, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %25, i8* %26, i8* %29)
store i32 %30, i32* %10
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%32 = load i8*, i8** %3
%33 = load %struct.dirent*, %struct.dirent** %5
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %31, i32 1023, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %32, i8* %35)
store i32 %36, i32* %11
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %38
store i8 0, i8* %39
%40 = load i32, i32* %11
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %41
store i8 0, i8* %42
%43 = load %struct.dirent*, %struct.dirent** %5
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 1
%45 = load i64, i64* %44
%46 = load i64, i64* @DT_REG
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %23
br label %19
49: ; preds = %23
%50 = load %struct.dirent*, %struct.dirent** %5
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = call i64 @strcmp(i8* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%54 = icmp eq i64 %53, 0
br i1 %54, label %61, label %55
55: ; preds = %49
%56 = load %struct.dirent*, %struct.dirent** %5
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i64 @strcmp(i8* %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55, %49
br label %19
62: ; preds = %55
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%64 = call i32 @stat(i8* %63, %struct.stat* %6)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%68 = call i32 @mkdir(i8* %67, i32 448)
br label %69
69: ; preds = %66, %62
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%71 = load i8*, i8** %4
br label %72
72: ; preds = %69
br label %73
73: ; preds = %72
br label %19
74: ; preds = %19
%75 = load i32*, i32** %7
%76 = call i32 @closedir(i32* %75)
br label %77
77: ; preds = %74, %16
ret void
}

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