#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;
};
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
scalar_t__ EISDIR;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ chmod(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stat(char *, struct stat *);
char *strdup(char *);
scalar_t__ strlen(char *);
char *strrchr(char *, char);
int /*<<< orphan*/ write(int, char *, int);
void cmd_cp(int argc, char *argv[]) {
  int src, dst, len;
  char buff[4096];
  struct stat s;
  char *path, *p, *t;
  path = strdup(argv[2]);
  src = open(argv[1], O_RDONLY);
  if (src == -1) {
    free(path);
    perror("open(src)");
  }
  dst = open(path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
  if (dst == -1) {
    if (errno == EISDIR) {
      t = strrchr(argv[1], '/');
      if (t != NULL) {
        t++;
      } else {
        t = argv[1];
      }
      p = malloc(strlen(path) + strlen(t) + 2);
      sprintf(p, "%s/%s", path, t);
      free(path);
      path = p;
      dst = open(path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
      if (dst == -1) {
        close(src);
        free(path);
        perror("open(dst)");
      }
    } else {
      close(src);
      free(path);
      perror("open(dst)");
    }
  }
  stat(argv[1], &s);
  while (1) {
    len = read(src, buff, sizeof(buff));
    if (len == -1)
      break;
    if (len == 0)
      break;
    write(dst, buff, len);
    if (len < sizeof(buff))
      break;
  }
  close(src);
  close(dst);
  chmod(path, s.st_mode);
  free(path);
}


x86 assembly (clang, -O0):
<cmd_cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10f0 <strdup@plt>
mov    %rax,-0x1030(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1233 <cmd_cp+0x63>
mov    -0x1030(%rbp),%rdi
call   1030 <free@plt>
lea    0xdd2(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x1030(%rbp),%rdi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
mov    0x2dae(%rip),%rax
or     (%rax),%esi
mov    0x2d9d(%rip),%rax
or     (%rax),%esi
mov    0x2d7c(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    13d6 <cmd_cp+0x206>
mov    0x2d37(%rip),%rax
mov    (%rax),%rax
mov    0x2d45(%rip),%rcx
cmp    (%rcx),%rax
jne    13b1 <cmd_cp+0x1e1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x1040(%rbp)
cmpq   $0x0,-0x1040(%rbp)
je     12ca <cmd_cp+0xfa>
mov    -0x1040(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1040(%rbp)
jmp    12d9 <cmd_cp+0x109>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x1040(%rbp)
mov    -0x1030(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x1048(%rbp)
mov    -0x1040(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1048(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x1038(%rbp)
mov    -0x1038(%rbp),%rdi
mov    -0x1030(%rbp),%rdx
mov    -0x1040(%rbp),%rcx
lea    0xcdc(%rip),%rsi
call   10e0 <sprintf@plt>
mov    -0x1030(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1038(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rdi
mov    0x2c75(%rip),%rax
mov    (%rax),%esi
mov    0x2c94(%rip),%rax
or     (%rax),%esi
mov    0x2c83(%rip),%rax
or     (%rax),%esi
mov    0x2c62(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    13ac <cmd_cp+0x1dc>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x1030(%rbp),%rdi
call   1030 <free@plt>
lea    0xc69(%rip),%rdi
call   10d0 <perror@plt>
jmp    13d1 <cmd_cp+0x201>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x1030(%rbp),%rdi
call   1030 <free@plt>
lea    0xc44(%rip),%rdi
call   10d0 <perror@plt>
jmp    13d6 <cmd_cp+0x206>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x1028(%rbp),%rsi
call   1090 <stat@plt>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1410 <cmd_cp+0x240>
jmp    144b <cmd_cp+0x27b>
cmpl   $0x0,-0x1c(%rbp)
jne    141f <cmd_cp+0x24f>
jmp    144b <cmd_cp+0x27b>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
movslq -0x1c(%rbp),%rax
cmp    $0x1000,%rax
jae    1446 <cmd_cp+0x276>
jmp    144b <cmd_cp+0x27b>
jmp    13ea <cmd_cp+0x21a>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x1030(%rbp),%rdi
mov    -0x1028(%rbp),%esi
call   10b0 <chmod@plt>
mov    -0x1030(%rbp),%rdi
call   1030 <free@plt>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_cp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
call   10f0 <strdup@plt>
mov    %rax,%r15
mov    0x8(%rbx),%rdi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   10c0 <open@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
je     1384 <cmd_cp+0x1b4>
mov    0x2db9(%rip),%r12
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
or     (%r12),%esi
mov    0x2dc5(%rip),%rax
or     (%rax),%esi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %eax,%eax
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     124d <cmd_cp+0x7d>
mov    %eax,%r14d
jmp    1303 <cmd_cp+0x133>
mov    0x2d5c(%rip),%rax
mov    (%rax),%rax
mov    0x2d6a(%rip),%rcx
cmp    (%rcx),%rax
jne    139d <cmd_cp+0x1cd>
mov    0x8(%rbx),%rbp
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rbx,%r14
lea    0x1(%rax),%rbx
test   %rax,%rax
cmove  %rbp,%rbx
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x2,%rdi
call   10a0 <malloc@plt>
mov    %r12,%rbp
mov    %rax,%r12
lea    0xd57(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %rbx,%rcx
mov    %r14,%rbx
call   10e0 <sprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
or     0x0(%rbp),%esi
mov    0x2d11(%rip),%rax
or     (%rax),%esi
mov    0x2cf0(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %eax,%eax
call   10c0 <open@plt>
mov    %eax,%r14d
mov    %r12,%r15
cmp    $0xffffffff,%eax
je     139d <cmd_cp+0x1cd>
mov    0x8(%rbx),%rdi
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
lea    0x10(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%eax
cmp    $0x2,%eax
jb     134f <cmd_cp+0x17f>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
cmp    $0xfff,%ebx
ja     1320 <cmd_cp+0x150>
mov    %r13d,%edi
call   1070 <close@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    0x8(%rsp),%esi
mov    %r15,%rdi
call   10b0 <chmod@plt>
mov    %r15,%rdi
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xc6d(%rip),%rdi
call   10d0 <perror@plt>
jmp    1210 <cmd_cp+0x40>
mov    %r13d,%edi
call   1070 <close@plt>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xc5c(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1303 <cmd_cp+0x133>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [10 x i8] c"open(src)\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@errno = global i64 0
@EISDIR = global i64 0
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [10 x i8] c"open(dst)\00"
define void @cmd_cp(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca %struct.stat
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%13 = load i8**, i8*** %4
%14 = getelementptr inbounds i8*, i8** %13, i64 2
%15 = load i8*, i8** %14
%16 = call i8* @strdup(i8* %15)
store i8* %16, i8** %10
%17 = load i8**, i8*** %4
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = call i32 (i8*, i32, ...) @open(i8* %19, i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %2
%25 = load i8*, i8** %10
%26 = call i32 @free(i8* %25)
%27 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %24, %2
%29 = load i8*, i8** %10
%30 = load i32, i32* @O_RDWR
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = load i32, i32* @O_TRUNC
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IRWXU
%36 = call i32 (i8*, i32, ...) @open(i8* %29, i32 %34, i32 %35)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %96
39: ; preds = %28
%40 = load i64, i64* @errno
%41 = load i64, i64* @EISDIR
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %89
43: ; preds = %39
%44 = load i8**, i8*** %4
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i8* @strrchr(i8* %46, i8 signext 47)
store i8* %47, i8** %12
%48 = load i8*, i8** %12
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %53
50: ; preds = %43
%51 = load i8*, i8** %12
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %12
br label %57
53: ; preds = %43
%54 = load i8**, i8*** %4
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
store i8* %56, i8** %12
br label %57
57: ; preds = %53, %50
%58 = load i8*, i8** %10
%59 = call i64 @strlen(i8* %58)
%60 = load i8*, i8** %12
%61 = call i64 @strlen(i8* %60)
%62 = add nsw i64 %59, %61
%63 = add nsw i64 %62, 2
%64 = call i8* @malloc(i64 %63)
store i8* %64, i8** %11
%65 = load i8*, i8** %11
%66 = load i8*, i8** %10
%67 = load i8*, i8** %12
%68 = call i32 @sprintf(i8* %65, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %66, i8* %67)
%69 = load i8*, i8** %10
%70 = call i32 @free(i8* %69)
%71 = load i8*, i8** %11
store i8* %71, i8** %10
%72 = load i8*, i8** %10
%73 = load i32, i32* @O_RDWR
%74 = load i32, i32* @O_CREAT
%75 = or i32 %73, %74
%76 = load i32, i32* @O_TRUNC
%77 = or i32 %75, %76
%78 = load i32, i32* @S_IRWXU
%79 = call i32 (i8*, i32, ...) @open(i8* %72, i32 %77, i32 %78)
store i32 %79, i32* %6
%80 = load i32, i32* %6
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %88
82: ; preds = %57
%83 = load i32, i32* %5
%84 = call i32 @close(i32 %83)
%85 = load i8*, i8** %10
%86 = call i32 @free(i8* %85)
%87 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %88
88: ; preds = %82, %57
br label %95
89: ; preds = %39
%90 = load i32, i32* %5
%91 = call i32 @close(i32 %90)
%92 = load i8*, i8** %10
%93 = call i32 @free(i8* %92)
%94 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %95
95: ; preds = %89, %88
br label %96
96: ; preds = %95, %28
%97 = load i8**, i8*** %4
%98 = getelementptr inbounds i8*, i8** %97, i64 1
%99 = load i8*, i8** %98
%100 = call i32 @stat(i8* %99, %struct.stat* %9)
br label %101
101: ; preds = %96, %121
%102 = load i32, i32* %5
%103 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%104 = call i32 @read(i32 %102, i8* %103, i32 4096)
store i32 %104, i32* %7
%105 = load i32, i32* %7
%106 = icmp eq i32 %105, -1
br i1 %106, label %107, label %108
107: ; preds = %101
br label %122
108: ; preds = %101
%109 = load i32, i32* %7
%110 = icmp eq i32 %109, 0
br i1 %110, label %111, label %112
111: ; preds = %108
br label %122
112: ; preds = %108
%113 = load i32, i32* %6
%114 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%115 = load i32, i32* %7
%116 = call i32 @write(i32 %113, i8* %114, i32 %115)
%117 = load i32, i32* %7
%118 = sext i32 %117 to i64
%119 = icmp ult i64 %118, 4096
br i1 %119, label %120, label %121
120: ; preds = %112
br label %122
121: ; preds = %112
br label %101
122: ; preds = %120, %111, %107
%123 = load i32, i32* %5
%124 = call i32 @close(i32 %123)
%125 = load i32, i32* %6
%126 = call i32 @close(i32 %125)
%127 = load i8*, i8** %10
%128 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%129 = load i32, i32* %128
%130 = call i32 @chmod(i8* %127, i32 %129)
%131 = load i8*, i8** %10
%132 = call i32 @free(i8* %131)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [10 x i8] c"open(src)\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@errno = global i64 0
@EISDIR = global i64 0
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [10 x i8] c"open(dst)\00"
define void @cmd_cp(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = tail call i8* @strdup(i8* %8)
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 (i8*, i32, ...) @open(i8* %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = tail call i32 @free(i8* %9)
%17 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %15, %2
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %19
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRWXU
%25 = tail call i32 (i8*, i32, ...) @open(i8* %9, i32 %23, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %57
27: ; preds = %18
%28 = load i64, i64* @errno
%29 = load i64, i64* @EISDIR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %52
31: ; preds = %27
%32 = load i8*, i8** %10
%33 = tail call i8* @strrchr(i8* dereferenceable(1) %32, i8 signext 47)
%34 = icmp eq i8* %33, null
%35 = getelementptr inbounds i8, i8* %33, i64 1
%36 = select i1 %34, i8* %32, i8* %35
%37 = tail call i64 @strlen(i8* dereferenceable(1) %9)
%38 = tail call i64 @strlen(i8* dereferenceable(1) %36)
%39 = add i64 %37, 2
%40 = add i64 %39, %38
%41 = tail call i8* @malloc(i64 %40)
%42 = tail call i32 @sprintf(i8* dereferenceable(1) %41, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %9, i8* %36)
%43 = tail call i32 @free(i8* %9)
%44 = load i32, i32* @O_RDWR
%45 = load i32, i32* @O_CREAT
%46 = or i32 %45, %44
%47 = load i32, i32* @O_TRUNC
%48 = or i32 %46, %47
%49 = load i32, i32* @S_IRWXU
%50 = tail call i32 (i8*, i32, ...) @open(i8* %41, i32 %48, i32 %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %57
52: ; preds = %27, %31
%53 = phi i8* [ %41, %31 ], [ %9, %27 ]
%54 = tail call i32 @close(i32 %13)
%55 = tail call i32 @free(i8* %53)
%56 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %57
57: ; preds = %52, %31, %18
%58 = phi i8* [ %41, %31 ], [ %9, %18 ], [ %53, %52 ]
%59 = phi i32 [ %50, %31 ], [ %25, %18 ], [ -1, %52 ]
%60 = load i8*, i8** %10
%61 = call i32 @stat(i8* %60, %struct.stat* %4)
br label %62
62: ; preds = %66, %57
%63 = call i32 @read(i32 %13, i8* %5, i32 4096)
%64 = add i32 %63, 1
%65 = icmp ult i32 %64, 2
br i1 %65, label %69, label %66
66: ; preds = %62
%67 = call i32 @write(i32 %59, i8* %5, i32 %63)
%68 = icmp ult i32 %63, 4096
br i1 %68, label %69, label %62
69: ; preds = %66, %62
%70 = tail call i32 @close(i32 %13)
%71 = tail call i32 @close(i32 %59)
%72 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%73 = load i32, i32* %72
%74 = tail call i32 @chmod(i8* %58, i32 %73)
%75 = tail call i32 @free(i8* %58)
ret void
}