#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 vt_stat {
  int v_active;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int R_OK;
int /*<<< orphan*/ VT_ACTIVATE;
int /*<<< orphan*/ VT_DISALLOCATE;
int /*<<< orphan*/ VT_GETSTATE;
int /*<<< orphan*/ VT_OPENQRY;
int /*<<< orphan*/ VT_WAITACTIVE;
int W_OK;
scalar_t__ access(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
scalar_t__ ioctl(int, int /*<<< orphan*/, ...);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  int vtnum;
  int vtfd;
  struct vt_stat vtstat;
  char device[32];
  int child;
  vtfd = open("/dev/tty", O_RDWR, 0);
  if (vtfd < 0) {
    perror("minopen: could not open /dev/tty");
    exit(1);
  }
  if (ioctl(vtfd, VT_GETSTATE, &vtstat) < 0) {
    perror("minopen: tty is not virtual console");
    exit(1);
  }
  if (ioctl(vtfd, VT_OPENQRY, &vtnum) < 0) {
    perror("minopen: no free virtual consoles");
    exit(1);
  }
  sprintf(device, "/dev/tty%d", vtnum);
  if (access(device, (W_OK | R_OK)) < 0) {
    perror("minopen: insufficient permission on tty");
    exit(1);
  }
  child = fork();
  if (child == 0) {
    ioctl(vtfd, VT_ACTIVATE, vtnum);
    ioctl(vtfd, VT_WAITACTIVE, vtnum);
    setsid();
    close(0);
    close(1);
    close(2);
    close(vtfd);
    vtfd = open(device, O_RDWR, 0);
    dup(vtfd);
    dup(vtfd);
    execlp("/bin/bash", "bash", NULL);
  }
  wait(&child);
  ioctl(vtfd, VT_ACTIVATE, vtstat.v_active);
  ioctl(vtfd, VT_WAITACTIVE, vtstat.v_active);
  ioctl(vtfd, VT_DISALLOCATE, vtnum);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
lea    0xe32(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1210 <main+0x50>
lea    0xdff(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1247 <main+0x87>
lea    0xde9(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
mov    $0x0,%al
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    127e <main+0xbe>
lea    0xdd6(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xddb(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
mov    0x2d0b(%rip),%rax
or     (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jge    12cc <main+0x10c>
lea    0xdb5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    1374 <main+0x1b4>
mov    -0x18(%rbp),%edi
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1040 <ioctl@plt>
mov    -0x18(%rbp),%edi
mov    0x2cf8(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1040 <ioctl@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
lea    -0x40(%rbp),%rdi
mov    0x2c9f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <dup@plt>
mov    -0x18(%rbp),%edi
call   1030 <dup@plt>
lea    0xd36(%rip),%rdi
lea    0xd34(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10c0 <execlp@plt>
lea    -0x44(%rbp),%rdi
call   10d0 <wait@plt>
mov    -0x18(%rbp),%edi
mov    0x2c51(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1040 <ioctl@plt>
mov    -0x18(%rbp),%edi
mov    0x2c5b(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1040 <ioctl@plt>
mov    -0x18(%rbp),%edi
mov    0x2c1d(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1040 <ioctl@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0x2e10(%rip),%r15
mov    (%r15),%esi
lea    0xe3e(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
test   %eax,%eax
jns    11f1 <main+0x31>
lea    0xe14(%rip),%rdi
jmp    127b <main+0xbb>
mov    %eax,%ebx
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
test   %rax,%rax
jns    1218 <main+0x58>
lea    0xe0b(%rip),%rdi
jmp    127b <main+0xbb>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
test   %rax,%rax
jns    123d <main+0x7d>
lea    0xe0a(%rip),%rdi
jmp    127b <main+0xbb>
mov    0x4(%rsp),%edx
lea    0xe1f(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   10a0 <sprintf@plt>
mov    0x2d64(%rip),%rax
mov    0x2d4d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
call   1080 <access@plt>
test   %rax,%rax
jns    128a <main+0xca>
lea    0xdf7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
jne    1326 <main+0x166>
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
mov    0x4(%rsp),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
mov    0x4(%rsp),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    (%r15),%esi
lea    0x10(%rsp),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <dup@plt>
mov    %ebx,%edi
call   1030 <dup@plt>
lea    0xd82(%rip),%rdi
lea    0xd80(%rip),%rsi
xor    %edx,%edx
call   10c0 <execlp@plt>
lea    0xc(%rsp),%rdi
call   10d0 <wait@plt>
mov    0x2ca1(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rsp),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
mov    0x2cab(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rsp),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
mov    0x2c6d(%rip),%rax
mov    (%rax),%esi
mov    0x4(%rsp),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.vt_stat = type { i32 }
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@.str.1 = [33 x i8] c"minopen: could not open /dev/tty\00"
@VT_GETSTATE = global i32 0
@.str.2 = [36 x i8] c"minopen: tty is not virtual console\00"
@VT_OPENQRY = global i32 0
@.str.3 = [34 x i8] c"minopen: no free virtual consoles\00"
@.str.4 = [11 x i8] c"/dev/tty%d\00"
@W_OK = global i32 0
@R_OK = global i32 0
@.str.5 = [40 x i8] c"minopen: insufficient permission on tty\00"
@VT_ACTIVATE = global i32 0
@VT_WAITACTIVE = global i32 0
@.str.6 = [10 x i8] c"/bin/bash\00"
@.str.7 = [5 x i8] c"bash\00"
@VT_DISALLOCATE = global i32 0
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.vt_stat
%9 = alloca [32 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11, i32 0)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %7
%20 = load i32, i32* @VT_GETSTATE
%21 = call i64 (i32, i32, ...) @ioctl(i32 %19, i32 %20, %struct.vt_stat* %8)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i32, i32* %7
%28 = load i32, i32* @VT_OPENQRY
%29 = call i64 (i32, i32, ...) @ioctl(i32 %27, i32 %28, i32* %6)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%36 = load i32, i32* %6
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%39 = load i32, i32* @W_OK
%40 = load i32, i32* @R_OK
%41 = or i32 %39, %40
%42 = call i64 @access(i8* %38, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %34
%45 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %34
%48 = call i32 (...) @fork()
store i32 %48, i32* %10
%49 = load i32, i32* %10
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %74
51: ; preds = %47
%52 = load i32, i32* %7
%53 = load i32, i32* @VT_ACTIVATE
%54 = load i32, i32* %6
%55 = call i64 (i32, i32, ...) @ioctl(i32 %52, i32 %53, i32 %54)
%56 = load i32, i32* %7
%57 = load i32, i32* @VT_WAITACTIVE
%58 = load i32, i32* %6
%59 = call i64 (i32, i32, ...) @ioctl(i32 %56, i32 %57, i32 %58)
%60 = call i32 (...) @setsid()
%61 = call i32 @close(i32 0)
%62 = call i32 @close(i32 1)
%63 = call i32 @close(i32 2)
%64 = load i32, i32* %7
%65 = call i32 @close(i32 %64)
%66 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%67 = load i32, i32* @O_RDWR
%68 = call i32 @open(i8* %66, i32 %67, i32 0)
store i32 %68, i32* %7
%69 = load i32, i32* %7
%70 = call i32 @dup(i32 %69)
%71 = load i32, i32* %7
%72 = call i32 @dup(i32 %71)
%73 = call i32 @execlp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32* null)
br label %74
74: ; preds = %51, %47
%75 = call i32 @wait(i32* %10)
%76 = load i32, i32* %7
%77 = load i32, i32* @VT_ACTIVATE
%78 = getelementptr inbounds %struct.vt_stat, %struct.vt_stat* %8, i32 0, i32 0
%79 = load i32, i32* %78
%80 = call i64 (i32, i32, ...) @ioctl(i32 %76, i32 %77, i32 %79)
%81 = load i32, i32* %7
%82 = load i32, i32* @VT_WAITACTIVE
%83 = getelementptr inbounds %struct.vt_stat, %struct.vt_stat* %8, i32 0, i32 0
%84 = load i32, i32* %83
%85 = call i64 (i32, i32, ...) @ioctl(i32 %81, i32 %82, i32 %84)
%86 = load i32, i32* %7
%87 = load i32, i32* @VT_DISALLOCATE
%88 = load i32, i32* %6
%89 = call i64 (i32, i32, ...) @ioctl(i32 %86, i32 %87, i32 %88)
%90 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.vt_stat = type { i32 }
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@.str.1 = [33 x i8] c"minopen: could not open /dev/tty\00"
@VT_GETSTATE = global i32 0
@.str.2 = [36 x i8] c"minopen: tty is not virtual console\00"
@VT_OPENQRY = global i32 0
@.str.3 = [34 x i8] c"minopen: no free virtual consoles\00"
@.str.4 = [11 x i8] c"/dev/tty%d\00"
@W_OK = global i32 0
@R_OK = global i32 0
@.str.5 = [40 x i8] c"minopen: insufficient permission on tty\00"
@VT_ACTIVATE = global i32 0
@VT_WAITACTIVE = global i32 0
@.str.6 = [10 x i8] c"/bin/bash\00"
@.str.7 = [5 x i8] c"bash\00"
@VT_DISALLOCATE = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.vt_stat
%5 = alloca [32 x i8]
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.vt_stat* %4 to i8*
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%10 = bitcast i32* %6 to i8*
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* @VT_GETSTATE
%19 = call i64 (i32, i32, ...) @ioctl(i32 %12, i32 %18, %struct.vt_stat* %4)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = load i32, i32* @VT_OPENQRY
%26 = call i64 (i32, i32, ...) @ioctl(i32 %12, i32 %25, i32* %3)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %24
%32 = load i32, i32* %3
%33 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = load i32, i32* @W_OK
%35 = load i32, i32* @R_OK
%36 = or i32 %35, %34
%37 = call i64 @access(i8* %9, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %31
%43 = call i32 (...) @fork()
store i32 %43, i32* %6
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %62
45: ; preds = %42
%46 = load i32, i32* @VT_ACTIVATE
%47 = load i32, i32* %3
%48 = call i64 (i32, i32, ...) @ioctl(i32 %12, i32 %46, i32 %47)
%49 = load i32, i32* @VT_WAITACTIVE
%50 = load i32, i32* %3
%51 = call i64 (i32, i32, ...) @ioctl(i32 %12, i32 %49, i32 %50)
%52 = call i32 (...) @setsid()
%53 = call i32 @close(i32 0)
%54 = call i32 @close(i32 1)
%55 = call i32 @close(i32 2)
%56 = call i32 @close(i32 %12)
%57 = load i32, i32* @O_RDWR
%58 = call i32 @open(i8* %9, i32 %57, i32 0)
%59 = call i32 @dup(i32 %58)
%60 = call i32 @dup(i32 %58)
%61 = call i32 @execlp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32* null)
br label %62
62: ; preds = %45, %42
%63 = phi i32 [ %58, %45 ], [ %12, %42 ]
%64 = call i32 @wait(i32* %6)
%65 = load i32, i32* @VT_ACTIVATE
%66 = getelementptr inbounds %struct.vt_stat, %struct.vt_stat* %4, i64 0, i32 0
%67 = load i32, i32* %66
%68 = call i64 (i32, i32, ...) @ioctl(i32 %63, i32 %65, i32 %67)
%69 = load i32, i32* @VT_WAITACTIVE
%70 = load i32, i32* %66
%71 = call i64 (i32, i32, ...) @ioctl(i32 %63, i32 %69, i32 %70)
%72 = load i32, i32* @VT_DISALLOCATE
%73 = load i32, i32* %3
%74 = call i64 (i32, i32, ...) @ioctl(i32 %63, i32 %72, i32 %73)
%75 = call i32 @exit(i32 0)
unreachable
}