#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;
  int c_lflag;
  int *c_cc;
  int /*<<< orphan*/ c_cflag;
  int /*<<< orphan*/ c_oflag;
};

/* Variables and functions */
int BRKINT;
int /*<<< orphan*/ CS8;
int ECHO;
int ICANON;
int ICRNL;
int IEXTEN;
int INPCK;
int ISIG;
int ISTRIP;
int IXON;
int /*<<< orphan*/ OPOST;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
bool ttyrawmode(bool enable) {
  static bool enabled = false;
  static struct termios orig_termios;
  if (enabled && !enable) {
    tcsetattr(STDOUT_FILENO, TCSAFLUSH, &orig_termios);
  } else if (!enabled && enable) {
    if (tcgetattr(STDOUT_FILENO, &orig_termios) < 0)
      goto done;
    struct termios raw = orig_termios;
    raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
    raw.c_oflag &= ~(OPOST);
    raw.c_cflag |= (CS8);
    raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
    raw.c_cc[VMIN] = 0;
    raw.c_cc[VTIME] = 1;
    if (tcsetattr(STDOUT_FILENO, TCSAFLUSH, &raw) < 0)
      goto done;
    enabled = true;
  }
done:
  return enabled;
}


x86 assembly (clang, -O0):
<ttyrawmode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,0x2f06(%rip)
je     1165 <ttyrawmode+0x45>
cmpl   $0x0,-0x4(%rbp)
jne    1165 <ttyrawmode+0x45>
mov    0x2ea7(%rip),%rax
mov    (%rax),%edi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0x2ee5(%rip),%rdx
call   1040 <tcsetattr@plt>
jmp    12a8 <ttyrawmode+0x188>
cmpl   $0x0,0x2ecc(%rip)
jne    12a3 <ttyrawmode+0x183>
cmpl   $0x0,-0x4(%rbp)
je     12a3 <ttyrawmode+0x183>
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
lea    0x2eb4(%rip),%rsi
call   1030 <tcgetattr@plt>
cmp    $0x0,%rax
jge    11a0 <ttyrawmode+0x80>
jmp    12ad <ttyrawmode+0x18d>
mov    0x2e99(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e96(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e93(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    0x2e0f(%rip),%rcx
or     (%rcx),%eax
mov    0x2dae(%rip),%rcx
or     (%rcx),%eax
mov    0x2d9d(%rip),%rcx
or     (%rcx),%eax
mov    0x2ddc(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    0x2d48(%rip),%rcx
or     (%rcx),%eax
mov    0x2d67(%rip),%rcx
or     (%rcx),%eax
mov    0x2d7e(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d50(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2d13(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1299 <ttyrawmode+0x179>
jmp    12ad <ttyrawmode+0x18d>
movl   $0x1,0x2d95(%rip)
jmp    12a8 <ttyrawmode+0x188>
jmp    12ad <ttyrawmode+0x18d>
mov    0x2d85(%rip),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyrawmode>:
push   %rbx
sub    $0x20,%rsp
mov    0x2f0d(%rip),%al
xor    $0x1,%al
test   %edi,%edi
jne    1158 <ttyrawmode+0x38>
test   $0x1,%al
jne    1158 <ttyrawmode+0x38>
mov    0x2eb4(%rip),%rax
mov    (%rax),%edi
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
lea    0x2ef2(%rip),%rdx
call   1040 <tcsetattr@plt>
jmp    125c <ttyrawmode+0x13c>
test   %edi,%edi
sete   %cl
not    %al
or     %cl,%al
test   $0x1,%al
jne    125c <ttyrawmode+0x13c>
mov    0x2e80(%rip),%rbx
mov    (%rbx),%edi
lea    0x2ec7(%rip),%rsi
call   1030 <tcgetattr@plt>
test   %rax,%rax
js     125c <ttyrawmode+0x13c>
movups 0x2eb2(%rip),%xmm0
mov    0x2e1b(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
movaps %xmm0,(%rsp)
mov    0x2ddd(%rip),%rax
or     (%rax),%ecx
mov    0x2e9c(%rip),%rax
mov    0x2dc5(%rip),%rdx
or     (%rdx),%ecx
mov    %rax,0x10(%rsp)
mov    0x2dff(%rip),%rdx
or     (%rdx),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x2df9(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %ecx,0x14(%rsp)
mov    0x2dc2(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd1(%rip),%rcx
mov    0x2d7a(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d97(%rip),%rcx
or     (%rcx),%edx
mov    %eax,0x10(%rsp)
mov    0x2daa(%rip),%rax
or     (%rax),%edx
not    %edx
and    %edx,0x4(%rsp)
mov    0x8(%rsp),%rax
mov    0x2d7e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d45(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    (%rbx),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1040 <tcsetattr@plt>
test   %rax,%rax
js     125c <ttyrawmode+0x13c>
movb   $0x1,0x2ddc(%rip)
movzbl 0x2dd5(%rip),%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i32, i32 }
@ttyrawmode.enabled = internal global i32 0
@ttyrawmode.orig_termios = internal global %struct.termios zeroinitializer
@STDOUT_FILENO = global i32 0
@TCSAFLUSH = global i32 0
@BRKINT = global i32 0
@ICRNL = global i32 0
@INPCK = global i32 0
@ISTRIP = global i32 0
@IXON = global i32 0
@OPOST = global i32 0
@CS8 = global i32 0
@ECHO = global i32 0
@ICANON = global i32 0
@IEXTEN = global i32 0
@ISIG = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
define i32 @ttyrawmode(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.termios
store i32 %0, i32* %2
%4 = load i32, i32* @ttyrawmode.enabled
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %1
%7 = load i32, i32* %2
%8 = icmp ne i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = load i32, i32* @STDOUT_FILENO
%11 = load i32, i32* @TCSAFLUSH
%12 = call i64 @tcsetattr(i32 %10, i32 %11, %struct.termios* @ttyrawmode.orig_termios)
br label %74
13: ; preds = %6, %1
%14 = load i32, i32* @ttyrawmode.enabled
%15 = icmp ne i32 %14, 0
br i1 %15, label %73, label %16
16: ; preds = %13
%17 = load i32, i32* %2
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %73
19: ; preds = %16
%20 = load i32, i32* @STDOUT_FILENO
%21 = call i64 @tcgetattr(i32 %20, %struct.termios* @ttyrawmode.orig_termios)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
br label %75
24: ; preds = %19
%25 = bitcast %struct.termios* %3 to i8*
%26 = load i32, i32* @BRKINT
%27 = load i32, i32* @ICRNL
%28 = or i32 %26, %27
%29 = load i32, i32* @INPCK
%30 = or i32 %28, %29
%31 = load i32, i32* @ISTRIP
%32 = or i32 %30, %31
%33 = load i32, i32* @IXON
%34 = or i32 %32, %33
%35 = xor i32 %34, -1
%36 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%37 = load i32, i32* %36
%38 = and i32 %37, %35
store i32 %38, i32* %36
%39 = load i32, i32* @OPOST
%40 = xor i32 %39, -1
%41 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 4
%42 = load i32, i32* %41
%43 = and i32 %42, %40
store i32 %43, i32* %41
%44 = load i32, i32* @CS8
%45 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 3
%46 = load i32, i32* %45
%47 = or i32 %46, %44
store i32 %47, i32* %45
%48 = load i32, i32* @ECHO
%49 = load i32, i32* @ICANON
%50 = or i32 %48, %49
%51 = load i32, i32* @IEXTEN
%52 = or i32 %50, %51
%53 = load i32, i32* @ISIG
%54 = or i32 %52, %53
%55 = xor i32 %54, -1
%56 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%57 = load i32, i32* %56
%58 = and i32 %57, %55
store i32 %58, i32* %56
%59 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 2
%60 = load i32*, i32** %59
%61 = load i64, i64* @VMIN
%62 = getelementptr inbounds i32, i32* %60, i64 %61
store i32 0, i32* %62
%63 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 2
%64 = load i32*, i32** %63
%65 = load i64, i64* @VTIME
%66 = getelementptr inbounds i32, i32* %64, i64 %65
store i32 1, i32* %66
%67 = load i32, i32* @STDOUT_FILENO
%68 = load i32, i32* @TCSAFLUSH
%69 = call i64 @tcsetattr(i32 %67, i32 %68, %struct.termios* %3)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %24
br label %75
72: ; preds = %24
store i32 1, i32* @ttyrawmode.enabled
br label %73
73: ; preds = %72, %16, %13
br label %74
74: ; preds = %73, %9
br label %75
75: ; preds = %74, %71, %23
%76 = load i32, i32* @ttyrawmode.enabled
ret i32 %76
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i32, i32 }
@ttyrawmode.enabled = internal unnamed_addr global i1 false
@ttyrawmode.orig_termios = internal global %struct.termios zeroinitializer
@STDOUT_FILENO = global i32 0
@TCSAFLUSH = global i32 0
@BRKINT = global i32 0
@ICRNL = global i32 0
@INPCK = global i32 0
@ISTRIP = global i32 0
@IXON = global i32 0
@OPOST = global i32 0
@CS8 = global i32 0
@ECHO = global i32 0
@ICANON = global i32 0
@IEXTEN = global i32 0
@ISIG = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
define i32 @ttyrawmode(i32 %0) {
%2 = alloca %struct.termios
%3 = load i1, i1* @ttyrawmode.enabled
%4 = xor i1 %3, true
%5 = icmp ne i32 %0, 0
%6 = or i1 %5, %4
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = load i32, i32* @STDOUT_FILENO
%9 = load i32, i32* @TCSAFLUSH
%10 = tail call i64 @tcsetattr(i32 %8, i32 %9, %struct.termios* @ttyrawmode.orig_termios)
br label %64
11: ; preds = %1
%12 = and i1 %5, %4
br i1 %12, label %13, label %64
13: ; preds = %11
%14 = load i32, i32* @STDOUT_FILENO
%15 = tail call i64 @tcgetattr(i32 %14, %struct.termios* @ttyrawmode.orig_termios)
%16 = icmp slt i64 %15, 0
br i1 %16, label %64, label %17
17: ; preds = %13
%18 = bitcast %struct.termios* %2 to i8*
%19 = load i32, i32* @BRKINT
%20 = load i32, i32* @ICRNL
%21 = or i32 %20, %19
%22 = load i32, i32* @INPCK
%23 = or i32 %21, %22
%24 = load i32, i32* @ISTRIP
%25 = or i32 %23, %24
%26 = load i32, i32* @IXON
%27 = or i32 %25, %26
%28 = xor i32 %27, -1
%29 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%30 = load i32, i32* %29
%31 = and i32 %30, %28
store i32 %31, i32* %29
%32 = load i32, i32* @OPOST
%33 = xor i32 %32, -1
%34 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 4
%35 = load i32, i32* %34
%36 = and i32 %35, %33
store i32 %36, i32* %34
%37 = load i32, i32* @CS8
%38 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
%39 = load i32, i32* %38
%40 = or i32 %39, %37
store i32 %40, i32* %38
%41 = load i32, i32* @ECHO
%42 = load i32, i32* @ICANON
%43 = or i32 %42, %41
%44 = load i32, i32* @IEXTEN
%45 = or i32 %43, %44
%46 = load i32, i32* @ISIG
%47 = or i32 %45, %46
%48 = xor i32 %47, -1
%49 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%50 = load i32, i32* %49
%51 = and i32 %50, %48
store i32 %51, i32* %49
%52 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%53 = load i32*, i32** %52
%54 = load i64, i64* @VMIN
%55 = getelementptr inbounds i32, i32* %53, i64 %54
store i32 0, i32* %55
%56 = load i64, i64* @VTIME
%57 = getelementptr inbounds i32, i32* %53, i64 %56
store i32 1, i32* %57
%58 = load i32, i32* @STDOUT_FILENO
%59 = load i32, i32* @TCSAFLUSH
%60 = call i64 @tcsetattr(i32 %58, i32 %59, %struct.termios* %2)
%61 = icmp slt i64 %60, 0
br i1 %61, label %63, label %62
62: ; preds = %17
store i1 true, i1* @ttyrawmode.enabled
br label %63
63: ; preds = %17, %62
br label %64
64: ; preds = %63, %7, %11, %13
%65 = load i1, i1* @ttyrawmode.enabled
%66 = zext i1 %65 to i32
ret i32 %66
}