#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_blksize;
  int /*<<< orphan*/ st_mode;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ mfm_copy_progress(long long, long long);
int /*<<< orphan*/ mfm_show_message(int /*<<< orphan*/, int);
int open(char *, int, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int stat(char *, struct stat *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
scalar_t__ write(int, char *, scalar_t__);
int mfm_copy_file(char *source, char *dest, long long *cur, long long ttl) {
  struct stat st;
  if (stat(dest, &st) != -1) {
    return 1;
  }
  stat(source, &st);
  int inp = open(source, O_RDONLY, 0);
  if (inp == -1) {
    return 1;
  }
  int otp = open(dest, O_WRONLY | O_CREAT, st.st_mode);
  if (otp == -1) {
    close(inp);
    return 1;
  }
  int bs = st.st_blksize;
  char *buff = malloc(bs);
  ssize_t readed;
  while (readed = read(inp, buff, bs)) {
    if (readed < 0) {
      mfm_show_message(strerror(errno), 1);
      break;
    }
    ssize_t written = 0;
    while (written < readed) {
      written += write(otp, buff + written, readed - written);
    }
    *cur += readed;
    mfm_copy_progress(*cur, ttl);
  }
  free(buff);
  close(inp);
  close(otp);
  return 0;
}


x86 assembly (clang, -O0):
<mfm_copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     11da <mfm_copy_file+0x3a>
movl   $0x1,-0x4(%rbp)
jmp    132a <mfm_copy_file+0x18a>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1214 <mfm_copy_file+0x74>
movl   $0x1,-0x4(%rbp)
jmp    132a <mfm_copy_file+0x18a>
mov    -0x18(%rbp),%rdi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
mov    -0x2c(%rbp),%edx
call   10b0 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    1253 <mfm_copy_file+0xb3>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    132a <mfm_copy_file+0x18a>
mov    -0x30(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1070 <read@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
je     130a <mfm_copy_file+0x16a>
cmpq   $0x0,-0x50(%rbp)
jge    12ac <mfm_copy_file+0x10c>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <mfm_show_message@plt>
jmp    130a <mfm_copy_file+0x16a>
movq   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    -0x50(%rbp),%rax
jge    12e7 <mfm_copy_file+0x147>
mov    -0x38(%rbp),%edi
mov    -0x48(%rbp),%rsi
add    -0x58(%rbp),%rsi
mov    -0x50(%rbp),%rdx
sub    -0x58(%rbp),%rdx
call   1050 <write@plt>
add    -0x58(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    12b4 <mfm_copy_file+0x114>
mov    -0x50(%rbp),%rcx
mov    -0x20(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
call   10a0 <mfm_copy_progress@plt>
jmp    1265 <mfm_copy_file+0xc5>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfm_copy_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1080 <stat@plt>
mov    $0x1,%r14d
cmp    $0xffffffff,%eax
jne    12f7 <mfm_copy_file+0x157>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   10b0 <open@plt>
mov    %eax,0x4(%rsp)
cmp    $0xffffffff,%eax
je     12f7 <mfm_copy_file+0x157>
mov    0x2db6(%rip),%rax
mov    0x2de7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0xc(%rsp),%edx
mov    %rbp,%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     12b6 <mfm_copy_file+0x116>
mov    %eax,%ebp
mov    0x8(%rsp),%r14d
mov    %r14d,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    0x4(%rsp),%edi
mov    %rax,%rsi
mov    %r14d,0x14(%rsp)
mov    %r14d,%edx
call   1070 <read@plt>
test   %rax,%rax
je     12dc <mfm_copy_file+0x13c>
mov    %rax,%r14
nopl   0x0(%rax)
test   %r14,%r14
js     12c2 <mfm_copy_file+0x122>
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
lea    (%rbx,%r15,1),%rsi
mov    %r14,%rdx
sub    %r15,%rdx
mov    %ebp,%edi
call   1050 <write@plt>
add    %rax,%r15
cmp    %r15,%r14
jg     1270 <mfm_copy_file+0xd0>
add    (%r12),%r14
mov    %r14,(%r12)
mov    %r14,%rdi
mov    %r13,%rsi
call   10a0 <mfm_copy_progress@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
mov    0x14(%rsp),%edx
call   1070 <read@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1260 <mfm_copy_file+0xc0>
jmp    12dc <mfm_copy_file+0x13c>
mov    $0x1,%r14d
mov    0x4(%rsp),%ebp
jmp    12f0 <mfm_copy_file+0x150>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <mfm_show_message@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
xor    %r14d,%r14d
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@errno = global i32 0
define i32 @mfm_copy_file(i8* %0, i8* %1, i64* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64*
%9 = alloca i64
%10 = alloca %struct.stat
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64* %2, i64** %8
store i64 %3, i64* %9
%17 = load i8*, i8** %7
%18 = call i32 @stat(i8* %17, %struct.stat* %10)
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %4
store i32 1, i32* %5
br label %93
21: ; preds = %4
%22 = load i8*, i8** %6
%23 = call i32 @stat(i8* %22, %struct.stat* %10)
%24 = load i8*, i8** %6
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25, i32 0)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %21
store i32 1, i32* %5
br label %93
30: ; preds = %21
%31 = load i8*, i8** %7
%32 = load i32, i32* @O_WRONLY
%33 = load i32, i32* @O_CREAT
%34 = or i32 %32, %33
%35 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%36 = load i32, i32* %35
%37 = call i32 @open(i8* %31, i32 %34, i32 %36)
store i32 %37, i32* %12
%38 = load i32, i32* %12
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %30
%41 = load i32, i32* %11
%42 = call i32 @close(i32 %41)
store i32 1, i32* %5
br label %93
43: ; preds = %30
%44 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%45 = load i32, i32* %44
store i32 %45, i32* %13
%46 = load i32, i32* %13
%47 = call i8* @malloc(i32 %46)
store i8* %47, i8** %14
br label %48
48: ; preds = %77, %43
%49 = load i32, i32* %11
%50 = load i8*, i8** %14
%51 = load i32, i32* %13
%52 = call i64 @read(i32 %49, i8* %50, i32 %51)
store i64 %52, i64* %15
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %86
54: ; preds = %48
%55 = load i64, i64* %15
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = load i32, i32* @errno
%59 = call i32 @strerror(i32 %58)
%60 = call i32 @mfm_show_message(i32 %59, i32 1)
br label %86
61: ; preds = %54
store i64 0, i64* %16
br label %62
62: ; preds = %66, %61
%63 = load i64, i64* %16
%64 = load i64, i64* %15
%65 = icmp slt i64 %63, %64
br i1 %65, label %66, label %77
66: ; preds = %62
%67 = load i32, i32* %12
%68 = load i8*, i8** %14
%69 = load i64, i64* %16
%70 = getelementptr inbounds i8, i8* %68, i64 %69
%71 = load i64, i64* %15
%72 = load i64, i64* %16
%73 = sub nsw i64 %71, %72
%74 = call i64 @write(i32 %67, i8* %70, i64 %73)
%75 = load i64, i64* %16
%76 = add nsw i64 %75, %74
store i64 %76, i64* %16
br label %62
77: ; preds = %62
%78 = load i64, i64* %15
%79 = load i64*, i64** %8
%80 = load i64, i64* %79
%81 = add nsw i64 %80, %78
store i64 %81, i64* %79
%82 = load i64*, i64** %8
%83 = load i64, i64* %82
%84 = load i64, i64* %9
%85 = call i32 @mfm_copy_progress(i64 %83, i64 %84)
br label %48
86: ; preds = %57, %48
%87 = load i8*, i8** %14
%88 = call i32 @free(i8* %87)
%89 = load i32, i32* %11
%90 = call i32 @close(i32 %89)
%91 = load i32, i32* %12
%92 = call i32 @close(i32 %91)
store i32 0, i32* %5
br label %93
93: ; preds = %86, %40, %29, %20
%94 = load i32, i32* %5
ret i32 %94
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@errno = global i32 0
define i32 @mfm_copy_file(i8* nocapture %0, i8* nocapture %1, i64* nocapture %2, i64 %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = call i32 @stat(i8* %1, %struct.stat* %5)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %55
9: ; preds = %4
%10 = call i32 @stat(i8* %0, %struct.stat* %5)
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %0, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %55, label %14
14: ; preds = %9
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %16, %15
%18 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%19 = load i32, i32* %18
%20 = tail call i32 @open(i8* %1, i32 %17, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %51, label %22
22: ; preds = %14
%23 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%24 = load i32, i32* %23
%25 = tail call i8* @malloc(i32 %24)
%26 = tail call i64 @read(i32 %12, i8* %25, i32 %24)
%27 = icmp eq i64 %26, 0
br i1 %27, label %48, label %28
28: ; preds = %22, %42
%29 = phi i64 [ %46, %42 ], [ %26, %22 ]
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i32, i32* @errno
%33 = tail call i32 @strerror(i32 %32)
%34 = tail call i32 @mfm_show_message(i32 %33, i32 1)
br label %48
35: ; preds = %28, %35
%36 = phi i64 [ %40, %35 ], [ 0, %28 ]
%37 = getelementptr inbounds i8, i8* %25, i64 %36
%38 = sub nsw i64 %29, %36
%39 = tail call i64 @write(i32 %20, i8* %37, i64 %38)
%40 = add nsw i64 %39, %36
%41 = icmp sgt i64 %29, %40
br i1 %41, label %35, label %42
42: ; preds = %35
%43 = load i64, i64* %2
%44 = add nsw i64 %43, %29
store i64 %44, i64* %2
%45 = tail call i32 @mfm_copy_progress(i64 %44, i64 %3)
%46 = tail call i64 @read(i32 %12, i8* %25, i32 %24)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %28
48: ; preds = %42, %22, %31
%49 = tail call i32 @free(i8* %25)
%50 = tail call i32 @close(i32 %12)
br label %51
51: ; preds = %14, %48
%52 = phi i32 [ %20, %48 ], [ %12, %14 ]
%53 = phi i32 [ 0, %48 ], [ 1, %14 ]
%54 = tail call i32 @close(i32 %52)
br label %55
55: ; preds = %51, %9, %4
%56 = phi i32 [ 1, %4 ], [ 1, %9 ], [ %53, %51 ]
ret i32 %56
}