#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
void redirect_stdout(char *path) {
  int fd;
  close(1);
  fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0666);
  if (fd < 0) {
    perror(path);
    return;
  }
  if (fd != 1) {
    dup2(fd, 1);
    close(fd);
  }
}


x86 assembly (clang, -O0):
<redirect_stdout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    0x2e86(%rip),%rax
or     (%rax),%esi
mov    0x2e85(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    119a <redirect_stdout+0x5a>
mov    -0x8(%rbp),%rdi
call   1060 <perror@plt>
jmp    11b9 <redirect_stdout+0x79>
cmpl   $0x1,-0xc(%rbp)
je     11b9 <redirect_stdout+0x79>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_stdout>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x2e71(%rip),%rax
mov    0x2e92(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e8f(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1b6,%edx
call   1050 <open@plt>
test   %eax,%eax
js     11a3 <redirect_stdout+0x63>
mov    %eax,%ebp
cmp    $0x1,%eax
jne    118a <redirect_stdout+0x4a>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %ebp,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <close@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <perror@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define void @redirect_stdout(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = call i32 @close(i32 1)
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, i32 %10, i32 438)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i32 @perror(i8* %15)
br label %25
17: ; preds = %1
%18 = load i32, i32* %3
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i32, i32* %3
%22 = call i32 @dup2(i32 %21, i32 1)
%23 = load i32, i32* %3
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %14, %20, %17
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define void @redirect_stdout(i8* nocapture %0) {
%2 = tail call i32 @close(i32 1)
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_TRUNC
%5 = or i32 %4, %3
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* %0, i32 %7, i32 438)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @perror(i8* %0)
br label %17
12: ; preds = %1
%13 = icmp eq i32 %8, 1
br i1 %13, label %17, label %14
14: ; preds = %12
%15 = tail call i32 @dup2(i32 %8, i32 1)
%16 = tail call i32 @close(i32 %8)
br label %17
17: ; preds = %12, %14, %10
ret void
}