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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
char *ctime(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int);
int strlen(char *);
scalar_t__ time(int /*<<< orphan*/ *);
int main(void) {
  time_t t;
  char *date;
  char fname[] = "data.txt";
  if (time(&t) < 0) {
    fprintf(stderr, "Err. %d time(); %s.\n", errno, strerror(errno));
    exit(EXIT_FAILURE);
  }
  fputs("Before freopen()\n", stdout);
  if (freopen(fname, "w", stdout) == NULL) {
    fprintf(stderr, "Unable to redirect stdout stream.\n");
    exit(EXIT_FAILURE);
  } else {
    if ((date = ctime(&t)) == NULL) {
      fprintf(stderr, "Err. %d ctime(); %s.\n", errno, strerror(errno));
      exit(EXIT_FAILURE);
    }
    date[strlen(date) - 1] = '\0';
    printf("Line executed at: \'%s\'\n", date);
  }
  freopen("/dev/tty", "a", stdout);
  fputs("After freopen()\n", stdout);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe5a(%rip),%rax
mov    %rax,-0x19(%rbp)
mov    0xe58(%rip),%al
mov    %al,-0x11(%rbp)
lea    -0x8(%rbp),%rdi
call   1090 <time@plt>
cmp    $0x0,%rax
jge    1211 <main+0x81>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
lea    0xdfd(%rip),%rdi
call   1060 <fputs@plt>
lea    -0x19(%rbp),%rdi
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
lea    0xdf6(%rip),%rsi
call   1070 <freopen@plt>
cmp    $0x0,%rax
jne    126e <main+0xde>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x8(%rbp),%rdi
call   1030 <ctime@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    12d0 <main+0x140>
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%edx
mov    %rax,%rcx
lea    0xd9a(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rsi
lea    0xd6f(%rip),%rdi
call   1050 <printf@plt>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edx
lea    0xd72(%rip),%rdi
lea    0xd74(%rip),%rsi
call   1070 <freopen@plt>
mov    0x2ca4(%rip),%rax
mov    (%rax),%esi
lea    0xd61(%rip),%rdi
call   1060 <fputs@plt>
mov    0x2c87(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movabs $0x7478742e61746164,%rax
74 78 74
mov    %rax,0x8(%rsp)
movb   $0x0,0x10(%rsp)
lea    0x4(%rsp),%rdi
call   1090 <time@plt>
test   %rax,%rax
js     1259 <main+0xc9>
mov    0x2e04(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xe50(%rip),%rdi
call   1060 <fputs@plt>
mov    0x0(%rbp),%edx
lea    0xe53(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1070 <freopen@plt>
test   %rax,%rax
je     127b <main+0xeb>
lea    0x4(%rsp),%rdi
call   1030 <ctime@plt>
test   %rax,%rax
je     1294 <main+0x104>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xe4f(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    0x0(%rbp),%edx
lea    0xe55(%rip),%rdi
lea    0xe57(%rip),%rsi
call   1070 <freopen@plt>
mov    0x0(%rbp),%esi
lea    0xe4a(%rip),%rdi
call   1060 <fputs@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d80(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6f(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10b0 <strerror@plt>
lea    0xd90(%rip),%rsi
jmp    12b4 <main+0x124>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xda7(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12c2 <main+0x132>
mov    0x2d45(%rip),%rax
mov    (%rax),%ebx
mov    0x2d34(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10b0 <strerror@plt>
lea    0xda1(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.fname = [9 x i8] c"data.txt\00"
@stderr = global i32 0
@.str = [21 x i8] c"Err. %d time(); %s.\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [18 x i8] c"Before freopen()\0A\00"
@stdout = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [35 x i8] c"Unable to redirect stdout stream.\0A\00"
@.str.4 = [22 x i8] c"Err. %d ctime(); %s.\0A\00"
@.str.5 = [24 x i8] c"Line executed at: '%s'\0A\00"
@.str.6 = [9 x i8] c"/dev/tty\00"
@.str.7 = [2 x i8] c"a\00"
@.str.8 = [17 x i8] c"After freopen()\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [9 x i8]
store i32 0, i32* %1
%5 = bitcast [9 x i8]* %4 to i8*
%6 = call i64 @time(i32* %2)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %10, i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = load i32, i32* @stdout
%18 = call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%20 = load i32, i32* @stdout
%21 = call i32* @freopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %28
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %16
%29 = call i8* @ctime(i32* %2)
store i8* %29, i8** %3
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %39
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %33, i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %28
%40 = load i8*, i8** %3
%41 = load i8*, i8** %3
%42 = call i32 @strlen(i8* %41)
%43 = sub nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %40, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %3
%47 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %39
%49 = load i32, i32* @stdout
%50 = call i32* @freopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 %49)
%51 = load i32, i32* @stdout
%52 = call i32 @fputs(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i32 %51)
%53 = load i32, i32* @EXIT_SUCCESS
ret i32 %53
}

LLVM IR (-O2):
@__const.main.fname = [9 x i8] c"data.txt\00"
@stderr = global i32 0
@.str = [21 x i8] c"Err. %d time(); %s.\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [18 x i8] c"Before freopen()\0A\00"
@stdout = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [35 x i8] c"Unable to redirect stdout stream.\0A\00"
@.str.4 = [22 x i8] c"Err. %d ctime(); %s.\0A\00"
@.str.5 = [24 x i8] c"Line executed at: '%s'\0A\00"
@.str.6 = [9 x i8] c"/dev/tty\00"
@.str.7 = [2 x i8] c"a\00"
@.str.8 = [17 x i8] c"After freopen()\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [9 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%5 = call i64 @time(i32* %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %9, i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = load i32, i32* @stdout
%16 = call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @stdout
%18 = call i32* @freopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %25
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %14
%26 = call i8* @ctime(i32* %1)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %35
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %30, i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %25
%36 = call i32 @strlen(i8* %26)
%37 = add nsw i32 %36, -1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %26, i64 %38
store i8 0, i8* %39
%40 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %26)
%41 = load i32, i32* @stdout
%42 = call i32* @freopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 %41)
%43 = load i32, i32* @stdout
%44 = call i32 @fputs(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i32 %43)
%45 = load i32, i32* @EXIT_SUCCESS
ret i32 %45
}