#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 dummy;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
scalar_t__ isatty(int);
int /*<<< orphan*/ kwboot_printv(char *, char, char);
int kwboot_term_pipe(int, int, char *, int *);
int /*<<< orphan*/ perror(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int kwboot_terminal(int tty) {
  int rc, in, s;
  char *quit = "\34c";
  struct termios otio, tio;
  rc = -1;
  in = STDIN_FILENO;
  if (isatty(in)) {
    rc = tcgetattr(in, &otio);
    if (!rc) {
      tio = otio;
      cfmakeraw(&tio);
      rc = tcsetattr(in, TCSANOW, &tio);
    }
    if (rc) {
      perror("tcsetattr");
      goto out;
    }
    kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n", quit[0] | 0100, quit[1]);
  } else
    in = -1;
  rc = 0;
  s = 0;
  do {
    fd_set rfds;
    int nfds = 0;
    FD_SET(tty, &rfds);
    nfds = nfds < tty ? tty : nfds;
    if (in >= 0) {
      FD_SET(in, &rfds);
      nfds = nfds < in ? in : nfds;
    }
    nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
    if (nfds < 0)
      break;
    if (FD_ISSET(tty, &rfds)) {
      rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
      if (rc)
        break;
    }
    if (FD_ISSET(in, &rfds)) {
      rc = kwboot_term_pipe(in, tty, quit, &s);
      if (rc)
        break;
    }
  } while (quit[s] != 0);
  tcsetattr(in, TCSANOW, &otio);
out:
  return rc;
}


x86 assembly (clang, -O0):
<kwboot_terminal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe4e(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0xffffffff,-0x8(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
je     125e <kwboot_terminal+0xbe>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    121b <kwboot_terminal+0x7b>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
call   1070 <cfmakeraw@plt>
mov    -0xc(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1236 <kwboot_terminal+0x96>
lea    0xdd7(%rip),%rdi
call   10a0 <perror@plt>
jmp    13c2 <kwboot_terminal+0x222>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
or     $0x40,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
lea    0xdc0(%rip),%rdi
movsbl %cl,%esi
movsbl 0x1(%rax),%edx
call   1050 <kwboot_printv@plt>
jmp    1265 <kwboot_terminal+0xc5>
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x30(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    129d <kwboot_terminal+0xfd>
mov    -0x4(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    12a3 <kwboot_terminal+0x103>
mov    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     12e2 <kwboot_terminal+0x142>
mov    -0xc(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x30(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12d6 <kwboot_terminal+0x136>
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    12dc <kwboot_terminal+0x13c>
mov    -0x30(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1060 <select@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    130e <kwboot_terminal+0x16e>
jmp    13ad <kwboot_terminal+0x20d>
mov    -0x4(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1353 <kwboot_terminal+0x1b3>
mov    -0x4(%rbp),%edi
mov    0x2cc2(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   10c0 <kwboot_term_pipe@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     134e <kwboot_terminal+0x1ae>
jmp    13ad <kwboot_terminal+0x20d>
jmp    1353 <kwboot_terminal+0x1b3>
mov    -0xc(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1393 <kwboot_terminal+0x1f3>
mov    -0xc(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    -0x10(%rbp),%rcx
call   10c0 <kwboot_term_pipe@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     138e <kwboot_terminal+0x1ee>
jmp    13ad <kwboot_terminal+0x20d>
jmp    1393 <kwboot_terminal+0x1f3>
jmp    1398 <kwboot_terminal+0x1f8>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1273 <kwboot_terminal+0xd3>
mov    -0xc(%rbp),%edi
mov    0x2c29(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0x8(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kwboot_terminal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    0x2e18(%rip),%rax
mov    (%rax),%r13d
mov    %r13d,%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     1226 <kwboot_terminal+0x86>
lea    0x8(%rsp),%rsi
mov    %r13d,%edi
call   1080 <tcgetattr@plt>
test   %eax,%eax
jne    1319 <kwboot_terminal+0x179>
mov    0x8(%rsp),%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <cfmakeraw@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
mov    %rbx,%rdx
call   1090 <tcsetattr@plt>
test   %eax,%eax
jne    1319 <kwboot_terminal+0x179>
lea    0xdf8(%rip),%rdi
mov    $0x5c,%esi
mov    $0x63,%edx
call   1050 <kwboot_printv@plt>
jmp    122c <kwboot_terminal+0x8c>
mov    $0xffffffff,%r13d
xor    %r15d,%r15d
test   %r14d,%r14d
cmovns %r14d,%r15d
movl   $0x0,0xc(%rsp)
cmp    %r13d,%r15d
mov    %r15d,%r12d
cmovb  %r13d,%r12d
lea    0x14(%rsp),%rbp
jmp    125b <kwboot_terminal+0xbb>
nop
cmpl   $0x2,0xc(%rsp)
je     12f0 <kwboot_terminal+0x150>
mov    %r14d,%edi
mov    %rbp,%rsi
call   10b0 <FD_SET@plt>
mov    %r15d,%edi
test   %r13d,%r13d
js     127c <kwboot_terminal+0xdc>
mov    %r13d,%edi
mov    %rbp,%rsi
call   10b0 <FD_SET@plt>
mov    %r12d,%edi
add    $0x1,%edi
xor    %ebx,%ebx
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <select@plt>
test   %eax,%eax
js     12f2 <kwboot_terminal+0x152>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12bd <kwboot_terminal+0x11d>
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   10c0 <kwboot_term_pipe@plt>
test   %eax,%eax
jne    12ec <kwboot_terminal+0x14c>
mov    %r13d,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1250 <kwboot_terminal+0xb0>
mov    %r13d,%edi
mov    %r14d,%esi
lea    0xd26(%rip),%rdx
lea    0xc(%rsp),%rcx
call   10c0 <kwboot_term_pipe@plt>
test   %eax,%eax
je     1250 <kwboot_terminal+0xb0>
mov    %eax,%ebx
jmp    12f2 <kwboot_terminal+0x152>
xor    %ebx,%ebx
mov    0x2ce7(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r13d,%edi
call   1090 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
lea    0xce1(%rip),%rdi
call   10a0 <perror@plt>
jmp    1308 <kwboot_terminal+0x168>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [3 x i8] c"\1Cc\00"
@STDIN_FILENO = global i32 0
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
@.str.2 = [30 x i8] c"[Type Ctrl-%c + %c to quit]\0D\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @kwboot_terminal(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.termios
%8 = alloca %struct.termios
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %6
store i32 -1, i32* %3
%11 = load i32, i32* @STDIN_FILENO
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = call i64 @isatty(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %43
15: ; preds = %1
%16 = load i32, i32* %4
%17 = call i32 @tcgetattr(i32 %16, %struct.termios* %7)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp ne i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %15
%21 = bitcast %struct.termios* %8 to i8*
%22 = bitcast %struct.termios* %7 to i8*
%23 = call i32 @cfmakeraw(%struct.termios* %8)
%24 = load i32, i32* %4
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 %24, i32 %25, %struct.termios* %8)
store i32 %26, i32* %3
br label %27
27: ; preds = %20, %15
%28 = load i32, i32* %3
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %116
32: ; preds = %27
%33 = load i8*, i8** %6
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = or i32 %36, 64
%38 = trunc i32 %37 to i8
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = load i8, i8* %40
%42 = call i32 @kwboot_printv(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8 signext %38, i8 signext %41)
br label %44
43: ; preds = %1
store i32 -1, i32* %4
br label %44
44: ; preds = %43, %32
store i32 0, i32* %3
store i32 0, i32* %5
br label %45
45: ; preds = %104, %44
store i32 0, i32* %10
%46 = load i32, i32* %2
%47 = call i32 @FD_SET(i32 %46, i32* %9)
%48 = load i32, i32* %10
%49 = load i32, i32* %2
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = load i32, i32* %2
br label %55
53: ; preds = %45
%54 = load i32, i32* %10
br label %55
55: ; preds = %53, %51
%56 = phi i32 [ %52, %51 ], [ %54, %53 ]
store i32 %56, i32* %10
%57 = load i32, i32* %4
%58 = icmp sge i32 %57, 0
br i1 %58, label %59, label %71
59: ; preds = %55
%60 = load i32, i32* %4
%61 = call i32 @FD_SET(i32 %60, i32* %9)
%62 = load i32, i32* %10
%63 = load i32, i32* %4
%64 = icmp slt i32 %62, %63
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = load i32, i32* %4
br label %69
67: ; preds = %59
%68 = load i32, i32* %10
br label %69
69: ; preds = %67, %65
%70 = phi i32 [ %66, %65 ], [ %68, %67 ]
store i32 %70, i32* %10
br label %71
71: ; preds = %69, %55
%72 = load i32, i32* %10
%73 = add nsw i32 %72, 1
%74 = call i32 @select(i32 %73, i32* %9, i32* null, i32* null, i32* null)
store i32 %74, i32* %10
%75 = load i32, i32* %10
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %71
br label %112
78: ; preds = %71
%79 = load i32, i32* %2
%80 = call i64 @FD_ISSET(i32 %79, i32* %9)
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %90
82: ; preds = %78
%83 = load i32, i32* %2
%84 = load i32, i32* @STDOUT_FILENO
%85 = call i32 @kwboot_term_pipe(i32 %83, i32 %84, i8* null, i32* null)
store i32 %85, i32* %3
%86 = load i32, i32* %3
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %89
88: ; preds = %82
br label %112
89: ; preds = %82
br label %90
90: ; preds = %89, %78
%91 = load i32, i32* %4
%92 = call i64 @FD_ISSET(i32 %91, i32* %9)
%93 = icmp ne i64 %92, 0
br i1 %93, label %94, label %103
94: ; preds = %90
%95 = load i32, i32* %4
%96 = load i32, i32* %2
%97 = load i8*, i8** %6
%98 = call i32 @kwboot_term_pipe(i32 %95, i32 %96, i8* %97, i32* %5)
store i32 %98, i32* %3
%99 = load i32, i32* %3
%100 = icmp ne i32 %99, 0
br i1 %100, label %101, label %102
101: ; preds = %94
br label %112
102: ; preds = %94
br label %103
103: ; preds = %102, %90
br label %104
104: ; preds = %103
%105 = load i8*, i8** %6
%106 = load i32, i32* %5
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds i8, i8* %105, i64 %107
%109 = load i8, i8* %108
%110 = sext i8 %109 to i32
%111 = icmp ne i32 %110, 0
br i1 %111, label %45, label %112
112: ; preds = %104, %101, %88, %77
%113 = load i32, i32* %4
%114 = load i32, i32* @TCSANOW
%115 = call i32 @tcsetattr(i32 %113, i32 %114, %struct.termios* %7)
br label %116
116: ; preds = %112, %30
%117 = load i32, i32* %3
ret i32 %117
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [3 x i8] c"\1Cc\00"
@STDIN_FILENO = global i32 0
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
@.str.2 = [30 x i8] c"[Type Ctrl-%c + %c to quit]\0D\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @kwboot_terminal(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca i32
%6 = bitcast i32* %2 to i8*
%7 = bitcast %struct.termios* %3 to i8*
%8 = bitcast %struct.termios* %4 to i8*
%9 = load i32, i32* @STDIN_FILENO
%10 = tail call i64 @isatty(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %28, label %12
12: ; preds = %1
%13 = call i32 @tcgetattr(i32 %9, %struct.termios* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%17 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%18 = load i32, i32* %16
store i32 %18, i32* %17
%19 = call i32 @cfmakeraw(%struct.termios* %4)
%20 = load i32, i32* @TCSANOW
%21 = call i32 @tcsetattr(i32 %9, i32 %20, %struct.termios* %4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %12, %15
%24 = phi i32 [ %21, %15 ], [ %13, %12 ]
%25 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %67
26: ; preds = %15
%27 = call i32 @kwboot_printv(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8 signext 92, i8 signext 99)
br label %28
28: ; preds = %1, %26
%29 = phi i32 [ %9, %26 ], [ -1, %1 ]
store i32 0, i32* %2
%30 = bitcast i32* %5 to i8*
%31 = icmp sgt i32 %0, 0
%32 = select i1 %31, i32 %0, i32 0
%33 = icmp sgt i32 %29, -1
%34 = icmp ult i32 %32, %29
%35 = select i1 %34, i32 %29, i32 %32
br label %36
36: ; preds = %60, %28
%37 = call i32 @FD_SET(i32 %0, i32* %5)
br i1 %33, label %38, label %40
38: ; preds = %36
%39 = call i32 @FD_SET(i32 %29, i32* %5)
br label %40
40: ; preds = %38, %36
%41 = phi i32 [ %35, %38 ], [ %32, %36 ]
%42 = add nuw nsw i32 %41, 1
%43 = call i32 @select(i32 %42, i32* %5, i32* null, i32* null, i32* null)
%44 = icmp slt i32 %43, 0
br i1 %44, label %58, label %45
45: ; preds = %40
%46 = call i64 @FD_ISSET(i32 %0, i32* %5)
%47 = icmp eq i64 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %45
%49 = load i32, i32* @STDOUT_FILENO
%50 = call i32 @kwboot_term_pipe(i32 %0, i32 %49, i8* null, i32* null)
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %48, %45
%53 = call i64 @FD_ISSET(i32 %29, i32* %5)
%54 = icmp eq i64 %53, 0
br i1 %54, label %60, label %55
55: ; preds = %52
%56 = call i32 @kwboot_term_pipe(i32 %29, i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%57 = icmp eq i32 %56, 0
br i1 %57, label %60, label %58
58: ; preds = %40, %48, %55
%59 = phi i32 [ %56, %55 ], [ %50, %48 ], [ 0, %40 ]
br label %63
60: ; preds = %52, %55
%61 = load i32, i32* %2
%62 = icmp eq i32 %61, 2
br i1 %62, label %63, label %36
63: ; preds = %60, %58
%64 = phi i32 [ %59, %58 ], [ 0, %60 ]
%65 = load i32, i32* @TCSANOW
%66 = call i32 @tcsetattr(i32 %29, i32 %65, %struct.termios* %3)
br label %67
67: ; preds = %63, %23
%68 = phi i32 [ %24, %23 ], [ %64, %63 ]
ret i32 %68
}