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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ tcsetpgrp(int, int /*<<< orphan*/);
int redirect_output(const char *dev) {
  pid_t p = setsid();
  int fd;
  chdir("/dev");
  fd = open(dev, O_RDWR);
  chdir("/");
  if (fd < 0)
    return -1;
  dup2(fd, STDIN_FILENO);
  dup2(fd, STDOUT_FILENO);
  dup2(fd, STDERR_FILENO);
  tcsetpgrp(fd, p);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<redirect_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %eax,-0x14(%rbp)
lea    0xe83(%rip),%rdi
call   1030 <chdir@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
lea    0xe67(%rip),%rdi
call   1030 <chdir@plt>
cmpl   $0x0,-0x18(%rbp)
jge    11b9 <redirect_output+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <redirect_output+0xa6>
mov    -0x18(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1070 <tcsetpgrp@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_output>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbp
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <setsid@plt>
mov    %eax,%r14d
lea    0xe86(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
lea    0xe6c(%rip),%rdi
call   1030 <chdir@plt>
test   %ebp,%ebp
js     11e5 <redirect_output+0x85>
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    %r14d,%esi
call   1070 <tcsetpgrp@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11ea <redirect_output+0x8a>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"/dev\00"
@O_RDWR = global i32 0
@.str.1 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @redirect_output(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = call i32 (...) @setsid()
store i32 %6, i32* %4
%7 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %5
%11 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 -1, i32* %2
br label %30
15: ; preds = %1
%16 = load i32, i32* %5
%17 = load i32, i32* @STDIN_FILENO
%18 = call i32 @dup2(i32 %16, i32 %17)
%19 = load i32, i32* %5
%20 = load i32, i32* @STDOUT_FILENO
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = load i32, i32* %5
%23 = load i32, i32* @STDERR_FILENO
%24 = call i32 @dup2(i32 %22, i32 %23)
%25 = load i32, i32* %5
%26 = load i32, i32* %4
%27 = call i32 @tcsetpgrp(i32 %25, i32 %26)
%28 = load i32, i32* %5
%29 = call i32 @close(i32 %28)
store i32 0, i32* %2
br label %30
30: ; preds = %15, %14
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [5 x i8] c"/dev\00"
@O_RDWR = global i32 0
@.str.1 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @redirect_output(i8* nocapture %0) {
%2 = tail call i32 (...) @setsid()
%3 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp slt i32 %5, 0
br i1 %7, label %17, label %8
8: ; preds = %1
%9 = load i32, i32* @STDIN_FILENO
%10 = tail call i32 @dup2(i32 %5, i32 %9)
%11 = load i32, i32* @STDOUT_FILENO
%12 = tail call i32 @dup2(i32 %5, i32 %11)
%13 = load i32, i32* @STDERR_FILENO
%14 = tail call i32 @dup2(i32 %5, i32 %13)
%15 = tail call i32 @tcsetpgrp(i32 %5, i32 %2)
%16 = tail call i32 @close(i32 %5)
br label %17
17: ; preds = %1, %8
%18 = phi i32 [ 0, %8 ], [ -1, %1 ]
ret i32 %18
}