#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_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int from_fd, to_fd;
  int bytes_read, bytes_write;
  char buffer[100];
  char *ptr;
  if (argc != 3) {
    fprintf(stderr, "Usage:%s from file to file\n\a", argv[0]);
    exit(1);
  }
  if ((from_fd = open(argv[1], O_RDONLY)) == -1) {
    return -1;
  }
  if ((to_fd = open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) {
    return -2;
  }
  while (bytes_read = read(from_fd, buffer, 100)) {
    if ((bytes_read == -1) && (errno != EINTR))
      break;
    else if (bytes_read > 0) {
      ptr = buffer;
      while (bytes_write = write(to_fd, ptr, bytes_read)) {
        if ((bytes_write == -1) && (errno != EINTR))
          break;
        else if (bytes_write == bytes_read)
          break;
        else if (bytes_write > 0) {
          ptr += bytes_write;
          bytes_read -= bytes_write;
        }
      }
      if (bytes_write == -1)
        break;
    }
  }
  close(from_fd);
  close(to_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11a9 <main+0x49>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe66(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11d9 <main+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    1352 <main+0x1f2>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
mov    0x2dd5(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1224 <main+0xc4>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1352 <main+0x1f2>
jmp    1229 <main+0xc9>
mov    -0x14(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     133b <main+0x1db>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1272 <main+0x112>
mov    0x2d66(%rip),%rax
mov    (%rax),%rax
mov    0x2d54(%rip),%rcx
cmp    (%rcx),%rax
je     1272 <main+0x112>
jmp    133b <main+0x1db>
cmpl   $0x0,-0x1c(%rbp)
jle    1331 <main+0x1d1>
lea    -0x90(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x98(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     131d <main+0x1bd>
cmpl   $0xffffffff,-0x20(%rbp)
jne    12d1 <main+0x171>
mov    0x2d07(%rip),%rax
mov    (%rax),%rax
mov    0x2cf5(%rip),%rcx
cmp    (%rcx),%rax
je     12d1 <main+0x171>
jmp    131d <main+0x1bd>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    12e2 <main+0x182>
jmp    131d <main+0x1bd>
cmpl   $0x0,-0x20(%rbp)
jle    130e <main+0x1ae>
mov    -0x20(%rbp),%ecx
mov    -0x98(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x98(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1313 <main+0x1b3>
jmp    1318 <main+0x1b8>
jmp    128a <main+0x12a>
cmpl   $0xffffffff,-0x20(%rbp)
jne    132c <main+0x1cc>
jmp    133b <main+0x1db>
jmp    1331 <main+0x1d1>
jmp    1336 <main+0x1d6>
jmp    1229 <main+0xc9>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    128d <main+0x12d>
mov    0x8(%rbx),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,0xc(%rsp)
cmp    $0xffffffff,%eax
je     1272 <main+0x112>
mov    0x10(%rbx),%rdi
mov    0x2dfa(%rip),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df8(%rip),%rax
mov    0x2e19(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %r13d,%r13d
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1279 <main+0x119>
mov    %eax,%r12d
mov    0x2de0(%rip),%r14
mov    0x2dd1(%rip),%r15
jmp    11f8 <main+0x98>
nopl   0x0(%rax)
mov    (%r14),%rax
cmp    (%r15),%rax
jne    125d <main+0xfd>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11f0 <main+0x90>
mov    %eax,%ebx
test   %eax,%eax
je     125d <main+0xfd>
jle    11f8 <main+0x98>
lea    0x10(%rsp),%rbp
jmp    122b <main+0xcb>
nop
test   %eax,%eax
cmovs  %r13d,%eax
sub    %eax,%ebx
add    %rax,%rbp
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1250 <main+0xf0>
test   %eax,%eax
je     11f8 <main+0x98>
cmp    %eax,%ebx
jne    1220 <main+0xc0>
jmp    11f8 <main+0x98>
nopw   0x0(%rax,%rax,1)
cmp    $0xffffffff,%ebx
je     125d <main+0xfd>
mov    (%r15),%rcx
cmp    %rcx,(%r14)
je     1220 <main+0xc0>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    %r12d,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    127e <main+0x11e>
mov    $0xffffffff,%eax
jmp    127e <main+0x11e>
mov    $0xfffffffe,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd60(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"Usage:%s from file to file\0A\07\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@errno = global i64 0
@EINTR = global i64 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 i32
%9 = alloca i32
%10 = alloca [100 x i8]
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne 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 ([29 x i8], [29 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 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %21
store i32 -1, i32* %3
br label %106
29: ; preds = %21
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = load i32, i32* @O_WRONLY
%34 = load i32, i32* @O_CREAT
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IRUSR
%37 = load i32, i32* @S_IWUSR
%38 = or i32 %36, %37
%39 = call i32 (i8*, i32, ...) @open(i8* %32, i32 %35, i32 %38)
store i32 %39, i32* %7
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %29
store i32 -2, i32* %3
br label %106
42: ; preds = %29
br label %43
43: ; preds = %100, %42
%44 = load i32, i32* %6
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 100)
store i32 %46, i32* %8
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %101
48: ; preds = %43
%49 = load i32, i32* %8
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i64, i64* @errno
%53 = load i64, i64* @EINTR
%54 = icmp ne i64 %52, %53
br i1 %54, label %55, label %56
55: ; preds = %51
br label %101
56: ; preds = %51, %48
%57 = load i32, i32* %8
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %99
59: ; preds = %56
%60 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
store i8* %60, i8** %11
br label %61
61: ; preds = %93, %59
%62 = load i32, i32* %7
%63 = load i8*, i8** %11
%64 = load i32, i32* %8
%65 = call i32 @write(i32 %62, i8* %63, i32 %64)
store i32 %65, i32* %9
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %94
67: ; preds = %61
%68 = load i32, i32* %9
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %75
70: ; preds = %67
%71 = load i64, i64* @errno
%72 = load i64, i64* @EINTR
%73 = icmp ne i64 %71, %72
br i1 %73, label %74, label %75
74: ; preds = %70
br label %94
75: ; preds = %70, %67
%76 = load i32, i32* %9
%77 = load i32, i32* %8
%78 = icmp eq i32 %76, %77
br i1 %78, label %79, label %80
79: ; preds = %75
br label %94
80: ; preds = %75
%81 = load i32, i32* %9
%82 = icmp sgt i32 %81, 0
br i1 %82, label %83, label %91
83: ; preds = %80
%84 = load i32, i32* %9
%85 = load i8*, i8** %11
%86 = sext i32 %84 to i64
%87 = getelementptr inbounds i8, i8* %85, i64 %86
store i8* %87, i8** %11
%88 = load i32, i32* %9
%89 = load i32, i32* %8
%90 = sub nsw i32 %89, %88
store i32 %90, i32* %8
br label %91
91: ; preds = %83, %80
br label %92
92: ; preds = %91
br label %93
93: ; preds = %92
br label %61
94: ; preds = %79, %74, %61
%95 = load i32, i32* %9
%96 = icmp eq i32 %95, -1
br i1 %96, label %97, label %98
97: ; preds = %94
br label %101
98: ; preds = %94
br label %99
99: ; preds = %98, %56
br label %100
100: ; preds = %99
br label %43
101: ; preds = %97, %55, %43
%102 = load i32, i32* %6
%103 = call i32 @close(i32 %102)
%104 = load i32, i32* %7
%105 = call i32 @close(i32 %104)
store i32 0, i32* %3
br label %106
106: ; preds = %101, %41, %28
%107 = load i32, i32* %3
ret i32 %107
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"Usage:%s from file to file\0A\07\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([29 x i8], [29 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 eq i32 %15, -1
br i1 %16, label %58, label %17
17: ; preds = %11
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_CREAT
%22 = or i32 %21, %20
%23 = load i32, i32* @S_IRUSR
%24 = load i32, i32* @S_IWUSR
%25 = or i32 %24, %23
%26 = tail call i32 (i8*, i32, ...) @open(i8* %19, i32 %22, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %58, label %28
28: ; preds = %17, %34
%29 = call i32 @read(i32 %15, i8* %4, i32 100)
switch i32 %29, label %35 [
i32 0, label %55
i32 -1, label %30
]
30: ; preds = %28
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %55
34: ; preds = %37, %47, %30, %35
br label %28
35: ; preds = %28
%36 = icmp sgt i32 %29, 0
br i1 %36, label %37, label %34
37: ; preds = %35, %49
%38 = phi i32 [ %52, %49 ], [ %29, %35 ]
%39 = phi i8* [ %54, %49 ], [ %4, %35 ]
%40 = call i32 @write(i32 %26, i8* %39, i32 %38)
switch i32 %40, label %47 [
i32 -1, label %41
i32 0, label %34
]
41: ; preds = %37
%42 = load i64, i64* @errno
%43 = load i64, i64* @EINTR
%44 = icmp ne i64 %42, %43
%45 = icmp eq i32 %38, -1
%46 = or i1 %45, %44
br i1 %46, label %55, label %49
47: ; preds = %37
%48 = icmp eq i32 %38, %40
br i1 %48, label %34, label %49
49: ; preds = %41, %47
%50 = icmp sgt i32 %40, 0
%51 = select i1 %50, i32 %40, i32 0
%52 = sub nsw i32 %38, %51
%53 = zext i32 %51 to i64
%54 = getelementptr i8, i8* %39, i64 %53
br label %37
55: ; preds = %28, %30, %41
%56 = tail call i32 @close(i32 %15)
%57 = tail call i32 @close(i32 %26)
br label %58
58: ; preds = %17, %11, %55
%59 = phi i32 [ 0, %55 ], [ -1, %11 ], [ -2, %17 ]
ret i32 %59
}