#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*/ TCSANOW;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
bool seticanon(bool icanon, bool echo) {
  struct termios termios;
  tcgetattr(0, &termios);
  bool ret = (termios.c_lflag & ICANON);
  if (icanon)
    termios.c_lflag |= ICANON;
  else
    termios.c_lflag &= ~ICANON;
  if (echo)
    termios.c_lflag |= ECHO;
  else
    termios.c_lflag &= ~ECHO;
  tcsetattr(0, TCSANOW, &termios);
  return ret;
}


x86 assembly (clang, -O0):
<seticanon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
xor    %edi,%edi
lea    -0x10(%rbp),%rsi
call   1030 <tcgetattr@plt>
mov    -0x10(%rbp),%eax
mov    0x2e8d(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1166 <seticanon+0x46>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1178 <seticanon+0x58>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1196 <seticanon+0x76>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11a8 <seticanon+0x88>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1040 <tcsetattr@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<seticanon>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%ebx
mov    %rsp,%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1030 <tcgetattr@plt>
mov    (%rsp),%eax
mov    0x2e8d(%rip),%rcx
mov    (%rcx),%ebp
mov    %ebp,%ecx
not    %ecx
and    %eax,%ecx
mov    %ebp,%edx
or     %eax,%edx
test   %ebx,%ebx
cmove  %ecx,%edx
mov    0x2e85(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,%esi
not    %esi
and    %edx,%esi
or     %ecx,%edx
test   %r15d,%r15d
cmove  %esi,%edx
mov    %edx,(%rsp)
and    %eax,%ebp
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1040 <tcsetattr@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.termios = type { i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @seticanon(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 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%7 = load i32, i32* %6
%8 = load i32, i32* @ICANON
%9 = icmp eq i32 %0, 0
%10 = xor i32 %8, -1
%11 = and i32 %7, %10
%12 = or i32 %8, %7
%13 = select i1 %9, i32 %11, i32 %12
%14 = icmp eq i32 %1, 0
%15 = load i32, i32* @ECHO
%16 = xor i32 %15, -1
%17 = and i32 %13, %16
%18 = or i32 %15, %13
%19 = select i1 %14, i32 %17, i32 %18
store i32 %19, i32* %6
%20 = and i32 %8, %7
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 0, i32 %21, %struct.termios* %3)
ret i32 %20
}