#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 */
#define S_IFDIR 129
#define S_IFREG 128
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int cp_file(char *, char *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ path_append_restore(char *);
char *path_append_store(char *);
int path_type(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int cp_recursive(char *src, char *dst) {
  int src_type;
  src_type = path_type(src);
  if (src_type == S_IFDIR) {
    char *src_store, *dst_store;
    struct dirent *dent;
    DIR *dir;
    dir = opendir(src);
    if (!dir) {
      return -errno;
    }
    src_store = path_append_store(src);
    dst_store = path_append_store(dst);
    while (NULL != (dent = readdir(dir))) {
      strcpy(src_store, "/");
      strcat(src_store, dent->d_name);
      strcpy(dst_store, "/");
      strcat(dst_store, dent->d_name);
      switch (path_type(src)) {
      case S_IFDIR:
        if (0 > mkdir(dst, 0777)) {
          return -errno;
        }
        cp_recursive(src, dst);
        break;
      case S_IFREG:
        cp_file(src, dst);
        break;
      default:
        break;
      }
    }
    closedir(dir);
    path_append_restore(src_store);
    path_append_restore(dst_store);
  } else if (src_type == S_IFREG) {
    return cp_file(src, dst);
  } else if (src_type > 0) {
    fprintf(stderr, "cp: "
                    "only directiry or file can be copied\n");
    return 0;
  } else {
    return src_type;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cp_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   10e0 <path_type@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x81,-0x1c(%rbp)
jne    1334 <cp_recursive+0x174>
mov    -0x10(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1214 <cp_recursive+0x54>
xor    %eax,%eax
mov    0x2dce(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1393 <cp_recursive+0x1d3>
mov    -0x10(%rbp),%rdi
call   1090 <path_append_store@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <path_append_store@plt>
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,%rcx
mov    %rcx,-0x38(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     1314 <cp_recursive+0x154>
mov    -0x28(%rbp),%rdi
lea    0xdac(%rip),%rsi
mov    %rsi,-0x50(%rbp)
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
call   10d0 <strcat@plt>
mov    -0x50(%rbp),%rsi
mov    -0x30(%rbp),%rdi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
call   10d0 <strcat@plt>
mov    -0x10(%rbp),%rdi
call   10e0 <path_type@plt>
mov    %eax,%ecx
mov    %ecx,-0x44(%rbp)
sub    $0x80,%eax
je     12f8 <cp_recursive+0x138>
jmp    12a6 <cp_recursive+0xe6>
mov    -0x44(%rbp),%eax
sub    $0x81,%eax
jne    130a <cp_recursive+0x14a>
jmp    12b9 <cp_recursive+0xf9>
mov    -0x18(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jle    12e6 <cp_recursive+0x126>
xor    %eax,%eax
mov    0x2cfc(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1393 <cp_recursive+0x1d3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <cp_recursive@plt>
jmp    130f <cp_recursive+0x14f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10c0 <cp_file@plt>
jmp    130f <cp_recursive+0x14f>
jmp    130f <cp_recursive+0x14f>
jmp    122e <cp_recursive+0x6e>
mov    -0x40(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x28(%rbp),%rdi
call   1050 <path_append_restore@plt>
mov    -0x30(%rbp),%rdi
call   1050 <path_append_restore@plt>
jmp    138c <cp_recursive+0x1cc>
cmpl   $0x80,-0x1c(%rbp)
jne    1356 <cp_recursive+0x196>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10c0 <cp_file@plt>
mov    %eax,-0x4(%rbp)
jmp    1393 <cp_recursive+0x1d3>
cmpl   $0x0,-0x1c(%rbp)
jle    1381 <cp_recursive+0x1c1>
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
lea    0xc92(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1393 <cp_recursive+0x1d3>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1393 <cp_recursive+0x1d3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp_recursive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%r15
call   10e0 <path_type@plt>
cmp    $0x80,%eax
je     12d3 <cp_recursive+0x113>
cmp    $0x81,%eax
jne    12ec <cp_recursive+0x12c>
mov    %r15,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     1323 <cp_recursive+0x163>
mov    %rax,%rbx
mov    %r15,%rdi
call   1090 <path_append_store@plt>
mov    %rax,%r12
mov    %rbp,(%rsp)
mov    %rbp,%rdi
call   1090 <path_append_store@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
mov    %rbx,%rbp
je     1307 <cp_recursive+0x147>
mov    %rax,%r14
lea    0xdc8(%rip),%rbx
jmp    1260 <cp_recursive+0xa0>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    (%rsp),%rsi
call   10c0 <cp_file@plt>
mov    %rbp,%rdi
call   10b0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1307 <cp_recursive+0x147>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    (%r14),%esi
mov    %r12,%rdi
call   10d0 <strcat@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    (%r14),%esi
mov    %r13,%rdi
call   10d0 <strcat@plt>
mov    %r15,%rdi
call   10e0 <path_type@plt>
cmp    $0x80,%eax
je     1240 <cp_recursive+0x80>
cmp    $0x81,%eax
jne    124c <cp_recursive+0x8c>
mov    %rbx,%r14
mov    %rbp,%rbx
mov    (%rsp),%rbp
mov    %rbp,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
test   %eax,%eax
js     1323 <cp_recursive+0x163>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1070 <cp_recursive@plt>
mov    %rbx,%rbp
mov    %r14,%rbx
jmp    124c <cp_recursive+0x8c>
mov    %r15,%rdi
mov    %rbp,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10c0 <cp_file@plt>
test   %eax,%eax
jle    132e <cp_recursive+0x16e>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    0xd02(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    131f <cp_recursive+0x15f>
mov    %rbp,%rdi
call   1080 <closedir@plt>
mov    %r12,%rdi
call   1050 <path_append_restore@plt>
mov    %r13,%rdi
call   1050 <path_append_restore@plt>
xor    %eax,%eax
jmp    132e <cp_recursive+0x16e>
mov    0x2cae(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@errno = global i32 0
@.str = [2 x i8] c"/\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"cp: only directiry or file can be copied\0A\00"
define i32 @cp_recursive(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.dirent*
%10 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = call i32 @path_type(i8* %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 129
br i1 %14, label %15, label %73
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32* @opendir(i8* %16)
store i32* %17, i32** %10
%18 = load i32*, i32** %10
%19 = icmp ne i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
store i32 %22, i32* %3
br label %89
23: ; preds = %15
%24 = load i8*, i8** %4
%25 = call i8* @path_append_store(i8* %24)
store i8* %25, i8** %7
%26 = load i8*, i8** %5
%27 = call i8* @path_append_store(i8* %26)
store i8* %27, i8** %8
br label %28
28: ; preds = %65, %23
%29 = load i32*, i32** %10
%30 = call %struct.dirent* @readdir(i32* %29)
store %struct.dirent* %30, %struct.dirent** %9
%31 = icmp ne %struct.dirent* null, %30
br i1 %31, label %32, label %66
32: ; preds = %28
%33 = load i8*, i8** %7
%34 = call i32 @strcpy(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%35 = load i8*, i8** %7
%36 = load %struct.dirent*, %struct.dirent** %9
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @strcat(i8* %35, i32 %38)
%40 = load i8*, i8** %8
%41 = call i32 @strcpy(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%42 = load i8*, i8** %8
%43 = load %struct.dirent*, %struct.dirent** %9
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @strcat(i8* %42, i32 %45)
%47 = load i8*, i8** %4
%48 = call i32 @path_type(i8* %47)
switch i32 %48, label %64 [
i32 129, label %49
i32 128, label %60
]
49: ; preds = %32
%50 = load i8*, i8** %5
%51 = call i32 @mkdir(i8* %50, i32 511)
%52 = icmp sgt i32 0, %51
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* @errno
%55 = sub nsw i32 0, %54
store i32 %55, i32* %3
br label %89
56: ; preds = %49
%57 = load i8*, i8** %4
%58 = load i8*, i8** %5
%59 = call i32 @cp_recursive(i8* %57, i8* %58)
br label %65
60: ; preds = %32
%61 = load i8*, i8** %4
%62 = load i8*, i8** %5
%63 = call i32 @cp_file(i8* %61, i8* %62)
br label %65
64: ; preds = %32
br label %65
65: ; preds = %64, %60, %56
br label %28
66: ; preds = %28
%67 = load i32*, i32** %10
%68 = call i32 @closedir(i32* %67)
%69 = load i8*, i8** %7
%70 = call i32 @path_append_restore(i8* %69)
%71 = load i8*, i8** %8
%72 = call i32 @path_append_restore(i8* %71)
br label %88
73: ; preds = %2
%74 = load i32, i32* %6
%75 = icmp eq i32 %74, 128
br i1 %75, label %76, label %80
76: ; preds = %73
%77 = load i8*, i8** %4
%78 = load i8*, i8** %5
%79 = call i32 @cp_file(i8* %77, i8* %78)
store i32 %79, i32* %3
br label %89
80: ; preds = %73
%81 = load i32, i32* %6
%82 = icmp sgt i32 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %80
%84 = load i32, i32* @stderr
%85 = call i32 @fprintf(i32 %84, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %89
86: ; preds = %80
%87 = load i32, i32* %6
store i32 %87, i32* %3
br label %89
88: ; preds = %66
store i32 0, i32* %3
br label %89
89: ; preds = %88, %86, %83, %76, %53, %20
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@errno = global i32 0
@.str = [2 x i8] c"/\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"cp: only directiry or file can be copied\0A\00"
define i32 @cp_recursive(i8* %0, i8* %1) {
%3 = tail call i32 @path_type(i8* %0)
switch i32 %3, label %44 [
i32 129, label %4
i32 128, label %42
]
4: ; preds = %2
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = load i32, i32* @errno
%9 = sub nsw i32 0, %8
br label %49
10: ; preds = %4
%11 = tail call i8* @path_append_store(i8* %0)
%12 = tail call i8* @path_append_store(i8* %1)
%13 = tail call %struct.dirent* @readdir(i32* %5)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %38, label %15
15: ; preds = %10, %35
%16 = phi %struct.dirent* [ %36, %35 ], [ %13, %10 ]
%17 = tail call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i32 @strcat(i8* %11, i32 %19)
%21 = tail call i32 @strcpy(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* %18
%23 = tail call i32 @strcat(i8* %12, i32 %22)
%24 = tail call i32 @path_type(i8* %0)
switch i32 %24, label %35 [
i32 129, label %25
i32 128, label %33
]
25: ; preds = %15
%26 = tail call i32 @mkdir(i8* %1, i32 511)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @errno
%30 = sub nsw i32 0, %29
br label %49
31: ; preds = %25
%32 = tail call i32 @cp_recursive(i8* %0, i8* %1)
br label %35
33: ; preds = %15
%34 = tail call i32 @cp_file(i8* %0, i8* %1)
br label %35
35: ; preds = %15, %33, %31
%36 = tail call %struct.dirent* @readdir(i32* %5)
%37 = icmp eq %struct.dirent* %36, null
br i1 %37, label %38, label %15
38: ; preds = %35, %10
%39 = tail call i32 @closedir(i32* %5)
%40 = tail call i32 @path_append_restore(i8* %11)
%41 = tail call i32 @path_append_restore(i8* %12)
br label %49
42: ; preds = %2
%43 = tail call i32 @cp_file(i8* %0, i8* %1)
br label %49
44: ; preds = %2
%45 = icmp sgt i32 %3, 0
br i1 %45, label %46, label %49
46: ; preds = %44
%47 = load i32, i32* @stderr
%48 = tail call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %7, %28, %38, %44, %46, %42
%50 = phi i32 [ %43, %42 ], [ 0, %46 ], [ %3, %44 ], [ 0, %38 ], [ %9, %7 ], [ %30, %28 ]
ret i32 %50
}