#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 */
scalar_t__ EINTR;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
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);
  }
  do {
    sfd = open(argv[1], O_RDONLY);
    if (sfd < 0) {
      if (errno != EINTR) {
        perror("open()");
        exit(1);
      }
    }
  } while (sfd < 0);
  do {
    dfd = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0600);
    if (dfd < 0) {
      if (errno != EINTR) {
        perror("open()");
        exit(1);
      }
    }
  } while (dfd < 0);
  while (1) {
    len = read(sfd, buf, 1024);
    if (len == 0)
      break;
    if (len < 0) {
      if (errno == EINTR)
        continue;
      perror("read()");
      break;
    }
    pos = 0;
    while (len > 0) {
      ret = write(dfd, buf + pos, len);
      if (ret < 0) {
        if (errno == EINTR)
          continue;
        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>
jmp    11be <main+0x4e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1218 <main+0xa8>
mov    0x2dd6(%rip),%rax
mov    (%rax),%rax
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%rax
je     1213 <main+0xa3>
lea    0xe1b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1218 <main+0xa8>
jmp    121d <main+0xad>
cmpl   $0x0,-0x14(%rbp)
jl     11be <main+0x4e>
jmp    122c <main+0xbc>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
mov    0x2dac(%rip),%rax
or     (%rax),%esi
mov    0x2dab(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    129d <main+0x12d>
mov    0x2d51(%rip),%rax
mov    (%rax),%rax
mov    0x2d3f(%rip),%rcx
cmp    (%rcx),%rax
je     1298 <main+0x128>
lea    0xd96(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    129d <main+0x12d>
jmp    12a2 <main+0x132>
cmpl   $0x0,-0x18(%rbp)
jl     122c <main+0xbc>
jmp    12b1 <main+0x141>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jne    12dd <main+0x16d>
jmp    13c5 <main+0x255>
cmpl   $0x0,-0x424(%rbp)
jge    131a <main+0x1aa>
mov    0x2ccf(%rip),%rax
mov    (%rax),%rax
mov    0x2cbd(%rip),%rcx
cmp    (%rcx),%rax
jne    1309 <main+0x199>
jmp    12b1 <main+0x141>
lea    0xd16(%rip),%rdi
call   1080 <perror@plt>
jmp    13c5 <main+0x255>
movl   $0x0,-0x42c(%rbp)
cmpl   $0x0,-0x424(%rbp)
jle    13c0 <main+0x250>
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    1395 <main+0x225>
mov    0x2c56(%rip),%rax
mov    (%rax),%rax
mov    0x2c44(%rip),%rcx
cmp    (%rcx),%rax
jne    1382 <main+0x212>
jmp    1324 <main+0x1b4>
lea    0xca4(%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    1324 <main+0x1b4>
jmp    12b1 <main+0x141>
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   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jl     12f7 <main+0x187>
mov    0x2e44(%rip),%rbp
mov    0x2e25(%rip),%r13
mov    0x2e16(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rbx),%rdi
mov    0x0(%rbp),%esi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
jns    11cd <main+0x5d>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     11b0 <main+0x40>
jmp    120c <main+0x9c>
mov    %eax,0xc(%rsp)
mov    0x2e18(%rip),%rbp
mov    0x2e19(%rip),%r15
nop
mov    0x10(%rbx),%rdi
mov    0x0(%rbp),%esi
mov    0x2dba(%rip),%rax
or     (%rax),%esi
or     (%r15),%esi
mov    $0x180,%edx
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
jns    1222 <main+0xb2>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     11e0 <main+0x70>
lea    0xe0c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %eax,%r12d
lea    0x10(%rsp),%rsi
mov    0xc(%rsp),%ebp
mov    %ebp,%edi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
je     12ce <main+0x15e>
mov    %eax,%ebx
jmp    1274 <main+0x104>
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%r13),%rax
cmp    (%r14),%rax
jne    12be <main+0x14e>
mov    0xc(%rsp),%ebp
mov    %ebp,%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
je     12ce <main+0x15e>
test   %ebx,%ebx
js     1250 <main+0xe0>
xor    %r15d,%r15d
lea    0x10(%rsp),%rbp
jmp    1299 <main+0x129>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%r13),%rax
cmp    (%r14),%rax
jne    12e4 <main+0x174>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1290 <main+0x120>
add    %eax,%r15d
movslq %r15d,%rcx
lea    (%rsp,%rcx,1),%rbp
add    $0x10,%rbp
sub    %eax,%ebx
jg     1299 <main+0x129>
jmp    1259 <main+0xe9>
lea    0xd61(%rip),%rdi
call   1080 <perror@plt>
mov    0xc(%rsp),%ebp
mov    %r12d,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xd42(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xcf6(%rip),%rsi
call   1060 <fprintf@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
@errno = global i64 0
@EINTR = global i64 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
br label %22
22: ; preds = %39, %21
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDONLY
%27 = call i32 (i8*, i32, ...) @open(i8* %25, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %22
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30
br label %38
38: ; preds = %37, %22
br label %39
39: ; preds = %38
%40 = load i32, i32* %6
%41 = icmp slt i32 %40, 0
br i1 %41, label %22, label %42
42: ; preds = %39
br label %43
43: ; preds = %64, %42
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = load i32, i32* @O_WRONLY
%48 = load i32, i32* @O_TRUNC
%49 = or i32 %47, %48
%50 = load i32, i32* @O_CREAT
%51 = or i32 %49, %50
%52 = call i32 (i8*, i32, ...) @open(i8* %46, i32 %51, i32 384)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %63
55: ; preds = %43
%56 = load i64, i64* @errno
%57 = load i64, i64* @EINTR
%58 = icmp ne i64 %56, %57
br i1 %58, label %59, label %62
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %55
br label %63
63: ; preds = %62, %43
br label %64
64: ; preds = %63
%65 = load i32, i32* %7
%66 = icmp slt i32 %65, 0
br i1 %66, label %43, label %67
67: ; preds = %64
br label %68
68: ; preds = %67, %82, %114
%69 = load i32, i32* %6
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%71 = call i32 @read(i32 %69, i8* %70, i32 1024)
store i32 %71, i32* %9
%72 = load i32, i32* %9
%73 = icmp eq i32 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %68
br label %115
75: ; preds = %68
%76 = load i32, i32* %9
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %85
78: ; preds = %75
%79 = load i64, i64* @errno
%80 = load i64, i64* @EINTR
%81 = icmp eq i64 %79, %80
br i1 %81, label %82, label %83
82: ; preds = %78
br label %68
83: ; preds = %78
%84 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %115
85: ; preds = %75
store i32 0, i32* %11
br label %86
86: ; preds = %107, %103, %85
%87 = load i32, i32* %9
%88 = icmp sgt i32 %87, 0
br i1 %88, label %89, label %114
89: ; preds = %86
%90 = load i32, i32* %7
%91 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%92 = load i32, i32* %11
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds i8, i8* %91, i64 %93
%95 = load i32, i32* %9
%96 = call i32 @write(i32 %90, i8* %94, i32 %95)
store i32 %96, i32* %10
%97 = load i32, i32* %10
%98 = icmp slt i32 %97, 0
br i1 %98, label %99, label %107
99: ; preds = %89
%100 = load i64, i64* @errno
%101 = load i64, i64* @EINTR
%102 = icmp eq i64 %100, %101
br i1 %102, label %103, label %104
103: ; preds = %99
br label %86
104: ; preds = %99
%105 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%106 = call i32 @exit(i32 0)
unreachable
107: ; preds = %89
%108 = load i32, i32* %10
%109 = load i32, i32* %9
%110 = sub nsw i32 %109, %108
store i32 %110, i32* %9
%111 = load i32, i32* %10
%112 = load i32, i32* %11
%113 = add nsw i32 %112, %111
store i32 %113, i32* %11
br label %86
114: ; preds = %86
br label %68
115: ; preds = %83, %74
%116 = load i32, i32* %7
%117 = call i32 @close(i32 %116)
%118 = load i32, i32* %6
%119 = call i32 @close(i32 %118)
%120 = 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
@errno = global i64 0
@EINTR = global i64 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 %8, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
br label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %6, %20
%14 = load i8*, i8** %7
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 (i8*, i32, ...) @open(i8* %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = getelementptr inbounds i8*, i8** %1, i64 2
br label %27
20: ; preds = %13
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %13, label %24
24: ; preds = %20
%25 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %18, %39
%28 = load i8*, i8** %19
%29 = load i32, i32* @O_WRONLY
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %30, %29
%32 = load i32, i32* @O_CREAT
%33 = or i32 %31, %32
%34 = tail call i32 (i8*, i32, ...) @open(i8* %28, i32 %33, i32 384)
%35 = icmp slt i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %27
%37 = call i32 @read(i32 %16, i8* %4, i32 1024)
%38 = icmp eq i32 %37, 0
br i1 %38, label %78, label %46
39: ; preds = %27
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINTR
%42 = icmp eq i64 %40, %41
br i1 %42, label %27, label %43
43: ; preds = %39
%44 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%45 = tail call i32 @exit(i32 1)
unreachable
46: ; preds = %36, %53
%47 = phi i32 [ %54, %53 ], [ %37, %36 ]
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %74
49: ; preds = %46
%50 = load i64, i64* @errno
%51 = load i64, i64* @EINTR
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %56
53: ; preds = %68, %49
%54 = call i32 @read(i32 %16, i8* %4, i32 1024)
%55 = icmp eq i32 %54, 0
br i1 %55, label %78, label %46
56: ; preds = %49
%57 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %78
58: ; preds = %74, %61
%59 = call i32 @write(i32 %34, i8* %75, i32 %77)
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %68
61: ; preds = %58
%62 = load i64, i64* @errno
%63 = load i64, i64* @EINTR
%64 = icmp eq i64 %62, %63
br i1 %64, label %58, label %65
65: ; preds = %61
%66 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%67 = tail call i32 @exit(i32 0)
unreachable
68: ; preds = %58
%69 = sub nsw i32 %77, %59
%70 = add nsw i32 %59, %76
%71 = icmp sgt i32 %69, 0
%72 = sext i32 %70 to i64
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %72
br i1 %71, label %74, label %53
74: ; preds = %46, %68
%75 = phi i8* [ %73, %68 ], [ %4, %46 ]
%76 = phi i32 [ %70, %68 ], [ 0, %46 ]
%77 = phi i32 [ %69, %68 ], [ %47, %46 ]
br label %58
78: ; preds = %53, %36, %56
%79 = tail call i32 @close(i32 %34)
%80 = tail call i32 @close(i32 %16)
%81 = tail call i32 @exit(i32 0)
unreachable
}