#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 termios {
  int c_iflag;
};

/* Variables and functions */
int IGNBRK;
int IUTF8;
int /*<<< orphan*/ O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCSCTTY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(int /*<<< orphan*/, char *);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
scalar_t__ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void setconsole(char *name) {
  int console;
  struct termios termios;
  setsid();
  if ((console = open(name, O_RDWR)) < 0)
    die(0, "Failed to open console in container");
  ioctl(console, TIOCSCTTY, NULL);
  if (tcgetattr(console, &termios) < 0)
    die(errno, "tcgetattr");
  termios.c_iflag |= IGNBRK | IUTF8;
  tcsetattr(console, TCSANOW, &termios);
  dup2(console, STDIN_FILENO);
  dup2(console, STDOUT_FILENO);
  dup2(console, STDERR_FILENO);
  if (console != STDIN_FILENO)
    if (console != STDOUT_FILENO)
      if (console != STDERR_FILENO)
        close(console);
}


x86 assembly (clang, -O0):
<setconsole>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1060 <setsid@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11bf <setconsole+0x3f>
xor    %edi,%edi
lea    0xe46(%rip),%rsi
call   10a0 <die@plt>
mov    -0xc(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <ioctl@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
jge    11ff <setconsole+0x7f>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   10a0 <die@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    0x2db9(%rip),%rcx
or     (%rcx),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    -0xc(%rbp),%edi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%eax
mov    0x2d37(%rip),%rcx
cmp    (%rcx),%eax
je     12a7 <setconsole+0x127>
mov    -0xc(%rbp),%eax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%eax
je     12a2 <setconsole+0x122>
mov    -0xc(%rbp),%eax
mov    0x2d2b(%rip),%rcx
cmp    (%rcx),%eax
je     129d <setconsole+0x11d>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    12a2 <setconsole+0x122>
jmp    12a7 <setconsole+0x127>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setconsole>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11b7 <setconsole+0x37>
lea    0xe50(%rip),%rsi
xor    %edi,%edi
call   10a0 <die@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1070 <tcgetattr@plt>
test   %rax,%rax
jns    11ed <setconsole+0x6d>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   10a0 <die@plt>
mov    0x2dbc(%rip),%rax
mov    0x2dcd(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     %ecx,(%rsp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1080 <tcsetattr@plt>
mov    0x2d84(%rip),%r12
mov    (%r12),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    0x2dc2(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    0x2d79(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
cmp    (%r12),%ebx
je     1260 <setconsole+0xe0>
cmp    (%r14),%ebx
je     1260 <setconsole+0xe0>
cmp    (%r15),%ebx
je     1260 <setconsole+0xe0>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@.str = [36 x i8] c"Failed to open console in container\00"
@TIOCSCTTY = global i32 0
@errno = global i32 0
@.str.1 = [10 x i8] c"tcgetattr\00"
@IGNBRK = global i32 0
@IUTF8 = global i32 0
@TCSANOW = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @setconsole(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.termios
store i8* %0, i8** %2
%5 = call i32 (...) @setsid()
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @die(i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = load i32, i32* %3
%14 = load i32, i32* @TIOCSCTTY
%15 = call i32 @ioctl(i32 %13, i32 %14, i32* null)
%16 = load i32, i32* %3
%17 = call i64 @tcgetattr(i32 %16, %struct.termios* %4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32, i32* @errno
%21 = call i32 @die(i32 %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %19, %12
%23 = load i32, i32* @IGNBRK
%24 = load i32, i32* @IUTF8
%25 = or i32 %23, %24
%26 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%27 = load i32, i32* %26
%28 = or i32 %27, %25
store i32 %28, i32* %26
%29 = load i32, i32* %3
%30 = load i32, i32* @TCSANOW
%31 = call i32 @tcsetattr(i32 %29, i32 %30, %struct.termios* %4)
%32 = load i32, i32* %3
%33 = load i32, i32* @STDIN_FILENO
%34 = call i32 @dup2(i32 %32, i32 %33)
%35 = load i32, i32* %3
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @dup2(i32 %35, i32 %36)
%38 = load i32, i32* %3
%39 = load i32, i32* @STDERR_FILENO
%40 = call i32 @dup2(i32 %38, i32 %39)
%41 = load i32, i32* %3
%42 = load i32, i32* @STDIN_FILENO
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %57
44: ; preds = %22
%45 = load i32, i32* %3
%46 = load i32, i32* @STDOUT_FILENO
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %56
48: ; preds = %44
%49 = load i32, i32* %3
%50 = load i32, i32* @STDERR_FILENO
%51 = icmp ne i32 %49, %50
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = load i32, i32* %3
%54 = call i32 @close(i32 %53)
br label %55
55: ; preds = %52, %48
br label %56
56: ; preds = %55, %44
br label %57
57: ; preds = %56, %22
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@.str = [36 x i8] c"Failed to open console in container\00"
@TIOCSCTTY = global i32 0
@errno = global i32 0
@.str.1 = [10 x i8] c"tcgetattr\00"
@IGNBRK = global i32 0
@IUTF8 = global i32 0
@TCSANOW = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @setconsole(i8* nocapture %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = tail call i32 (...) @setsid()
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @die(i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = load i32, i32* @TIOCSCTTY
%12 = tail call i32 @ioctl(i32 %6, i32 %11, i32* null)
%13 = call i64 @tcgetattr(i32 %6, %struct.termios* %2)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @errno
%17 = call i32 @die(i32 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %15, %10
%19 = load i32, i32* @IGNBRK
%20 = load i32, i32* @IUTF8
%21 = or i32 %20, %19
%22 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%23 = load i32, i32* %22
%24 = or i32 %21, %23
store i32 %24, i32* %22
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 %6, i32 %25, %struct.termios* %2)
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @dup2(i32 %6, i32 %27)
%29 = load i32, i32* @STDOUT_FILENO
%30 = call i32 @dup2(i32 %6, i32 %29)
%31 = load i32, i32* @STDERR_FILENO
%32 = call i32 @dup2(i32 %6, i32 %31)
%33 = load i32, i32* @STDIN_FILENO
%34 = icmp eq i32 %6, %33
%35 = load i32, i32* @STDOUT_FILENO
%36 = icmp eq i32 %6, %35
%37 = select i1 %34, i1 true, i1 %36
%38 = load i32, i32* @STDERR_FILENO
%39 = icmp eq i32 %6, %38
%40 = select i1 %37, i1 true, i1 %39
br i1 %40, label %43, label %41
41: ; preds = %18
%42 = call i32 @close(i32 %6)
br label %43
43: ; preds = %41, %18
ret void
}