#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 errno;
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int /*<<< orphan*/ usleep(int);
void read_nonblock(void) {
  int rc;
  char buf[32];
  int saved_flags = fcntl(0, F_GETFL, 0);
  printf("Setting non-block mode for fd 0\r\n");
  rc = fcntl(0, F_SETFL, saved_flags | O_NONBLOCK);
  if (rc != 0) {
    fprintf(stderr, "fcntl() failed: %s\r\n", strerror(errno));
    return;
  }
  for (int i = 0;; i++) {
    rc = read(0, buf, 1);
    if (rc >= 0) {
      buf[rc] = 0;
      printf("[iter %d] read() = %d [buf: '%s']\r\n", i, rc, buf);
      if (buf[0] == 'q')
        break;
    } else {
      printf("[iter %d] read() = %d (errno: %d => %s)\r\n", i, rc, errno,
             strerror(errno));
      usleep(500 * 1000);
    }
  }
  rc = fcntl(0, F_SETFL, saved_flags);
  if (rc != 0)
    fprintf(stderr, "fcntl() failed: %s\r\n", strerror(errno));
}


x86 assembly (clang, -O0):
<read_nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
mov    %edx,%edi
call   1030 <fcntl@plt>
mov    %eax,-0x34(%rbp)
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    -0x34(%rbp),%edx
mov    0x2e4a(%rip),%rax
or     (%rax),%edx
xor    %edi,%edi
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11e5 <read_nonblock+0x85>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xe47(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12ea <read_nonblock+0x18a>
movl   $0x0,-0x38(%rbp)
lea    -0x30(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     1241 <read_nonblock+0xe1>
movslq -0x4(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
mov    -0x38(%rbp),%esi
mov    -0x4(%rbp),%edx
lea    -0x30(%rbp),%rcx
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0x30(%rbp),%eax
cmp    $0x71,%eax
jne    123c <read_nonblock+0xdc>
jmp    129e <read_nonblock+0x13e>
jmp    128b <read_nonblock+0x12b>
mov    -0x38(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x48(%rbp),%esi
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%ecx
mov    %rax,%r8
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x7a120,%edi
call   1080 <usleep@plt>
jmp    1290 <read_nonblock+0x130>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11ec <read_nonblock+0x8c>
mov    0x2d2b(%rip),%rax
mov    (%rax),%esi
mov    -0x34(%rbp),%edx
xor    %edi,%edi
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     12ea <read_nonblock+0x18a>
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xd3d(%rip),%rsi
call   1060 <fprintf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_nonblock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%ebx
lea    0xeca(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
or     %ebx,%edx
xor    %edi,%edi
call   1040 <fcntl@plt>
test   %eax,%eax
je     11f3 <read_nonblock+0x83>
mov    0x2e14(%rip),%rax
mov    (%rax),%ebx
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe25(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fprintf@plt>
mov    %ebx,0xc(%rsp)
lea    0x10(%rsp),%r13
lea    0xe12(%rip),%r12
lea    0xe2f(%rip),%r15
xor    %ebp,%ebp
jmp    1243 <read_nonblock+0xd3>
xchg   %ax,%ax
mov    0x2db1(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%edi
call   1080 <strerror@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %r14d,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x7a120,%edi
call   1090 <usleep@plt>
add    $0x1,%ebp
xor    %edi,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1210 <read_nonblock+0xa0>
mov    %ebx,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r12,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
cmpb   $0x71,0x10(%rsp)
jne    1240 <read_nonblock+0xd0>
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    0xc(%rsp),%edx
call   1040 <fcntl@plt>
test   %eax,%eax
je     12b7 <read_nonblock+0x147>
mov    0x2d42(%rip),%rax
mov    (%rax),%ebx
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd53(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [34 x i8] c"Setting non-block mode for fd 0\0D\0A\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [21 x i8] c"fcntl() failed: %s\0D\0A\00"
@errno = global i32 0
@.str.2 = [36 x i8] c"[iter %d] read() = %d [buf: '%s']\0D\0A\00"
@.str.3 = [42 x i8] c"[iter %d] read() = %d (errno: %d => %s)\0D\0A\00"
define void @read_nonblock() {
%1 = alloca i32
%2 = alloca [32 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @F_GETFL
%6 = call i32 @fcntl(i32 0, i32 %5, i32 0)
store i32 %6, i32* %3
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @F_SETFL
%9 = load i32, i32* %3
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %9, %10
%12 = call i32 @fcntl(i32 0, i32 %8, i32 %11)
store i32 %12, i32* %1
%13 = load i32, i32* %1
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %0
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %63
20: ; preds = %0
store i32 0, i32* %4
br label %21
21: ; preds = %49, %20
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%23 = call i32 @read(i32 0, i8* %22, i32 1)
store i32 %23, i32* %1
%24 = load i32, i32* %1
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %40
26: ; preds = %21
%27 = load i32, i32* %1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 %28
store i8 0, i8* %29
%30 = load i32, i32* %4
%31 = load i32, i32* %1
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %30, i32 %31, i8* %32)
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 113
br i1 %37, label %38, label %39
38: ; preds = %26
br label %52
39: ; preds = %26
br label %48
40: ; preds = %21
%41 = load i32, i32* %4
%42 = load i32, i32* %1
%43 = load i32, i32* @errno
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %41, i32 %42, i32 %43, i8* %45)
%47 = call i32 @usleep(i32 500000)
br label %48
48: ; preds = %40, %39
br label %49
49: ; preds = %48
%50 = load i32, i32* %4
%51 = add nsw i32 %50, 1
store i32 %51, i32* %4
br label %21
52: ; preds = %38
%53 = load i32, i32* @F_SETFL
%54 = load i32, i32* %3
%55 = call i32 @fcntl(i32 0, i32 %53, i32 %54)
store i32 %55, i32* %1
%56 = load i32, i32* %1
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %52
%59 = load i32, i32* @stderr
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %61)
br label %63
63: ; preds = %15, %58, %52
ret void
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [21 x i8] c"fcntl() failed: %s\0D\0A\00"
@errno = global i32 0
@.str.2 = [36 x i8] c"[iter %d] read() = %d [buf: '%s']\0D\0A\00"
@.str.3 = [42 x i8] c"[iter %d] read() = %d (errno: %d => %s)\0D\0A\00"
@str = [33 x i8] c"Setting non-block mode for fd 0\0D\00"
define void @read_nonblock() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @F_GETFL
%4 = tail call i32 @fcntl(i32 0, i32 %3, i32 0)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%6 = load i32, i32* @F_SETFL
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %7, %4
%9 = tail call i32 @fcntl(i32 0, i32 %6, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %42
16: ; preds = %0, %35
%17 = phi i32 [ %36, %35 ], [ 0, %0 ]
%18 = call i32 @read(i32 0, i8* %2, i32 1)
%19 = icmp sgt i32 %18, -1
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = zext i32 %18 to i64
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 %21
store i8 0, i8* %22
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %18, i8* %2)
%24 = load i8, i8* %2
%25 = icmp eq i8 %24, 113
br i1 %25, label %26, label %35
26: ; preds = %20
%27 = load i32, i32* @F_SETFL
%28 = call i32 @fcntl(i32 0, i32 %27, i32 %4)
%29 = icmp eq i32 %28, 0
br i1 %29, label %42, label %37
30: ; preds = %16
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %18, i32 %31, i8* %32)
%34 = call i32 @usleep(i32 500000)
br label %35
35: ; preds = %30, %20
%36 = add nuw nsw i32 %17, 1
br label %16
37: ; preds = %26
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %26, %37, %11
ret void
}