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


x86 assembly (clang, -O0):
<setSysNonBlockMode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    0x2e93(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <tcgetattr@plt>
movzbl -0x1(%rbp),%eax
cmp    $0x1,%eax
jne    118b <setSysNonBlockMode+0x6b>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e5c(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
jmp    11bb <setSysNonBlockMode+0x9b>
movzbl -0x1(%rbp),%eax
cmp    $0x0,%eax
jne    11b6 <setSysNonBlockMode+0x96>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11bb <setSysNonBlockMode+0x9b>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <tcsetattr@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setSysNonBlockMode>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    0x2e98(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1030 <tcgetattr@plt>
test   %ebx,%ebx
je     1176 <setSysNonBlockMode+0x56>
cmp    $0x1,%bl
jne    1190 <setSysNonBlockMode+0x70>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    0x2e8a(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    %eax,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e6e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
jmp    1190 <setSysNonBlockMode+0x70>
mov    0x2e43(%rip),%rax
mov    0x10(%rsp),%ecx
or     (%rax),%ecx
mov    0x2e56(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x10(%rsp)
mov    (%r14),%edi
mov    0x2e4e(%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
@ECHO = global i32 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define void @setSysNonBlockMode(i8 zeroext %0) {
%2 = alloca i8
%3 = alloca %struct.termios
store i8 %0, i8* %2
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @tcgetattr(i32 %4, %struct.termios* %3)
%6 = load i8, i8* %2
%7 = zext i8 %6 to i32
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %24
9: ; preds = %1
%10 = load i32, i32* @ICANON
%11 = xor i32 %10, -1
%12 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%13 = load i32, i32* %12
%14 = and i32 %13, %11
store i32 %14, i32* %12
%15 = load i32, i32* @ECHO
%16 = xor i32 %15, -1
%17 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%18 = load i32, i32* %17
%19 = and i32 %18, %16
store i32 %19, i32* %17
%20 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%21 = load i32*, i32** %20
%22 = load i64, i64* @VMIN
%23 = getelementptr inbounds i32, i32* %21, i64 %22
store i32 1, i32* %23
br label %38
24: ; preds = %1
%25 = load i8, i8* %2
%26 = zext i8 %25 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %24
%29 = load i32, i32* @ICANON
%30 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%31 = load i32, i32* %30
%32 = or i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* @ECHO
%34 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%35 = load i32, i32* %34
%36 = or i32 %35, %33
store i32 %36, i32* %34
br label %37
37: ; preds = %28, %24
br label %38
38: ; preds = %37, %9
%39 = load i32, i32* @STDIN_FILENO
%40 = load i32, i32* @TCSANOW
%41 = call i32 @tcsetattr(i32 %39, i32 %40, %struct.termios* %3)
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define void @setSysNonBlockMode(i8 zeroext %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 i8 %0, label %25 [
i8 1, label %6
i8 0, label %18
]
6: ; preds = %1
%7 = load i32, i32* @ICANON
%8 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%9 = load i32, i32* %8
%10 = load i32, i32* @ECHO
%11 = or i32 %7, %10
%12 = xor i32 %11, -1
%13 = and i32 %9, %12
store i32 %13, i32* %8
%14 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 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 %25
18: ; preds = %1
%19 = load i32, i32* @ICANON
%20 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%21 = load i32, i32* %20
%22 = or i32 %21, %19
%23 = load i32, i32* @ECHO
%24 = or i32 %23, %22
store i32 %24, i32* %20
br label %25
25: ; preds = %1, %18, %6
%26 = load i32, i32* @STDIN_FILENO
%27 = load i32, i32* @TCSANOW
%28 = call i32 @tcsetattr(i32 %26, i32 %27, %struct.termios* %2)
ret void
}