#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;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int creat(char const *, int);
scalar_t__ errno;
char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
scalar_t__ rename(char const *, char const *);
scalar_t__ unlink(char const *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  const char *oldpath = *++argv;
  const char *newpath = *++argv;
  if (rename(oldpath, newpath) < 0) {
    if (errno == EXDEV) {
      int fdOld = open(oldpath, O_RDONLY);
      if (fdOld < 0) {
        perror("mv");
        return EXIT_FAILURE;
      }
      int fdNew = creat(newpath, 666);
      if (fdNew < 0) {
        perror("mv");
        close(fdOld);
        return EXIT_FAILURE;
      }
      char *tmpBuf = malloc(1024);
      if (tmpBuf == NULL) {
        perror("mv");
        close(fdOld);
        close(fdNew);
        return EXIT_FAILURE;
      }
      int bufRead;
      while ((bufRead = read(fdOld, tmpBuf, 1024))) {
        write(fdNew, tmpBuf, bufRead);
      }
      if (unlink(oldpath) < 0) {
        perror("mv");
        close(fdOld);
        close(fdNew);
        return EXIT_FAILURE;
      }
      close(fdOld);
      close(fdNew);
    } else {
      perror("mv");
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10a0 <rename@plt>
cmp    $0x0,%rax
jge    1363 <main+0x1d3>
mov    0x2dde(%rip),%rax
mov    (%rax),%rax
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%rax
jne    1352 <main+0x1c2>
mov    -0x18(%rbp),%rdi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1241 <main+0xb1>
lea    0xdd5(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <main+0x1df>
mov    -0x20(%rbp),%rdi
mov    $0x29a,%esi
call   10b0 <creat@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1281 <main+0xf1>
lea    0xd9d(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <main+0x1df>
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12c7 <main+0x137>
lea    0xd5f(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <main+0x1df>
jmp    12cc <main+0x13c>
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     12fd <main+0x16d>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1040 <write@plt>
jmp    12cc <main+0x13c>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    133d <main+0x1ad>
lea    0xce9(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    0x2c95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <main+0x1df>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
jmp    135e <main+0x1ce>
lea    0xca7(%rip),%rdi
call   1090 <perror@plt>
jmp    1363 <main+0x1d3>
mov    0x2c56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x8(%rsi),%r14
mov    0x10(%rsi),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <rename@plt>
mov    0x2e0e(%rip),%r12
test   %rax,%rax
jns    125e <main+0xce>
mov    0x2e0e(%rip),%rax
mov    (%rax),%rax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
jne    1283 <main+0xf3>
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1291 <main+0x101>
mov    %eax,%r15d
mov    %rbx,%rdi
mov    $0x29a,%esi
call   10b0 <creat@plt>
test   %eax,%eax
js     12a6 <main+0x116>
mov    %eax,%ebp
mov    $0x400,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     126b <main+0xdb>
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
je     1242 <main+0xb2>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
jmp    1220 <main+0x90>
mov    %r14,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
js     126b <main+0xdb>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%r12),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8e(%rip),%rdi
call   1090 <perror@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
jmp    12b5 <main+0x125>
lea    0xd76(%rip),%rdi
call   1090 <perror@plt>
jmp    125e <main+0xce>
lea    0xd68(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d24(%rip),%r12
jmp    125e <main+0xce>
lea    0xd53(%rip),%rdi
call   1090 <perror@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    0x2d07(%rip),%r12
jmp    125e <main+0xce>

LLVM IR (-O0):
@errno = global i64 0
@EXDEV = global i64 0
@O_RDONLY = global i32 0
@.str = [3 x i8] c"mv\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i32 1
store i8** %13, i8*** %5
%14 = load i8*, i8** %13
store i8* %14, i8** %6
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i32 1
store i8** %16, i8*** %5
%17 = load i8*, i8** %16
store i8* %17, i8** %7
%18 = load i8*, i8** %6
%19 = load i8*, i8** %7
%20 = call i64 @rename(i8* %18, i8* %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %86
22: ; preds = %2
%23 = load i64, i64* @errno
%24 = load i64, i64* @EXDEV
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %83
26: ; preds = %22
%27 = load i8*, i8** %6
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
store i32 %34, i32* %3
br label %88
35: ; preds = %26
%36 = load i8*, i8** %7
%37 = call i32 @creat(i8* %36, i32 666)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%42 = load i32, i32* %8
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* @EXIT_FAILURE
store i32 %44, i32* %3
br label %88
45: ; preds = %35
%46 = call i8* @malloc(i32 1024)
store i8* %46, i8** %10
%47 = load i8*, i8** %10
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %56
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%51 = load i32, i32* %8
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* %9
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* @EXIT_FAILURE
store i32 %55, i32* %3
br label %88
56: ; preds = %45
br label %57
57: ; preds = %62, %56
%58 = load i32, i32* %8
%59 = load i8*, i8** %10
%60 = call i32 @read(i32 %58, i8* %59, i32 1024)
store i32 %60, i32* %11
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %57
%63 = load i32, i32* %9
%64 = load i8*, i8** %10
%65 = load i32, i32* %11
%66 = call i32 @write(i32 %63, i8* %64, i32 %65)
br label %57
67: ; preds = %57
%68 = load i8*, i8** %6
%69 = call i64 @unlink(i8* %68)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %78
71: ; preds = %67
%72 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%73 = load i32, i32* %8
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %9
%76 = call i32 @close(i32 %75)
%77 = load i32, i32* @EXIT_FAILURE
store i32 %77, i32* %3
br label %88
78: ; preds = %67
%79 = load i32, i32* %8
%80 = call i32 @close(i32 %79)
%81 = load i32, i32* %9
%82 = call i32 @close(i32 %81)
br label %85
83: ; preds = %22
%84 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %85
85: ; preds = %83, %78
br label %86
86: ; preds = %85, %2
%87 = load i32, i32* @EXIT_SUCCESS
store i32 %87, i32* %3
br label %88
88: ; preds = %86, %71, %49, %40, %32
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
@errno = global i64 0
@EXDEV = global i64 0
@O_RDONLY = global i32 0
@.str = [3 x i8] c"mv\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = getelementptr inbounds i8*, i8** %1, i64 2
%6 = load i8*, i8** %5
%7 = tail call i64 @rename(i8* %4, i8* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %52
9: ; preds = %2
%10 = load i64, i64* @errno
%11 = load i64, i64* @EXDEV
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %50
13: ; preds = %9
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %4, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %52
19: ; preds = %13
%20 = tail call i32 @creat(i8* %6, i32 666)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%24 = tail call i32 @close(i32 %15)
br label %52
25: ; preds = %19
%26 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%27 = icmp eq i8* %26, null
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = tail call i32 @read(i32 %15, i8* %26, i32 1024)
%30 = icmp eq i32 %29, 0
br i1 %30, label %40, label %35
31: ; preds = %25
%32 = tail call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%33 = tail call i32 @close(i32 %15)
%34 = tail call i32 @close(i32 %20)
br label %52
35: ; preds = %28, %35
%36 = phi i32 [ %38, %35 ], [ %29, %28 ]
%37 = tail call i32 @write(i32 %20, i8* %26, i32 %36)
%38 = tail call i32 @read(i32 %15, i8* %26, i32 1024)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %35
40: ; preds = %35, %28
%41 = tail call i64 @unlink(i8* %4)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = tail call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%45 = tail call i32 @close(i32 %15)
%46 = tail call i32 @close(i32 %20)
br label %52
47: ; preds = %40
%48 = tail call i32 @close(i32 %15)
%49 = tail call i32 @close(i32 %20)
br label %52
50: ; preds = %9
%51 = tail call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %52
52: ; preds = %2, %50, %47, %43, %31, %22, %17
%53 = phi i32* [ @EXIT_FAILURE, %43 ], [ @EXIT_FAILURE, %31 ], [ @EXIT_FAILURE, %22 ], [ @EXIT_FAILURE, %17 ], [ @EXIT_SUCCESS, %47 ], [ @EXIT_SUCCESS, %50 ], [ @EXIT_SUCCESS, %2 ]
%54 = load i32, i32* %53
ret i32 %54
}