#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 */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ STDIN_FILENO;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ sleep(int);
int main(void) {
  printf("Blocking on read...press enter to unblock.\n");
  int c;
  read(STDIN_FILENO, &c, 1);
  printf("Now making stdin non-blocking\n");
  int flags;
  flags = fcntl(STDIN_FILENO, F_GETFL, 0);
  flags |= O_NONBLOCK;
  fcntl(STDIN_FILENO, F_SETFL, flags);
  printf("Press enter again to exit the program...\n");
  while (1) {
    int ret = read(STDIN_FILENO, &c, 1);
    if (ret == -1) {
      if (errno == EAGAIN || errno == EWOULDBLOCK)
        printf("read() would have blocked\n");
      else {
        perror("read");
        exit(EXIT_FAILURE);
      }
    } else {
      printf("read succeeded\n");
      exit(EXIT_SUCCESS);
    }
    sleep(1);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
lea    0xe93(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <fcntl@plt>
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1277 <main+0x117>
mov    0x2db6(%rip),%rax
mov    (%rax),%rax
mov    0x2d74(%rip),%rcx
cmp    (%rcx),%rax
je     1247 <main+0xe7>
mov    0x2d9c(%rip),%rax
mov    (%rax),%rax
mov    0x2d8a(%rip),%rcx
cmp    (%rcx),%rax
jne    1258 <main+0xf8>
lea    0xe27(%rip),%rdi
call   1040 <printf@plt>
jmp    1272 <main+0x112>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    1291 <main+0x131>
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11ef <main+0x8f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e2a(%rip),%r15
mov    (%r15),%edi
lea    0x4(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
mov    (%r15),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2e2f(%rip),%rcx
or     (%rcx),%eax
mov    (%r15),%edi
mov    0x2e13(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edx
call   1040 <fcntl@plt>
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    1271 <main+0x111>
mov    0x2dd6(%rip),%r12
mov    0x2d97(%rip),%r13
mov    0x2dc0(%rip),%rbp
lea    0xe77(%rip),%r14
lea    0x4(%rsp),%rbx
jmp    1247 <main+0xe7>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    1271 <main+0x111>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1220 <main+0xc0>
cmp    0x0(%rbp),%rax
je     1220 <main+0xc0>
lea    0xda2(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xdff(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [44 x i8] c"Blocking on read...press enter to unblock.\0A\00"
@STDIN_FILENO = global i32 0
@.str.1 = [31 x i8] c"Now making stdin non-blocking\0A\00"
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.2 = [42 x i8] c"Press enter again to exit the program...\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.3 = [27 x i8] c"read() would have blocked\0A\00"
@.str.4 = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [16 x i8] c"read succeeded\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @STDIN_FILENO
%7 = call i32 @read(i32 %6, i32* %2, i32 1)
%8 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @STDIN_FILENO
%10 = load i32, i32* @F_GETFL
%11 = call i32 @fcntl(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %3
%12 = load i32, i32* @O_NONBLOCK
%13 = load i32, i32* %3
%14 = or i32 %13, %12
store i32 %14, i32* %3
%15 = load i32, i32* @STDIN_FILENO
%16 = load i32, i32* @F_SETFL
%17 = load i32, i32* %3
%18 = call i32 @fcntl(i32 %15, i32 %16, i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %0, %44
%21 = load i32, i32* @STDIN_FILENO
%22 = call i32 @read(i32 %21, i32* %2, i32 1)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %40
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i64, i64* @EAGAIN
%28 = icmp eq i64 %26, %27
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @EWOULDBLOCK
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29, %25
%34 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %39
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %33
br label %44
40: ; preds = %20
%41 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %39
%45 = call i32 @sleep(i32 1)
br label %20
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.4 = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [43 x i8] c"Blocking on read...press enter to unblock.\00"
@str.6 = [30 x i8] c"Now making stdin non-blocking\00"
@str.7 = [41 x i8] c"Press enter again to exit the program...\00"
@str.8 = [15 x i8] c"read succeeded\00"
@str.9 = [26 x i8] c"read() would have blocked\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
%3 = bitcast i32* %1 to i8*
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @read(i32 %4, i32* %1, i32 1)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.6, i64 0, i64 0))
%7 = load i32, i32* @STDIN_FILENO
%8 = load i32, i32* @F_GETFL
%9 = tail call i32 @fcntl(i32 %7, i32 %8, i32 0)
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %10, %9
%12 = load i32, i32* @STDIN_FILENO
%13 = load i32, i32* @F_SETFL
%14 = tail call i32 @fcntl(i32 %12, i32 %13, i32 %11)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.7, i64 0, i64 0))
%16 = load i32, i32* @STDIN_FILENO
%17 = call i32 @read(i32 %16, i32* %1, i32 1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %36
19: ; preds = %0, %26
%20 = load i64, i64* @errno
%21 = load i64, i64* @EAGAIN
%22 = icmp eq i64 %20, %21
%23 = load i64, i64* @EWOULDBLOCK
%24 = icmp eq i64 %20, %23
%25 = select i1 %22, i1 true, i1 %24
br i1 %25, label %26, label %32
26: ; preds = %19
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.9, i64 0, i64 0))
%28 = tail call i32 @sleep(i32 1)
%29 = load i32, i32* @STDIN_FILENO
%30 = call i32 @read(i32 %29, i32* %1, i32 1)
%31 = icmp eq i32 %30, -1
br i1 %31, label %19, label %36
32: ; preds = %19
%33 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = tail call i32 @exit(i32 %34)
unreachable
36: ; preds = %26, %0
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0))
%38 = load i32, i32* @EXIT_SUCCESS
%39 = tail call i32 @exit(i32 %38)
unreachable
}