#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IREAD;
int S_IWRITE;
scalar_t__ close(int);
int dup(int);
int /*<<< orphan*/ exit(int);
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void redirect_stdouterr(const char *outfile) {
  int newout = -1;
  int stdout_fd = fileno(stdout);
  int stderr_fd = fileno(stderr);
  if (outfile != NULL) {
    newout = open(outfile, O_CREAT | O_WRONLY | O_TRUNC, S_IREAD | S_IWRITE);
    if (newout < 0) {
      perror("open new stdout failed");
      exit(1);
    }
  }
  if (close(stdout_fd) != 0 || (outfile != NULL && dup(newout) != stdout_fd)) {
    fprintf(stderr, "stdout redirect FAILED");
    exit(1);
  }
  if (close(stderr_fd) != 0 || (outfile != NULL && dup(newout) != stderr_fd)) {
    fprintf(stderr, "stderr redirect FAILED");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<redirect_stdouterr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     120e <redirect_stdouterr+0x9e>
mov    -0x8(%rbp),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    0x2de4(%rip),%rax
or     (%rax),%esi
mov    0x2e23(%rip),%rax
or     (%rax),%esi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
mov    0x2df9(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1209 <redirect_stdouterr+0x99>
lea    0xe06(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    120e <redirect_stdouterr+0x9e>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jne    123c <redirect_stdouterr+0xcc>
cmpq   $0x0,-0x8(%rbp)
je     125b <redirect_stdouterr+0xeb>
mov    -0xc(%rbp),%edi
call   1030 <dup@plt>
cmp    -0x10(%rbp),%eax
je     125b <redirect_stdouterr+0xeb>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jne    1289 <redirect_stdouterr+0x119>
cmpq   $0x0,-0x8(%rbp)
je     12a8 <redirect_stdouterr+0x138>
mov    -0xc(%rbp),%edi
call   1030 <dup@plt>
cmp    -0x14(%rbp),%eax
je     12a8 <redirect_stdouterr+0x138>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xd95(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_stdouterr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%ebp
mov    0x2e37(%rip),%r15
mov    (%r15),%edi
call   1060 <fileno@plt>
mov    %eax,%r14d
test   %rbx,%rbx
je     1217 <redirect_stdouterr+0xa7>
mov    0x2e50(%rip),%rax
mov    0x2df9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
mov    0x2df5(%rip),%rax
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1255 <redirect_stdouterr+0xe5>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    123c <redirect_stdouterr+0xcc>
mov    %ebx,%edi
call   1030 <dup@plt>
cmp    %ebp,%eax
jne    123c <redirect_stdouterr+0xcc>
mov    %r14d,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    1230 <redirect_stdouterr+0xc0>
mov    %ebx,%edi
call   1030 <dup@plt>
cmp    %r14d,%eax
jne    1230 <redirect_stdouterr+0xc0>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    123c <redirect_stdouterr+0xcc>
mov    %r14d,%edi
call   1040 <close@plt>
test   %rax,%rax
je     120c <redirect_stdouterr+0x9c>
mov    (%r15),%edi
lea    0xdf4(%rip),%rsi
jmp    1246 <redirect_stdouterr+0xd6>
mov    (%r15),%edi
lea    0xdd1(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xda4(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@stderr = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IREAD = global i32 0
@S_IWRITE = global i32 0
@.str = [23 x i8] c"open new stdout failed\00"
@.str.1 = [23 x i8] c"stdout redirect FAILED\00"
@.str.2 = [23 x i8] c"stderr redirect FAILED\00"
define void @redirect_stdouterr(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %3
%6 = load i32, i32* @stdout
%7 = call i32 @fileno(i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* @stderr
%9 = call i32 @fileno(i32 %8)
store i32 %9, i32* %5
%10 = load i8*, i8** %2
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %29
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_WRONLY
%16 = or i32 %14, %15
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IREAD
%20 = load i32, i32* @S_IWRITE
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %13, i32 %18, i32 %21)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %12
%26 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %12
br label %29
29: ; preds = %28, %1
%30 = load i32, i32* %4
%31 = call i64 @close(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %41, label %33
33: ; preds = %29
%34 = load i8*, i8** %2
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %45
36: ; preds = %33
%37 = load i32, i32* %3
%38 = call i32 @dup(i32 %37)
%39 = load i32, i32* %4
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %45
41: ; preds = %36, %29
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %36, %33
%46 = load i32, i32* %5
%47 = call i64 @close(i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %57, label %49
49: ; preds = %45
%50 = load i8*, i8** %2
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %61
52: ; preds = %49
%53 = load i32, i32* %3
%54 = call i32 @dup(i32 %53)
%55 = load i32, i32* %5
%56 = icmp ne i32 %54, %55
br i1 %56, label %57, label %61
57: ; preds = %52, %45
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %52, %49
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
@stderr = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IREAD = global i32 0
@S_IWRITE = global i32 0
@.str = [23 x i8] c"open new stdout failed\00"
@.str.1 = [23 x i8] c"stdout redirect FAILED\00"
@.str.2 = [23 x i8] c"stderr redirect FAILED\00"
define void @redirect_stdouterr(i8* %0) {
%2 = load i32, i32* @stdout
%3 = tail call i32 @fileno(i32 %2)
%4 = load i32, i32* @stderr
%5 = tail call i32 @fileno(i32 %4)
%6 = icmp eq i8* %0, null
br i1 %6, label %21, label %7
7: ; preds = %1
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_WRONLY
%10 = or i32 %9, %8
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IREAD
%14 = load i32, i32* @S_IWRITE
%15 = or i32 %14, %13
%16 = tail call i32 @open(i8* %0, i32 %12, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %7
%19 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %1
%22 = tail call i64 @close(i32 %3)
%23 = icmp eq i64 %22, 0
br i1 %23, label %34, label %30
24: ; preds = %7
%25 = tail call i64 @close(i32 %3)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = tail call i32 @dup(i32 %16)
%29 = icmp eq i32 %28, %3
br i1 %29, label %37, label %30
30: ; preds = %24, %27, %21
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %21
%35 = tail call i64 @close(i32 %5)
%36 = icmp eq i64 %35, 0
br i1 %36, label %47, label %43
37: ; preds = %27
%38 = tail call i64 @close(i32 %5)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = tail call i32 @dup(i32 %16)
%42 = icmp eq i32 %41, %5
br i1 %42, label %47, label %43
43: ; preds = %37, %40, %34
%44 = load i32, i32* @stderr
%45 = tail call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%46 = tail call i32 @exit(i32 1)
unreachable
47: ; preds = %34, %40
ret void
}