#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 /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ copy_fd(int, int);
int creat(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *, char *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char const *);
int copy_file(const char *from, const struct stat *st, const char *to) {
  int from_fd, to_fd, error = 1;
  if ((from_fd = open(from, O_RDONLY)) == -1) {
    perror(from);
    return 1;
  }
  unlink(to);
  if ((to_fd = creat(to, st->st_mode)) < 0) {
    perror(to);
    close(from_fd);
    return 1;
  }
  if (copy_fd(from_fd, to_fd)) {
    fprintf(stderr, "%s -> %s: %s\n", from, to, strerror(errno));
    unlink(to);
    goto out;
  }
  error = 0;
out:
  close(from_fd);
  close(to_fd);
  return error;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11ce <copy_file+0x4e>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1293 <copy_file+0x113>
mov    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1080 <creat@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    120f <copy_file+0x8f>
mov    -0x20(%rbp),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1293 <copy_file+0x113>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1090 <copy_fd@plt>
cmp    $0x0,%rax
je     1276 <copy_file+0xf6>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    %rax,%r8
lea    0xd9d(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
jmp    127d <copy_file+0xfd>
movl   $0x0,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%r14
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     122e <copy_file+0xae>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1080 <creat@plt>
test   %eax,%eax
js     123d <copy_file+0xbd>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %eax,%esi
call   1090 <copy_fd@plt>
test   %rax,%rax
je     1212 <copy_file+0x92>
mov    0x2dfe(%rip),%rax
mov    (%rax),%r12d
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe0e(%rip),%rsi
mov    %r12d,%edi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %rax,%r8
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    $0x1,%ebx
jmp    1214 <copy_file+0x94>
xor    %ebx,%ebx
mov    %r15d,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    1223 <copy_file+0xa3>
mov    %rbx,%rdi
call   1070 <perror@plt>
mov    %r15d,%edi
call   1040 <close@plt>
mov    $0x1,%ebx
jmp    1223 <copy_file+0xa3>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"%s -> %s: %s\0A\00"
@errno = global i32 0
define i32 @copy_file(i8* %0, %struct.stat* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.stat*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store %struct.stat* %1, %struct.stat** %6
store i8* %2, i8** %7
store i32 1, i32* %10
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = call i32 @perror(i8* %16)
store i32 1, i32* %4
br label %53
18: ; preds = %3
%19 = load i8*, i8** %7
%20 = call i32 @unlink(i8* %19)
%21 = load i8*, i8** %7
%22 = load %struct.stat*, %struct.stat** %6
%23 = getelementptr inbounds %struct.stat, %struct.stat* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @creat(i8* %21, i32 %24)
store i32 %25, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = load i8*, i8** %7
%29 = call i32 @perror(i8* %28)
%30 = load i32, i32* %8
%31 = call i32 @close(i32 %30)
store i32 1, i32* %4
br label %53
32: ; preds = %18
%33 = load i32, i32* %8
%34 = load i32, i32* %9
%35 = call i64 @copy_fd(i32 %33, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %5
%40 = load i8*, i8** %7
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %39, i8* %40, i8* %42)
%44 = load i8*, i8** %7
%45 = call i32 @unlink(i8* %44)
br label %47
46: ; preds = %32
store i32 0, i32* %10
br label %47
47: ; preds = %46, %37
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %9
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %10
store i32 %52, i32* %4
br label %53
53: ; preds = %47, %27, %15
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"%s -> %s: %s\0A\00"
@errno = global i32 0
define i32 @copy_file(i8* %0, %struct.stat* nocapture %1, i8* %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @perror(i8* %0)
br label %31
9: ; preds = %3
%10 = tail call i32 @unlink(i8* %2)
%11 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i32 @creat(i8* %2, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = tail call i32 @perror(i8* %2)
%17 = tail call i32 @close(i32 %5)
br label %31
18: ; preds = %9
%19 = tail call i64 @copy_fd(i32 %5, i32 %13)
%20 = icmp eq i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %2, i8* %24)
%26 = tail call i32 @unlink(i8* %2)
br label %27
27: ; preds = %18, %21
%28 = phi i32 [ 1, %21 ], [ 0, %18 ]
%29 = tail call i32 @close(i32 %5)
%30 = tail call i32 @close(i32 %13)
br label %31
31: ; preds = %27, %15, %7
%32 = phi i32 [ 1, %7 ], [ 1, %15 ], [ %28, %27 ]
ret i32 %32
}