#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 */
typedef int /*<<< orphan*/ mode_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int BUFSIZ;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int _mv_error(char const *, int);
int /*<<< orphan*/ close(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int open(char const *, int, int /*<<< orphan*/);
scalar_t__ unlink(char const *);
int _mv_single_regular(char const *src, char const *dst, mode_t mode) {
  int ret = 0;
  FILE *fsrc;
  int fd;
  FILE *fdst;
  char buf[BUFSIZ];
  size_t size;
  if ((fsrc = fopen(src, "r")) == NULL)
    return _mv_error(dst, 1);
  if ((fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0 ||
      (fdst = fdopen(fd, "w")) == NULL) {
    ret |= _mv_error(dst, 1);
    if (fd >= 0)
      close(fd);
    fclose(fsrc);
    return ret;
  }
  while ((size = fread(buf, sizeof(char), sizeof(buf), fsrc)) > 0)
    if (fwrite(buf, sizeof(char), size, fdst) != size)
      break;
  if (!feof(fsrc))
    ret |= _mv_error(size == 0 ? src : dst, 1);
  if (fclose(fsrc) != 0)
    ret |= _mv_error(src, 1);
  if (fclose(fdst) != 0)
    ret |= _mv_error(dst, 1);
  if (unlink(src) != 0)
    _mv_error(src, 0);
  return ret;
}


x86 assembly (clang, -O0):
<_mv_single_regular>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1225 <_mv_single_regular+0x85>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1090 <_mv_error@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13ca <_mv_single_regular+0x22a>
mov    -0x18(%rbp),%rdi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    0x2dbf(%rip),%rax
or     (%rax),%esi
mov    0x2dae(%rip),%rax
or     (%rax),%esi
mov    -0x1c(%rbp),%edx
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jl     1275 <_mv_single_regular+0xd5>
mov    -0x2c(%rbp),%edi
lea    0xda0(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    12b6 <_mv_single_regular+0x116>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1090 <_mv_error@plt>
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     129b <_mv_single_regular+0xfb>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13ca <_mv_single_regular+0x22a>
jmp    12bb <_mv_single_regular+0x11b>
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rax
mov    %eax,%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
jbe    130b <_mv_single_regular+0x16b>
mov    -0x60(%rbp),%rdi
mov    -0x50(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
cmp    -0x50(%rbp),%rax
je     1306 <_mv_single_regular+0x166>
jmp    130b <_mv_single_regular+0x16b>
jmp    12bb <_mv_single_regular+0x11b>
mov    -0x28(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
jne    1351 <_mv_single_regular+0x1b1>
cmpq   $0x0,-0x50(%rbp)
jne    1335 <_mv_single_regular+0x195>
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
jmp    133d <_mv_single_regular+0x19d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rdi
mov    $0x1,%esi
call   1090 <_mv_error@plt>
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     1378 <_mv_single_regular+0x1d8>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1090 <_mv_error@plt>
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     139f <_mv_single_regular+0x1ff>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1090 <_mv_error@plt>
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
je     13bd <_mv_single_regular+0x21d>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1090 <_mv_error@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_mv_single_regular>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,-0x30(%rbp)
mov    %rsi,%r13
mov    %rdi,%r15
mov    0x2e0f(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe23(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12ce <_mv_single_regular+0x12e>
mov    %rax,%r14
mov    %r15,-0x38(%rbp)
mov    0x2dc7(%rip),%rax
mov    0x2df8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2de5(%rip),%rax
or     (%rax),%esi
mov    %r13,%rdi
mov    -0x30(%rbp),%edx
call   10a0 <open@plt>
test   %eax,%eax
js     12e9 <_mv_single_regular+0x149>
mov    %eax,%r15d
mov    %r13,-0x30(%rbp)
lea    0xdd4(%rip),%rsi
mov    %eax,%edi
call   1080 <fdopen@plt>
test   %rax,%rax
je     12fb <_mv_single_regular+0x15b>
mov    %rax,%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %rax,%r15
test   %rax,%rax
je     1283 <_mv_single_regular+0xe3>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r13,%rcx
call   10c0 <fwrite@plt>
cmp    %r15,%rax
je     1250 <_mv_single_regular+0xb0>
mov    %r14,%rdi
call   1070 <feof@plt>
xor    %r12d,%r12d
test   %eax,%eax
jne    12ab <_mv_single_regular+0x10b>
test   %r15,%r15
mov    -0x30(%rbp),%rdi
cmove  -0x38(%rbp),%rdi
mov    $0x1,%esi
call   1090 <_mv_error@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
je     131e <_mv_single_regular+0x17e>
mov    -0x38(%rbp),%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1090 <_mv_error@plt>
or     %eax,%r12d
jmp    1322 <_mv_single_regular+0x182>
mov    %r13,%rdi
mov    $0x1,%esi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <_mv_error@plt>
mov    %r13,%rdi
mov    $0x1,%esi
call   1090 <_mv_error@plt>
mov    %eax,%r12d
jmp    1314 <_mv_single_regular+0x174>
mov    -0x30(%rbp),%rdi
mov    $0x1,%esi
call   1090 <_mv_error@plt>
mov    %eax,%r12d
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
jmp    135a <_mv_single_regular+0x1ba>
mov    -0x38(%rbp),%rbx
mov    -0x30(%rbp),%r14
mov    %r13,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
je     1343 <_mv_single_regular+0x1a3>
mov    %r14,%rdi
mov    $0x1,%esi
call   1090 <_mv_error@plt>
or     %eax,%r12d
mov    %rbx,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
je     135a <_mv_single_regular+0x1ba>
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <_mv_error@plt>
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [2 x i8] c"r\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [2 x i8] c"w\00"
define i32 @_mv_single_regular(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%16 = load i32, i32* @BUFSIZ
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %12
%19 = alloca i8, i64 %17
store i64 %17, i64* %13
%20 = load i8*, i8** %5
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %9
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = load i8*, i8** %6
%25 = call i32 @_mv_error(i8* %24, i32 1)
store i32 %25, i32* %4
store i32 1, i32* %15
br label %111
26: ; preds = %3
%27 = load i8*, i8** %6
%28 = load i32, i32* @O_WRONLY
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = load i32, i32* @O_TRUNC
%32 = or i32 %30, %31
%33 = load i32, i32* %7
%34 = call i32 @open(i8* %27, i32 %32, i32 %33)
store i32 %34, i32* %10
%35 = icmp slt i32 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %26
%37 = load i32, i32* %10
%38 = call i32* @fdopen(i32 %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %38, i32** %11
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %54
40: ; preds = %36, %26
%41 = load i8*, i8** %6
%42 = call i32 @_mv_error(i8* %41, i32 1)
%43 = load i32, i32* %8
%44 = or i32 %43, %42
store i32 %44, i32* %8
%45 = load i32, i32* %10
%46 = icmp sge i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %40
%48 = load i32, i32* %10
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %47, %40
%51 = load i32*, i32** %9
%52 = call i64 @fclose(i32* %51)
%53 = load i32, i32* %8
store i32 %53, i32* %4
store i32 1, i32* %15
br label %111
54: ; preds = %36
br label %55
55: ; preds = %67, %54
%56 = trunc i64 %17 to i32
%57 = load i32*, i32** %9
%58 = call i64 @fread(i8* %19, i32 1, i32 %56, i32* %57)
store i64 %58, i64* %14
%59 = icmp ugt i64 %58, 0
br i1 %59, label %60, label %68
60: ; preds = %55
%61 = load i64, i64* %14
%62 = load i32*, i32** %11
%63 = call i64 @fwrite(i8* %19, i32 1, i64 %61, i32* %62)
%64 = load i64, i64* %14
%65 = icmp ne i64 %63, %64
br i1 %65, label %66, label %67
66: ; preds = %60
br label %68
67: ; preds = %60
br label %55
68: ; preds = %66, %55
%69 = load i32*, i32** %9
%70 = call i32 @feof(i32* %69)
%71 = icmp ne i32 %70, 0
br i1 %71, label %84, label %72
72: ; preds = %68
%73 = load i64, i64* %14
%74 = icmp eq i64 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %72
%76 = load i8*, i8** %5
br label %79
77: ; preds = %72
%78 = load i8*, i8** %6
br label %79
79: ; preds = %77, %75
%80 = phi i8* [ %76, %75 ], [ %78, %77 ]
%81 = call i32 @_mv_error(i8* %80, i32 1)
%82 = load i32, i32* %8
%83 = or i32 %82, %81
store i32 %83, i32* %8
br label %84
84: ; preds = %79, %68
%85 = load i32*, i32** %9
%86 = call i64 @fclose(i32* %85)
%87 = icmp ne i64 %86, 0
br i1 %87, label %88, label %93
88: ; preds = %84
%89 = load i8*, i8** %5
%90 = call i32 @_mv_error(i8* %89, i32 1)
%91 = load i32, i32* %8
%92 = or i32 %91, %90
store i32 %92, i32* %8
br label %93
93: ; preds = %88, %84
%94 = load i32*, i32** %11
%95 = call i64 @fclose(i32* %94)
%96 = icmp ne i64 %95, 0
br i1 %96, label %97, label %102
97: ; preds = %93
%98 = load i8*, i8** %6
%99 = call i32 @_mv_error(i8* %98, i32 1)
%100 = load i32, i32* %8
%101 = or i32 %100, %99
store i32 %101, i32* %8
br label %102
102: ; preds = %97, %93
%103 = load i8*, i8** %5
%104 = call i64 @unlink(i8* %103)
%105 = icmp ne i64 %104, 0
br i1 %105, label %106, label %109
106: ; preds = %102
%107 = load i8*, i8** %5
%108 = call i32 @_mv_error(i8* %107, i32 0)
br label %109
109: ; preds = %106, %102
%110 = load i32, i32* %8
store i32 %110, i32* %4
store i32 1, i32* %15
br label %111
111: ; preds = %109, %50, %23
%112 = load i8*, i8** %12
%113 = load i32, i32* %4
ret i32 %113
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [2 x i8] c"r\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [2 x i8] c"w\00"
define i32 @_mv_single_regular(i8* %0, i8* %1, i32 %2) {
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @_mv_error(i8* %1, i32 1)
br label %62
11: ; preds = %3
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* %1, i32 %16, i32 %2)
%18 = icmp slt i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %11
%20 = tail call i32* @fdopen(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %24, label %30
22: ; preds = %11
%23 = tail call i32 @_mv_error(i8* %1, i32 1)
br label %27
24: ; preds = %19
%25 = tail call i32 @_mv_error(i8* %1, i32 1)
%26 = tail call i32 @close(i32 %17)
br label %27
27: ; preds = %22, %24
%28 = phi i32 [ %25, %24 ], [ %23, %22 ]
%29 = tail call i64 @fclose(i32* %7)
br label %62
30: ; preds = %19, %33
%31 = call i64 @fread(i8* %6, i32 1, i32 %4, i32* %7)
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = call i64 @fwrite(i8* %6, i32 1, i64 %31, i32* %20)
%35 = icmp eq i64 %34, %31
br i1 %35, label %30, label %36
36: ; preds = %33, %30
%37 = tail call i32 @feof(i32* %7)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = select i1 %32, i8* %0, i8* %1
%41 = tail call i32 @_mv_error(i8* %40, i32 1)
br label %42
42: ; preds = %39, %36
%43 = phi i32 [ 0, %36 ], [ %41, %39 ]
%44 = tail call i64 @fclose(i32* %7)
%45 = icmp eq i64 %44, 0
br i1 %45, label %49, label %46
46: ; preds = %42
%47 = tail call i32 @_mv_error(i8* %0, i32 1)
%48 = or i32 %47, %43
br label %49
49: ; preds = %46, %42
%50 = phi i32 [ %48, %46 ], [ %43, %42 ]
%51 = tail call i64 @fclose(i32* %20)
%52 = icmp eq i64 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %49
%54 = tail call i32 @_mv_error(i8* %1, i32 1)
%55 = or i32 %54, %50
br label %56
56: ; preds = %53, %49
%57 = phi i32 [ %55, %53 ], [ %50, %49 ]
%58 = tail call i64 @unlink(i8* %0)
%59 = icmp eq i64 %58, 0
br i1 %59, label %62, label %60
60: ; preds = %56
%61 = tail call i32 @_mv_error(i8* %0, i32 0)
br label %62
62: ; preds = %56, %60, %27, %9
%63 = phi i32 [ %10, %9 ], [ %28, %27 ], [ %57, %60 ], [ %57, %56 ]
ret i32 %63
}