#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 {
  scalar_t__ st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ errno;
scalar_t__ fstat(int, struct stat *);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(void *, void *, scalar_t__);
void *mmap(int /*<<< orphan*/, scalar_t__, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, char *, ...);
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fdin, fdout;
  void *src, *dst;
  struct stat statbuf;
  if (argc != 3) {
    printf("Usage:%s <fromfile> <tofile>\n", argv[0]);
    return -1;
  }
  if ((fdin = open(argv[1], O_RDONLY)) < 0) {
    printf("open %s:%s\n", argv[1], strerror(errno));
    return -1;
  }
  if ((fdout = open(
           argv[2], O_RDWR | O_CREAT | O_TRUNC,
           (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH))) < 0) {
    printf("open %s:%s\n", argv[1], strerror(errno));
    return -1;
  }
  if (fstat(fdin, &statbuf) < 0) {
    printf("fstat:%s\n", strerror(errno));
    return -1;
  }
  if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) < 0) {
    printf("lseek:%s\n", strerror(errno));
    return -1;
  }
  if (write(fdout, "", 1) != 1) {
    printf("write:%s\n", strerror(errno));
    return -1;
  }
  return 0;
  src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0);
  if (src == MAP_FAILED) {
    printf("mmap1:%s\n", strerror(errno));
    return -1;
  }
  dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout, 0);
  if (dst == MAP_FAILED) {
    printf("mmap2:%s\n", strerror(errno));
    return -1;
  }
  memcpy(dst, src, statbuf.st_size - 1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11a1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <main+0x225>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1200 <main+0xa0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <main+0x225>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
mov    0x2de0(%rip),%rax
or     (%rax),%esi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
mov    0x2d66(%rip),%rax
mov    (%rax),%edx
mov    0x2d8d(%rip),%rax
or     (%rax),%edx
mov    0x2d64(%rip),%rax
or     (%rax),%edx
mov    0x2d83(%rip),%rax
or     (%rax),%edx
mov    0x2d4a(%rip),%rax
or     (%rax),%edx
mov    0x2d29(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12a7 <main+0x147>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <main+0x225>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
jge    12e8 <main+0x188>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <main+0x225>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
sub    $0x1,%rsi
mov    0x2cae(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    1336 <main+0x1d6>
mov    0x2c9e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xd11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <main+0x225>
mov    -0x18(%rbp),%edi
lea    0xcdd(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     137e <main+0x21e>
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xcd3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <main+0x225>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1265 <main+0x105>
mov    0x8(%rbx),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     1271 <main+0x111>
mov    %eax,%ebp
mov    0x2e3f(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e45(%rip),%rax
or     (%rax),%esi
mov    0x2ddc(%rip),%rax
mov    0x2e05(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dda(%rip),%rax
or     (%rax),%edx
mov    0x2df9(%rip),%rax
or     (%rax),%edx
mov    0x2dc0(%rip),%rax
or     (%rax),%edx
mov    0x10(%rbx),%rdi
mov    0x2d9b(%rip),%rax
or     (%rax),%edx
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     1271 <main+0x111>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1080 <fstat@plt>
test   %rax,%rax
js     1299 <main+0x139>
mov    0x8(%rsp),%rsi
add    $0xffffffffffffffff,%rsi
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
call   1050 <lseek@plt>
test   %rax,%rax
js     12b0 <main+0x150>
lea    0xde9(%rip),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
je     12d4 <main+0x174>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xddb(%rip),%rdi
jmp    12c5 <main+0x165>
mov    (%rbx),%rsi
lea    0xd91(%rip),%rdi
jmp    12c8 <main+0x168>
mov    0x8(%rbx),%rbx
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xd94(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12cf <main+0x16f>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xd7c(%rip),%rdi
jmp    12c5 <main+0x165>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xd6f(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [30 x i8] c"Usage:%s <fromfile> <tofile>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"open %s:%s\0A\00"
@errno = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.2 = [10 x i8] c"fstat:%s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [10 x i8] c"lseek:%s\0A\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [10 x i8] c"write:%s\0A\00"
@MAP_FAILED = global i8* null
@MAP_SHARED = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %90
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDONLY
%23 = call i32 (i8*, i32, ...) @open(i8* %21, i32 %22)
store i32 %23, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %18
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %30)
store i32 -1, i32* %3
br label %90
32: ; preds = %18
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = load i32, i32* @O_RDWR
%37 = load i32, i32* @O_CREAT
%38 = or i32 %36, %37
%39 = load i32, i32* @O_TRUNC
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IRUSR
%42 = load i32, i32* @S_IWUSR
%43 = or i32 %41, %42
%44 = load i32, i32* @S_IRGRP
%45 = or i32 %43, %44
%46 = load i32, i32* @S_IWGRP
%47 = or i32 %45, %46
%48 = load i32, i32* @S_IROTH
%49 = or i32 %47, %48
%50 = load i32, i32* @S_IWOTH
%51 = or i32 %49, %50
%52 = call i32 (i8*, i32, ...) @open(i8* %35, i32 %40, i32 %51)
store i32 %52, i32* %7
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %61
54: ; preds = %32
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 1
%57 = load i8*, i8** %56
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %57, i8* %59)
store i32 -1, i32* %3
br label %90
61: ; preds = %32
%62 = load i32, i32* %6
%63 = call i64 @fstat(i32 %62, %struct.stat* %10)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %61
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %67)
store i32 -1, i32* %3
br label %90
69: ; preds = %61
%70 = load i32, i32* %7
%71 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%72 = load i64, i64* %71
%73 = sub nsw i64 %72, 1
%74 = load i32, i32* @SEEK_SET
%75 = call i64 @lseek(i32 %70, i64 %73, i32 %74)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %81
77: ; preds = %69
%78 = load i32, i32* @errno
%79 = call i8* @strerror(i32 %78)
%80 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %79)
store i32 -1, i32* %3
br label %90
81: ; preds = %69
%82 = load i32, i32* %7
%83 = call i32 @write(i32 %82, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i32 1)
%84 = icmp ne i32 %83, 1
br i1 %84, label %85, label %89
85: ; preds = %81
%86 = load i32, i32* @errno
%87 = call i8* @strerror(i32 %86)
%88 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %87)
store i32 -1, i32* %3
br label %90
89: ; preds = %81
store i32 0, i32* %3
br label %90
90: ; preds = %89, %85, %77, %65, %54, %25, %13
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [30 x i8] c"Usage:%s <fromfile> <tofile>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"open %s:%s\0A\00"
@errno = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.2 = [10 x i8] c"fstat:%s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [10 x i8] c"lseek:%s\0A\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [10 x i8] c"write:%s\0A\00"
@MAP_FAILED = global i8* null
@MAP_SHARED = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %71
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 (i8*, i32, ...) @open(i8* %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %9
%16 = load i8*, i8** %10
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* %18)
br label %71
20: ; preds = %9
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = load i32, i32* @O_TRUNC
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IWUSR
%30 = or i32 %29, %28
%31 = load i32, i32* @S_IRGRP
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IWGRP
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IROTH
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IWOTH
%38 = or i32 %36, %37
%39 = tail call i32 (i8*, i32, ...) @open(i8* %22, i32 %27, i32 %38)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %20
%42 = load i8*, i8** %10
%43 = load i32, i32* @errno
%44 = tail call i8* @strerror(i32 %43)
%45 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %42, i8* %44)
br label %71
46: ; preds = %20
%47 = call i64 @fstat(i32 %13, %struct.stat* %3)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i32, i32* @errno
%51 = tail call i8* @strerror(i32 %50)
%52 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %51)
br label %71
53: ; preds = %46
%54 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%55 = load i64, i64* %54
%56 = add nsw i64 %55, -1
%57 = load i32, i32* @SEEK_SET
%58 = tail call i64 @lseek(i32 %39, i64 %56, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %53
%61 = load i32, i32* @errno
%62 = tail call i8* @strerror(i32 %61)
%63 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %62)
br label %71
64: ; preds = %53
%65 = tail call i32 @write(i32 %39, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i32 1)
%66 = icmp eq i32 %65, 1
br i1 %66, label %71, label %67
67: ; preds = %64
%68 = load i32, i32* @errno
%69 = tail call i8* @strerror(i32 %68)
%70 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %69)
br label %71
71: ; preds = %64, %67, %60, %49, %41, %15, %6
%72 = phi i32 [ -1, %6 ], [ -1, %15 ], [ -1, %41 ], [ -1, %49 ], [ -1, %60 ], [ -1, %67 ], [ 0, %64 ]
ret i32 %72
}