#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 */

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ STDIN_FILENO;
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int set_getch_nb(int nb) {
  int flags;
  if (!nb) {
    flags = fcntl(STDIN_FILENO, F_GETFL, 0);
    if (fcntl(STDIN_FILENO, F_SETFL, flags & ~O_NONBLOCK) != 0)
      goto err;
  } else {
    if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) != 0)
      goto err;
  }
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<set_getch_nb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    117c <set_getch_nb+0x6c>
mov    0x2e9c(%rip),%rax
mov    (%rax),%edi
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
je     1177 <set_getch_nb+0x67>
jmp    11bb <set_getch_nb+0xab>
jmp    11af <set_getch_nb+0x9f>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
je     11aa <set_getch_nb+0x9a>
jmp    11bb <set_getch_nb+0xab>
jmp    11af <set_getch_nb+0x9f>
movl   $0x0,-0x4(%rbp)
jmp    11c2 <set_getch_nb+0xb2>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_getch_nb>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%eax
mov    0x2eab(%rip),%r14
mov    (%r14),%edi
test   %eax,%eax
je     1143 <set_getch_nb+0x33>
mov    0x2ead(%rip),%rax
mov    (%rax),%esi
mov    0x2eb4(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
test   %eax,%eax
jne    1177 <set_getch_nb+0x67>
xor    %ebx,%ebx
jmp    117c <set_getch_nb+0x6c>
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    (%r14),%edi
mov    0x2e79(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e80(%rip),%rcx
mov    (%rcx),%edx
not    %edx
and    %eax,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
je     117c <set_getch_nb+0x6c>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @set_getch_nb(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp ne i32 %5, 0
br i1 %6, label %21, label %7
7: ; preds = %1
%8 = load i32, i32* @STDIN_FILENO
%9 = load i32, i32* @F_GETFL
%10 = call i32 @fcntl(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* @STDIN_FILENO
%12 = load i32, i32* @F_SETFL
%13 = load i32, i32* %4
%14 = load i32, i32* @O_NONBLOCK
%15 = xor i32 %14, -1
%16 = and i32 %13, %15
%17 = call i32 @fcntl(i32 %11, i32 %12, i32 %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %7
br label %30
20: ; preds = %7
br label %29
21: ; preds = %1
%22 = load i32, i32* @STDIN_FILENO
%23 = load i32, i32* @F_SETFL
%24 = load i32, i32* @O_NONBLOCK
%25 = call i32 @fcntl(i32 %22, i32 %23, i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
br label %30
28: ; preds = %21
br label %29
29: ; preds = %28, %20
store i32 0, i32* %2
br label %31
30: ; preds = %27, %19
store i32 -1, i32* %2
br label %31
31: ; preds = %30, %29
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @set_getch_nb(i32 %0) {
%2 = icmp eq i32 %0, 0
%3 = load i32, i32* @STDIN_FILENO
br i1 %2, label %4, label %14
4: ; preds = %1
%5 = load i32, i32* @F_GETFL
%6 = tail call i32 @fcntl(i32 %3, i32 %5, i32 0)
%7 = load i32, i32* @STDIN_FILENO
%8 = load i32, i32* @F_SETFL
%9 = load i32, i32* @O_NONBLOCK
%10 = xor i32 %9, -1
%11 = and i32 %6, %10
%12 = tail call i32 @fcntl(i32 %7, i32 %8, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %20, label %19
14: ; preds = %1
%15 = load i32, i32* @F_SETFL
%16 = load i32, i32* @O_NONBLOCK
%17 = tail call i32 @fcntl(i32 %3, i32 %15, i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %14, %4
br label %20
20: ; preds = %4, %14, %19
%21 = phi i32 [ -1, %19 ], [ 0, %14 ], [ 0, %4 ]
ret i32 %21
}