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

/* Variables and functions */
int BRKINT;
int CS8;
int CSIZE;
int ECHO;
int ICANON;
int ICRNL;
int IEXTEN;
int INPCK;
int ISIG;
int ISTRIP;
int IXON;
int /*<<< orphan*/ OPOST;
int PARENB;
int STDIN_FILENO;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int ttyraw() {
  int fd = STDIN_FILENO;
  struct termios oldtermios;
  struct termios newtermios;
  if (tcgetattr(fd, &oldtermios) < 0)
    return 1;
  newtermios = oldtermios;
  newtermios.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
  newtermios.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  newtermios.c_cflag &= ~(CSIZE | PARENB);
  newtermios.c_cflag |= CS8;
  newtermios.c_oflag &= ~(OPOST);
  newtermios.c_cc[VMIN] = 1;
  newtermios.c_cc[VTIME] = 0;
  if (tcsetattr(fd, TCSAFLUSH, &newtermios) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<ttyraw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1156 <ttyraw+0x36>
movl   $0x1,-0x4(%rbp)
jmp    1271 <ttyraw+0x151>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    0x2dda(%rip),%rcx
or     (%rcx),%eax
mov    0x2e01(%rip),%rcx
or     (%rcx),%eax
mov    0x2e20(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    0x2e35(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc4(%rip),%rcx
or     (%rcx),%eax
mov    0x2db3(%rip),%rcx
or     (%rcx),%eax
mov    0x2e02(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    0x2ddf(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
or     -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2d80(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
mov    0x2d3b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0x0,%rax
jge    126a <ttyraw+0x14a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <ttyraw+0x151>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyraw>:
push   %rbx
sub    $0x40,%rsp
mov    0x2e3c(%rip),%rax
mov    (%rax),%ebx
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
call   1030 <tcgetattr@plt>
test   %rax,%rax
js     1226 <ttyraw+0x106>
movups 0x20(%rsp),%xmm0
movups 0x30(%rsp),%xmm1
movaps %xmm0,(%rsp)
mov    0x2e68(%rip),%rax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
movaps %xmm1,0x10(%rsp)
mov    0x2e21(%rip),%rax
or     (%rax),%ecx
mov    0x2e40(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x2e2a(%rip),%rax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2de8(%rip),%rax
or     (%rax),%ecx
mov    0x2dd7(%rip),%rax
or     (%rax),%ecx
mov    0x2e26(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
mov    0x2ddf(%rip),%rax
mov    0x2e08(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    0x2dd7(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2dae(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d6d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <tcsetattr@plt>
sar    $0x3f,%rax
jmp    122b <ttyraw+0x10b>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbx
ret

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

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