#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*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ *stdout;
void tee(char *filename, int flag) {
  int i;
  FILE *fp = NULL;
  char *openType = "w";
  if (flag == 1) {
    openType = "a";
  }
  if (filename != NULL) {
    fp = fopen(filename, openType);
  }
  while ((i = fgetc(stdin)) != EOF) {
    fputc(i, stdout);
    if (fp != NULL) {
      fputc(i, fp);
    }
  }
  if (fp != NULL) {
    fclose(fp);
  }
}


x86 assembly (clang, -O0):
<tee>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xea2(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    1177 <tee+0x37>
lea    0xe8f(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1193 <tee+0x53>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
jmp    1198 <tee+0x58>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1040 <fgetc@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%eax
je     11e6 <tee+0xa6>
mov    -0x10(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%rsi
call   1050 <fputc@plt>
cmpq   $0x0,-0x18(%rbp)
je     11e1 <tee+0xa1>
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <fputc@plt>
jmp    1198 <tee+0x58>
cmpq   $0x0,-0x18(%rbp)
je     11fa <tee+0xba>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tee>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     116c <tee+0x2c>
cmp    $0x1,%esi
lea    0xeab(%rip),%rax
lea    0xea2(%rip),%rsi
cmove  %rax,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
jmp    116e <tee+0x2e>
xor    %ebx,%ebx
mov    0x2e73(%rip),%r14
mov    (%r14),%edi
call   1040 <fgetc@plt>
mov    0x2e5c(%rip),%r15
cmp    (%r15),%eax
jne    119e <tee+0x5e>
test   %rbx,%rbx
je     11db <tee+0x9b>
mov    %rbx,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
mov    %eax,%ebp
mov    0x2e29(%rip),%r12
jmp    11bf <tee+0x7f>
nopl   0x0(%rax)
mov    (%r14),%edi
call   1040 <fgetc@plt>
mov    %eax,%ebp
cmp    (%r15),%eax
je     1189 <tee+0x49>
mov    (%r12),%rsi
mov    %ebp,%edi
call   1050 <fputc@plt>
test   %rbx,%rbx
je     11b0 <tee+0x70>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
jmp    11b0 <tee+0x70>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"a\00"
@stdin = global i32 0
@EOF = global i32 0
@stdout = global i32* null
define void @tee(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32* null, i32** %6
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %11
11: ; preds = %10, %2
%12 = load i8*, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i8*, i8** %3
%16 = load i8*, i8** %7
%17 = call i32* @fopen(i8* %15, i8* %16)
store i32* %17, i32** %6
br label %18
18: ; preds = %14, %11
br label %19
19: ; preds = %34, %18
%20 = load i32, i32* @stdin
%21 = call i32 @fgetc(i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* @EOF
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %35
24: ; preds = %19
%25 = load i32, i32* %5
%26 = load i32*, i32** @stdout
%27 = call i32 @fputc(i32 %25, i32* %26)
%28 = load i32*, i32** %6
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = load i32, i32* %5
%32 = load i32*, i32** %6
%33 = call i32 @fputc(i32 %31, i32* %32)
br label %34
34: ; preds = %30, %24
br label %19
35: ; preds = %19
%36 = load i32*, i32** %6
%37 = icmp ne i32* %36, null
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32*, i32** %6
%40 = call i32 @fclose(i32* %39)
br label %41
41: ; preds = %38, %35
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"a\00"
@stdin = global i32 0
@EOF = global i32 0
@stdout = global i32* null
define void @tee(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = icmp eq i32 %1, 1
%6 = select i1 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0)
%7 = tail call i32* @fopen(i8* %0, i8* %6)
br label %8
8: ; preds = %4, %2
%9 = phi i32* [ %7, %4 ], [ null, %2 ]
%10 = load i32, i32* @stdin
%11 = tail call i32 @fgetc(i32 %10)
%12 = load i32, i32* @EOF
%13 = icmp eq i32 %11, %12
br i1 %13, label %27, label %14
14: ; preds = %8
%15 = icmp eq i32* %9, null
br label %16
16: ; preds = %14, %22
%17 = phi i32 [ %11, %14 ], [ %24, %22 ]
%18 = load i32*, i32** @stdout
%19 = tail call i32 @fputc(i32 %17, i32* %18)
br i1 %15, label %22, label %20
20: ; preds = %16
%21 = tail call i32 @fputc(i32 %17, i32* %9)
br label %22
22: ; preds = %20, %16
%23 = load i32, i32* @stdin
%24 = tail call i32 @fgetc(i32 %23)
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %16
27: ; preds = %22, %8
%28 = icmp eq i32* %9, null
br i1 %28, label %31, label %29
29: ; preds = %27
%30 = tail call i32 @fclose(i32* %9)
br label %31
31: ; preds = %29, %27
ret void
}