#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*/ TCSANOW;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
void io_changemode(int dir) {
  static struct termios oldt, newt;
  if (dir == 1) {
    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  } else
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}


x86 assembly (clang, -O0):
<io_changemode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    119a <io_changemode+0x7a>
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
lea    0x2eef(%rip),%rsi
call   1030 <tcgetattr@plt>
mov    0x2ee4(%rip),%eax
mov    %eax,0x2ee2(%rip)
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    0x2e7a(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    0x2ec7(%rip),%eax
mov    %eax,0x2ec1(%rip)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    0x2ea8(%rip),%rdx
call   1040 <tcsetattr@plt>
jmp    11b8 <io_changemode+0x98>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
lea    0x2e81(%rip),%rdx
call   1040 <tcsetattr@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<io_changemode>:
push   %rbx
mov    %edi,%eax
mov    0x2ea6(%rip),%rbx
mov    (%rbx),%edi
cmp    $0x1,%eax
jne    1175 <io_changemode+0x55>
lea    0x2efc(%rip),%rsi
call   1030 <tcgetattr@plt>
mov    0x2e84(%rip),%rax
mov    0x2e95(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x2edd(%rip),%ecx
mov    %ecx,0x2edb(%rip)
lea    0x2ed4(%rip),%rdx
mov    (%rbx),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
pop    %rbx
jmp    1040 <tcsetattr@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
lea    0x2eaf(%rip),%rdx
pop    %rbx
jmp    1040 <tcsetattr@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@io_changemode.oldt = internal global %struct.termios zeroinitializer
@io_changemode.newt = internal global %struct.termios zeroinitializer
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define void @io_changemode(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* %2
%4 = icmp eq i32 %3, 1
br i1 %4, label %5, label %17
5: ; preds = %1
%6 = load i32, i32* @STDIN_FILENO
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* @io_changemode.oldt)
%8 = load i32, i32* @ICANON
%9 = load i32, i32* @ECHO
%10 = or i32 %8, %9
%11 = xor i32 %10, -1
%12 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @io_changemode.newt, i32 0, i32 0)
%13 = and i32 %12, %11
store i32 %13, i32* getelementptr inbounds (%struct.termios, %struct.termios* @io_changemode.newt, i32 0, i32 0)
%14 = load i32, i32* @STDIN_FILENO
%15 = load i32, i32* @TCSANOW
%16 = call i32 @tcsetattr(i32 %14, i32 %15, %struct.termios* @io_changemode.newt)
br label %21
17: ; preds = %1
%18 = load i32, i32* @STDIN_FILENO
%19 = load i32, i32* @TCSANOW
%20 = call i32 @tcsetattr(i32 %18, i32 %19, %struct.termios* @io_changemode.oldt)
br label %21
21: ; preds = %17, %5
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@io_changemode.oldt = internal global %struct.termios zeroinitializer
@io_changemode.newt = internal global %struct.termios zeroinitializer
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define void @io_changemode(i32 %0) {
%2 = icmp eq i32 %0, 1
%3 = load i32, i32* @STDIN_FILENO
br i1 %2, label %4, label %15
4: ; preds = %1
%5 = tail call i32 @tcgetattr(i32 %3, %struct.termios* @io_changemode.oldt)
%6 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @io_changemode.oldt, i64 0, i32 0)
%7 = load i32, i32* @ICANON
%8 = load i32, i32* @ECHO
%9 = or i32 %8, %7
%10 = xor i32 %9, -1
%11 = and i32 %6, %10
store i32 %11, i32* getelementptr inbounds (%struct.termios, %struct.termios* @io_changemode.newt, i64 0, i32 0)
%12 = load i32, i32* @STDIN_FILENO
%13 = load i32, i32* @TCSANOW
%14 = tail call i32 @tcsetattr(i32 %12, i32 %13, %struct.termios* @io_changemode.newt)
br label %18
15: ; preds = %1
%16 = load i32, i32* @TCSANOW
%17 = tail call i32 @tcsetattr(i32 %3, i32 %16, %struct.termios* @io_changemode.oldt)
br label %18
18: ; preds = %15, %4
ret void
}