#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*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
void setBufferedInput(bool enable) {
  static bool enabled = true;
  static struct termios old;
  struct termios new;
  if (enable && !enabled) {
    tcsetattr(STDIN_FILENO, TCSANOW, &old);
    enabled = true;
  } else if (!enable && enabled) {
    tcgetattr(STDIN_FILENO, &new);
    old = new;
    new.c_lflag &= (~ICANON & ~ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &new);
    enabled = false;
  }
}


x86 assembly (clang, -O0):
<setBufferedInput>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     116f <setBufferedInput+0x4f>
cmpl   $0x0,0x2ef4(%rip)
jne    116f <setBufferedInput+0x4f>
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
lea    0x2edd(%rip),%rdx
call   1040 <tcsetattr@plt>
movl   $0x1,0x2ec6(%rip)
jmp    11eb <setBufferedInput+0xcb>
cmpl   $0x0,-0x4(%rbp)
jne    11e6 <setBufferedInput+0xc6>
cmpl   $0x0,0x2eb0(%rip)
je     11e6 <setBufferedInput+0xc6>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1030 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,0x2e97(%rip)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
and    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1040 <tcsetattr@plt>
movl   $0x0,0x2e4a(%rip)
jmp    11eb <setBufferedInput+0xcb>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setBufferedInput>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
sete   %bl
mov    0x2f00(%rip),%al
xor    $0x1,%al
or     %al,%bl
test   $0x1,%bl
je     1195 <setBufferedInput+0x75>
test   %edi,%edi
sete   %cl
and    %al,%cl
cmp    $0x1,%cl
jne    11bc <setBufferedInput+0x9c>
mov    0x2e80(%rip),%r15
mov    (%r15),%edi
lea    0x8(%rsp),%r14
mov    %r14,%rsi
call   1030 <tcgetattr@plt>
mov    0x8(%rsp),%eax
mov    %eax,0x2ece(%rip)
mov    0x2e57(%rip),%rcx
mov    0x2e68(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
not    %edx
and    %eax,%edx
mov    %edx,0x8(%rsp)
mov    (%r15),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdx
jmp    11ae <setBufferedInput+0x8e>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0x2e8a(%rip),%rdx
call   1040 <tcsetattr@plt>
and    $0x1,%bl
mov    %bl,0x2e78(%rip)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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