#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 /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ _IONBF;
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ setlinebuf(int /*<<< orphan*/);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
void BarTermSetBuffer(char enable) {
  struct termios termopts;
  tcgetattr(fileno(stdin), &termopts);
  if (enable == 1) {
    termopts.c_lflag |= ICANON;
    setlinebuf(stdin);
  } else {
    termopts.c_lflag &= ~ICANON;
    setvbuf(stdin, NULL, _IONBF, 1);
  }
  tcsetattr(fileno(stdin), TCSANOW, &termopts);
}


x86 assembly (clang, -O0):
<BarTermSetBuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
call   1040 <fileno@plt>
mov    %eax,%edi
lea    -0x8(%rbp),%rsi
call   1060 <tcgetattr@plt>
movsbl -0x1(%rbp),%eax
cmp    $0x1,%eax
jne    11a6 <BarTermSetBuffer+0x56>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   1030 <setlinebuf@plt>
jmp    11d8 <BarTermSetBuffer+0x88>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
mov    $0x1,%ecx
call   1050 <setvbuf@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1070 <tcsetattr@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BarTermSetBuffer>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e8b(%rip),%r14
mov    (%r14),%edi
call   1040 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <tcgetattr@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
cmp    $0x1,%bl
jne    118a <BarTermSetBuffer+0x3a>
or     %eax,(%rsp)
mov    (%r14),%edi
call   1030 <setlinebuf@plt>
jmp    11a7 <BarTermSetBuffer+0x57>
not    %eax
and    %eax,(%rsp)
mov    (%r14),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    $0x1,%ecx
call   1050 <setvbuf@plt>
mov    (%r14),%edi
call   1040 <fileno@plt>
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1070 <tcsetattr@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@stdin = global i32 0
@ICANON = global i32 0
@_IONBF = global i32 0
@TCSANOW = global i32 0
define void @BarTermSetBuffer(i8 signext %0) {
%2 = alloca i8
%3 = alloca %struct.termios
store i8 %0, i8* %2
%4 = load i32, i32* @stdin
%5 = call i32 @fileno(i32 %4)
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %3)
%7 = load i8, i8* %2
%8 = sext i8 %7 to i32
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %17
10: ; preds = %1
%11 = load i32, i32* @ICANON
%12 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%13 = load i32, i32* %12
%14 = or i32 %13, %11
store i32 %14, i32* %12
%15 = load i32, i32* @stdin
%16 = call i32 @setlinebuf(i32 %15)
br label %26
17: ; preds = %1
%18 = load i32, i32* @ICANON
%19 = xor i32 %18, -1
%20 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = and i32 %21, %19
store i32 %22, i32* %20
%23 = load i32, i32* @stdin
%24 = load i32, i32* @_IONBF
%25 = call i32 @setvbuf(i32 %23, i32* null, i32 %24, i32 1)
br label %26
26: ; preds = %17, %10
%27 = load i32, i32* @stdin
%28 = call i32 @fileno(i32 %27)
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %28, i32 %29, %struct.termios* %3)
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@stdin = global i32 0
@ICANON = global i32 0
@_IONBF = global i32 0
@TCSANOW = global i32 0
define void @BarTermSetBuffer(i8 signext %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @stdin
%5 = tail call i32 @fileno(i32 %4)
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %2)
%7 = icmp eq i8 %0, 1
%8 = load i32, i32* @ICANON
br i1 %7, label %9, label %15
9: ; preds = %1
%10 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = or i32 %11, %8
store i32 %12, i32* %10
%13 = load i32, i32* @stdin
%14 = call i32 @setlinebuf(i32 %13)
br label %23
15: ; preds = %1
%16 = xor i32 %8, -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* @stdin
%21 = load i32, i32* @_IONBF
%22 = call i32 @setvbuf(i32 %20, i32* null, i32 %21, i32 1)
br label %23
23: ; preds = %15, %9
%24 = load i32, i32* @stdin
%25 = call i32 @fileno(i32 %24)
%26 = load i32, i32* @TCSANOW
%27 = call i32 @tcsetattr(i32 %25, i32 %26, %struct.termios* %2)
ret void
}