#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 ssize_t;
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_APPEND;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ STDIN_FILENO;
int STDOUT_FILENO;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int output_filename_index;
  if (!(argc == 2 || argc == 3)) {
    fprintf(stderr, "Usage 1: tee out.txt\n");
    fprintf(stderr, "Usage 2: tee -a out.txt\n");
    exit(EXIT_FAILURE);
  }
  if (argc == 2) {
    output_filename_index = 1;
  } else {
    if (strcmp(argv[1], "-a") != 0) {
      fprintf(stderr, "Option doesn't exist: %s\n", argv[1]);
      exit(EXIT_FAILURE);
    }
    output_filename_index = 2;
  }
  int outputFd;
  char buf[1024];
  ssize_t numRead;
  int openFlags = O_CREAT | O_WRONLY;
  if (strcmp(argv[1], "-a") == 0) {
    openFlags |= O_APPEND;
  } else {
    openFlags |= O_TRUNC;
  }
  mode_t filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
  outputFd = open(argv[output_filename_index], openFlags, filePerms);
  if (outputFd == -1) {
    fprintf(stderr, "Open failed for %s\n", argv[1]);
    perror("open");
    exit(EXIT_FAILURE);
  }
  while ((numRead = read(STDIN_FILENO, buf, 1024)) > 0) {
    if (write(outputFd, buf, numRead) != numRead) {
      fprintf(stderr, "Could not write the entire buffer.\n");
      perror("write to output file");
      exit(EXIT_FAILURE);
    }
    if (write(STDOUT_FILENO, buf, numRead) != numRead) {
      fprintf(stderr, "Could not write the entire buffer.\n");
      perror("write to standard output");
      exit(EXIT_FAILURE);
    }
  }
  if (numRead == -1) {
    fprintf(stderr, "Could not read from standard input.");
    perror("read from standard input");
  }
  if (close(outputFd) == -1) {
    fprintf(stderr, "Close failed for %s\n", argv[1]);
    perror("close");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e9 <main+0x69>
cmpl   $0x3,-0x8(%rbp)
je     11e9 <main+0x69>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jne    11ff <main+0x7f>
movl   $0x1,-0x14(%rbp)
jmp    1251 <main+0xd1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe21(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     124a <main+0xca>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x2,-0x14(%rbp)
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    0x2d17(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x428(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdb7(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12a1 <main+0x121>
mov    0x2ce2(%rip),%rax
mov    (%rax),%eax
or     -0x428(%rbp),%eax
mov    %eax,-0x428(%rbp)
jmp    12b6 <main+0x136>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
or     -0x428(%rbp),%eax
mov    %eax,-0x428(%rbp)
mov    0x2cdb(%rip),%rax
mov    (%rax),%eax
mov    0x2d02(%rip),%rcx
or     (%rcx),%eax
mov    0x2ce1(%rip),%rcx
or     (%rcx),%eax
mov    0x2cf8(%rip),%rcx
or     (%rcx),%eax
mov    0x2cc7(%rip),%rcx
or     (%rcx),%eax
mov    0x2c7e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x42c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x428(%rbp),%esi
mov    -0x42c(%rbp),%edx
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1355 <main+0x1d5>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd18(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xd1e(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c52(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    135a <main+0x1da>
mov    0x2c27(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jle    1432 <main+0x2b2>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1030 <write@plt>
cmp    -0x424(%rbp),%eax
je     13d5 <main+0x255>
mov    0x2c0d(%rip),%rax
mov    (%rax),%edi
lea    0xcb1(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xcc7(%rip),%rdi
call   1090 <perror@plt>
mov    0x2bd2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2c04(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1030 <write@plt>
cmp    -0x424(%rbp),%eax
je     142d <main+0x2ad>
mov    0x2bb5(%rip),%rax
mov    (%rax),%edi
lea    0xc59(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xc84(%rip),%rdi
call   1090 <perror@plt>
mov    0x2b7a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    135a <main+0x1da>
cmpl   $0xffffffff,-0x424(%rbp)
jne    1462 <main+0x2e2>
mov    0x2b72(%rip),%rax
mov    (%rax),%edi
lea    0xc68(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xc7e(%rip),%rdi
call   1090 <perror@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    14ac <main+0x32c>
mov    0x2b3e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xc69(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xc70(%rip),%rdi
call   1090 <perror@plt>
mov    0x2afb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2add(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    11f6 <main+0x76>
mov    %rsi,%r14
mov    $0x1,%ebx
cmp    $0x2,%edi
je     11c6 <main+0x46>
mov    0x8(%r14),%rdi
lea    0xe7c(%rip),%rsi
call   1060 <strcmp@plt>
mov    $0x2,%ebx
test   %rax,%rax
jne    1311 <main+0x191>
mov    0x2e2b(%rip),%rax
mov    0x2da4(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x8(%r14),%rdi
lea    0xe4c(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1222 <main+0xa2>
mov    0x2d7c(%rip),%rax
jmp    1229 <main+0xa9>
mov    0x2dbb(%rip),%rbx
mov    (%rbx),%edi
lea    0xdfa(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe00(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    13aa <main+0x22a>
mov    0x2dc7(%rip),%rax
or     (%rax),%ebp
mov    0x2d66(%rip),%rax
mov    0x2d8f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d6c(%rip),%rax
or     (%rax),%edx
mov    0x2d83(%rip),%rax
or     (%rax),%edx
mov    0x2d52(%rip),%rax
or     (%rax),%edx
mov    0x2d09(%rip),%rax
or     (%rax),%edx
mov    (%r14,%rbx,8),%rdi
mov    %ebp,%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    1298 <main+0x118>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xdc7(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xdcd(%rip),%rdi
jmp    13a5 <main+0x225>
mov    %eax,%r15d
mov    0x2ce6(%rip),%r12
mov    %rsp,%rbx
mov    0x2d34(%rip),%r13
nopl   0x0(%rax)
mov    (%r12),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    132e <main+0x1ae>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    1387 <main+0x207>
mov    0x0(%r13),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     12b0 <main+0x130>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xd92(%rip),%rdi
jmp    13a5 <main+0x225>
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xd0d(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    13aa <main+0x22a>
cmp    $0xffffffff,%ebp
jne    1356 <main+0x1d6>
mov    0x2c7e(%rip),%rax
mov    (%rax),%edi
lea    0xd74(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xd8a(%rip),%rdi
call   1090 <perror@plt>
mov    %r15d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    13b8 <main+0x238>
mov    0x2c4e(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xd7d(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xd84(%rip),%rdi
jmp    13a5 <main+0x225>
mov    0x2c2a(%rip),%rax
mov    (%rax),%edi
lea    0xcce(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xce4(%rip),%rdi
call   1090 <perror@plt>
mov    0x2bef(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2bd1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Usage 1: tee out.txt\0A\00"
@.str.1 = [25 x i8] c"Usage 2: tee -a out.txt\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"-a\00"
@.str.3 = [26 x i8] c"Option doesn't exist: %s\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.4 = [20 x i8] c"Open failed for %s\0A\00"
@.str.5 = [5 x i8] c"open\00"
@STDIN_FILENO = global i32 0
@.str.6 = [36 x i8] c"Could not write the entire buffer.\0A\00"
@.str.7 = [21 x i8] c"write to output file\00"
@STDOUT_FILENO = global i32 0
@.str.8 = [25 x i8] c"write to standard output\00"
@.str.9 = [36 x i8] c"Could not read from standard input.\00"
@.str.10 = [25 x i8] c"read from standard input\00"
@.str.11 = [21 x i8] c"Close failed for %s\0A\00"
@.str.12 = [6 x i8] c"close\00"
@EXIT_SUCCESS = global i32 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 [1024 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 2
br i1 %13, label %24, label %14
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 3
br i1 %16, label %24, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %14, %2
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 2
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 1, i32* %6
br label %43
28: ; preds = %24
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i64 @strcmp(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %28
%35 = load i32, i32* @stderr
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %28
store i32 2, i32* %6
br label %43
43: ; preds = %42, %27
%44 = load i32, i32* @O_CREAT
%45 = load i32, i32* @O_WRONLY
%46 = or i32 %44, %45
store i32 %46, i32* %10
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i64 @strcmp(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %43
%53 = load i32, i32* @O_APPEND
%54 = load i32, i32* %10
%55 = or i32 %54, %53
store i32 %55, i32* %10
br label %60
56: ; preds = %43
%57 = load i32, i32* @O_TRUNC
%58 = load i32, i32* %10
%59 = or i32 %58, %57
store i32 %59, i32* %10
br label %60
60: ; preds = %56, %52
%61 = load i32, i32* @S_IRUSR
%62 = load i32, i32* @S_IWUSR
%63 = or i32 %61, %62
%64 = load i32, i32* @S_IRGRP
%65 = or i32 %63, %64
%66 = load i32, i32* @S_IWGRP
%67 = or i32 %65, %66
%68 = load i32, i32* @S_IROTH
%69 = or i32 %67, %68
%70 = load i32, i32* @S_IWOTH
%71 = or i32 %69, %70
store i32 %71, i32* %11
%72 = load i8**, i8*** %5
%73 = load i32, i32* %6
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8*, i8** %72, i64 %74
%76 = load i8*, i8** %75
%77 = load i32, i32* %10
%78 = load i32, i32* %11
%79 = call i32 @open(i8* %76, i32 %77, i32 %78)
store i32 %79, i32* %7
%80 = load i32, i32* %7
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %91
82: ; preds = %60
%83 = load i32, i32* @stderr
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 1
%86 = load i8*, i8** %85
%87 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %86)
%88 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%89 = load i32, i32* @EXIT_FAILURE
%90 = call i32 @exit(i32 %89)
unreachable
91: ; preds = %60
br label %92
92: ; preds = %123, %91
%93 = load i32, i32* @STDIN_FILENO
%94 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%95 = call i32 @read(i32 %93, i8* %94, i32 1024)
store i32 %95, i32* %9
%96 = icmp sgt i32 %95, 0
br i1 %96, label %97, label %124
97: ; preds = %92
%98 = load i32, i32* %7
%99 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%100 = load i32, i32* %9
%101 = call i32 @write(i32 %98, i8* %99, i32 %100)
%102 = load i32, i32* %9
%103 = icmp ne i32 %101, %102
br i1 %103, label %104, label %110
104: ; preds = %97
%105 = load i32, i32* @stderr
%106 = call i32 (i32, i8*, ...) @fprintf(i32 %105, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%107 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%108 = load i32, i32* @EXIT_FAILURE
%109 = call i32 @exit(i32 %108)
unreachable
110: ; preds = %97
%111 = load i32, i32* @STDOUT_FILENO
%112 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%113 = load i32, i32* %9
%114 = call i32 @write(i32 %111, i8* %112, i32 %113)
%115 = load i32, i32* %9
%116 = icmp ne i32 %114, %115
br i1 %116, label %117, label %123
117: ; preds = %110
%118 = load i32, i32* @stderr
%119 = call i32 (i32, i8*, ...) @fprintf(i32 %118, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%120 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
%121 = load i32, i32* @EXIT_FAILURE
%122 = call i32 @exit(i32 %121)
unreachable
123: ; preds = %110
br label %92
124: ; preds = %92
%125 = load i32, i32* %9
%126 = icmp eq i32 %125, -1
br i1 %126, label %127, label %131
127: ; preds = %124
%128 = load i32, i32* @stderr
%129 = call i32 (i32, i8*, ...) @fprintf(i32 %128, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.9, i64 0, i64 0))
%130 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0))
br label %131
131: ; preds = %127, %124
%132 = load i32, i32* %7
%133 = call i32 @close(i32 %132)
%134 = icmp eq i32 %133, -1
br i1 %134, label %135, label %144
135: ; preds = %131
%136 = load i32, i32* @stderr
%137 = load i8**, i8*** %5
%138 = getelementptr inbounds i8*, i8** %137, i64 1
%139 = load i8*, i8** %138
%140 = call i32 (i32, i8*, ...) @fprintf(i32 %136, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.11, i64 0, i64 0), i8* %139)
%141 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0))
%142 = load i32, i32* @EXIT_FAILURE
%143 = call i32 @exit(i32 %142)
unreachable
144: ; preds = %131
%145 = load i32, i32* @EXIT_SUCCESS
%146 = call i32 @exit(i32 %145)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Usage 1: tee out.txt\0A\00"
@.str.1 = [25 x i8] c"Usage 2: tee -a out.txt\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"-a\00"
@.str.3 = [26 x i8] c"Option doesn't exist: %s\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.4 = [20 x i8] c"Open failed for %s\0A\00"
@.str.5 = [5 x i8] c"open\00"
@STDIN_FILENO = global i32 0
@.str.6 = [36 x i8] c"Could not write the entire buffer.\0A\00"
@.str.7 = [21 x i8] c"write to output file\00"
@STDOUT_FILENO = global i32 0
@.str.8 = [25 x i8] c"write to standard output\00"
@.str.9 = [36 x i8] c"Could not read from standard input.\00"
@.str.10 = [25 x i8] c"read from standard input\00"
@.str.11 = [21 x i8] c"Close failed for %s\0A\00"
@.str.12 = [6 x i8] c"close\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = and i32 %0, -2
%5 = icmp eq i32 %4, 2
br i1 %5, label %13, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = icmp eq i32 %0, 2
br i1 %14, label %26, label %15
15: ; preds = %13
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %16
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %15, %13
%27 = phi i64 [ 1, %13 ], [ 2, %15 ]
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%29 = load i32, i32* @O_CREAT
%30 = load i32, i32* @O_WRONLY
%31 = or i32 %30, %29
%32 = getelementptr inbounds i8*, i8** %1, i64 1
%33 = load i8*, i8** %32
%34 = tail call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
%36 = load i32, i32* @O_APPEND
%37 = load i32, i32* @O_TRUNC
%38 = select i1 %35, i32 %36, i32 %37
%39 = or i32 %31, %38
%40 = load i32, i32* @S_IRUSR
%41 = load i32, i32* @S_IWUSR
%42 = or i32 %41, %40
%43 = load i32, i32* @S_IRGRP
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IWGRP
%46 = or i32 %44, %45
%47 = load i32, i32* @S_IROTH
%48 = or i32 %46, %47
%49 = load i32, i32* @S_IWOTH
%50 = or i32 %48, %49
%51 = getelementptr inbounds i8*, i8** %1, i64 %27
%52 = load i8*, i8** %51
%53 = tail call i32 @open(i8* %52, i32 %39, i32 %50)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %62
55: ; preds = %26
%56 = load i32, i32* @stderr
%57 = load i8*, i8** %32
%58 = tail call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %57)
%59 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
%61 = tail call i32 @exit(i32 %60)
unreachable
62: ; preds = %26, %75
%63 = load i32, i32* @STDIN_FILENO
%64 = call i32 @read(i32 %63, i8* %28, i32 1024)
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %85
66: ; preds = %62
%67 = call i32 @write(i32 %53, i8* %28, i32 %64)
%68 = icmp eq i32 %67, %64
br i1 %68, label %75, label %69
69: ; preds = %66
%70 = load i32, i32* @stderr
%71 = tail call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%72 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = tail call i32 @exit(i32 %73)
unreachable
75: ; preds = %66
%76 = load i32, i32* @STDOUT_FILENO
%77 = call i32 @write(i32 %76, i8* %28, i32 %64)
%78 = icmp eq i32 %77, %64
br i1 %78, label %62, label %79
79: ; preds = %75
%80 = load i32, i32* @stderr
%81 = tail call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%82 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
%83 = load i32, i32* @EXIT_FAILURE
%84 = tail call i32 @exit(i32 %83)
unreachable
85: ; preds = %62
%86 = icmp eq i32 %64, -1
br i1 %86, label %87, label %91
87: ; preds = %85
%88 = load i32, i32* @stderr
%89 = tail call i32 (i32, i8*, ...) @fprintf(i32 %88, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.9, i64 0, i64 0))
%90 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0))
br label %91
91: ; preds = %87, %85
%92 = tail call i32 @close(i32 %53)
%93 = icmp eq i32 %92, -1
br i1 %93, label %94, label %101
94: ; preds = %91
%95 = load i32, i32* @stderr
%96 = load i8*, i8** %32
%97 = tail call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.11, i64 0, i64 0), i8* %96)
%98 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0))
%99 = load i32, i32* @EXIT_FAILURE
%100 = tail call i32 @exit(i32 %99)
unreachable
101: ; preds = %91
%102 = load i32, i32* @EXIT_SUCCESS
%103 = tail call i32 @exit(i32 %102)
unreachable
}