#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;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ copy(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, int);
char *sstrdup(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int dircopy(int argc, char **argv) {
  char *dir = argv[argc - 1];
  struct stat st;
  if (stat(dir, &st) < 0) {
    perror("cp");
    return -1;
  }
  if (!S_ISDIR(st.st_mode)) {
    fprintf(stderr, "cp: target '%s' not a directory\n", dir);
    return -1;
  }
  int i, ret = 0;
  for (i = 1; i < argc - 1; i++) {
    char *target = sstrdup(dir);
    int len = strlen(target);
    if (target[len - 1] != '/') {
      target = realloc(target, ++len + 1);
      strcat(target, "/");
    }
    len += strlen(argv[i]);
    target = realloc(target, len + 1);
    strcat(target, argv[i]);
    if (copy(argv[i], target) < 0) {
      perror("cp");
      ret = -1;
    }
    free(target);
  }
  return ret;
}


x86 assembly (clang, -O0):
<dircopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jge    11f3 <dircopy+0x53>
lea    0xe1e(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <dircopy+0x192>
mov    -0x20(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1229 <dircopy+0x89>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdeb(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <dircopy+0x192>
movl   $0x0,-0x28(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    132c <dircopy+0x18c>
mov    -0x18(%rbp),%rdi
call   1060 <sstrdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     12a4 <dircopy+0x104>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
add    $0x1,%esi
call   10a0 <realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xd85(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
add    $0x1,%esi
call   10a0 <realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   10c0 <strcat@plt>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <copy@plt>
cmp    $0x0,%rax
jge    1315 <dircopy+0x175>
lea    0xcf7(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1237 <dircopy+0x97>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dircopy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movslq %edi,%rax
mov    -0x8(%rsi,%rax,8),%r14
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1090 <stat@plt>
test   %rax,%rax
js     1306 <dircopy+0x166>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     12d3 <dircopy+0x133>
movl   $0x0,0x4(%rsp)
cmp    $0x3,%ebp
jl     12f3 <dircopy+0x153>
add    $0xffffffff,%ebp
mov    %ebp,%r12d
movl   $0x0,0x4(%rsp)
mov    $0x1,%r13d
mov    %rbx,0x10(%rsp)
jmp    125d <dircopy+0xbd>
xchg   %ax,%ax
mov    (%rbx,%r13,8),%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%esi
add    $0x1,%esi
mov    %rbp,%rdi
call   10a0 <realloc@plt>
mov    %rax,%rbp
mov    (%rbx,%r13,8),%rsi
mov    %rax,%rdi
call   10c0 <strcat@plt>
mov    (%rbx,%r13,8),%rdi
mov    %rbp,%rsi
call   1070 <copy@plt>
test   %rax,%rax
js     12ba <dircopy+0x11a>
mov    %rbp,%rdi
call   1030 <free@plt>
add    $0x1,%r13
cmp    %r13,%r12
je     12f3 <dircopy+0x153>
mov    %r14,%rdi
call   1060 <sstrdup@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
cltq
cmpb   $0x2f,-0x1(%rax,%rbp,1)
je     1210 <dircopy+0x70>
mov    %r12,%rbx
mov    %r14,%r12
lea    0x1(%r15),%r14d
add    $0x2,%r15d
mov    %rbp,%rdi
mov    %r15d,%esi
call   10a0 <realloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
lea    0xd82(%rip),%rsi
call   10c0 <strcat@plt>
mov    %r14d,%r15d
mov    %r12,%r14
mov    %rbx,%r12
mov    0x10(%rsp),%rbx
jmp    1210 <dircopy+0x70>
lea    0xd3f(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,0x4(%rsp)
ff
jmp    1248 <dircopy+0xa8>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
lea    0xd20(%rip),%rsi
mov    %r14,%rdx
call   1080 <fprintf@plt>
movl   $0xffffffff,0x4(%rsp)
ff
mov    0x4(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf3(%rip),%rdi
call   10b0 <perror@plt>
jmp    12eb <dircopy+0x14b>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"cp\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"cp: target '%s' not a directory\0A\00"
@.str.2 = [2 x i8] c"/\00"
define i32 @dircopy(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = add nsw i32 %0, -1
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8*, i8** %1, i64 %5
%7 = load i8*, i8** %6
%8 = bitcast %struct.stat* %3 to i8*
%9 = call i64 @stat(i8* %7, %struct.stat* %3)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %61
13: ; preds = %2
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @S_ISDIR(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = icmp sgt i32 %0, 2
br i1 %19, label %20, label %61
20: ; preds = %18
%21 = zext i32 %4 to i64
br label %25
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %61
25: ; preds = %20, %56
%26 = phi i64 [ 1, %20 ], [ %59, %56 ]
%27 = phi i32 [ 0, %20 ], [ %57, %56 ]
%28 = call i8* @sstrdup(i8* %7)
%29 = call i32 @strlen(i8* %28)
%30 = add nsw i32 %29, -1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 47
br i1 %34, label %40, label %35
35: ; preds = %25
%36 = add nsw i32 %29, 1
%37 = add nsw i32 %29, 2
%38 = call i8* @realloc(i8* %28, i32 %37)
%39 = call i32 @strcat(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %35, %25
%41 = phi i8* [ %38, %35 ], [ %28, %25 ]
%42 = phi i32 [ %36, %35 ], [ %29, %25 ]
%43 = getelementptr inbounds i8*, i8** %1, i64 %26
%44 = load i8*, i8** %43
%45 = call i32 @strlen(i8* %44)
%46 = add i32 %42, 1
%47 = add i32 %46, %45
%48 = call i8* @realloc(i8* %41, i32 %47)
%49 = load i8*, i8** %43
%50 = call i32 @strcat(i8* %48, i8* %49)
%51 = load i8*, i8** %43
%52 = call i64 @copy(i8* %51, i8* %48)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %40
%55 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %56
56: ; preds = %54, %40
%57 = phi i32 [ -1, %54 ], [ %27, %40 ]
%58 = call i32 @free(i8* %48)
%59 = add nuw nsw i64 %26, 1
%60 = icmp eq i64 %59, %21
br i1 %60, label %61, label %25
61: ; preds = %56, %18, %22, %11
%62 = phi i32 [ -1, %11 ], [ -1, %22 ], [ 0, %18 ], [ %57, %56 ]
ret i32 %62
}