#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 */

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char **argv) {
  int sfd, dfd;
  char buf[1024];
  int len, ret, pos;
  if (argc < 3) {
    fprintf(stderr, "Usage:%s <srcfile> <destfile>\n", argv[0]);
    exit(1);
  }
  sfd = open(argv[1], O_RDONLY);
  if (sfd < 0) {
    perror("open()");
    exit(1);
  }
  dfd = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0600);
  if (dfd < 0) {
    close(sfd);
    perror("open()");
    exit(1);
  }
  while (1) {
    len = read(sfd, buf, 1024);
    if (len == 0)
      break;
    if (len < 0) {
      perror("read()");
      break;
    }
    pos = 0;
    while (len > 0) {
      ret = write(dfd, buf + pos, len);
      if (ret < 0) {
        perror("write()");
        exit(0);
      }
      len -= ret;
      pos += ret;
    }
  }
  close(dfd);
  close(sfd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11b9 <main+0x49>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe56(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f4 <main+0x84>
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    0x2de4(%rip),%rax
or     (%rax),%esi
mov    0x2de3(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    124e <main+0xde>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
lea    0xde0(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1253 <main+0xe3>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jne    127f <main+0x10f>
jmp    1329 <main+0x1b9>
cmpl   $0x0,-0x424(%rbp)
jge    129d <main+0x12d>
lea    0xd93(%rip),%rdi
call   1080 <perror@plt>
jmp    1329 <main+0x1b9>
movl   $0x0,-0x42c(%rbp)
cmpl   $0x0,-0x424(%rbp)
jle    1324 <main+0x1b4>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
movslq -0x42c(%rbp),%rax
add    %rax,%rsi
mov    -0x424(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0x0,-0x428(%rbp)
jge    12f9 <main+0x189>
lea    0xd40(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x428(%rbp),%ecx
mov    -0x424(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x424(%rbp)
mov    -0x428(%rbp),%eax
add    -0x42c(%rbp),%eax
mov    %eax,-0x42c(%rbp)
jmp    12a7 <main+0x137>
jmp    1253 <main+0xe3>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jle    1279 <main+0x109>
mov    0x8(%rbx),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     12a3 <main+0x133>
mov    %eax,%r15d
mov    0x2e07(%rip),%rax
mov    0x2e38(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbx),%rdi
mov    0x2e31(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     129b <main+0x12b>
mov    %eax,%r12d
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
je     124f <main+0xdf>
mov    %eax,%ebx
mov    %rsp,%r14
nopl   0x0(%rax)
test   %ebx,%ebx
js     1243 <main+0xd3>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rsi
mov    %r12d,%edi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1266 <main+0xf6>
add    %eax,%ebp
sub    %eax,%ebx
jg     1210 <main+0xa0>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1200 <main+0x90>
jmp    124f <main+0xdf>
lea    0xddc(%rip),%rdi
call   1080 <perror@plt>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xdc0(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd74(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %r15d,%edi
call   1040 <close@plt>
lea    0xd75(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"Usage:%s <srcfile> <destfile>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.2 = [7 x i8] c"read()\00"
@.str.3 = [8 x i8] c"write()\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDONLY
%26 = call i32 (i8*, i32, ...) @open(i8* %24, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = load i32, i32* @O_WRONLY
%37 = load i32, i32* @O_TRUNC
%38 = or i32 %36, %37
%39 = load i32, i32* @O_CREAT
%40 = or i32 %38, %39
%41 = call i32 (i8*, i32, ...) @open(i8* %35, i32 %40, i32 384)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %32
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %32
br label %50
50: ; preds = %49, %86
%51 = load i32, i32* %6
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%53 = call i32 @read(i32 %51, i8* %52, i32 1024)
store i32 %53, i32* %9
%54 = load i32, i32* %9
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %50
br label %87
57: ; preds = %50
%58 = load i32, i32* %9
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %87
62: ; preds = %57
store i32 0, i32* %11
br label %63
63: ; preds = %79, %62
%64 = load i32, i32* %9
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %86
66: ; preds = %63
%67 = load i32, i32* %7
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%69 = load i32, i32* %11
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = load i32, i32* %9
%73 = call i32 @write(i32 %67, i8* %71, i32 %72)
store i32 %73, i32* %10
%74 = load i32, i32* %10
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %66
%77 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%78 = call i32 @exit(i32 0)
unreachable
79: ; preds = %66
%80 = load i32, i32* %10
%81 = load i32, i32* %9
%82 = sub nsw i32 %81, %80
store i32 %82, i32* %9
%83 = load i32, i32* %10
%84 = load i32, i32* %11
%85 = add nsw i32 %84, %83
store i32 %85, i32* %11
br label %63
86: ; preds = %63
br label %50
87: ; preds = %60, %56
%88 = load i32, i32* %7
%89 = call i32 @close(i32 %88)
%90 = load i32, i32* %6
%91 = call i32 @close(i32 %90)
%92 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"Usage:%s <srcfile> <destfile>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.2 = [7 x i8] c"read()\00"
@.str.3 = [8 x i8] c"write()\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 (i8*, i32, ...) @open(i8* %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_TRUNC
%25 = or i32 %24, %23
%26 = load i32, i32* @O_CREAT
%27 = or i32 %25, %26
%28 = tail call i32 (i8*, i32, ...) @open(i8* %22, i32 %27, i32 384)
%29 = icmp slt i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %20
%31 = call i32 @read(i32 %15, i8* %4, i32 1024)
%32 = icmp eq i32 %31, 0
br i1 %32, label %59, label %40
33: ; preds = %20
%34 = tail call i32 @close(i32 %15)
%35 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %55
%38 = call i32 @read(i32 %15, i8* %4, i32 1024)
%39 = icmp eq i32 %38, 0
br i1 %39, label %59, label %40
40: ; preds = %30, %37
%41 = phi i32 [ %38, %37 ], [ %31, %30 ]
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %59
45: ; preds = %40, %55
%46 = phi i32 [ %57, %55 ], [ 0, %40 ]
%47 = phi i32 [ %56, %55 ], [ %41, %40 ]
%48 = sext i32 %46 to i64
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %48
%50 = call i32 @write(i32 %28, i8* %49, i32 %47)
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %45
%53 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%54 = tail call i32 @exit(i32 0)
unreachable
55: ; preds = %45
%56 = sub nsw i32 %47, %50
%57 = add nsw i32 %50, %46
%58 = icmp sgt i32 %56, 0
br i1 %58, label %45, label %37
59: ; preds = %37, %30, %43
%60 = tail call i32 @close(i32 %28)
%61 = tail call i32 @close(i32 %15)
%62 = tail call i32 @exit(i32 0)
unreachable
}