#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*/ close_autotee();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int /*<<< orphan*/, int /*<<< orphan*/);
int init_autotee(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int main() {
  int result;
  printf("Every stdout print after this line should also appear in %s:\n",
         "tee.out");
  result = init_autotee("tee.out");
  if (result != 0)
    return -1;
  printf("First line\n");
  printf("Second %s\n", "line");
  fprintf(stdout, "Third line\n");
  fprintf(stdout, "%s line\n", "Forth");
  puts("Fifth line");
  fputs("Sixth ", stdout);
  fputs("line\n", stdout);
  fwrite("Seventh line\n", 1, strlen("Seventh line\n"), stdout);
  fprintf(stderr, "Eighth line is stderr and should not appear in in %s\n",
          "tee.out");
  close_autotee();
  printf("Ninth line is after close and should not appear in %s\n", "tee.out");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xea1(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe93(%rip),%rdi
call   1040 <init_autotee@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11c9 <main+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <main+0x140>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe74(%rip),%rdi
lea    0xe99(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
lea    0xe58(%rip),%rdx
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    0xe46(%rip),%rdi
call   1070 <fputs@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
lea    0xe11(%rip),%rdi
call   1070 <fputs@plt>
lea    0xe2c(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%edx
mov    0x2d6c(%rip),%rax
mov    (%rax),%ecx
lea    0xe15(%rip),%rdi
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
lea    0xda8(%rip),%rdx
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   10a0 <close_autotee@plt>
lea    0xe23(%rip),%rdi
lea    0xd8c(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe75(%rip),%rdi
lea    0xeac(%rip),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <init_autotee@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    128a <main+0x10a>
lea    0xf3f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe80(%rip),%rdi
lea    0xea5(%rip),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2dfd(%rip),%r14
mov    (%r14),%edi
lea    0xe6c(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r14),%edi
lea    0xe67(%rip),%rsi
lea    0xe69(%rip),%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
lea    0xe61(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%esi
lea    0xe5d(%rip),%rdi
call   1070 <fputs@plt>
mov    (%r14),%esi
lea    0xe2e(%rip),%rdi
call   1070 <fputs@plt>
lea    0xe49(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    (%r14),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   1090 <fwrite@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
lea    0xdd8(%rip),%rbx
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
call   10a0 <close_autotee@plt>
lea    0xe44(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [62 x i8] c"Every stdout print after this line should also appear in %s:\0A\00"
@.str.1 = [8 x i8] c"tee.out\00"
@.str.2 = [12 x i8] c"First line\0A\00"
@.str.3 = [11 x i8] c"Second %s\0A\00"
@.str.4 = [5 x i8] c"line\00"
@stdout = global i32 0
@.str.5 = [12 x i8] c"Third line\0A\00"
@.str.6 = [9 x i8] c"%s line\0A\00"
@.str.7 = [6 x i8] c"Forth\00"
@.str.8 = [11 x i8] c"Fifth line\00"
@.str.9 = [7 x i8] c"Sixth \00"
@.str.10 = [6 x i8] c"line\0A\00"
@.str.11 = [14 x i8] c"Seventh line\0A\00"
@stderr = global i32 0
@.str.12 = [54 x i8] c"Eighth line is stderr and should not appear in in %s\0A\00"
@.str.13 = [55 x i8] c"Ninth line is after close and should not appear in %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%4 = call i32 @init_autotee(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 -1, i32* %1
br label %27
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%11 = load i32, i32* @stdout
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%13 = load i32, i32* @stdout
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%15 = call i32 @puts(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
%16 = load i32, i32* @stdout
%17 = call i32 @fputs(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @stdout
%19 = call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0), i32 %18)
%20 = call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i64 0, i64 0))
%21 = load i32, i32* @stdout
%22 = call i32 @fwrite(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i64 0, i64 0), i32 1, i32 %20, i32 %21)
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 (...) @close_autotee()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.13, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %27
27: ; preds = %8, %7
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [62 x i8] c"Every stdout print after this line should also appear in %s:\0A\00"
@.str.1 = [8 x i8] c"tee.out\00"
@.str.3 = [11 x i8] c"Second %s\0A\00"
@.str.4 = [5 x i8] c"line\00"
@stdout = global i32 0
@.str.5 = [12 x i8] c"Third line\0A\00"
@.str.6 = [9 x i8] c"%s line\0A\00"
@.str.7 = [6 x i8] c"Forth\00"
@.str.8 = [11 x i8] c"Fifth line\00"
@.str.9 = [7 x i8] c"Sixth \00"
@.str.10 = [6 x i8] c"line\0A\00"
@.str.11 = [14 x i8] c"Seventh line\0A\00"
@stderr = global i32 0
@.str.12 = [54 x i8] c"Eighth line is stderr and should not appear in in %s\0A\00"
@.str.13 = [55 x i8] c"Ninth line is after close and should not appear in %s\0A\00"
@str = [11 x i8] c"First line\00"
define i32 @main() {
%1 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i32 @init_autotee(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %23
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%7 = load i32, i32* @stdout
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%9 = load i32, i32* @stdout
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
%12 = load i32, i32* @stdout
%13 = tail call i32 @fputs(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @stdout
%15 = tail call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0), i32 %14)
%16 = tail call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i64 0, i64 0))
%17 = load i32, i32* @stdout
%18 = tail call i32 @fwrite(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i64 0, i64 0), i32 1, i32 %16, i32 %17)
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 (...) @close_autotee()
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.13, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %0, %4
%24 = phi i32 [ 0, %4 ], [ -1, %0 ]
ret i32 %24
}