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

/* Variables and functions */
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ memcpy(struct termios *, struct termios *, int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
void char_term(int on) {
  static struct termios orig;
  static char state = 0;
  if (!state && on) {
    struct termios charbased;
    tcgetattr(0, &orig);
    memcpy(&charbased, &orig, sizeof orig);
    charbased.c_lflag &= ~ICANON;
    charbased.c_cc[VMIN] = 1;
    charbased.c_cc[VTIME] = 0;
    tcsetattr(0, TCSANOW, &charbased);
    state = 1;
  }
  if (state && !on) {
    tcsetattr(0, TCSANOW, &orig);
    state = 0;
  }
}


x86 assembly (clang, -O0):
<char_term>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpb   $0x0,0x2f0e(%rip)
jne    11cc <char_term+0x9c>
cmpl   $0x0,-0x4(%rbp)
je     11cc <char_term+0x9c>
xor    %edi,%edi
lea    0x2ee5(%rip),%rsi
call   1040 <tcgetattr@plt>
lea    0x2ed9(%rip),%rsi
lea    -0x18(%rbp),%rdi
mov    $0x10,%edx
call   1030 <memcpy@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1050 <tcsetattr@plt>
movb   $0x1,0x2e84(%rip)
movsbl 0x2e7d(%rip),%eax
cmp    $0x0,%eax
je     1204 <char_term+0xd4>
cmpl   $0x0,-0x4(%rbp)
jne    1204 <char_term+0xd4>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0x2e48(%rip),%rdx
call   1050 <tcsetattr@plt>
movb   $0x0,0x2e4c(%rip)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<char_term>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2f0f(%rip),%al
test   %edi,%edi
je     11be <char_term+0x8e>
test   $0x1,%al
jne    11be <char_term+0x8e>
lea    0x2ef0(%rip),%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x10,%edx
call   1030 <memcpy@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x8(%rsp)
mov    (%rsp),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e38(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    %r15,%rdx
call   1050 <tcsetattr@plt>
movb   $0x1,0x2e94(%rip)
mov    $0x1,%al
test   %ebx,%ebx
jne    11e4 <char_term+0xb4>
test   $0x1,%al
je     11e4 <char_term+0xb4>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    0x2e6a(%rip),%rdx
xor    %edi,%edi
call   1050 <tcsetattr@plt>
movb   $0x0,0x2e6c(%rip)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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