#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 /*<<< orphan*/ O_RDONLY;
int close(int);
int /*<<< orphan*/ exit(int);
int fileno(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stdout;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int infd = -1;
  int outfd = fileno(stdout);
  char buf[512];
  int read_bytes = -1;
  int written_bytes = -1;
  int to_be_written = -1;
  if (argc < 2) {
    exit(1);
  }
  for (int i = 1; i < argc; ++i) {
    infd = open(argv[i], O_RDONLY);
    if (infd == -1) {
      perror("Open input file ends in error");
      exit(1);
    }
    while (read_bytes != 0) {
      read_bytes = read(infd, buf, 512);
      if (read_bytes == -1) {
        perror("Failed read");
        close(infd);
        exit(1);
      }
      for (int to_be_write = read_bytes; to_be_write > 0;) {
        written_bytes =
            write(outfd, buf + (read_bytes - to_be_write), to_be_write);
        if (written_bytes == -1 || written_bytes == 0) {
          perror("Failed write");
          close(infd);
          exit(1);
        }
        to_be_write -= written_bytes;
      }
    }
    if (close(infd) == -1)
      perror("Close Input file ends in error");
    read_bytes = written_bytes = to_be_written = -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,-0x18(%rbp)
movl   $0xffffffff,-0x224(%rbp)
ff ff ff
movl   $0xffffffff,-0x228(%rbp)
ff ff ff
movl   $0xffffffff,-0x22c(%rbp)
ff ff ff
cmpl   $0x2,-0x8(%rbp)
jge    11d3 <main+0x63>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x1,-0x230(%rbp)
mov    -0x230(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    136d <main+0x1fd>
mov    -0x10(%rbp),%rax
movslq -0x230(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    122c <main+0xbc>
lea    0xde3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1231 <main+0xc1>
cmpl   $0x0,-0x224(%rbp)
je     131e <main+0x1ae>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x224(%rbp)
cmpl   $0xffffffff,-0x224(%rbp)
jne    1283 <main+0x113>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x224(%rbp),%eax
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
jle    1319 <main+0x1a9>
mov    -0x18(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x224(%rbp),%eax
sub    -0x234(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x234(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x228(%rbp)
cmpl   $0xffffffff,-0x228(%rbp)
je     12e2 <main+0x172>
cmpl   $0x0,-0x228(%rbp)
jne    1300 <main+0x190>
lea    0xd41(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x228(%rbp),%ecx
mov    -0x234(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x234(%rbp)
jmp    128f <main+0x11f>
jmp    1231 <main+0xc1>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    133b <main+0x1cb>
lea    0xd01(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x22c(%rbp)
ff ff ff
movl   $0xffffffff,-0x228(%rbp)
ff ff ff
movl   $0xffffffff,-0x224(%rbp)
ff ff ff
mov    -0x230(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x230(%rbp)
jmp    11dd <main+0x6d>
xor    %eax,%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,0x8(%rsp)
mov    %edi,%ebx
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
cmp    $0x2,%ebx
jl     12c0 <main+0x150>
mov    %eax,%ebp
mov    %ebx,%eax
mov    %rax,(%rsp)
mov    $0x1,%r14d
lea    0x10(%rsp),%r12
jmp    11df <main+0x6f>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1268 <main+0xf8>
add    $0x1,%r14
cmp    (%rsp),%r14
je     1279 <main+0x109>
mov    0x8(%rsp),%rax
mov    (%rax,%r14,8),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    1215 <main+0xa5>
jmp    12b4 <main+0x144>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %r15d,%r15d
je     11c0 <main+0x50>
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x200,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     12ab <main+0x13b>
mov    %eax,%r15d
mov    %eax,%ebx
test   %eax,%eax
jle    1210 <main+0xa0>
nopw   0x0(%rax,%rax,1)
mov    %r15d,%eax
sub    %ebx,%eax
cltq
lea    (%rsp,%rax,1),%rsi
add    $0x10,%rsi
mov    %ebp,%edi
mov    %ebx,%edx
call   1030 <write@plt>
lea    0x1(%rax),%ecx
cmp    $0x1,%ecx
jbe    128d <main+0x11d>
sub    %eax,%ebx
test   %ebx,%ebx
jg     1240 <main+0xd0>
jmp    1210 <main+0xa0>
lea    0xdc8(%rip),%rdi
call   1080 <perror@plt>
jmp    11d1 <main+0x61>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd96(%rip),%rdi
call   1080 <perror@plt>
mov    %r13d,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd6c(%rip),%rdi
jmp    1294 <main+0x124>
lea    0xd45(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@stdout = global i32 0
@O_RDONLY = global i32 0
@.str = [30 x i8] c"Open input file ends in error\00"
@.str.1 = [12 x i8] c"Failed read\00"
@.str.2 = [13 x i8] c"Failed write\00"
@.str.3 = [31 x i8] c"Close Input file ends in error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = load i32, i32* @stdout
%5 = tail call i32 @fileno(i32 %4)
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%7 = icmp slt i32 %0, 2
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = zext i32 %0 to i64
br label %13
10: ; preds = %2
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %54
ret i32 0
13: ; preds = %8, %54
%14 = phi i64 [ 1, %8 ], [ %55, %54 ]
%15 = getelementptr inbounds i8*, i8** %1, i64 %14
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 @open(i8* %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %34, %28
%24 = icmp eq i32 %26, 0
br i1 %24, label %49, label %25
25: ; preds = %13, %23
%26 = call i32 @read(i32 %18, i8* %6, i32 512)
%27 = icmp eq i32 %26, -1
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = icmp sgt i32 %26, 0
br i1 %29, label %36, label %23
30: ; preds = %25
%31 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%32 = tail call i32 @close(i32 %18)
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %36
%35 = icmp sgt i32 %44, 0
br i1 %35, label %36, label %23
36: ; preds = %28, %34
%37 = phi i32 [ %44, %34 ], [ %26, %28 ]
%38 = sub nsw i32 %26, %37
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %39
%41 = call i32 @write(i32 %5, i8* %40, i32 %37)
%42 = add i32 %41, 1
%43 = icmp ult i32 %42, 2
%44 = sub nsw i32 %37, %41
br i1 %43, label %45, label %34
45: ; preds = %36
%46 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%47 = tail call i32 @close(i32 %18)
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %23
%50 = tail call i32 @close(i32 %18)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %54
54: ; preds = %52, %49
%55 = add nuw nsw i64 %14, 1
%56 = icmp eq i64 %55, %9
br i1 %56, label %12, label %13
}