#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 */
typedef int /*<<< orphan*/ Message;
typedef int /*<<< orphan*/ Buff;

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int open(char *, int, ...);
int read(int, char *, int);
int snprintf(char *, int, char *, ...);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int ArgCount, char *ArgVals[]) {
  if (ArgCount < 2) {
    char Message[1024];
    int Length = snprintf(Message, (sizeof(Message) / sizeof(*(Message))),
                          "Usage: %s FileToCat [DestFile]\n", ArgVals[0]);
    write(STDOUT_FILENO, Message, Length);
    return 1;
  }
  int FileDesc = open(ArgVals[1], O_RDONLY);
  if (FileDesc < 0) {
    return 1;
  }
  if (ArgCount >= 3) {
    int OutputDesc = open(ArgVals[2], O_WRONLY | O_CREAT | O_EXCL, 0644);
    if (OutputDesc >= 0) {
      dup2(OutputDesc, STDOUT_FILENO);
      close(OutputDesc);
    } else {
      char Message[1024];
      int Length =
          snprintf(Message, (sizeof(Message) / sizeof(*(Message))),
                   "Could not open output file \"%s\"!\n\n", ArgVals[2]);
      write(STDOUT_FILENO, Message, Length);
    }
  }
  int FileLength = 0;
  char Buff[64];
  for (int ByteCount;
       (ByteCount = read(FileDesc, Buff, (sizeof(Buff) / sizeof(*(Buff)))));) {
    write(STDOUT_FILENO, Buff, ByteCount);
    FileLength += ByteCount;
  }
  close(FileDesc);
  {
    char Message[1024];
    int Length = snprintf(Message, (sizeof(Message) / sizeof(*(Message))),
                          "File size: %d bytes\n", FileLength);
    write(STDOUT_FILENO, Message, Length);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d1 <main+0x71>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    $0x400,%esi
lea    0xe63(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0x414(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
movl   $0x1,-0x4(%rbp)
jmp    1375 <main+0x215>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x418(%rbp)
jge    1208 <main+0xa8>
movl   $0x1,-0x4(%rbp)
jmp    1375 <main+0x215>
cmpl   $0x3,-0x8(%rbp)
jl     12c0 <main+0x160>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
mov    0x2dce(%rip),%rax
or     (%rax),%esi
mov    0x2d95(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0x0,-0x41c(%rbp)
jl     1278 <main+0x118>
mov    -0x41c(%rbp),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x41c(%rbp),%edi
call   1060 <close@plt>
jmp    12bb <main+0x15b>
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
mov    $0x400,%esi
lea    0xd8d(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0x824(%rbp)
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    -0x820(%rbp),%rsi
mov    -0x824(%rbp),%edx
call   1030 <write@plt>
jmp    12c0 <main+0x160>
movl   $0x0,-0x828(%rbp)
mov    -0x418(%rbp),%edi
lea    -0x870(%rbp),%rsi
mov    $0x40,%edx
call   1070 <read@plt>
mov    %eax,-0x874(%rbp)
cmp    $0x0,%eax
je     1322 <main+0x1c2>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
lea    -0x870(%rbp),%rsi
mov    -0x874(%rbp),%edx
call   1030 <write@plt>
mov    -0x874(%rbp),%eax
add    -0x828(%rbp),%eax
mov    %eax,-0x828(%rbp)
jmp    12ca <main+0x16a>
mov    -0x418(%rbp),%edi
call   1060 <close@plt>
lea    -0xc80(%rbp),%rdi
mov    -0x828(%rbp),%ecx
mov    $0x400,%esi
lea    0xcfd(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0xc84(%rbp)
mov    0x2c8e(%rip),%rax
mov    (%rax),%edi
lea    -0xc80(%rbp),%rsi
mov    -0xc84(%rbp),%edx
call   1030 <write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
mov    %rsi,%rbp
cmp    $0x1,%edi
jg     11b6 <main+0x56>
mov    0x0(%rbp),%rcx
lea    0xe7c(%rip),%rdx
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    0x2e49(%rip),%rcx
mov    (%rcx),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    $0x1,%r14d
jmp    12ee <main+0x18e>
mov    %edi,%ebx
mov    0x8(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     1226 <main+0xc6>
mov    %eax,%r15d
cmp    $0x3,%ebx
jb     1263 <main+0x103>
mov    0x2dd5(%rip),%rax
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbp),%rdi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     1231 <main+0xd1>
mov    %eax,%ebx
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1263 <main+0x103>
mov    $0x1,%r14d
jmp    12ee <main+0x18e>
mov    0x10(%rbp),%rcx
lea    0xde4(%rip),%rdx
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    0x2d91(%rip),%rcx
mov    (%rcx),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x40,%edx
call   1070 <read@plt>
xor    %r14d,%r14d
mov    $0x0,%ebp
test   %eax,%eax
je     12b6 <main+0x156>
mov    %eax,%ebx
xor    %ebp,%ebp
mov    0x2d5e(%rip),%r13
mov    %rsp,%r12
nopl   (%rax)
mov    0x0(%r13),%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
add    %ebx,%ebp
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x40,%edx
call   1070 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1290 <main+0x130>
mov    %r15d,%edi
call   1060 <close@plt>
lea    0xd7e(%rip),%rdx
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    0x2d06(%rip),%rcx
mov    (%rcx),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14d,%eax
add    $0x448,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"Usage: %s FileToCat [DestFile]\0A\00"
@STDOUT_FILENO = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [35 x i8] c"Could not open output file \22%s\22!\0A\0A\00"
@.str.2 = [21 x i8] c"File size: %d bytes\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca [64 x i8]
%14 = alloca i32
%15 = alloca [1024 x i8]
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 2
br i1 %18, label %19, label %29
19: ; preds = %2
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %20, i32 1024, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 %24, i32* %7
%25 = load i32, i32* @STDOUT_FILENO
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%27 = load i32, i32* %7
%28 = call i32 @write(i32 %25, i8* %26, i32 %27)
store i32 1, i32* %3
br label %94
29: ; preds = %2
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i32, i32* @O_RDONLY
%34 = call i32 (i8*, i32, ...) @open(i8* %32, i32 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %29
store i32 1, i32* %3
br label %94
38: ; preds = %29
%39 = load i32, i32* %4
%40 = icmp sge i32 %39, 3
br i1 %40, label %41, label %70
41: ; preds = %38
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = load i32, i32* @O_WRONLY
%46 = load i32, i32* @O_CREAT
%47 = or i32 %45, %46
%48 = load i32, i32* @O_EXCL
%49 = or i32 %47, %48
%50 = call i32 (i8*, i32, ...) @open(i8* %44, i32 %49, i32 420)
store i32 %50, i32* %9
%51 = load i32, i32* %9
%52 = icmp sge i32 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %41
%54 = load i32, i32* %9
%55 = load i32, i32* @STDOUT_FILENO
%56 = call i32 @dup2(i32 %54, i32 %55)
%57 = load i32, i32* %9
%58 = call i32 @close(i32 %57)
br label %69
59: ; preds = %41
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 2
%63 = load i8*, i8** %62
%64 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %60, i32 1024, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %63)
store i32 %64, i32* %11
%65 = load i32, i32* @STDOUT_FILENO
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%67 = load i32, i32* %11
%68 = call i32 @write(i32 %65, i8* %66, i32 %67)
br label %69
69: ; preds = %59, %53
br label %70
70: ; preds = %69, %38
store i32 0, i32* %12
br label %71
71: ; preds = %76, %70
%72 = load i32, i32* %8
%73 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%74 = call i32 @read(i32 %72, i8* %73, i32 64)
store i32 %74, i32* %14
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %84
76: ; preds = %71
%77 = load i32, i32* @STDOUT_FILENO
%78 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%79 = load i32, i32* %14
%80 = call i32 @write(i32 %77, i8* %78, i32 %79)
%81 = load i32, i32* %14
%82 = load i32, i32* %12
%83 = add nsw i32 %82, %81
store i32 %83, i32* %12
br label %71
84: ; preds = %71
%85 = load i32, i32* %8
%86 = call i32 @close(i32 %85)
%87 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%88 = load i32, i32* %12
%89 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %87, i32 1024, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %88)
store i32 %89, i32* %16
%90 = load i32, i32* @STDOUT_FILENO
%91 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%92 = load i32, i32* %16
%93 = call i32 @write(i32 %90, i8* %91, i32 %92)
store i32 0, i32* %3
br label %94
94: ; preds = %84, %37, %19
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
@.str = [32 x i8] c"Usage: %s FileToCat [DestFile]\0A\00"
@STDOUT_FILENO = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [35 x i8] c"Could not open output file \22%s\22!\0A\0A\00"
@.str.2 = [21 x i8] c"File size: %d bytes\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca [64 x i8]
%6 = alloca [1024 x i8]
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%10 = load i8*, i8** %1
%11 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %9, i32 1024, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @STDOUT_FILENO
%13 = call i32 @write(i32 %12, i8* %9, i32 %11)
br label %61
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 (i8*, i32, ...) @open(i8* %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %61, label %20
20: ; preds = %14
%21 = icmp ugt i32 %0, 2
br i1 %21, label %22, label %42
22: ; preds = %20
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_WRONLY
%26 = load i32, i32* @O_CREAT
%27 = or i32 %26, %25
%28 = load i32, i32* @O_EXCL
%29 = or i32 %27, %28
%30 = tail call i32 (i8*, i32, ...) @open(i8* %24, i32 %29, i32 420)
%31 = icmp sgt i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %22
%33 = load i32, i32* @STDOUT_FILENO
%34 = tail call i32 @dup2(i32 %30, i32 %33)
%35 = tail call i32 @close(i32 %30)
br label %42
36: ; preds = %22
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%38 = load i8*, i8** %23
%39 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %37, i32 1024, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @STDOUT_FILENO
%41 = call i32 @write(i32 %40, i8* %37, i32 %39)
br label %42
42: ; preds = %32, %36, %20
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%44 = call i32 @read(i32 %18, i8* %43, i32 64)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %53, %42
%47 = phi i32 [ 0, %42 ], [ %58, %53 ]
%48 = call i32 @close(i32 %18)
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%50 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %49, i32 1024, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%51 = load i32, i32* @STDOUT_FILENO
%52 = call i32 @write(i32 %51, i8* %49, i32 %50)
br label %61
53: ; preds = %42, %53
%54 = phi i32 [ %59, %53 ], [ %44, %42 ]
%55 = phi i32 [ %58, %53 ], [ 0, %42 ]
%56 = load i32, i32* @STDOUT_FILENO
%57 = call i32 @write(i32 %56, i8* %43, i32 %54)
%58 = add nsw i32 %54, %55
%59 = call i32 @read(i32 %18, i8* %43, i32 64)
%60 = icmp eq i32 %59, 0
br i1 %60, label %46, label %53
61: ; preds = %46, %14, %8
%62 = phi i32 [ 1, %8 ], [ 0, %46 ], [ 1, %14 ]
ret i32 %62
}