#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 */
struct winsize {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ TIOCGWINSZ;
int /*<<< orphan*/ bridge(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exec_cmd(int, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fatal(char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ ioctl(int /*<<< orphan*/, int /*<<< orphan*/,
                         struct winsize *);
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
scalar_t__ openpty(int *, int *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   struct winsize *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (!isatty(STDIN_FILENO)) {
    fprintf(stderr, "You don't want to run this outside a tty...");
    exit(-1);
  }
  if (argc < 3) {
    fprintf(stderr, "Usage: hijack <dir> <cmd ...>\n");
    exit(-1);
  }
  int m, s;
  struct winsize w;
  ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
  if (openpty(&m, &s, NULL, NULL, &w)) {
    fatal("openpty failed");
  }
  pid_t pid = fork();
  if (pid == -1) {
    fatal("fork failed");
  }
  if (pid == 0) {
    close(m);
    exec_cmd(s, argv + 2);
  } else {
    close(s);
    bridge(argv[1], m);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    11ec <main+0x4c>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
cmpl   $0x3,-0x8(%rbp)
jge    1215 <main+0x75>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
lea    -0x14(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1080 <openpty@plt>
cmp    $0x0,%rax
je     125e <main+0xbe>
lea    0xdf2(%rip),%rdi
call   10b0 <fatal@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    127e <main+0xde>
lea    0xde1(%rip),%rdi
call   10b0 <fatal@plt>
cmpl   $0x0,-0x24(%rbp)
jne    12a5 <main+0x105>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   1070 <exec_cmd@plt>
jmp    12bd <main+0x11d>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x14(%rbp),%esi
call   1090 <bridge@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
test   %eax,%eax
je     1266 <main+0xc6>
cmp    $0x2,%ebp
jle    1278 <main+0xd8>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdx
call   1040 <ioctl@plt>
lea    0xc(%rsp),%rdi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1080 <openpty@plt>
test   %rax,%rax
je     1210 <main+0x70>
lea    0xe40(%rip),%rdi
call   10b0 <fatal@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1244 <main+0xa4>
cmp    $0xffffffff,%eax
jne    122c <main+0x8c>
lea    0xe33(%rip),%rdi
call   10b0 <fatal@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rbx),%rdi
mov    0xc(%rsp),%esi
call   1090 <bridge@plt>
jmp    125d <main+0xbd>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
add    $0x10,%rbx
mov    %rbx,%rsi
call   1070 <exec_cmd@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xd8a(%rip),%rsi
jmp    1288 <main+0xe8>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.winsize = type { i32 }
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [44 x i8] c"You don't want to run this outside a tty...\00"
@.str.1 = [31 x i8] c"Usage: hijack <dir> <cmd ...>\0A\00"
@STDOUT_FILENO = global i32 0
@TIOCGWINSZ = global i32 0
@.str.2 = [15 x i8] c"openpty failed\00"
@.str.3 = [12 x i8] c"fork failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.winsize
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @STDIN_FILENO
%11 = call i32 @isatty(i32 %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 3
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %17
%25 = load i32, i32* @STDOUT_FILENO
%26 = load i32, i32* @TIOCGWINSZ
%27 = call i32 @ioctl(i32 %25, i32 %26, %struct.winsize* %8)
%28 = call i64 @openpty(i32* %6, i32* %7, i32* null, i32* null, %struct.winsize* %8)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @fatal(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
%33 = call i32 (...) @fork()
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = load i32, i32* %9
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %7
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 2
%47 = call i32 @exec_cmd(i32 %44, i8** %46)
br label %56
48: ; preds = %38
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = load i32, i32* %6
%55 = call i32 @bridge(i8* %53, i32 %54)
br label %56
56: ; preds = %48, %41
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
%struct.winsize = type { i32 }
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [44 x i8] c"You don't want to run this outside a tty...\00"
@.str.1 = [31 x i8] c"Usage: hijack <dir> <cmd ...>\0A\00"
@STDOUT_FILENO = global i32 0
@TIOCGWINSZ = global i32 0
@.str.2 = [15 x i8] c"openpty failed\00"
@.str.3 = [12 x i8] c"fork failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.winsize
%6 = load i32, i32* @STDIN_FILENO
%7 = tail call i32 @isatty(i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = icmp slt i32 %0, 3
br i1 %14, label %15, label %19
15: ; preds = %13
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %13
%20 = bitcast i32* %3 to i8*
%21 = bitcast i32* %4 to i8*
%22 = bitcast %struct.winsize* %5 to i8*
%23 = load i32, i32* @STDOUT_FILENO
%24 = load i32, i32* @TIOCGWINSZ
%25 = call i32 @ioctl(i32 %23, i32 %24, %struct.winsize* %5)
%26 = call i64 @openpty(i32* %3, i32* %4, i32* null, i32* null, %struct.winsize* %5)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %19
%29 = call i32 @fatal(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %19
%31 = call i32 (...) @fork()
switch i32 %31, label %40 [
i32 -1, label %32
i32 0, label %34
]
32: ; preds = %30
%33 = call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %40
34: ; preds = %30
%35 = load i32, i32* %3
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %4
%38 = getelementptr inbounds i8*, i8** %1, i64 2
%39 = call i32 @exec_cmd(i32 %37, i8** %38)
br label %47
40: ; preds = %32, %30
%41 = load i32, i32* %4
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds i8*, i8** %1, i64 1
%44 = load i8*, i8** %43
%45 = load i32, i32* %3
%46 = call i32 @bridge(i8* %44, i32 %45)
br label %47
47: ; preds = %40, %34
ret i32 0
}