#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 mode_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int NAME_MAX;
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_IWUSR;
int close(int);
int /*<<< orphan*/ errExit(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int, int);
size_t read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  char filename[NAME_MAX + 1];
  int openFlags = O_WRONLY | O_CREAT;
  mode_t openMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
  if (argc == 2) {
    strncpy(filename, argv[1], NAME_MAX);
    filename[NAME_MAX] = '\0';
    openFlags = openFlags | O_TRUNC;
  } else if (argc == 3) {
    if (!strcmp(argv[1], "-a")) {
      strncpy(filename, argv[2], NAME_MAX);
      filename[NAME_MAX] = '\0';
      openFlags = openFlags | O_APPEND;
    } else
      errExit("Usage: tee [-a] filename");
  } else
    errExit("Usage: tee [-a] filename");
  int fd = open(filename, openFlags, openMode);
  if (fd == -1)
    errExit("Opening File");
  char byte;
  size_t readReturn = read(STDIN_FILENO, &byte, 1);
  while (readReturn > 0) {
    if (write(fd, &byte, 1) != 1)
      errExit("Writing to file");
    if (write(STDOUT_FILENO, &byte, 1) != 1)
      errExit("Writing to stdout");
    readReturn = read(STDIN_FILENO, &byte, 1);
  }
  if (readReturn == -1)
    errExit("Reading File");
  if (close(fd) == -1)
    errExit("Closing File");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    0x2e23(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    0x2dde(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc5(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd4(%rip),%rcx
or     (%rcx),%eax
mov    0x2dab(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1254 <main+0xd4>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    0x2d51(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rax
mov    0x2d3f(%rip),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
mov    0x2da6(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    12dd <main+0x15d>
cmpl   $0x3,-0x8(%rbp)
jne    12cc <main+0x14c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd93(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    12bb <main+0x13b>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    0x2cea(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rax
mov    0x2cd8(%rip),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
mov    0x2ccf(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    12c7 <main+0x147>
lea    0xd41(%rip),%rdi
call   1050 <errExit@plt>
jmp    12d8 <main+0x158>
lea    0xd30(%rip),%rdi
call   1050 <errExit@plt>
jmp    12dd <main+0x15d>
mov    -0x40(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1090 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1305 <main+0x185>
lea    0xd1c(%rip),%rdi
call   1050 <errExit@plt>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
lea    -0x2d(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jbe    139d <main+0x21d>
mov    -0x2c(%rbp),%edi
lea    -0x2d(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     1351 <main+0x1d1>
lea    0xcdd(%rip),%rdi
call   1050 <errExit@plt>
mov    0x2c88(%rip),%rax
mov    (%rax),%edi
lea    -0x2d(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     137d <main+0x1fd>
lea    0xcc1(%rip),%rdi
call   1050 <errExit@plt>
mov    0x2c14(%rip),%rax
mov    (%rax),%edi
lea    -0x2d(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %rax,-0x38(%rbp)
jmp    1320 <main+0x1a0>
cmpq   $0xffffffffffffffff,-0x38(%rbp)
jne    13b4 <main+0x234>
lea    0xc9c(%rip),%rdi
call   1050 <errExit@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    13d1 <main+0x251>
lea    0xc8c(%rip),%rdi
call   1050 <errExit@plt>
mov    0x2bc8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    0x2de0(%rip),%rdx
mov    (%rdx),%r12d
lea    0x1(%r12),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2dd0(%rip),%rax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%r14d
or     (%rax),%r14d
mov    0x2ddc(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2dda(%rip),%rax
or     (%rax),%ebx
mov    0x2de9(%rip),%rax
or     (%rax),%ebx
mov    0x2dc0(%rip),%rax
or     (%rax),%ebx
cmp    $0x3,%edi
je     1223 <main+0xa3>
cmp    $0x2,%edi
jne    1261 <main+0xe1>
mov    0x8(%r13),%rsi
mov    %r15,%rdi
mov    %rdx,%r13
mov    %r12d,%edx
call   1030 <strncpy@plt>
movslq 0x0(%r13),%rax
movb   $0x0,(%r15,%rax,1)
mov    0x2dd2(%rip),%rax
or     (%rax),%r14d
jmp    126d <main+0xed>
mov    0x8(%r13),%rdi
lea    0xdd2(%rip),%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
jne    1261 <main+0xe1>
mov    0x10(%r13),%rsi
mov    %r15,%rdi
mov    %r12d,%edx
call   1030 <strncpy@plt>
mov    0x2d2b(%rip),%rax
movslq (%rax),%rax
movb   $0x0,(%r15,%rax,1)
mov    0x2d24(%rip),%rax
or     (%rax),%r14d
jmp    126d <main+0xed>
lea    0xd9b(%rip),%rdi
call   1050 <errExit@plt>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %ebx,%edx
call   1090 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    128e <main+0x10e>
lea    0xd93(%rip),%rdi
call   1050 <errExit@plt>
mov    0x2d03(%rip),%r13
mov    0x0(%r13),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jne    12d3 <main+0x153>
mov    %r14d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    12c5 <main+0x145>
lea    0xd8b(%rip),%rdi
call   1050 <errExit@plt>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x29(%rbp),%rbx
mov    0x2d02(%rip),%r15
lea    0xd44(%rip),%r12
jmp    1306 <main+0x186>
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
je     12ac <main+0x12c>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     1323 <main+0x1a3>
mov    %r12,%rdi
call   1050 <errExit@plt>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     12f0 <main+0x170>
lea    0xcfa(%rip),%rdi
call   1050 <errExit@plt>
jmp    12f0 <main+0x170>

LLVM IR (-O0):
@NAME_MAX = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = 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
@O_TRUNC = global i32 0
@.str = [3 x i8] c"-a\00"
@O_APPEND = global i32 0
@.str.1 = [25 x i8] c"Usage: tee [-a] filename\00"
@.str.2 = [13 x i8] c"Opening File\00"
@STDIN_FILENO = global i32 0
@.str.3 = [16 x i8] c"Writing to file\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [18 x i8] c"Writing to stdout\00"
@.str.5 = [13 x i8] c"Reading File\00"
@.str.6 = [13 x i8] c"Closing File\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @NAME_MAX
%14 = add nsw i32 %13, 1
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
store i32 %20, i32* %8
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRGRP
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IWGRP
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IROTH
%29 = or i32 %27, %28
store i32 %29, i32* %9
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, 2
br i1 %31, label %32, label %44
32: ; preds = %2
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = load i32, i32* @NAME_MAX
%37 = call i32 @strncpy(i8* %17, i8* %35, i32 %36)
%38 = load i32, i32* @NAME_MAX
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %17, i64 %39
store i8 0, i8* %40
%41 = load i32, i32* %8
%42 = load i32, i32* @O_TRUNC
%43 = or i32 %41, %42
store i32 %43, i32* %8
br label %71
44: ; preds = %2
%45 = load i32, i32* %4
%46 = icmp eq i32 %45, 3
br i1 %46, label %47, label %68
47: ; preds = %44
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = call i32 @strcmp(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%52 = icmp ne i32 %51, 0
br i1 %52, label %65, label %53
53: ; preds = %47
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 2
%56 = load i8*, i8** %55
%57 = load i32, i32* @NAME_MAX
%58 = call i32 @strncpy(i8* %17, i8* %56, i32 %57)
%59 = load i32, i32* @NAME_MAX
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %17, i64 %60
store i8 0, i8* %61
%62 = load i32, i32* %8
%63 = load i32, i32* @O_APPEND
%64 = or i32 %62, %63
store i32 %64, i32* %8
br label %67
65: ; preds = %47
%66 = call i32 @errExit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %67
67: ; preds = %65, %53
br label %70
68: ; preds = %44
%69 = call i32 @errExit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %70
70: ; preds = %68, %67
br label %71
71: ; preds = %70, %32
%72 = load i32, i32* %8
%73 = load i32, i32* %9
%74 = call i32 @open(i8* %17, i32 %72, i32 %73)
store i32 %74, i32* %10
%75 = load i32, i32* %10
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %79
77: ; preds = %71
%78 = call i32 @errExit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %79
79: ; preds = %77, %71
%80 = load i32, i32* @STDIN_FILENO
%81 = call i64 @read(i32 %80, i8* %11, i32 1)
store i64 %81, i64* %12
br label %82
82: ; preds = %97, %79
%83 = load i64, i64* %12
%84 = icmp ugt i64 %83, 0
br i1 %84, label %85, label %100
85: ; preds = %82
%86 = load i32, i32* %10
%87 = call i32 @write(i32 %86, i8* %11, i32 1)
%88 = icmp ne i32 %87, 1
br i1 %88, label %89, label %91
89: ; preds = %85
%90 = call i32 @errExit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %91
91: ; preds = %89, %85
%92 = load i32, i32* @STDOUT_FILENO
%93 = call i32 @write(i32 %92, i8* %11, i32 1)
%94 = icmp ne i32 %93, 1
br i1 %94, label %95, label %97
95: ; preds = %91
%96 = call i32 @errExit(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %97
97: ; preds = %95, %91
%98 = load i32, i32* @STDIN_FILENO
%99 = call i64 @read(i32 %98, i8* %11, i32 1)
store i64 %99, i64* %12
br label %82
100: ; preds = %82
%101 = load i64, i64* %12
%102 = icmp eq i64 %101, -1
br i1 %102, label %103, label %105
103: ; preds = %100
%104 = call i32 @errExit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %105
105: ; preds = %103, %100
%106 = load i32, i32* %10
%107 = call i32 @close(i32 %106)
%108 = icmp eq i32 %107, -1
br i1 %108, label %109, label %111
109: ; preds = %105
%110 = call i32 @errExit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %111
111: ; preds = %109, %105
%112 = load i32, i32* @EXIT_SUCCESS
%113 = call i32 @exit(i32 %112)
unreachable
}

LLVM IR (-O2):
@NAME_MAX = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = 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
@O_TRUNC = global i32 0
@.str = [3 x i8] c"-a\00"
@O_APPEND = global i32 0
@.str.1 = [25 x i8] c"Usage: tee [-a] filename\00"
@.str.2 = [13 x i8] c"Opening File\00"
@STDIN_FILENO = global i32 0
@.str.3 = [16 x i8] c"Writing to file\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [18 x i8] c"Writing to stdout\00"
@.str.6 = [13 x i8] c"Closing File\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = load i32, i32* @NAME_MAX
%5 = add nsw i32 %4, 1
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IROTH
%19 = or i32 %17, %18
switch i32 %0, label %45 [
i32 2, label %20
i32 3, label %29
]
20: ; preds = %2
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @strncpy(i8* %7, i8* %22, i32 %4)
%24 = load i32, i32* @NAME_MAX
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %7, i64 %25
store i8 0, i8* %26
%27 = load i32, i32* @O_TRUNC
%28 = or i32 %27, %10
br label %47
29: ; preds = %2
%30 = getelementptr inbounds i8*, i8** %1, i64 1
%31 = load i8*, i8** %30
%32 = tail call i32 @strcmp(i8* dereferenceable(1) %31, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %43
34: ; preds = %29
%35 = getelementptr inbounds i8*, i8** %1, i64 2
%36 = load i8*, i8** %35
%37 = call i32 @strncpy(i8* %7, i8* %36, i32 %4)
%38 = load i32, i32* @NAME_MAX
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %7, i64 %39
store i8 0, i8* %40
%41 = load i32, i32* @O_APPEND
%42 = or i32 %41, %10
br label %47
43: ; preds = %29
%44 = tail call i32 @errExit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %47
45: ; preds = %2
%46 = tail call i32 @errExit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %47
47: ; preds = %45, %43, %34, %20
%48 = phi i32 [ %28, %20 ], [ %10, %43 ], [ %42, %34 ], [ %10, %45 ]
%49 = call i32 @open(i8* %7, i32 %48, i32 %19)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @errExit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
%54 = load i32, i32* @STDIN_FILENO
%55 = call i64 @read(i32 %54, i8* %3, i32 1)
%56 = icmp eq i64 %55, 0
br i1 %56, label %72, label %57
57: ; preds = %53, %68
%58 = call i32 @write(i32 %49, i8* %3, i32 1)
%59 = icmp eq i32 %58, 1
br i1 %59, label %62, label %60
60: ; preds = %57
%61 = call i32 @errExit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %60, %57
%63 = load i32, i32* @STDOUT_FILENO
%64 = call i32 @write(i32 %63, i8* %3, i32 1)
%65 = icmp eq i32 %64, 1
br i1 %65, label %68, label %66
66: ; preds = %62
%67 = call i32 @errExit(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %68
68: ; preds = %66, %62
%69 = load i32, i32* @STDIN_FILENO
%70 = call i64 @read(i32 %69, i8* %3, i32 1)
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %57
72: ; preds = %68, %53
%73 = call i32 @close(i32 %49)
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %77
75: ; preds = %72
%76 = call i32 @errExit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %77
77: ; preds = %75, %72
%78 = load i32, i32* @EXIT_SUCCESS
%79 = call i32 @exit(i32 %78)
unreachable
}