#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *, ...);
int /*<<< orphan*/ lseek(int, long, int);
int open(char *, int /*<<< orphan*/, int);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd1, fd2, n;
  char buf[128];
  if (argc < 2) {
    while ((n = read(0, buf, 128)) > 0) {
      if (n != write(1, buf, n)) {
        error("writing to stdout");
      }
    }
  } else {
    if ((fd1 = open(*++argv, O_RDWR, 0644)) == -1) {
      error("open file %s", *argv);
    }
    lseek(fd1, 0L, 2);
    while (*++argv != NULL) {
      if ((fd2 = open(*argv, O_RDONLY, 0)) == -1) {
        error("open file %s", *argv);
      }
      while ((n = read(fd2, buf, 128)) > 0) {
        if (n != write(fd1, buf, n)) {
          error("write file %s", *argv);
        }
      }
      close(fd2);
    }
    lseek(fd1, 0L, 0);
    while ((n = read(fd1, buf, 128)) > 0) {
      if (n != write(1, buf, n)) {
        error("writing to stdout");
      }
    }
    close(fd1);
  }
  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   $0x2,-0x8(%rbp)
jge    11ec <main+0x8c>
jmp    1188 <main+0x28>
lea    -0xa0(%rbp),%rsi
xor    %edi,%edi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    11e7 <main+0x87>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xa4(%rbp)
lea    -0xa0(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,%ecx
mov    -0xa4(%rbp),%eax
cmp    %ecx,%eax
je     11e2 <main+0x82>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1070 <error@plt>
jmp    1188 <main+0x28>
jmp    1383 <main+0x223>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1233 <main+0xd3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x2,%edx
call   1040 <lseek@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
cmpq   $0x0,0x8(%rax)
je     130d <main+0x1ad>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1296 <main+0x136>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1070 <error@plt>
jmp    129b <main+0x13b>
mov    -0x18(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1300 <main+0x1a0>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xa8(%rbp)
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    -0xa8(%rbp),%eax
cmp    %ecx,%eax
je     12fb <main+0x19b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1070 <error@plt>
jmp    129b <main+0x13b>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
jmp    1244 <main+0xe4>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    137b <main+0x21b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xac(%rbp)
lea    -0xa0(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,%ecx
mov    -0xac(%rbp),%eax
cmp    %ecx,%eax
je     1376 <main+0x216>
lea    0xc91(%rip),%rdi
mov    $0x0,%al
call   1070 <error@plt>
jmp    131b <main+0x1bb>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
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    $0x88,%rsp
cmp    $0x1,%edi
jg     11d8 <main+0x78>
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x80,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    134d <main+0x1ed>
mov    %eax,%ebp
mov    %rsp,%rbx
lea    0xe67(%rip),%r14
jmp    11b9 <main+0x59>
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    134d <main+0x1ed>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %eax,%ebp
je     11a0 <main+0x40>
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <error@plt>
jmp    11a0 <main+0x40>
mov    %rsi,%r13
mov    0x8(%rsi),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    120c <main+0xac>
mov    0x8(%r13),%rsi
lea    0xe0d(%rip),%rdi
xor    %eax,%eax
call   1070 <error@plt>
mov    %r15d,%edi
xor    %esi,%esi
mov    $0x2,%edx
call   1040 <lseek@plt>
mov    0x10(%r13),%rdi
test   %rdi,%rdi
je     12d9 <main+0x179>
add    $0x10,%r13
mov    %rsp,%rbp
lea    0xde9(%rip),%r12
jmp    1258 <main+0xf8>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x8(%r13),%rdi
add    $0x8,%r13
test   %rdi,%rdi
je     12d9 <main+0x179>
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1281 <main+0x121>
mov    0x0(%r13),%rsi
lea    0xd98(%rip),%rdi
xor    %eax,%eax
call   1070 <error@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1240 <main+0xe0>
mov    %eax,%r14d
jmp    12b6 <main+0x156>
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,%r14d
test   %eax,%eax
jle    1240 <main+0xe0>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
cmp    %eax,%r14d
je     12a0 <main+0x140>
mov    0x0(%r13),%rsi
mov    %r12,%rdi
xor    %eax,%eax
call   1070 <error@plt>
jmp    12a0 <main+0x140>
mov    %r15d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <lseek@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x80,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1345 <main+0x1e5>
mov    %eax,%ebp
mov    %rsp,%rbx
lea    0xcfb(%rip),%r14
jmp    1326 <main+0x1c6>
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1345 <main+0x1e5>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %eax,%ebp
je     1310 <main+0x1b0>
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <error@plt>
jmp    1310 <main+0x1b0>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [18 x i8] c"writing to stdout\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"open file %s\00"
@O_RDONLY = global i32 0
@.str.2 = [14 x i8] c"write file %s\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 slt i32 %0, 2
br i1 %5, label %6, label %18
6: ; preds = %2
%7 = call i32 @read(i32 0, i8* %4, i32 128)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %74
9: ; preds = %6, %15
%10 = phi i32 [ %16, %15 ], [ %7, %6 ]
%11 = call i32 @write(i32 1, i8* %4, i32 %10)
%12 = icmp eq i32 %10, %11
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = call i32 @read(i32 0, i8* %4, i32 128)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %9, label %74
18: ; preds = %2
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = tail call i32 @open(i8* %20, i32 %21, i32 420)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i8*, i8** %19
%26 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %24, %18
%28 = tail call i32 @lseek(i32 %22, i64 0, i32 2)
%29 = getelementptr inbounds i8*, i8** %1, i64 2
%30 = load i8*, i8** %29
%31 = icmp eq i8* %30, null
br i1 %31, label %59, label %32
32: ; preds = %27, %54
%33 = phi i8* [ %57, %54 ], [ %30, %27 ]
%34 = phi i8** [ %56, %54 ], [ %29, %27 ]
%35 = load i32, i32* @O_RDONLY
%36 = tail call i32 @open(i8* %33, i32 %35, i32 0)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i8*, i8** %34
%40 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %38, %32
%42 = call i32 @read(i32 %36, i8* %4, i32 128)
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %54
44: ; preds = %41, %51
%45 = phi i32 [ %52, %51 ], [ %42, %41 ]
%46 = call i32 @write(i32 %22, i8* %4, i32 %45)
%47 = icmp eq i32 %45, %46
br i1 %47, label %51, label %48
48: ; preds = %44
%49 = load i8*, i8** %34
%50 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %49)
br label %51
51: ; preds = %48, %44
%52 = call i32 @read(i32 %36, i8* %4, i32 128)
%53 = icmp sgt i32 %52, 0
br i1 %53, label %44, label %54
54: ; preds = %51, %41
%55 = tail call i32 @close(i32 %36)
%56 = getelementptr inbounds i8*, i8** %34, i64 1
%57 = load i8*, i8** %56
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %32
59: ; preds = %54, %27
%60 = tail call i32 @lseek(i32 %22, i64 0, i32 0)
%61 = call i32 @read(i32 %22, i8* %4, i32 128)
%62 = icmp sgt i32 %61, 0
br i1 %62, label %63, label %72
63: ; preds = %59, %69
%64 = phi i32 [ %70, %69 ], [ %61, %59 ]
%65 = call i32 @write(i32 1, i8* %4, i32 %64)
%66 = icmp eq i32 %64, %65
br i1 %66, label %69, label %67
67: ; preds = %63
%68 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %69
69: ; preds = %67, %63
%70 = call i32 @read(i32 %22, i8* %4, i32 128)
%71 = icmp sgt i32 %70, 0
br i1 %71, label %63, label %72
72: ; preds = %69, %59
%73 = tail call i32 @close(i32 %22)
br label %74
74: ; preds = %15, %6, %72
ret i32 0
}