#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  int n;
  char buf[128];
  if (argc == 1) {
    while ((n = read(0, buf, 128)) > 0) {
      if (n != write(1, buf, n)) {
        fprintf(stderr, "error while writing\n");
      }
    }
  } else {
    while (--argc > 0) {
      if ((fd = open(*++argv, O_RDONLY, 0)) == -1) {
        fprintf(stderr, "error opening file %s\n", *argv);
        continue;
      }
      while ((n = read(fd, buf, 128)) > 0) {
        if (n != write(1, buf, n)) {
          fprintf(stderr, "erro while writing\n");
        }
      }
      close(fd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11e5 <main+0x95>
jmp    1178 <main+0x28>
lea    -0xa0(%rbp),%rsi
xor    %edi,%edi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    11e0 <main+0x90>
mov    -0x18(%rbp),%eax
mov    %eax,-0xa4(%rbp)
lea    -0xa0(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,%ecx
mov    -0xa4(%rbp),%eax
cmp    %ecx,%eax
je     11db <main+0x8b>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1178 <main+0x28>
jmp    12ce <main+0x17e>
jmp    11ea <main+0x9a>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    12c9 <main+0x179>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    124e <main+0xfe>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdd3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    11ea <main+0x9a>
jmp    1253 <main+0x103>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    12bc <main+0x16c>
mov    -0x18(%rbp),%eax
mov    %eax,-0xa8(%rbp)
lea    -0xa0(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,%ecx
mov    -0xa8(%rbp),%eax
cmp    %ecx,%eax
je     12b7 <main+0x167>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1253 <main+0x103>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
jmp    11ea <main+0x9a>
jmp    12ce <main+0x17e>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
cmp    $0x1,%edi
jne    11db <main+0x8b>
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    $0x80,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1299 <main+0x149>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
mov    0x2e4b(%rip),%r15
lea    0xe6c(%rip),%r14
jmp    11b9 <main+0x69>
cs nopw 0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1299 <main+0x149>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %eax,%ebp
je     11a0 <main+0x50>
mov    (%r15),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11a0 <main+0x50>
jle    1299 <main+0x149>
mov    %rsi,%r15
mov    %edi,%r12d
mov    0x2dea(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xe32(%rip),%r13
jmp    121a <main+0xca>
nopl   0x0(%rax)
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%r15
lea    -0x1(%r12),%eax
cmp    $0x2,%r12d
mov    %eax,%r12d
jle    1299 <main+0x149>
mov    0x8(%r15),%rdi
add    $0x8,%r15
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1280 <main+0x130>
mov    %eax,%ebp
mov    %r15,0x8(%rsp)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,%r15d
test   %eax,%eax
jle    1200 <main+0xb0>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
cmp    %eax,%r15d
je     1240 <main+0xf0>
mov    (%r14),%edi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1240 <main+0xf0>
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    (%r15),%rdx
lea    0xd88(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    120c <main+0xbc>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"error while writing\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"error opening file %s\0A\00"
@.str.2 = [20 x i8] c"erro while writing\0A\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 [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %27
11: ; preds = %2
br label %12
12: ; preds = %25, %11
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%14 = call i32 @read(i32 0, i8* %13, i32 128)
store i32 %14, i32* %7
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = load i32, i32* %7
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%19 = load i32, i32* %7
%20 = call i32 @write(i32 1, i8* %18, i32 %19)
%21 = icmp ne i32 %17, %20
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %22, %16
br label %12
26: ; preds = %12
br label %64
27: ; preds = %2
br label %28
28: ; preds = %60, %39, %27
%29 = load i32, i32* %4
%30 = add nsw i32 %29, -1
store i32 %30, i32* %4
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %63
32: ; preds = %28
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i32 1
store i8** %34, i8*** %5
%35 = load i8*, i8** %34
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* %35, i32 %36, i32 0)
store i32 %37, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = load i8**, i8*** %5
%42 = load i8*, i8** %41
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %42)
br label %28
44: ; preds = %32
br label %45
45: ; preds = %59, %44
%46 = load i32, i32* %6
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 128)
store i32 %48, i32* %7
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %60
50: ; preds = %45
%51 = load i32, i32* %7
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%53 = load i32, i32* %7
%54 = call i32 @write(i32 1, i8* %52, i32 %53)
%55 = icmp ne i32 %51, %54
br i1 %55, label %56, label %59
56: ; preds = %50
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %59
59: ; preds = %56, %50
br label %45
60: ; preds = %45
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
br label %28
63: ; preds = %28
br label %64
64: ; preds = %63, %26
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"error while writing\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"error opening file %s\0A\00"
@.str.2 = [20 x i8] c"erro while writing\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 1
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = icmp sgt i32 %0, 1
br i1 %7, label %21, label %51
8: ; preds = %2
%9 = call i32 @read(i32 0, i8* %4, i32 128)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %51
11: ; preds = %8, %18
%12 = phi i32 [ %19, %18 ], [ %9, %8 ]
%13 = call i32 @write(i32 1, i8* %4, i32 %12)
%14 = icmp eq i32 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %15, %11
%19 = call i32 @read(i32 0, i8* %4, i32 128)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %11, label %51
21: ; preds = %6, %37
%22 = phi i32 [ %24, %37 ], [ %0, %6 ]
%23 = phi i8** [ %25, %37 ], [ %1, %6 ]
%24 = add nsw i32 %22, -1
%25 = getelementptr inbounds i8*, i8** %23, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDONLY
%28 = tail call i32 @open(i8* %26, i32 %27, i32 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %33, label %30
30: ; preds = %21
%31 = call i32 @read(i32 %28, i8* %4, i32 128)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %39, label %49
33: ; preds = %21
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %25
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %33, %49
%38 = icmp sgt i32 %22, 2
br i1 %38, label %21, label %51
39: ; preds = %30, %46
%40 = phi i32 [ %47, %46 ], [ %31, %30 ]
%41 = call i32 @write(i32 1, i8* %4, i32 %40)
%42 = icmp eq i32 %40, %41
br i1 %42, label %46, label %43
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %43, %39
%47 = call i32 @read(i32 %28, i8* %4, i32 128)
%48 = icmp sgt i32 %47, 0
br i1 %48, label %39, label %49
49: ; preds = %46, %30
%50 = tail call i32 @close(i32 %28)
br label %37
51: ; preds = %37, %18, %6, %8
ret i32 0
}