#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 */
int CopyObject(char *, char *, int, int);
int EXDEV;
int EraseObject(char *, int);
int /*<<< orphan*/ W_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fsRename(char *, char *, int, int *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strrchr(char *, char);
void fsMove(char *sourceP, char *targetP, int replace, int *rcP) {
  fsRename(sourceP, targetP, replace, rcP);
  if (*rcP == 0 || *rcP != EXDEV)
    return;
  {
    char dir[1024], *p;
    strcpy(dir, sourceP);
    p = strrchr(dir, '/');
    if (p == 0)
      strcpy(dir, ".");
    else if (p == dir)
      strcpy(dir, "/");
    else
      *p = 0;
    if (access(dir, W_OK) != 0) {
      *rcP = errno;
      return;
    }
  }
  *rcP = CopyObject(sourceP, targetP, replace, 1);
  if (*rcP != 0)
    return;
  *rcP = EraseObject(sourceP, replace);
}


x86 assembly (clang, -O0):
<fsMove>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1080 <fsRename@plt>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
je     11b0 <fsMove+0x50>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%eax
je     11b5 <fsMove+0x55>
jmp    12b7 <fsMove+0x157>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x420(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    1203 <fsMove+0xa3>
lea    -0x420(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1241 <fsMove+0xe1>
mov    -0x428(%rbp),%rax
lea    -0x420(%rbp),%rcx
cmp    %rcx,%rax
jne    1232 <fsMove+0xd2>
lea    -0x420(%rbp),%rdi
lea    0xdda(%rip),%rsi
call   1030 <strcpy@plt>
jmp    123c <fsMove+0xdc>
mov    -0x428(%rbp),%rax
movb   $0x0,(%rax)
jmp    1241 <fsMove+0xe1>
lea    -0x420(%rbp),%rdi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
je     1274 <fsMove+0x114>
mov    0x2d69(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    12b7 <fsMove+0x157>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x1,%ecx
call   1060 <CopyObject@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
je     12a3 <fsMove+0x143>
jmp    12b7 <fsMove+0x157>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <EraseObject@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsMove>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rcx,%rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
call   1080 <fsRename@plt>
mov    (%rbx),%eax
test   %eax,%eax
je     1213 <fsMove+0xb3>
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%eax
jne    1213 <fsMove+0xb3>
mov    %rsp,%r12
mov    %r12,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %r12,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11bf <fsMove+0x5f>
cmp    %r12,%rax
je     11c8 <fsMove+0x68>
movb   $0x0,(%rax)
jmp    11d7 <fsMove+0x77>
lea    0xe3a(%rip),%rsi
jmp    11cf <fsMove+0x6f>
lea    0xe33(%rip),%rsi
mov    %rsp,%rdi
call   1030 <strcpy@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1070 <access@plt>
test   %rax,%rax
je     11fa <fsMove+0x9a>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
jmp    1213 <fsMove+0xb3>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
mov    $0x1,%ecx
call   1060 <CopyObject@plt>
mov    %eax,(%rbx)
test   %eax,%eax
je     1223 <fsMove+0xc3>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
mov    %r14d,%esi
call   1040 <EraseObject@plt>
jmp    11f6 <fsMove+0x96>

LLVM IR (-O0):
@EXDEV = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
@W_OK = global i32 0
@errno = global i32 0
define void @fsMove(i8* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [1024 x i8]
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%11 = load i8*, i8** %5
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = load i32*, i32** %8
%15 = call i32 @fsRename(i8* %11, i8* %12, i32 %13, i32* %14)
%16 = load i32*, i32** %8
%17 = load i32, i32* %16
%18 = icmp eq i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %4
%20 = load i32*, i32** %8
%21 = load i32, i32* %20
%22 = load i32, i32* @EXDEV
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %19, %4
br label %69
25: ; preds = %19
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%27 = load i8*, i8** %5
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%30 = call i8* @strrchr(i8* %29, i8 signext 47)
store i8* %30, i8** %10
%31 = load i8*, i8** %10
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %25
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%35 = call i32 @strcpy(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %46
36: ; preds = %25
%37 = load i8*, i8** %10
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%42 = call i32 @strcpy(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %45
43: ; preds = %36
%44 = load i8*, i8** %10
store i8 0, i8* %44
br label %45
45: ; preds = %43, %40
br label %46
46: ; preds = %45, %33
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%48 = load i32, i32* @W_OK
%49 = call i64 @access(i8* %47, i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %46
%52 = load i32, i32* @errno
%53 = load i32*, i32** %8
store i32 %52, i32* %53
br label %69
54: ; preds = %46
%55 = load i8*, i8** %5
%56 = load i8*, i8** %6
%57 = load i32, i32* %7
%58 = call i32 @CopyObject(i8* %55, i8* %56, i32 %57, i32 1)
%59 = load i32*, i32** %8
store i32 %58, i32* %59
%60 = load i32*, i32** %8
%61 = load i32, i32* %60
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %54
br label %69
64: ; preds = %54
%65 = load i8*, i8** %5
%66 = load i32, i32* %7
%67 = call i32 @EraseObject(i8* %65, i32 %66)
%68 = load i32*, i32** %8
store i32 %67, i32* %68
br label %69
69: ; preds = %64, %63, %51, %24
ret void
}

LLVM IR (-O2):
@EXDEV = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
@W_OK = global i32 0
@errno = global i32 0
define void @fsMove(i8* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca [1024 x i8]
%6 = tail call i32 @fsRename(i8* %0, i8* %1, i32 %2, i32* %3)
%7 = load i32, i32* %3
%8 = icmp ne i32 %7, 0
%9 = load i32, i32* @EXDEV
%10 = icmp eq i32 %7, %9
%11 = select i1 %8, i1 %10, i1 false
br i1 %11, label %12, label %35
12: ; preds = %4
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%14 = call i32 @strcpy(i8* %13, i8* %0)
%15 = call i8* @strrchr(i8* %13, i8 signext 47)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @strcpy(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %24
19: ; preds = %12
%20 = icmp eq i8* %15, %13
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = call i32 @strcpy(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %24
23: ; preds = %19
store i8 0, i8* %15
br label %24
24: ; preds = %21, %23, %17
%25 = load i32, i32* @W_OK
%26 = call i64 @access(i8* %13, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = load i32, i32* @errno
store i32 %29, i32* %3
br label %35
30: ; preds = %24
%31 = call i32 @CopyObject(i8* %0, i8* %1, i32 %2, i32 1)
store i32 %31, i32* %3
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @EraseObject(i8* %0, i32 %2)
store i32 %34, i32* %3
br label %35
35: ; preds = %28, %30, %4, %33
ret void
}