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

/* Variables and functions */
int ECHO;
int ICANON;
int ISIG;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void term_canon(int fd, int on) {
  struct termios attr;
  if (tcgetattr(fd, &attr) == -1)
    return;
  if (on) {
    attr.c_lflag |= (ISIG | ICANON | ECHO);
    attr.c_cc[VMIN] = 0;
    attr.c_cc[VTIME] = 0;
  } else {
    attr.c_lflag &= ~(ISIG | ICANON | ECHO);
    attr.c_cc[VMIN] = 1;
    attr.c_cc[VTIME] = 2;
  }
  tcsetattr(fd, TCSANOW, &attr);
}


x86 assembly (clang, -O0):
<term_canon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    1148 <term_canon+0x28>
jmp    1205 <term_canon+0xe5>
cmpl   $0x0,-0x8(%rbp)
je     11a2 <term_canon+0x82>
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    0x2e56(%rip),%rcx
or     (%rcx),%eax
mov    0x2e75(%rip),%rcx
or     (%rcx),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
jmp    11f0 <term_canon+0xd0>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    0x2e06(%rip),%rcx
or     (%rcx),%eax
mov    0x2e25(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    0x2dda(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x2,(%rax,%rcx,4)
mov    -0x4(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <tcsetattr@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<term_canon>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     11a6 <term_canon+0x86>
mov    0x2e98(%rip),%rcx
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e8e(%rip),%rcx
or     (%rcx),%eax
test   %ebp,%ebp
je     1162 <term_canon+0x42>
or     %eax,0x8(%rsp)
xor    %ecx,%ecx
xor    %eax,%eax
jmp    1172 <term_canon+0x52>
not    %eax
and    %eax,0x8(%rsp)
mov    $0x2,%eax
mov    $0x1,%ecx
mov    0x10(%rsp),%rdx
mov    0x2e52(%rip),%rsi
mov    (%rsi),%rsi
mov    %ecx,(%rdx,%rsi,4)
mov    0x2e35(%rip),%rcx
mov    (%rcx),%rcx
mov    %eax,(%rdx,%rcx,4)
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <tcsetattr@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@ISIG = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define void @term_canon(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.termios
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* %5)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %2
br label %52
10: ; preds = %2
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %30
13: ; preds = %10
%14 = load i32, i32* @ISIG
%15 = load i32, i32* @ICANON
%16 = or i32 %14, %15
%17 = load i32, i32* @ECHO
%18 = or i32 %16, %17
%19 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%20 = load i32, i32* %19
%21 = or i32 %20, %18
store i32 %21, i32* %19
%22 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%23 = load i32*, i32** %22
%24 = load i64, i64* @VMIN
%25 = getelementptr inbounds i32, i32* %23, i64 %24
store i32 0, i32* %25
%26 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%27 = load i32*, i32** %26
%28 = load i64, i64* @VTIME
%29 = getelementptr inbounds i32, i32* %27, i64 %28
store i32 0, i32* %29
br label %48
30: ; preds = %10
%31 = load i32, i32* @ISIG
%32 = load i32, i32* @ICANON
%33 = or i32 %31, %32
%34 = load i32, i32* @ECHO
%35 = or i32 %33, %34
%36 = xor i32 %35, -1
%37 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%38 = load i32, i32* %37
%39 = and i32 %38, %36
store i32 %39, i32* %37
%40 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%41 = load i32*, i32** %40
%42 = load i64, i64* @VMIN
%43 = getelementptr inbounds i32, i32* %41, i64 %42
store i32 1, i32* %43
%44 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%45 = load i32*, i32** %44
%46 = load i64, i64* @VTIME
%47 = getelementptr inbounds i32, i32* %45, i64 %46
store i32 2, i32* %47
br label %48
48: ; preds = %30, %13
%49 = load i32, i32* %3
%50 = load i32, i32* @TCSANOW
%51 = call i32 @tcsetattr(i32 %49, i32 %50, %struct.termios* %5)
br label %52
52: ; preds = %48, %9
ret void
}

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