#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*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
void nonblock(int state) {
  struct termios ttystate;
  tcgetattr(STDIN_FILENO, &ttystate);
  if (state == 0) {
    ttystate.c_lflag &= ~ICANON;
    ttystate.c_cc[VMIN] = 1;
  } else if (state == 1) {
    ttystate.c_lflag |= ICANON;
  }
  tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
}


x86 assembly (clang, -O0):
<nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmpl   $0x0,-0x4(%rbp)
jne    1173 <nonblock+0x53>
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e7c(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
jmp    1191 <nonblock+0x71>
cmpl   $0x1,-0x4(%rbp)
jne    118c <nonblock+0x6c>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1191 <nonblock+0x71>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <tcsetattr@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nonblock>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    0x2ea0(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0x1,%ebx
je     116d <nonblock+0x4d>
test   %ebx,%ebx
jne    117a <nonblock+0x5a>
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
jmp    117a <nonblock+0x5a>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
or     %eax,0x10(%rsp)
mov    (%r14),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1040 <tcsetattr@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define void @nonblock(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.termios
store i32 %0, i32* %2
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @tcgetattr(i32 %4, %struct.termios* %3)
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %1
%9 = load i32, i32* @ICANON
%10 = xor i32 %9, -1
%11 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%12 = load i32, i32* %11
%13 = and i32 %12, %10
store i32 %13, i32* %11
%14 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%15 = load i32*, i32** %14
%16 = load i64, i64* @VMIN
%17 = getelementptr inbounds i32, i32* %15, i64 %16
store i32 1, i32* %17
br label %27
18: ; preds = %1
%19 = load i32, i32* %2
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* @ICANON
%23 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%24 = load i32, i32* %23
%25 = or i32 %24, %22
store i32 %25, i32* %23
br label %26
26: ; preds = %21, %18
br label %27
27: ; preds = %26, %8
%28 = load i32, i32* @STDIN_FILENO
%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*, i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define void @nonblock(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @tcgetattr(i32 %4, %struct.termios* %2)
switch i32 %0, label %21 [
i32 0, label %6
i32 1, label %16
]
6: ; preds = %1
%7 = load i32, i32* @ICANON
%8 = xor i32 %7, -1
%9 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%10 = load i32, i32* %9
%11 = and i32 %10, %8
store i32 %11, i32* %9
%12 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%13 = load i32*, i32** %12
%14 = load i64, i64* @VMIN
%15 = getelementptr inbounds i32, i32* %13, i64 %14
store i32 1, i32* %15
br label %21
16: ; preds = %1
%17 = load i32, i32* @ICANON
%18 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%19 = load i32, i32* %18
%20 = or i32 %19, %17
store i32 %20, i32* %18
br label %21
21: ; preds = %1, %16, %6
%22 = load i32, i32* @STDIN_FILENO
%23 = load i32, i32* @TCSANOW
%24 = call i32 @tcsetattr(i32 %22, i32 %23, %struct.termios* %2)
ret void
}