#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 EXIT_FAILURE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ TIOCSCTTY;
int /*<<< orphan*/ close(int);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int setsid();
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char *argv[]) {
  int console_fd = 0;
  if (2 > argc) {
    (void)write(STDOUT_FILENO, "Usage: cttyhack COMMAND\n",
                sizeof("Usage: cttyhack COMMAND\n"));
    goto end;
  }
  if (-1 == setsid()) {
    goto end;
  }
  console_fd = open("/dev/console", O_RDWR);
  if (-1 == console_fd) {
    goto end;
  }
  if (-1 == ioctl(console_fd, TIOCSCTTY, NULL)) {
    goto close_console;
  }
  if (-1 == dup2(console_fd, STDIN_FILENO)) {
    goto close_console;
  }
  if (-1 == dup2(console_fd, STDOUT_FILENO)) {
    goto close_console;
  }
  if (-1 == dup2(console_fd, STDERR_FILENO)) {
    goto close_console;
  }
  (void)close(console_fd);
  (void)execvp(argv[1], &argv[1]);
  goto end;
close_console:
  (void)close(console_fd);
end:
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
jle    11ba <main+0x4a>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
mov    $0x19,%edx
call   1030 <write@plt>
jmp    12c2 <main+0x152>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11d5 <main+0x65>
jmp    12c2 <main+0x152>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    1200 <main+0x90>
jmp    12c2 <main+0x152>
mov    -0x14(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <ioctl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1229 <main+0xb9>
jmp    12ba <main+0x14a>
mov    -0x14(%rbp),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    124e <main+0xde>
jmp    12ba <main+0x14a>
mov    -0x14(%rbp),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1273 <main+0x103>
jmp    12ba <main+0x14a>
mov    -0x14(%rbp),%edi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1298 <main+0x128>
jmp    12ba <main+0x14a>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1090 <execvp@plt>
jmp    12c2 <main+0x152>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     1197 <main+0x27>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
mov    $0x19,%edx
call   1030 <write@plt>
jmp    123c <main+0xcc>
mov    %rsi,%rbx
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     123c <main+0xcc>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     123c <main+0xcc>
mov    %eax,%ebp
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1235 <main+0xc5>
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1235 <main+0xc5>
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1235 <main+0xc5>
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1235 <main+0xc5>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
call   1090 <execvp@plt>
jmp    123c <main+0xcc>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [25 x i8] c"Usage: cttyhack COMMAND\0A\00"
@.str.1 = [13 x i8] c"/dev/console\00"
@O_RDWR = global i32 0
@TIOCSCTTY = global i32 0
@STDIN_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%7 = load i32, i32* %4
%8 = icmp sgt i32 2, %7
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @STDOUT_FILENO
%11 = call i32 @write(i32 %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 25)
br label %58
12: ; preds = %2
%13 = call i32 (...) @setsid()
%14 = icmp eq i32 -1, %13
br i1 %14, label %15, label %16
15: ; preds = %12
br label %58
16: ; preds = %12
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 -1, %19
br i1 %20, label %21, label %22
21: ; preds = %16
br label %58
22: ; preds = %16
%23 = load i32, i32* %6
%24 = load i32, i32* @TIOCSCTTY
%25 = call i32 @ioctl(i32 %23, i32 %24, i32* null)
%26 = icmp eq i32 -1, %25
br i1 %26, label %27, label %28
27: ; preds = %22
br label %55
28: ; preds = %22
%29 = load i32, i32* %6
%30 = load i32, i32* @STDIN_FILENO
%31 = call i32 @dup2(i32 %29, i32 %30)
%32 = icmp eq i32 -1, %31
br i1 %32, label %33, label %34
33: ; preds = %28
br label %55
34: ; preds = %28
%35 = load i32, i32* %6
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @dup2(i32 %35, i32 %36)
%38 = icmp eq i32 -1, %37
br i1 %38, label %39, label %40
39: ; preds = %34
br label %55
40: ; preds = %34
%41 = load i32, i32* %6
%42 = load i32, i32* @STDERR_FILENO
%43 = call i32 @dup2(i32 %41, i32 %42)
%44 = icmp eq i32 -1, %43
br i1 %44, label %45, label %46
45: ; preds = %40
br label %55
46: ; preds = %40
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 1
%54 = call i32 @execvp(i8* %51, i8** %53)
br label %58
55: ; preds = %45, %39, %33, %27
%56 = load i32, i32* %6
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %55, %46, %21, %15, %9
%59 = load i32, i32* @EXIT_FAILURE
ret i32 %59
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [25 x i8] c"Usage: cttyhack COMMAND\0A\00"
@.str.1 = [13 x i8] c"/dev/console\00"
@O_RDWR = global i32 0
@TIOCSCTTY = global i32 0
@STDIN_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @STDOUT_FILENO
%6 = tail call i32 @write(i32 %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 25)
br label %37
7: ; preds = %2
%8 = tail call i32 (...) @setsid()
%9 = icmp eq i32 %8, -1
br i1 %9, label %37, label %10
10: ; preds = %7
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %37, label %14
14: ; preds = %10
%15 = load i32, i32* @TIOCSCTTY
%16 = tail call i32 @ioctl(i32 %12, i32 %15, i32* null)
%17 = icmp eq i32 %16, -1
br i1 %17, label %35, label %18
18: ; preds = %14
%19 = load i32, i32* @STDIN_FILENO
%20 = tail call i32 @dup2(i32 %12, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %35, label %22
22: ; preds = %18
%23 = load i32, i32* @STDOUT_FILENO
%24 = tail call i32 @dup2(i32 %12, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %35, label %26
26: ; preds = %22
%27 = load i32, i32* @STDERR_FILENO
%28 = tail call i32 @dup2(i32 %12, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %35, label %30
30: ; preds = %26
%31 = tail call i32 @close(i32 %12)
%32 = getelementptr inbounds i8*, i8** %1, i64 1
%33 = load i8*, i8** %32
%34 = tail call i32 @execvp(i8* %33, i8** %32)
br label %37
35: ; preds = %26, %22, %18, %14
%36 = tail call i32 @close(i32 %12)
br label %37
37: ; preds = %10, %7, %35, %30, %4
%38 = load i32, i32* @EXIT_FAILURE
ret i32 %38
}