#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 */
scalar_t__ O_CREAT;
scalar_t__ O_TRUNC;
scalar_t__ O_WRONLY;
int dup2(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ geteuid();
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ log_stdout(char *, char *, int /*<<< orphan*/,
                              int /*<<< orphan*/);
char *mklogfile(char *);
int open(char *, scalar_t__, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, char *, int /*<<< orphan*/);
void redirect_to_file(char *filepath) {
  char buf[(4096)];
  char *logfile = filepath;
  if (strncmp(filepath, "/dev/null", strlen("/dev/null"))) {
    logfile = mklogfile(filepath);
  }
  if (logfile == NULL)
    exit(1);
  log_stdout("1200 Redirecting stdout and stderr to %s as uid %d euid %d\n",
             logfile, getuid(), geteuid());
  fflush(stdout);
  fflush(stderr);
  int fd = open(logfile, O_CREAT + O_WRONLY + O_TRUNC, 0644);
  if (fd >= 0) {
    int rc1 = dup2(fd, 1);
    int rc2 = dup2(fd, 2);
  } else {
    snprintf(buf, ((4096) - 1), "1300 cannot open file %s", logfile);
    buf[((4096) - 1)] = '\0';
    perror(buf);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<redirect_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x1018(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x1030(%rbp)
lea    0xe14(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x1030(%rbp),%rdi
mov    %eax,%edx
lea    0xdff(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1220 <redirect_to_file+0x60>
mov    -0x8(%rbp),%rdi
call   1090 <mklogfile@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
jne    1238 <redirect_to_file+0x78>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x1018(%rbp),%rax
mov    %rax,-0x1040(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,-0x1034(%rbp)
mov    $0x0,%al
call   1080 <geteuid@plt>
mov    -0x1040(%rbp),%rsi
mov    -0x1034(%rbp),%edx
mov    %eax,%ecx
lea    0xd9a(%rip),%rdi
call   10b0 <log_stdout@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    -0x1018(%rbp),%rdi
mov    0x2d59(%rip),%rax
mov    (%rax),%rsi
mov    0x2d0f(%rip),%rax
add    (%rax),%rsi
mov    0x2d3d(%rip),%rax
add    (%rax),%rsi
mov    $0x1a4,%edx
call   10c0 <open@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jl     1304 <redirect_to_file+0x144>
mov    -0x101c(%rbp),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    %eax,-0x1020(%rbp)
mov    -0x101c(%rbp),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    %eax,-0x1024(%rbp)
jmp    133d <redirect_to_file+0x17d>
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%rcx
mov    $0xfff,%esi
lea    0xd28(%rip),%rdx
call   1070 <snprintf@plt>
movb   $0x0,-0x11(%rbp)
lea    -0x1010(%rbp),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_to_file>:
push   %rbp
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%rbx
lea    0xe2d(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11f8 <redirect_to_file+0x38>
mov    %rbx,%rdi
call   1090 <mklogfile@plt>
mov    %rax,%rbx
test   %rbx,%rbx
je     12bc <redirect_to_file+0xfc>
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <geteuid@plt>
lea    0xdf2(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %eax,%ecx
call   10b0 <log_stdout@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    0x2db1(%rip),%rax
mov    0x2d6a(%rip),%rcx
mov    (%rcx),%rsi
add    (%rax),%rsi
mov    0x2d95(%rip),%rax
add    (%rax),%rsi
mov    %rbx,%rdi
mov    $0x1a4,%edx
call   10c0 <open@plt>
test   %eax,%eax
js     1292 <redirect_to_file+0xd2>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
add    $0x1008,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <dup2@plt>
lea    0xdad(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xfff,%esi
mov    %rbx,%rcx
call   1070 <snprintf@plt>
movb   $0x0,0xfff(%rsp)
mov    %rbp,%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [60 x i8] c"1200 Redirecting stdout and stderr to %s as uid %d euid %d\0A\00"
@stdout = global i32 0
@stderr = global i32 0
@O_CREAT = global i64 0
@O_WRONLY = global i64 0
@O_TRUNC = global i64 0
@.str.2 = [25 x i8] c"1300 cannot open file %s\00"
define void @redirect_to_file(i8* %0) {
%2 = alloca i8*
%3 = alloca [4096 x i8]
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
store i8* %8, i8** %4
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%11 = call i64 @strncmp(i8* %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = call i8* @mklogfile(i8* %14)
store i8* %15, i8** %4
br label %16
16: ; preds = %13, %1
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = call i32 (...) @getuid()
%24 = call i32 (...) @geteuid()
%25 = call i32 @log_stdout(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i8* %22, i32 %23, i32 %24)
%26 = load i32, i32* @stdout
%27 = call i32 @fflush(i32 %26)
%28 = load i32, i32* @stderr
%29 = call i32 @fflush(i32 %28)
%30 = load i8*, i8** %4
%31 = load i64, i64* @O_CREAT
%32 = load i64, i64* @O_WRONLY
%33 = add nsw i64 %31, %32
%34 = load i64, i64* @O_TRUNC
%35 = add nsw i64 %33, %34
%36 = call i32 @open(i8* %30, i64 %35, i32 420)
store i32 %36, i32* %5
%37 = load i32, i32* %5
%38 = icmp sge i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %21
%40 = load i32, i32* %5
%41 = call i32 @dup2(i32 %40, i32 1)
store i32 %41, i32* %6
%42 = load i32, i32* %5
%43 = call i32 @dup2(i32 %42, i32 2)
store i32 %43, i32* %7
br label %52
44: ; preds = %21
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%46 = load i8*, i8** %4
%47 = call i32 @snprintf(i8* %45, i32 4095, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 4095
store i8 0, i8* %48
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%50 = call i32 @perror(i8* %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %39
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [60 x i8] c"1200 Redirecting stdout and stderr to %s as uid %d euid %d\0A\00"
@stdout = global i32 0
@stderr = global i32 0
@O_CREAT = global i64 0
@O_WRONLY = global i64 0
@O_TRUNC = global i64 0
@.str.2 = [25 x i8] c"1300 cannot open file %s\00"
define void @redirect_to_file(i8* %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @strlen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%5 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = tail call i8* @mklogfile(i8* %0)
br label %9
9: ; preds = %7, %1
%10 = phi i8* [ %8, %7 ], [ %0, %1 ]
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %9
%15 = tail call i32 (...) @getuid()
%16 = tail call i32 (...) @geteuid()
%17 = tail call i32 @log_stdout(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i8* %10, i32 %15, i32 %16)
%18 = load i32, i32* @stdout
%19 = tail call i32 @fflush(i32 %18)
%20 = load i32, i32* @stderr
%21 = tail call i32 @fflush(i32 %20)
%22 = load i64, i64* @O_CREAT
%23 = load i64, i64* @O_WRONLY
%24 = add nsw i64 %23, %22
%25 = load i64, i64* @O_TRUNC
%26 = add nsw i64 %24, %25
%27 = tail call i32 @open(i8* %10, i64 %26, i32 420)
%28 = icmp sgt i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %14
%30 = tail call i32 @dup2(i32 %27, i32 1)
%31 = tail call i32 @dup2(i32 %27, i32 2)
ret void
32: ; preds = %14
%33 = call i32 @snprintf(i8* %3, i32 4095, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 4095
store i8 0, i8* %34
%35 = call i32 @perror(i8* %3)
%36 = call i32 @exit(i32 1)
unreachable
}