#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*/ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
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)
    error(1, 0, "Failed to open console in container");
  ioctl(console, TIOCSCTTY, NULL);
  if (tcgetattr(console, &termios) < 0)
    error(1, 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   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11c4 <setconsole+0x44>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe41(%rip),%rdx
call   1070 <error@plt>
mov    -0xc(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <ioctl@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1209 <setconsole+0x89>
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe20(%rip),%rdx
call   1070 <error@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    0x2daf(%rip),%rcx
or     (%rcx),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0xc(%rbp),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%eax
mov    0x2d2d(%rip),%rcx
cmp    (%rcx),%eax
je     12b1 <setconsole+0x131>
mov    -0xc(%rbp),%eax
mov    0x2d6b(%rip),%rcx
cmp    (%rcx),%eax
je     12ac <setconsole+0x12c>
mov    -0xc(%rbp),%eax
mov    0x2d21(%rip),%rcx
cmp    (%rcx),%eax
je     12a7 <setconsole+0x127>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    12ac <setconsole+0x12c>
jmp    12b1 <setconsole+0x131>
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   10a0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11bc <setconsole+0x3c>
lea    0xe50(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <error@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1080 <tcgetattr@plt>
test   %rax,%rax
jns    11f7 <setconsole+0x77>
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
lea    0xe37(%rip),%rdx
mov    $0x1,%edi
call   1070 <error@plt>
mov    0x2db2(%rip),%rax
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     %ecx,(%rsp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1090 <tcsetattr@plt>
mov    0x2d7a(%rip),%r12
mov    (%r12),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    0x2db8(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    0x2d6f(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
cmp    (%r12),%ebx
je     126a <setconsole+0xea>
cmp    (%r14),%ebx
je     126a <setconsole+0xea>
cmp    (%r15),%ebx
je     126a <setconsole+0xea>
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 @error(i32 1, 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 @error(i32 1, 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 @error(i32 1, 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 @error(i32 1, 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
}