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

/* Variables and functions */
scalar_t__ ENOENT;
int EOF;
scalar_t__ S_ISLNK(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fputc(int, int /*<<< orphan*/ *);
int lstat(char const *, struct stat *);
int rename(char const *, char const *);
int /*<<< orphan*/ unlink(char const *);
int movefile(const char *src, const char *dest) {
  int copydata = 0;
  int *linktgt = NULL;
  struct stat st;
  if (lstat(dest, &st) != -1) {
    if (st.st_nlink > 1 || S_ISLNK(st.st_mode))
      copydata = 1;
  } else if (errno != ENOENT) {
    return -1;
  }
  if (!copydata)
    if (rename(src, dest) == -1)
      copydata = 1;
  if (copydata) {
    int result = 0;
    FILE *fpsrc = fopen(src, "r");
    if (!fpsrc) {
      result = -1;
      goto err_open_fpsrc;
    }
    FILE *fpdest = fopen(dest, "w");
    if (!fpdest) {
      result = -2;
      goto err_open_fpdest;
    }
    while (1) {
      int ch = fgetc(fpsrc);
      if (ch == EOF) {
        if (feof(fpsrc)) {
          break;
        } else {
          result = -1;
          goto err_copy;
        }
      } else {
        if (fputc(ch, fpdest) == EOF) {
          result = -2;
          goto err_copy;
        }
      }
    }
    unlink(src);
  err_copy:
    fclose(fpdest);
  err_open_fpdest:
    fclose(fpsrc);
  err_open_fpsrc:
    return result;
  }
  return 0;
}


x86 assembly (clang, -O0):
<movefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <lstat@plt>
cmp    $0xffffffff,%eax
je     11ed <movefile+0x5d>
cmpl   $0x1,-0x30(%rbp)
jg     11e1 <movefile+0x51>
mov    -0x2c(%rbp),%edi
call   1040 <S_ISLNK@plt>
cmp    $0x0,%rax
je     11e8 <movefile+0x58>
movl   $0x1,-0x1c(%rbp)
jmp    1218 <movefile+0x88>
mov    0x2de4(%rip),%rax
mov    (%rax),%rax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
je     1213 <movefile+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    1350 <movefile+0x1c0>
jmp    1218 <movefile+0x88>
cmpl   $0x0,-0x1c(%rbp)
jne    1244 <movefile+0xb4>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10b0 <rename@plt>
cmp    $0xffffffff,%eax
jne    123f <movefile+0xaf>
movl   $0x1,-0x1c(%rbp)
jmp    1244 <movefile+0xb4>
cmpl   $0x0,-0x1c(%rbp)
je     1349 <movefile+0x1b9>
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xda0(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1280 <movefile+0xf0>
movl   $0xffffffff,-0x34(%rbp)
jmp    133e <movefile+0x1ae>
mov    -0x18(%rbp),%rdi
lea    0xd77(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12ab <movefile+0x11b>
movl   $0xfffffffe,-0x34(%rbp)
jmp    1335 <movefile+0x1a5>
jmp    12b0 <movefile+0x120>
mov    -0x40(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    0x2d22(%rip),%rcx
cmp    (%rcx),%eax
jne    12f2 <movefile+0x162>
mov    -0x40(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%rax
je     12e6 <movefile+0x156>
jmp    1323 <movefile+0x193>
movl   $0xffffffff,-0x34(%rbp)
jmp    132c <movefile+0x19c>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
call   1070 <fputc@plt>
mov    0x2ce3(%rip),%rcx
cmp    (%rcx),%eax
jne    1319 <movefile+0x189>
movl   $0xfffffffe,-0x34(%rbp)
jmp    132c <movefile+0x19c>
jmp    131e <movefile+0x18e>
jmp    12b0 <movefile+0x120>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1350 <movefile+0x1c0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<movefile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1080 <lstat@plt>
cmp    $0xffffffff,%eax
je     11cb <movefile+0x3b>
cmpl   $0x1,0x8(%rsp)
jg     1200 <movefile+0x70>
mov    0xc(%rsp),%edi
call   1040 <S_ISLNK@plt>
test   %rax,%rax
jne    1200 <movefile+0x70>
jmp    11ea <movefile+0x5a>
mov    0x2e06(%rip),%rax
mov    (%rax),%rax
mov    0x2dec(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    129f <movefile+0x10f>
mov    %r14,%rdi
mov    %r15,%rsi
call   10b0 <rename@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
jne    129f <movefile+0x10f>
lea    0xdf9(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1263 <movefile+0xd3>
mov    %rax,%r12
lea    0xde4(%rip),%rsi
mov    %r15,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     126a <movefile+0xda>
mov    %rax,%rbx
mov    0x2db3(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1060 <fgetc@plt>
cmp    0x0(%rbp),%eax
je     1271 <movefile+0xe1>
mov    %eax,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
cmp    0x0(%rbp),%eax
jne    1240 <movefile+0xb0>
mov    $0xfffffffe,%ebp
jmp    128f <movefile+0xff>
mov    $0xffffffff,%ebp
jmp    129f <movefile+0x10f>
mov    $0xfffffffe,%ebp
jmp    1297 <movefile+0x107>
mov    %r12,%rdi
call   1090 <feof@plt>
test   %rax,%rax
je     128a <movefile+0xfa>
mov    %r14,%rdi
call   1030 <unlink@plt>
xor    %ebp,%ebp
jmp    128f <movefile+0xff>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@EOF = global i32 0
define i32 @movefile(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i32* null, i32** %7
%13 = load i8*, i8** %5
%14 = call i32 @lstat(i8* %13, %struct.stat* %8)
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %27
16: ; preds = %2
%17 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%18 = load i32, i32* %17
%19 = icmp sgt i32 %18, 1
br i1 %19, label %25, label %20
20: ; preds = %16
%21 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%22 = load i32, i32* %21
%23 = call i64 @S_ISLNK(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20, %16
store i32 1, i32* %6
br label %26
26: ; preds = %25, %20
br label %33
27: ; preds = %2
%28 = load i64, i64* @errno
%29 = load i64, i64* @ENOENT
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 -1, i32* %3
br label %92
32: ; preds = %27
br label %33
33: ; preds = %32, %26
%34 = load i32, i32* %6
%35 = icmp ne i32 %34, 0
br i1 %35, label %43, label %36
36: ; preds = %33
%37 = load i8*, i8** %4
%38 = load i8*, i8** %5
%39 = call i32 @rename(i8* %37, i8* %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %36
store i32 1, i32* %6
br label %42
42: ; preds = %41, %36
br label %43
43: ; preds = %42, %33
%44 = load i32, i32* %6
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %91
46: ; preds = %43
store i32 0, i32* %9
%47 = load i8*, i8** %4
%48 = call i32* @fopen(i8* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %48, i32** %10
%49 = load i32*, i32** %10
%50 = icmp ne i32* %49, null
br i1 %50, label %52, label %51
51: ; preds = %46
store i32 -1, i32* %9
br label %89
52: ; preds = %46
%53 = load i8*, i8** %5
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %54, i32** %11
%55 = load i32*, i32** %11
%56 = icmp ne i32* %55, null
br i1 %56, label %58, label %57
57: ; preds = %52
store i32 -2, i32* %9
br label %86
58: ; preds = %52
br label %59
59: ; preds = %58, %79
%60 = load i32*, i32** %10
%61 = call i32 @fgetc(i32* %60)
store i32 %61, i32* %12
%62 = load i32, i32* %12
%63 = load i32, i32* @EOF
%64 = icmp eq i32 %62, %63
br i1 %64, label %65, label %71
65: ; preds = %59
%66 = load i32*, i32** %10
%67 = call i64 @feof(i32* %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %65
br label %80
70: ; preds = %65
store i32 -1, i32* %9
br label %83
71: ; preds = %59
%72 = load i32, i32* %12
%73 = load i32*, i32** %11
%74 = call i32 @fputc(i32 %72, i32* %73)
%75 = load i32, i32* @EOF
%76 = icmp eq i32 %74, %75
br i1 %76, label %77, label %78
77: ; preds = %71
store i32 -2, i32* %9
br label %83
78: ; preds = %71
br label %79
79: ; preds = %78
br label %59
80: ; preds = %69
%81 = load i8*, i8** %4
%82 = call i32 @unlink(i8* %81)
br label %83
83: ; preds = %80, %77, %70
%84 = load i32*, i32** %11
%85 = call i32 @fclose(i32* %84)
br label %86
86: ; preds = %83, %57
%87 = load i32*, i32** %10
%88 = call i32 @fclose(i32* %87)
br label %89
89: ; preds = %86, %51
%90 = load i32, i32* %9
store i32 %90, i32* %3
br label %92
91: ; preds = %43
store i32 0, i32* %3
br label %92
92: ; preds = %91, %89, %31
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@EOF = global i32 0
define i32 @movefile(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @lstat(i8* %1, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %16, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = icmp sgt i32 %9, 1
br i1 %10, label %23, label %11
11: ; preds = %7
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%13 = load i32, i32* %12
%14 = tail call i64 @S_ISLNK(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %23
16: ; preds = %2
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENOENT
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %48
20: ; preds = %11, %16
%21 = tail call i32 @rename(i8* %0, i8* %1)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %48
23: ; preds = %11, %7, %20
%24 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %48, label %26
26: ; preds = %23
%27 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %45, label %29
29: ; preds = %26, %36
%30 = tail call i32 @fgetc(i32* %24)
%31 = load i32, i32* @EOF
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = tail call i64 @feof(i32* %24)
%35 = icmp eq i64 %34, 0
br i1 %35, label %42, label %40
36: ; preds = %29
%37 = tail call i32 @fputc(i32 %30, i32* %27)
%38 = load i32, i32* @EOF
%39 = icmp eq i32 %37, %38
br i1 %39, label %42, label %29
40: ; preds = %33
%41 = tail call i32 @unlink(i8* %0)
br label %42
42: ; preds = %36, %33, %40
%43 = phi i32 [ 0, %40 ], [ -1, %33 ], [ -2, %36 ]
%44 = tail call i32 @fclose(i32* %27)
br label %45
45: ; preds = %26, %42
%46 = phi i32 [ %43, %42 ], [ -2, %26 ]
%47 = tail call i32 @fclose(i32* %24)
br label %48
48: ; preds = %20, %23, %45, %16
%49 = phi i32 [ -1, %16 ], [ %46, %45 ], [ -1, %23 ], [ 0, %20 ]
ret i32 %49
}