#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_lflag;
};

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int tcgetattr(int /*<<< orphan*/, struct termios *);
int tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
void nmstermio_set_terminal(int s) {
  struct termios tp;
  static struct termios save;
  static int state = 1;
  if (!isatty(STDIN_FILENO)) {
    stdin = freopen("/dev/tty", "r", stdin);
  }
  if (s == 0) {
    if (tcgetattr(STDIN_FILENO, &tp) == -1) {
      return;
    }
    save = tp;
    tp.c_lflag &= (~ICANON & ~ECHO);
    if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tp) == -1) {
      return;
    }
  } else {
    if (state == 0 && tcsetattr(STDIN_FILENO, TCSANOW, &save) == -1)
      return;
  }
  state = s;
}


x86 assembly (clang, -O0):
<nmstermio_set_terminal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    1189 <nmstermio_set_terminal+0x49>
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
lea    0xe8e(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1040 <freopen@plt>
mov    %eax,%ecx
mov    0x2e59(%rip),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x4(%rbp)
jne    120a <nmstermio_set_terminal+0xca>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    11b3 <nmstermio_set_terminal+0x73>
jmp    1251 <nmstermio_set_terminal+0x111>
mov    -0x8(%rbp),%eax
mov    %eax,0x2e8c(%rip)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
and    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    1205 <nmstermio_set_terminal+0xc5>
jmp    1251 <nmstermio_set_terminal+0x111>
jmp    1248 <nmstermio_set_terminal+0x108>
cmpl   $0x0,0x2e2f(%rip)
jne    1243 <nmstermio_set_terminal+0x103>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
lea    0x2e18(%rip),%rdx
call   1060 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    1243 <nmstermio_set_terminal+0x103>
jmp    1251 <nmstermio_set_terminal+0x111>
jmp    1248 <nmstermio_set_terminal+0x108>
mov    -0x4(%rbp),%eax
mov    %eax,0x2def(%rip)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nmstermio_set_terminal>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2e6e(%rip),%r14
mov    (%r14),%edi
call   1030 <isatty@plt>
test   %eax,%eax
je     1184 <nmstermio_set_terminal+0x44>
test   %ebx,%ebx
je     11a8 <nmstermio_set_terminal+0x68>
cmpl   $0x0,0x2ed7(%rip)
jne    11f9 <nmstermio_set_terminal+0xb9>
mov    (%r14),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    0x2ec6(%rip),%rdx
jmp    11ef <nmstermio_set_terminal+0xaf>
mov    0x2e55(%rip),%r15
mov    (%r15),%edx
lea    0xe6b(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   1040 <freopen@plt>
mov    %eax,(%r15)
test   %ebx,%ebx
jne    1162 <nmstermio_set_terminal+0x22>
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     11ff <nmstermio_set_terminal+0xbf>
mov    0x8(%rsp),%eax
mov    %eax,0x2e84(%rip)
mov    0x2ded(%rip),%rcx
mov    0x2dfe(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
not    %edx
and    %eax,%edx
mov    %edx,0x8(%rsp)
mov    (%r14),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0xffffffff,%eax
je     11ff <nmstermio_set_terminal+0xbf>
mov    %ebx,0x2e41(%rip)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@nmstermio_set_terminal.save = internal global %struct.termios zeroinitializer
@nmstermio_set_terminal.state = internal global i32 1
@STDIN_FILENO = global i32 0
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSAFLUSH = global i32 0
@TCSANOW = global i32 0
define void @nmstermio_set_terminal(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.termios
store i32 %0, i32* %2
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @isatty(i32 %4)
%6 = icmp ne i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i32, i32* @stdin
%9 = call i32 @freopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 %9, i32* @stdin
br label %10
10: ; preds = %7, %1
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %34
13: ; preds = %10
%14 = load i32, i32* @STDIN_FILENO
%15 = call i32 @tcgetattr(i32 %14, %struct.termios* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %13
br label %46
18: ; preds = %13
%19 = bitcast %struct.termios* %3 to i8*
%20 = load i32, i32* @ICANON
%21 = xor i32 %20, -1
%22 = load i32, i32* @ECHO
%23 = xor i32 %22, -1
%24 = and i32 %21, %23
%25 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%26 = load i32, i32* %25
%27 = and i32 %26, %24
store i32 %27, i32* %25
%28 = load i32, i32* @STDIN_FILENO
%29 = load i32, i32* @TCSAFLUSH
%30 = call i32 @tcsetattr(i32 %28, i32 %29, %struct.termios* %3)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %18
br label %46
33: ; preds = %18
br label %44
34: ; preds = %10
%35 = load i32, i32* @nmstermio_set_terminal.state
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = load i32, i32* @STDIN_FILENO
%39 = load i32, i32* @TCSANOW
%40 = call i32 @tcsetattr(i32 %38, i32 %39, %struct.termios* @nmstermio_set_terminal.save)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %37
br label %46
43: ; preds = %37, %34
br label %44
44: ; preds = %43, %33
%45 = load i32, i32* %2
store i32 %45, i32* @nmstermio_set_terminal.state
br label %46
46: ; preds = %44, %42, %32, %17
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@nmstermio_set_terminal.save = internal global %struct.termios zeroinitializer
@nmstermio_set_terminal.state = internal unnamed_addr global i32 1
@STDIN_FILENO = global i32 0
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSAFLUSH = global i32 0
@TCSANOW = global i32 0
define void @nmstermio_set_terminal(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @STDIN_FILENO
%5 = tail call i32 @isatty(i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stdin
%9 = tail call i32 @freopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 %9, i32* @stdin
br label %10
10: ; preds = %7, %1
%11 = icmp eq i32 %0, 0
br i1 %11, label %12, label %28
12: ; preds = %10
%13 = load i32, i32* @STDIN_FILENO
%14 = call i32 @tcgetattr(i32 %13, %struct.termios* %2)
%15 = icmp eq i32 %14, -1
br i1 %15, label %37, label %16
16: ; preds = %12
%17 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%18 = load i32, i32* %17
store i32 %18, i32* getelementptr inbounds (%struct.termios, %struct.termios* @nmstermio_set_terminal.save, i64 0, i32 0)
%19 = load i32, i32* @ICANON
%20 = load i32, i32* @ECHO
%21 = or i32 %20, %19
%22 = xor i32 %21, -1
%23 = and i32 %18, %22
store i32 %23, i32* %17
%24 = load i32, i32* @STDIN_FILENO
%25 = load i32, i32* @TCSAFLUSH
%26 = call i32 @tcsetattr(i32 %24, i32 %25, %struct.termios* %2)
%27 = icmp eq i32 %26, -1
br i1 %27, label %37, label %36
28: ; preds = %10
%29 = load i32, i32* @nmstermio_set_terminal.state
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i32, i32* @STDIN_FILENO
%33 = load i32, i32* @TCSANOW
%34 = tail call i32 @tcsetattr(i32 %32, i32 %33, %struct.termios* @nmstermio_set_terminal.save)
%35 = icmp eq i32 %34, -1
br i1 %35, label %37, label %36
36: ; preds = %28, %31, %16
store i32 %0, i32* @nmstermio_set_terminal.state
br label %37
37: ; preds = %31, %16, %12, %36
ret void
}