#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 {
  size_t st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fchmod(int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
int /*<<< orphan*/ fsync(int);
scalar_t__ malloc(size_t);
int open(char *, int, ...);
int read(int, char *, size_t);
int write(int, char *, size_t);
int main(int argc, char *argv[]) {
  char *src;
  char *dest;
  int src_fd;
  int dest_fd;
  struct stat src_stat;
  if (argc < 3)
    exit(-1);
  src = argv[1];
  dest = argv[2];
  src_fd = open(src, O_RDONLY);
  dest_fd =
      open(dest, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  int stat_res = fstat(src_fd, &src_stat);
  if (stat_res != 0)
    exit(-1);
  size_t src_filesize = src_stat.st_size;
  mode_t file_mode = src_stat.st_mode;
  char *buff = (char *)malloc(src_filesize);
  int read_res = read(src_fd, buff, src_filesize);
  int write_res = write(dest_fd, buff, src_filesize);
  if (read_res == -1 || write_res == -1)
    exit(-1);
  int err = errno;
  fsync(dest_fd);
  err = errno;
  fchmod(dest_fd, file_mode);
  close(dest_fd);
  close(src_fd);
  free(buff);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ca <main+0x2a>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    0x2d93(%rip),%rax
or     (%rax),%esi
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
mov    0x2db9(%rip),%rax
or     (%rax),%edx
mov    0x2d98(%rip),%rax
or     (%rax),%edx
mov    0x2d87(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10c0 <fstat@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     1260 <main+0xc0>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x60(%rbp)
cmpl   $0xffffffff,-0x5c(%rbp)
je     12b5 <main+0x115>
cmpl   $0xffffffff,-0x60(%rbp)
jne    12bf <main+0x11f>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x28(%rbp),%edi
call   1090 <fsync@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x4c(%rbp),%esi
call   1080 <fchmod@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jle    129c <main+0xfc>
mov    0x8(%rsi),%rdi
mov    0x10(%rsi),%rbx
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   10a0 <open@plt>
mov    0x2e22(%rip),%rcx
mov    0x2dcb(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2dd0(%rip),%rcx
mov    0x2de9(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2dce(%rip),%rcx
or     (%rcx),%edx
mov    %eax,%r14d
mov    0x2dba(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
call   10c0 <fstat@plt>
test   %eax,%eax
jne    129c <main+0xfc>
mov    0x8(%rsp),%rbx
mov    0x10(%rsp),%r15d
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %r14d,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1060 <read@plt>
mov    %eax,%r13d
mov    %ebp,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%r13d
je     129c <main+0xfc>
cmp    $0xffffffff,%eax
je     129c <main+0xfc>
mov    %ebp,%edi
call   1090 <fsync@plt>
mov    %ebp,%edi
mov    %r15d,%esi
call   1080 <fchmod@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 (i8*, i32, ...) @open(i8* %10, i32 %13)
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_WRONLY
%17 = or i32 %16, %15
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %19, %18
%21 = load i32, i32* @S_IRGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IROTH
%24 = or i32 %22, %23
%25 = tail call i32 (i8*, i32, ...) @open(i8* %12, i32 %17, i32 %24)
%26 = call i32 @fstat(i32 %14, %struct.stat* %3)
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %8
%29 = tail call i32 @exit(i32 -1)
unreachable
30: ; preds = %8
%31 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%32 = load i64, i64* %31
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%34 = load i32, i32* %33
%35 = tail call i64 @malloc(i64 %32)
%36 = inttoptr i64 %35 to i8*
%37 = tail call i32 @read(i32 %14, i8* %36, i64 %32)
%38 = tail call i32 @write(i32 %25, i8* %36, i64 %32)
%39 = icmp eq i32 %37, -1
%40 = icmp eq i32 %38, -1
%41 = or i1 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %30
%43 = tail call i32 @exit(i32 -1)
unreachable
44: ; preds = %30
%45 = tail call i32 @fsync(i32 %25)
%46 = tail call i32 @fchmod(i32 %25, i32 %34)
%47 = tail call i32 @close(i32 %25)
%48 = tail call i32 @close(i32 %14)
%49 = tail call i32 @free(i8* %36)
ret i32 0
}