#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 */
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ STDIN_FILENO;
scalar_t__ errno;
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
char get_next_char() {
  char c = 0;
  int orig = fcntl(STDIN_FILENO, F_GETFL);
  if (fcntl(STDIN_FILENO, F_SETFL, orig | O_NONBLOCK) == -1) {
    fprintf(stderr, "Failed to set `O_NONBLOCK` on `STDIN_FILENO`\n");
  }
  ssize_t nread = read(STDIN_FILENO, &c, 1);
  if (nread == -1) {
    c = 0;
  }
  if (fcntl(STDIN_FILENO, F_SETFL, orig) == -1) {
    fprintf(stderr, "Failed to turn off `O_NONBLOCK` on `STDIN_FILENO`\n");
  }
  errno = 0;
  return c;
}


x86 assembly (clang, -O0):
<get_next_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movb   $0x0,-0x1(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2e74(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    119b <get_next_char+0x6b>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    -0x1(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c3 <get_next_char+0x93>
movb   $0x0,-0x1(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11fd <get_next_char+0xcd>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dc4(%rip),%rax
movq   $0x0,(%rax)
movsbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_next_char>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movb   $0x0,0xf(%rsp)
mov    0x2e73(%rip),%r14
mov    (%r14),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebx
mov    (%r14),%edi
mov    0x2e6c(%rip),%r15
mov    (%r15),%esi
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
or     %ebx,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1193 <get_next_char+0x63>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
call   1050 <fprintf@plt>
mov    (%r14),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    11af <get_next_char+0x7f>
movb   $0x0,0xf(%rsp)
mov    (%r14),%edi
mov    (%r15),%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11d8 <get_next_char+0xa8>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2de9(%rip),%rax
movq   $0x0,(%rax)
mov    0xf(%rsp),%al
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"Failed to set `O_NONBLOCK` on `STDIN_FILENO`\0A\00"
@.str.1 = [51 x i8] c"Failed to turn off `O_NONBLOCK` on `STDIN_FILENO`\0A\00"
@errno = global i64 0
define signext i8 @get_next_char() {
%1 = alloca i8
%2 = alloca i32
%3 = alloca i32
store i8 0, i8* %1
%4 = load i32, i32* @STDIN_FILENO
%5 = load i32, i32* @F_GETFL
%6 = call i32 (i32, i32, ...) @fcntl(i32 %4, i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* @STDIN_FILENO
%8 = load i32, i32* @F_SETFL
%9 = load i32, i32* %2
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %9, %10
%12 = call i32 (i32, i32, ...) @fcntl(i32 %7, i32 %8, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %14, %0
%18 = load i32, i32* @STDIN_FILENO
%19 = call i32 @read(i32 %18, i8* %1, i32 1)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %17
store i8 0, i8* %1
br label %23
23: ; preds = %22, %17
%24 = load i32, i32* @STDIN_FILENO
%25 = load i32, i32* @F_SETFL
%26 = load i32, i32* %2
%27 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %29, %23
store i64 0, i64* @errno
%33 = load i8, i8* %1
ret i8 %33
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"Failed to set `O_NONBLOCK` on `STDIN_FILENO`\0A\00"
@.str.1 = [51 x i8] c"Failed to turn off `O_NONBLOCK` on `STDIN_FILENO`\0A\00"
@errno = global i64 0
define signext i8 @get_next_char() {
%1 = alloca i8
store i8 0, i8* %1
%2 = load i32, i32* @STDIN_FILENO
%3 = load i32, i32* @F_GETFL
%4 = tail call i32 (i32, i32, ...) @fcntl(i32 %2, i32 %3)
%5 = load i32, i32* @STDIN_FILENO
%6 = load i32, i32* @F_SETFL
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %7, %4
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %6, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %11, %0
%15 = load i32, i32* @STDIN_FILENO
%16 = call i32 @read(i32 %15, i8* %1, i32 1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %14
store i8 0, i8* %1
br label %19
19: ; preds = %18, %14
%20 = load i32, i32* @STDIN_FILENO
%21 = load i32, i32* @F_SETFL
%22 = tail call i32 (i32, i32, ...) @fcntl(i32 %20, i32 %21, i32 %4)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %24, %19
store i64 0, i64* @errno
%28 = load i8, i8* %1
ret i8 %28
}