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

/* Variables and functions */
int /*<<< orphan*/ F_OK;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ copy_file(char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int copy_dir(char *src, char *dst, int depth) {
  DIR *dir;
  struct dirent *de;
  struct stat src_st;
  char src_path[4096];
  char dst_path[4096];
  if (depth > 3) {
    fprintf(stderr, "too deep\n");
    return 0;
  }
  if (access(dst, F_OK) != 0) {
    if (stat(src, &src_st) == 0) {
      if (mkdir(dst, src_st.st_mode)) {
        perror("mkdir");
        exit(1);
      }
    }
  }
  dir = opendir(src);
  while ((de = readdir(dir))) {
    if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) {
      continue;
    }
    strcpy(src_path, src);
    strcat(src_path, "/");
    strcat(src_path, de->d_name);
    stat(src_path, &src_st);
    strcpy(dst_path, dst);
    strcat(dst_path, "/");
    strcat(dst_path, de->d_name);
    if (S_ISDIR(src_st.st_mode)) {
      copy_dir(src_path, dst_path, depth + 1);
    } else {
      copy_file(src_path, dst_path);
    }
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<copy_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jle    1231 <copy_dir+0x41>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
call   10b0 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13d1 <copy_dir+0x1e1>
mov    -0x18(%rbp),%rdi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
call   10e0 <access@plt>
cmp    $0x0,%rax
je     129a <copy_dir+0xaa>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10c0 <stat@plt>
cmp    $0x0,%rax
jne    1295 <copy_dir+0xa5>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%esi
call   1050 <mkdir@plt>
cmp    $0x0,%rax
je     1290 <copy_dir+0xa0>
lea    0xd89(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
jmp    1295 <copy_dir+0xa5>
jmp    129a <copy_dir+0xaa>
mov    -0x10(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     13c1 <copy_dir+0x1d1>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xd45(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     12f8 <copy_dir+0x108>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xd27(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    12fd <copy_dir+0x10d>
jmp    12a7 <copy_dir+0xb7>
lea    -0x1040(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x1040(%rbp),%rdi
lea    0xcf8(%rip),%rsi
call   1100 <strcat@plt>
lea    -0x1040(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   1100 <strcat@plt>
lea    -0x1040(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10c0 <stat@plt>
lea    -0x2040(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x2040(%rbp),%rdi
lea    0xcb2(%rip),%rsi
call   1100 <strcat@plt>
lea    -0x2040(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   1100 <strcat@plt>
mov    -0x38(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     13a9 <copy_dir+0x1b9>
lea    -0x1040(%rbp),%rdi
lea    -0x2040(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   10a0 <copy_dir@plt>
jmp    13bc <copy_dir+0x1cc>
lea    -0x1040(%rbp),%rdi
lea    -0x2040(%rbp),%rsi
call   1030 <copy_file@plt>
jmp    12a7 <copy_dir+0xb7>
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2028,%rsp
mov    %rdi,0x10(%rsp)
mov    %edx,0xc(%rsp)
cmp    $0x4,%edx
jl     1229 <copy_dir+0x39>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
call   10b0 <fprintf@plt>
jmp    1382 <copy_dir+0x192>
mov    %rsi,%rbx
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10e0 <access@plt>
test   %rax,%rax
je     126b <copy_dir+0x7b>
lea    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdi
call   10c0 <stat@plt>
test   %rax,%rax
jne    126b <copy_dir+0x7b>
mov    0x8(%rsp),%esi
mov    %rbx,%rdi
call   1050 <mkdir@plt>
test   %rax,%rax
jne    1396 <copy_dir+0x1a6>
mov    %rbx,0x18(%rsp)
mov    0x10(%rsp),%rdi
call   1060 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10d0 <readdir@plt>
test   %rax,%rax
je     137a <copy_dir+0x18a>
mov    %rax,%r12
addl   $0x1,0xc(%rsp)
lea    0xd74(%rip),%rbp
lea    0x1020(%rsp),%r13
lea    0xd67(%rip),%r14
lea    0x20(%rsp),%r15
jmp    12dd <copy_dir+0xed>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0xc(%rsp),%edx
call   10a0 <copy_dir@plt>
mov    %rbx,%rdi
call   10d0 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     137a <copy_dir+0x18a>
mov    (%r12),%rdi
mov    %rbp,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     12c9 <copy_dir+0xd9>
mov    (%r12),%rdi
lea    0xd17(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     12c9 <copy_dir+0xd9>
mov    %r13,%rdi
mov    0x10(%rsp),%rsi
call   1040 <strcpy@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   1100 <strcat@plt>
mov    (%r12),%rsi
mov    %r13,%rdi
call   1100 <strcat@plt>
mov    %r13,%rdi
lea    0x8(%rsp),%rsi
call   10c0 <stat@plt>
mov    %r15,%rdi
mov    0x18(%rsp),%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1100 <strcat@plt>
mov    (%r12),%rsi
mov    %r15,%rdi
call   1100 <strcat@plt>
mov    0x8(%rsp),%edi
call   1070 <S_ISDIR@plt>
mov    %r13,%rdi
mov    %r15,%rsi
test   %rax,%rax
jne    12c0 <copy_dir+0xd0>
call   1030 <copy_file@plt>
jmp    12c9 <copy_dir+0xd9>
mov    %rbx,%rdi
call   1080 <closedir@plt>
xor    %eax,%eax
add    $0x2028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc6d(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [10 x i8] c"too deep\0A\00"
@F_OK = global i32 0
@.str.1 = [6 x i8] c"mkdir\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [2 x i8] c"/\00"
define i32 @copy_dir(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca %struct.stat
%11 = alloca [4096 x i8]
%12 = alloca [4096 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%13 = load i32, i32* %7
%14 = icmp sgt i32 %13, 3
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %99
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = load i32, i32* @F_OK
%21 = call i64 @access(i8* %19, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %38
23: ; preds = %18
%24 = load i8*, i8** %5
%25 = call i64 @stat(i8* %24, %struct.stat* %10)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %23
%28 = load i8*, i8** %6
%29 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @mkdir(i8* %28, i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
br label %37
37: ; preds = %36, %23
br label %38
38: ; preds = %37, %18
%39 = load i8*, i8** %5
%40 = call i32* @opendir(i8* %39)
store i32* %40, i32** %8
br label %41
41: ; preds = %95, %57, %38
%42 = load i32*, i32** %8
%43 = call %struct.dirent* @readdir(i32* %42)
store %struct.dirent* %43, %struct.dirent** %9
%44 = icmp ne %struct.dirent* %43, null
br i1 %44, label %45, label %96
45: ; preds = %41
%46 = load %struct.dirent*, %struct.dirent** %9
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i64 @strcmp(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%50 = icmp eq i64 %49, 0
br i1 %50, label %57, label %51
51: ; preds = %45
%52 = load %struct.dirent*, %struct.dirent** %9
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = call i64 @strcmp(i8* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %51, %45
br label %41
58: ; preds = %51
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%60 = load i8*, i8** %5
%61 = call i32 @strcpy(i8* %59, i8* %60)
%62 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%63 = call i32 @strcat(i8* %62, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%65 = load %struct.dirent*, %struct.dirent** %9
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i8*, i8** %66
%68 = call i32 @strcat(i8* %64, i8* %67)
%69 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%70 = call i64 @stat(i8* %69, %struct.stat* %10)
%71 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%72 = load i8*, i8** %6
%73 = call i32 @strcpy(i8* %71, i8* %72)
%74 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%75 = call i32 @strcat(i8* %74, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%76 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%77 = load %struct.dirent*, %struct.dirent** %9
%78 = getelementptr inbounds %struct.dirent, %struct.dirent* %77, i32 0, i32 0
%79 = load i8*, i8** %78
%80 = call i32 @strcat(i8* %76, i8* %79)
%81 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%82 = load i32, i32* %81
%83 = call i64 @S_ISDIR(i32 %82)
%84 = icmp ne i64 %83, 0
br i1 %84, label %85, label %91
85: ; preds = %58
%86 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%87 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%88 = load i32, i32* %7
%89 = add nsw i32 %88, 1
%90 = call i32 @copy_dir(i8* %86, i8* %87, i32 %89)
br label %95
91: ; preds = %58
%92 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%93 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%94 = call i32 @copy_file(i8* %92, i8* %93)
br label %95
95: ; preds = %91, %85
br label %41
96: ; preds = %41
%97 = load i32*, i32** %8
%98 = call i32 @closedir(i32* %97)
store i32 0, i32* %4
br label %99
99: ; preds = %96, %15
%100 = load i32, i32* %4
ret i32 %100
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [10 x i8] c"too deep\0A\00"
@F_OK = global i32 0
@.str.1 = [6 x i8] c"mkdir\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [2 x i8] c"/\00"
define i32 @copy_dir(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.stat
%5 = alloca [4096 x i8]
%6 = alloca [4096 x i8]
%7 = bitcast %struct.stat* %4 to i8*
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%10 = icmp sgt i32 %2, 3
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %68
14: ; preds = %3
%15 = load i32, i32* @F_OK
%16 = tail call i64 @access(i8* %1, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %29, label %18
18: ; preds = %14
%19 = call i64 @stat(i8* %0, %struct.stat* %4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %18
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i64 @mkdir(i8* %1, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %18, %21, %14
%30 = call i32* @opendir(i8* %0)
%31 = call %struct.dirent* @readdir(i32* %30)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %66, label %33
33: ; preds = %29
%34 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%35 = add nsw i32 %2, 1
br label %36
36: ; preds = %33, %46
%37 = phi %struct.dirent* [ %31, %33 ], [ %47, %46 ]
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i64 0, i32 0
%39 = load i8*, i8** %38
%40 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %36
%43 = load i8*, i8** %38
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %62, %64, %36, %42
%47 = call %struct.dirent* @readdir(i32* %30)
%48 = icmp eq %struct.dirent* %47, null
br i1 %48, label %66, label %36
49: ; preds = %42
%50 = call i32 @strcpy(i8* %8, i8* %0)
%51 = call i32 @strcat(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%52 = load i8*, i8** %38
%53 = call i32 @strcat(i8* %8, i8* %52)
%54 = call i64 @stat(i8* %8, %struct.stat* %4)
%55 = call i32 @strcpy(i8* %9, i8* %1)
%56 = call i32 @strcat(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%57 = load i8*, i8** %38
%58 = call i32 @strcat(i8* %9, i8* %57)
%59 = load i32, i32* %34
%60 = call i64 @S_ISDIR(i32 %59)
%61 = icmp eq i64 %60, 0
br i1 %61, label %64, label %62
62: ; preds = %49
%63 = call i32 @copy_dir(i8* %8, i8* %9, i32 %35)
br label %46
64: ; preds = %49
%65 = call i32 @copy_file(i8* %8, i8* %9)
br label %46
66: ; preds = %46, %29
%67 = call i32 @closedir(i32* %30)
br label %68
68: ; preds = %66, %11
ret i32 0
}