#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*/ TCSAFLUSH;
int /*<<< orphan*/ TCSANOW;
int tcgetattr(int /*<<< orphan*/, struct termios *);
int tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
void btktermio_set_terminal(int s) {
  struct termios tp;
  static struct termios save;
  static int state = 1;
  if (s == 0) {
    if (tcgetattr(STDIN_FILENO, &tp) == -1) {
      return;
    }
    save = tp;
    tp.c_lflag &= (~ICANON & ~ECHO);
    if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tp) == -1) {
      return;
    }
  } else {
    if (state == 0 && tcsetattr(STDIN_FILENO, TCSANOW, &save) == -1)
      return;
  }
  state = s;
}


x86 assembly (clang, -O0):
<btktermio_set_terminal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    11ac <btktermio_set_terminal+0x8c>
mov    0x2e8c(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    1155 <btktermio_set_terminal+0x35>
jmp    11f3 <btktermio_set_terminal+0xd3>
mov    -0x8(%rbp),%eax
mov    %eax,0x2eda(%rip)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
mov    0x2e67(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
and    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    11a7 <btktermio_set_terminal+0x87>
jmp    11f3 <btktermio_set_terminal+0xd3>
jmp    11ea <btktermio_set_terminal+0xca>
cmpl   $0x0,0x2e7d(%rip)
jne    11e5 <btktermio_set_terminal+0xc5>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
lea    0x2e66(%rip),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    11e5 <btktermio_set_terminal+0xc5>
jmp    11f3 <btktermio_set_terminal+0xd3>
jmp    11ea <btktermio_set_terminal+0xca>
mov    -0x4(%rbp),%eax
mov    %eax,0x2e3d(%rip)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<btktermio_set_terminal>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
test   %edi,%edi
je     114e <btktermio_set_terminal+0x2e>
cmpl   $0x0,0x2eff(%rip)
jne    11a0 <btktermio_set_terminal+0x80>
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
lea    0x2eec(%rip),%rdx
jmp    1196 <btktermio_set_terminal+0x76>
mov    0x2e73(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     11a6 <btktermio_set_terminal+0x86>
mov    (%rsp),%eax
mov    %eax,0x2eca(%rip)
mov    0x2e4b(%rip),%rcx
mov    0x2e5c(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
not    %edx
and    %eax,%edx
mov    %edx,(%rsp)
mov    (%r14),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
je     11a6 <btktermio_set_terminal+0x86>
mov    %ebx,0x2e8a(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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