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

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ PATH_MAX;
int STDERR_FILENO;
int STDOUT_FILENO;
int S_IRUSR;
int S_IWUSR;
scalar_t__ close(int);
scalar_t__ dup2(int, int);
char *getenv(char *);
scalar_t__ isatty(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int redirect_tty(void) {
  int fd;
  char buffer[PATH_MAX + 1], *home;
  if (isatty(STDOUT_FILENO)) {
    fd = open("nohup.out", O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
    if (fd < 0) {
      home = getenv("HOME");
      if (home) {
        snprintf(buffer, sizeof(buffer), "%s/%s", home, "nohup.out");
        buffer[sizeof(buffer) - 1] = 0;
        fd = open(buffer, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
      }
    }
    if (fd < 0) {
      perror("cannot create "
             "nohup.out"
             " and $HOME/"
             "nohup.out");
      return -1;
    }
    if (dup2(fd, STDOUT_FILENO) < 0 || close(fd) < 0) {
      perror("cannot redirect stdout");
      return -1;
    }
  }
  if (isatty(STDERR_FILENO)) {
    if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) {
      perror("cannot redirect stderr");
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<redirect_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
cmp    $0x0,%rax
je     1309 <redirect_tty+0x199>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
or     (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
mov    0x2de2(%rip),%rax
or     (%rax),%edx
lea    0xe36(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    128f <redirect_tty+0x11f>
lea    0xdf0(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     128a <redirect_tty+0x11a>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rcx
lea    0xdcc(%rip),%rdx
lea    0xded(%rip),%r8
call   1060 <snprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,(%rdi,%rax,1)
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
mov    0x2d3b(%rip),%rax
or     (%rax),%esi
mov    0x2d8a(%rip),%rax
or     (%rax),%esi
mov    0x2d41(%rip),%rax
mov    (%rax),%edx
mov    0x2d50(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
jmp    128f <redirect_tty+0x11f>
cmpl   $0x0,-0x8(%rbp)
jge    12b8 <redirect_tty+0x148>
lea    0xd6b(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1374 <redirect_tty+0x204>
mov    -0x8(%rbp),%edi
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jl     12e5 <redirect_tty+0x175>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
cmp    $0x0,%rax
jge    1304 <redirect_tty+0x194>
lea    0xd4b(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1374 <redirect_tty+0x204>
jmp    1309 <redirect_tty+0x199>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
cmp    $0x0,%rax
je     1366 <redirect_tty+0x1f6>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
mov    0x2c8f(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    1361 <redirect_tty+0x1f1>
lea    0xd05(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1374 <redirect_tty+0x204>
jmp    1366 <redirect_tty+0x1f6>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_tty>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e63(%rip),%rax
mov    (%rax),%r14d
add    $0x1,%r14d
mov    %rsp,%r12
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
test   %rax,%rax
je     128e <redirect_tty+0x11e>
mov    0x2ded(%rip),%r15
mov    0x2dde(%rip),%r13
mov    0x0(%r13),%esi
or     (%r15),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe42(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1266 <redirect_tty+0xf6>
mov    %r15,%rbx
lea    0xe00(%rip),%rdi
call   1030 <getenv@plt>
lea    0xdff(%rip),%r15
test   %rax,%rax
je     12d2 <redirect_tty+0x162>
lea    0xde9(%rip),%rdx
lea    0xe0a(%rip),%r8
mov    %r12,%rdi
mov    %r14d,%esi
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    0x0(%r13),%esi
or     (%rbx),%esi
mov    0x2dba(%rip),%rax
or     (%rax),%esi
movb   $0x0,-0x1(%r14,%r12,1)
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
mov    0x2d62(%rip),%rax
or     (%rax),%edx
mov    %r12,%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12d2 <redirect_tty+0x162>
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
lea    0xdba(%rip),%r15
test   %rax,%rax
js     12d2 <redirect_tty+0x162>
mov    %ebx,%edi
call   1070 <close@plt>
test   %rax,%rax
js     12d2 <redirect_tty+0x162>
mov    0x2d2b(%rip),%r14
mov    (%r14),%edi
call   1040 <isatty@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     12ba <redirect_tty+0x14a>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     12cb <redirect_tty+0x15b>
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7c(%rip),%r15
mov    %r15,%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    12ba <redirect_tty+0x14a>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@STDOUT_FILENO = global i32 0
@.str = [10 x i8] c"nohup.out\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [44 x i8] c"cannot create nohup.out and $HOME/nohup.out\00"
@.str.4 = [23 x i8] c"cannot redirect stdout\00"
@STDERR_FILENO = global i32 0
@.str.5 = [23 x i8] c"cannot redirect stderr\00"
define i32 @redirect_tty() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = load i32, i32* @PATH_MAX
%8 = add nsw i32 %7, 1
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %3
%11 = alloca i8, i64 %9
store i64 %9, i64* %4
%12 = load i32, i32* @STDOUT_FILENO
%13 = call i64 @isatty(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %64
15: ; preds = %0
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_APPEND
%18 = or i32 %16, %17
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %21, %22
%24 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %23)
store i32 %24, i32* %2
%25 = load i32, i32* %2
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %47
27: ; preds = %15
%28 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %28, i8** %5
%29 = load i8*, i8** %5
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %46
31: ; preds = %27
%32 = trunc i64 %9 to i32
%33 = load i8*, i8** %5
%34 = call i32 @snprintf(i8* %11, i32 %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %33, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%35 = sub i64 %9, 1
%36 = getelementptr inbounds i8, i8* %11, i64 %35
store i8 0, i8* %36
%37 = load i32, i32* @O_WRONLY
%38 = load i32, i32* @O_APPEND
%39 = or i32 %37, %38
%40 = load i32, i32* @O_CREAT
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IRUSR
%43 = load i32, i32* @S_IWUSR
%44 = or i32 %42, %43
%45 = call i32 @open(i8* %11, i32 %41, i32 %44)
store i32 %45, i32* %2
br label %46
46: ; preds = %31, %27
br label %47
47: ; preds = %46, %15
%48 = load i32, i32* %2
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
store i32 1, i32* %6
br label %77
52: ; preds = %47
%53 = load i32, i32* %2
%54 = load i32, i32* @STDOUT_FILENO
%55 = call i64 @dup2(i32 %53, i32 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %61, label %57
57: ; preds = %52
%58 = load i32, i32* %2
%59 = call i64 @close(i32 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %57, %52
%62 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
store i32 1, i32* %6
br label %77
63: ; preds = %57
br label %64
64: ; preds = %63, %0
%65 = load i32, i32* @STDERR_FILENO
%66 = call i64 @isatty(i32 %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %76
68: ; preds = %64
%69 = load i32, i32* @STDOUT_FILENO
%70 = load i32, i32* @STDERR_FILENO
%71 = call i64 @dup2(i32 %69, i32 %70)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %68
%74 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %1
store i32 1, i32* %6
br label %77
75: ; preds = %68
br label %76
76: ; preds = %75, %64
store i32 0, i32* %1
store i32 1, i32* %6
br label %77
77: ; preds = %76, %73, %61, %50
%78 = load i8*, i8** %3
%79 = load i32, i32* %1
ret i32 %79
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@STDOUT_FILENO = global i32 0
@.str = [10 x i8] c"nohup.out\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [44 x i8] c"cannot create nohup.out and $HOME/nohup.out\00"
@.str.4 = [23 x i8] c"cannot redirect stdout\00"
@STDERR_FILENO = global i32 0
@.str.5 = [23 x i8] c"cannot redirect stderr\00"
define i32 @redirect_tty() {
%1 = load i32, i32* @PATH_MAX
%2 = add nsw i32 %1, 1
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = load i32, i32* @STDOUT_FILENO
%6 = tail call i64 @isatty(i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %44, label %8
8: ; preds = %0
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_APPEND
%11 = or i32 %10, %9
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %36
19: ; preds = %8
%20 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %53, label %22
22: ; preds = %19
%23 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%24 = add nsw i64 %3, -1
%25 = getelementptr inbounds i8, i8* %4, i64 %24
store i8 0, i8* %25
%26 = load i32, i32* @O_WRONLY
%27 = load i32, i32* @O_APPEND
%28 = or i32 %27, %26
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IRUSR
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %32, %31
%34 = call i32 @open(i8* %4, i32 %30, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %53, label %36
36: ; preds = %8, %22
%37 = phi i32 [ %34, %22 ], [ %17, %8 ]
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i64 @dup2(i32 %37, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %53, label %41
41: ; preds = %36
%42 = call i64 @close(i32 %37)
%43 = icmp slt i64 %42, 0
br i1 %43, label %53, label %44
44: ; preds = %41, %0
%45 = load i32, i32* @STDERR_FILENO
%46 = call i64 @isatty(i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %56, label %48
48: ; preds = %44
%49 = load i32, i32* @STDOUT_FILENO
%50 = load i32, i32* @STDERR_FILENO
%51 = call i64 @dup2(i32 %49, i32 %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %48, %36, %41, %22, %19
%54 = phi i8* [ getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), %19 ], [ getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), %22 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), %41 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), %36 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), %48 ]
%55 = call i32 @perror(i8* %54)
br label %56
56: ; preds = %53, %44, %48
%57 = phi i32 [ 0, %48 ], [ 0, %44 ], [ -1, %53 ]
ret i32 %57
}