#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_cc;
  int c_lflag;
};

/* Variables and functions */
int ECHO;
int ICANON;
int IEXTEN;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
void set_tty_raw(bool raw) {
  struct termios trm;
  tcgetattr(STDIN_FILENO, &trm);
  if (raw) {
    trm.c_cc[VMIN] = 1;
    trm.c_cc[VTIME] = 0;
    trm.c_lflag &= ~(ECHO | ICANON | IEXTEN);
  }
  tcsetattr(STDIN_FILENO, TCSANOW, &trm);
}


x86 assembly (clang, -O0):
<set_tty_raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmpl   $0x0,-0x4(%rbp)
je     1195 <set_tty_raw+0x75>
mov    -0x18(%rbp),%rax
mov    0x2e86(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    0x2e2f(%rip),%rcx
or     (%rcx),%eax
mov    0x2e46(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <tcsetattr@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_tty_raw>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    0x2e88(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1030 <tcgetattr@plt>
test   %ebx,%ebx
je     1189 <set_tty_raw+0x69>
mov    0x8(%rsp),%rax
mov    0x2e8b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e62(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e71(%rip),%rax
mov    0x2e3a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e4f(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x10(%rsp)
mov    (%r14),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1040 <tcsetattr@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32 }
@STDIN_FILENO = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@ECHO = global i32 0
@ICANON = global i32 0
@IEXTEN = global i32 0
@TCSANOW = global i32 0
define void @set_tty_raw(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.termios
store i32 %0, i32* %2
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @tcgetattr(i32 %4, %struct.termios* %3)
%6 = load i32, i32* %2
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %26
8: ; preds = %1
%9 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%10 = load i32*, i32** %9
%11 = load i64, i64* @VMIN
%12 = getelementptr inbounds i32, i32* %10, i64 %11
store i32 1, i32* %12
%13 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%14 = load i32*, i32** %13
%15 = load i64, i64* @VTIME
%16 = getelementptr inbounds i32, i32* %14, i64 %15
store i32 0, i32* %16
%17 = load i32, i32* @ECHO
%18 = load i32, i32* @ICANON
%19 = or i32 %17, %18
%20 = load i32, i32* @IEXTEN
%21 = or i32 %19, %20
%22 = xor i32 %21, -1
%23 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%24 = load i32, i32* %23
%25 = and i32 %24, %22
store i32 %25, i32* %23
br label %26
26: ; preds = %8, %1
%27 = load i32, i32* @STDIN_FILENO
%28 = load i32, i32* @TCSANOW
%29 = call i32 @tcsetattr(i32 %27, i32 %28, %struct.termios* %3)
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32 }
@STDIN_FILENO = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@ECHO = global i32 0
@ICANON = global i32 0
@IEXTEN = global i32 0
@TCSANOW = global i32 0
define void @set_tty_raw(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @tcgetattr(i32 %4, %struct.termios* %2)
%6 = icmp eq i32 %0, 0
br i1 %6, label %23, label %7
7: ; preds = %1
%8 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%9 = load i32*, i32** %8
%10 = load i64, i64* @VMIN
%11 = getelementptr inbounds i32, i32* %9, i64 %10
store i32 1, i32* %11
%12 = load i64, i64* @VTIME
%13 = getelementptr inbounds i32, i32* %9, i64 %12
store i32 0, i32* %13
%14 = load i32, i32* @ECHO
%15 = load i32, i32* @ICANON
%16 = or i32 %15, %14
%17 = load i32, i32* @IEXTEN
%18 = or i32 %16, %17
%19 = xor i32 %18, -1
%20 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%21 = load i32, i32* %20
%22 = and i32 %21, %19
store i32 %22, i32* %20
br label %23
23: ; preds = %7, %1
%24 = load i32, i32* @STDIN_FILENO
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 %24, i32 %25, %struct.termios* %2)
ret void
}