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

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strlen(char *);
scalar_t__ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("usage: %s fromfile tofile\n", argv[0]);
  }
  time_t pre_time = time(NULL);
  FILE *fp = NULL;
  char buf[4096];
  if ((fp = fopen(argv[1], "r+")) == NULL) {
    perror("fopen");
    goto error;
  }
  int fd = 0;
  if ((fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC)) < 0) {
    perror("open");
    goto error;
  }
  while (fgets(buf, 4096, fp)) {
    write(fd, buf, strlen(buf));
  }
  printf("time used: %d\n", (int)(time(NULL) - pre_time));
  fclose(fp);
  close(fd);
  return 0;
error:
  if (!fp) {
    fclose(fp);
  }
  if (!fd) {
    close(fd);
  }
  exit(-1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e8 <main+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0f(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1230 <main+0x80>
lea    0xdf8(%rip),%rdi
call   10c0 <perror@plt>
jmp    1321 <main+0x171>
movl   $0x0,-0x1024(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
mov    0x2da6(%rip),%rax
or     (%rax),%esi
mov    0x2d95(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1024(%rbp)
cmp    $0x0,%eax
jge    1282 <main+0xd2>
lea    0xda7(%rip),%rdi
call   10c0 <perror@plt>
jmp    1321 <main+0x171>
jmp    1287 <main+0xd7>
lea    -0x1020(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x1000,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12e5 <main+0x135>
mov    -0x1024(%rbp),%eax
mov    %eax,-0x1034(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1030(%rbp)
lea    -0x1020(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1034(%rbp),%edi
mov    -0x1030(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1287 <main+0xd7>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
sub    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xd29(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1024(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x1040,%rsp
pop    %rbp
ret
cmpq   $0x0,-0x20(%rbp)
jne    1335 <main+0x185>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x1024(%rbp)
jne    134d <main+0x19d>
mov    -0x1024(%rbp),%edi
call   1070 <close@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rbp
cmp    $0x1,%edi
jg     11d7 <main+0x27>
mov    0x0(%rbp),%rsi
lea    0xe30(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   1090 <time@plt>
mov    %rax,%r14
mov    0x8(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12b2 <main+0x102>
mov    %rax,%r15
mov    0x2dc4(%rip),%rax
mov    0x2ded(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbp),%rdi
mov    0x2dd6(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
test   %eax,%eax
js     12cf <main+0x11f>
mov    %eax,%ebp
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1279 <main+0xc9>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    1250 <main+0xa0>
xor    %edi,%edi
call   1090 <time@plt>
sub    %r14d,%eax
lea    0xd9a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd65(%rip),%rdi
call   10c0 <perror@plt>
xor    %edi,%edi
call   1040 <fclose@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xd49(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"usage: %s fromfile tofile\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [6 x i8] c"fopen\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [15 x i8] c"time used: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32*
%8 = alloca [4096 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %12, %2
%18 = call i64 @time(i32* null)
store i64 %18, i64* %6
store i32* null, i32** %7
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %7
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %61
26: ; preds = %17
store i32 0, i32* %9
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = load i32, i32* @O_TRUNC
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %29, i32 %34)
store i32 %35, i32* %9
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %61
39: ; preds = %26
br label %40
40: ; preds = %45, %39
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%42 = load i32*, i32** %7
%43 = call i64 @fgets(i8* %41, i32 4096, i32* %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %40
%46 = load i32, i32* %9
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%49 = call i32 @strlen(i8* %48)
%50 = call i32 @write(i32 %46, i8* %47, i32 %49)
br label %40
51: ; preds = %40
%52 = call i64 @time(i32* null)
%53 = load i64, i64* %6
%54 = sub nsw i64 %52, %53
%55 = trunc i64 %54 to i32
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %55)
%57 = load i32*, i32** %7
%58 = call i32 @fclose(i32* %57)
%59 = load i32, i32* %9
%60 = call i32 @close(i32 %59)
ret i32 0
61: ; preds = %37, %24
%62 = load i32*, i32** %7
%63 = icmp ne i32* %62, null
br i1 %63, label %67, label %64
64: ; preds = %61
%65 = load i32*, i32** %7
%66 = call i32 @fclose(i32* %65)
br label %67
67: ; preds = %64, %61
%68 = load i32, i32* %9
%69 = icmp ne i32 %68, 0
br i1 %69, label %73, label %70
70: ; preds = %67
%71 = load i32, i32* %9
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %70, %67
%74 = call i32 @exit(i32 -1)
unreachable
}

LLVM IR (-O2):
@.str = [27 x i8] c"usage: %s fromfile tofile\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [6 x i8] c"fopen\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [15 x i8] c"time used: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %8
8: ; preds = %5, %2
%9 = tail call i64 @time(i32* null)
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %42, label %15
15: ; preds = %8
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = tail call i32 @open(i8* %17, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %40, label %25
25: ; preds = %15
%26 = call i64 @fgets(i8* %10, i32 4096, i32* %13)
%27 = icmp eq i64 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %25, %28
%29 = call i32 @strlen(i8* %10)
%30 = call i32 @write(i32 %23, i8* %10, i32 %29)
%31 = call i64 @fgets(i8* %10, i32 4096, i32* %13)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %28
33: ; preds = %28, %25
%34 = call i64 @time(i32* null)
%35 = sub nsw i64 %34, %9
%36 = trunc i64 %35 to i32
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = call i32 @fclose(i32* %13)
%39 = call i32 @close(i32 %23)
ret i32 0
40: ; preds = %15
%41 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %45
42: ; preds = %8
%43 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%44 = tail call i32 @fclose(i32* null)
br label %45
45: ; preds = %40, %42
%46 = tail call i32 @exit(i32 -1)
unreachable
}