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

/* Variables and functions */
scalar_t__ EXDEV;
scalar_t__ access(char *, int /*<<< orphan*/);
char *buildname(char *, char *);
int /*<<< orphan*/ copyfile(char *, char *, int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int isadir(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ rename(char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ unlink(char *);
int main(int argc, char *argv[]) {
  int dirflag;
  char *srcname;
  char *destname;
  char *lastarg;
  lastarg = argv[argc - 1];
  dirflag = isadir(lastarg);
  if ((argc > 3) && !dirflag) {
    fprintf(stderr, "%s: not a directory\n", lastarg);
    return 1;
  }
  while (argc-- > 2) {
    srcname = *(++argv);
    if (access(srcname, 0) < 0) {
      perror(srcname);
      continue;
    }
    destname = lastarg;
    if (dirflag)
      destname = buildname(destname, srcname);
    if (rename(srcname, destname) >= 0)
      continue;
    if (errno != EXDEV) {
      perror(destname);
      continue;
    }
    if (!copyfile(srcname, destname, 1))
      continue;
    if (unlink(srcname) < 0)
      perror(srcname);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
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,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <isadir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    11f0 <main+0x70>
cmpl   $0x0,-0x14(%rbp)
jne    11f0 <main+0x70>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xe21(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f2 <main+0x172>
jmp    11f5 <main+0x75>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x2,%eax
jle    12eb <main+0x16b>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jge    1243 <main+0xc3>
mov    -0x20(%rbp),%rdi
call   1070 <perror@plt>
jmp    11f5 <main+0x75>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1266 <main+0xe6>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <buildname@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <rename@plt>
cmp    $0x0,%rax
jl     1282 <main+0x102>
jmp    11f5 <main+0x75>
mov    0x2d47(%rip),%rax
mov    (%rax),%rax
mov    0x2d4d(%rip),%rcx
cmp    (%rcx),%rax
je     12aa <main+0x12a>
mov    -0x28(%rbp),%rdi
call   1070 <perror@plt>
jmp    11f5 <main+0x75>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   10a0 <copyfile@plt>
cmp    $0x0,%eax
jne    12ca <main+0x14a>
jmp    11f5 <main+0x75>
mov    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    12e6 <main+0x166>
mov    -0x20(%rbp),%rdi
call   1070 <perror@plt>
jmp    11f5 <main+0x75>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %edi,%ebp
movslq %edi,%rbx
mov    -0x8(%rsi,%rbx,8),%r14
mov    %r14,%rdi
call   1040 <isadir@plt>
mov    %eax,%r15d
cmp    $0x4,%ebx
jl     11cf <main+0x4f>
test   %r15d,%r15d
jne    11cf <main+0x4f>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    1273 <main+0xf3>
xor    %eax,%eax
cmp    $0x3,%ebp
jl     1273 <main+0xf3>
add    $0x1,%ebp
add    $0x8,%r13
jmp    11fc <main+0x7c>
mov    %rbx,%r12
mov    %r12,%rdi
call   1070 <perror@plt>
xchg   %ax,%ax
add    $0xffffffff,%ebp
add    $0x8,%r13
cmp    $0x3,%ebp
jbe    1271 <main+0xf1>
mov    0x0(%r13),%r12
mov    %r12,%rdi
xor    %esi,%esi
call   1060 <access@plt>
test   %rax,%rax
js     11e6 <main+0x66>
mov    %r14,%rbx
test   %r15d,%r15d
je     1225 <main+0xa5>
mov    %r14,%rdi
mov    %r12,%rsi
call   1090 <buildname@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <rename@plt>
test   %rax,%rax
jns    11f0 <main+0x70>
mov    0x2d94(%rip),%rax
mov    (%rax),%rax
mov    0x2d9a(%rip),%rcx
cmp    (%rcx),%rax
jne    11e3 <main+0x63>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   10a0 <copyfile@plt>
test   %eax,%eax
je     11f0 <main+0x70>
mov    %r12,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jns    11f0 <main+0x70>
jmp    11e6 <main+0x66>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"%s: not a directory\0A\00"
@errno = global i64 0
@EXDEV = global i64 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = load i32, i32* %4
%12 = sub nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8*, i8** %10, i64 %13
%15 = load i8*, i8** %14
store i8* %15, i8** %9
%16 = load i8*, i8** %9
%17 = call i32 @isadir(i8* %16)
store i32 %17, i32* %6
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 3
br i1 %19, label %20, label %27
20: ; preds = %2
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %9
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %25)
store i32 1, i32* %3
br label %78
27: ; preds = %20, %2
br label %28
28: ; preds = %76, %68, %60, %55, %39, %27
%29 = load i32, i32* %4
%30 = add nsw i32 %29, -1
store i32 %30, i32* %4
%31 = icmp sgt i32 %29, 2
br i1 %31, label %32, label %77
32: ; preds = %28
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i32 1
store i8** %34, i8*** %5
%35 = load i8*, i8** %34
store i8* %35, i8** %7
%36 = load i8*, i8** %7
%37 = call i64 @access(i8* %36, i32 0)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = load i8*, i8** %7
%41 = call i32 @perror(i8* %40)
br label %28
42: ; preds = %32
%43 = load i8*, i8** %9
store i8* %43, i8** %8
%44 = load i32, i32* %6
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = load i8*, i8** %8
%48 = load i8*, i8** %7
%49 = call i8* @buildname(i8* %47, i8* %48)
store i8* %49, i8** %8
br label %50
50: ; preds = %46, %42
%51 = load i8*, i8** %7
%52 = load i8*, i8** %8
%53 = call i64 @rename(i8* %51, i8* %52)
%54 = icmp sge i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %50
br label %28
56: ; preds = %50
%57 = load i64, i64* @errno
%58 = load i64, i64* @EXDEV
%59 = icmp ne i64 %57, %58
br i1 %59, label %60, label %63
60: ; preds = %56
%61 = load i8*, i8** %8
%62 = call i32 @perror(i8* %61)
br label %28
63: ; preds = %56
%64 = load i8*, i8** %7
%65 = load i8*, i8** %8
%66 = call i32 @copyfile(i8* %64, i8* %65, i32 1)
%67 = icmp ne i32 %66, 0
br i1 %67, label %69, label %68
68: ; preds = %63
br label %28
69: ; preds = %63
%70 = load i8*, i8** %7
%71 = call i64 @unlink(i8* %70)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %69
%74 = load i8*, i8** %7
%75 = call i32 @perror(i8* %74)
br label %76
76: ; preds = %73, %69
br label %28
77: ; preds = %28
store i32 0, i32* %3
br label %78
78: ; preds = %77, %23
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"%s: not a directory\0A\00"
@errno = global i64 0
@EXDEV = global i64 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add nsw i32 %0, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8*, i8** %1, i64 %4
%6 = load i8*, i8** %5
%7 = tail call i32 @isadir(i8* %6)
%8 = icmp slt i32 %0, 4
%9 = icmp ne i32 %7, 0
%10 = select i1 %8, i1 true, i1 %9
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = icmp sgt i32 %0, 2
br i1 %12, label %16, label %46
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %46
16: ; preds = %11, %27
%17 = phi i32 [ %19, %27 ], [ %0, %11 ]
%18 = phi i8** [ %20, %27 ], [ %1, %11 ]
%19 = add nsw i32 %17, -1
%20 = getelementptr inbounds i8*, i8** %18, i64 1
%21 = load i8*, i8** %20
%22 = tail call i64 @access(i8* %21, i32 0)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %16, %43, %36
%25 = phi i8* [ %33, %36 ], [ %21, %43 ], [ %21, %16 ]
%26 = tail call i32 @perror(i8* %25)
br label %27
27: ; preds = %24, %43, %32, %40
%28 = icmp ugt i32 %17, 3
br i1 %28, label %16, label %46
29: ; preds = %16
br i1 %9, label %30, label %32
30: ; preds = %29
%31 = tail call i8* @buildname(i8* %6, i8* %21)
br label %32
32: ; preds = %30, %29
%33 = phi i8* [ %31, %30 ], [ %6, %29 ]
%34 = tail call i64 @rename(i8* %21, i8* %33)
%35 = icmp sgt i64 %34, -1
br i1 %35, label %27, label %36
36: ; preds = %32
%37 = load i64, i64* @errno
%38 = load i64, i64* @EXDEV
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %24
40: ; preds = %36
%41 = tail call i32 @copyfile(i8* %21, i8* %33, i32 1)
%42 = icmp eq i32 %41, 0
br i1 %42, label %27, label %43
43: ; preds = %40
%44 = tail call i64 @unlink(i8* %21)
%45 = icmp slt i64 %44, 0
br i1 %45, label %24, label %27
46: ; preds = %27, %11, %13
%47 = phi i32 [ 1, %13 ], [ 0, %11 ], [ 0, %27 ]
ret i32 %47
}