#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 dummy;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ reset_terminal(struct termios *);
int select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ setup_terminal(struct termios *);
int main() {
  struct termios old_args;
  setup_terminal(&old_args);
  fd_set rfds;
  int sres = 0;
  char buffer[256];
  int nread = 0;
  for (;;) {
    FD_ZERO(&rfds);
    FD_SET(0, &rfds);
    printf("\nselect begin\n");
    sres = select(0 + 1, &rfds, NULL, NULL, NULL);
    printf("\nselect return\n");
    if (sres < 0) {
      perror("select error");
      break;
    } else if (sres == 0) {
      printf("select timeout\n");
    } else {
      if (FD_ISSET(0, &rfds)) {
        nread = read(0, buffer, sizeof(buffer) - 1);
        if (nread <= 0) {
          perror("read error");
          break;
        }
        buffer[nread] = '\0';
        printf("read string(%d): %s\n", nread, buffer);
      }
    }
  }
  reset_terminal(&old_args);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1050 <setup_terminal@plt>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x114(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0xc(%rbp),%rsi
call   10b0 <FD_SET@plt>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
mov    %eax,-0x10(%rbp)
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x10(%rbp)
jge    1223 <main+0x93>
lea    0xe06(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c5 <main+0x135>
cmpl   $0x0,-0x10(%rbp)
jne    1240 <main+0xb0>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bb <main+0x12b>
xor    %edi,%edi
lea    -0xc(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12b6 <main+0x126>
lea    -0x110(%rbp),%rsi
xor    %edi,%edi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jg     128c <main+0xfc>
lea    0xdba(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c5 <main+0x135>
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
mov    -0x114(%rbp),%esi
lea    -0x110(%rbp),%rdx
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bb <main+0x12b>
jmp    12c0 <main+0x130>
jmp    11bc <main+0x2c>
lea    -0x8(%rbp),%rdi
call   1090 <reset_terminal@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0x8(%rsp),%rdi
call   1060 <setup_terminal@plt>
lea    0xc(%rsp),%rbx
lea    0xe66(%rip),%r15
lea    0xe6d(%rip),%r12
lea    0x10(%rsp),%rbp
lea    0xe3e(%rip),%r14
jmp    11ec <main+0x4c>
nopl   0x0(%rax)
lea    0xe63(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
mov    %eax,%r13d
mov    %r12,%rdi
call   1030 <puts@plt>
test   %r13d,%r13d
js     1266 <main+0xc6>
je     11e0 <main+0x40>
xor    %edi,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11ec <main+0x4c>
xor    %edi,%edi
mov    %rbp,%rsi
mov    $0xff,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    126f <main+0xcf>
mov    %eax,%ecx
movb   $0x0,0x10(%rsp,%rcx,1)
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11ec <main+0x4c>
lea    0xd93(%rip),%rdi
jmp    1276 <main+0xd6>
lea    0xd97(%rip),%rdi
call   10b0 <perror@plt>
lea    0x8(%rsp),%rdi
call   10a0 <reset_terminal@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [15 x i8] c"\0Aselect begin\0A\00"
@.str.1 = [16 x i8] c"\0Aselect return\0A\00"
@.str.2 = [13 x i8] c"select error\00"
@.str.3 = [16 x i8] c"select timeout\0A\00"
@.str.4 = [11 x i8] c"read error\00"
@.str.5 = [21 x i8] c"read string(%d): %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @setup_terminal(%struct.termios* %2)
store i32 0, i32* %4
store i32 0, i32* %6
br label %8
8: ; preds = %42, %0
%9 = call i32 @FD_ZERO(i32* %3)
%10 = call i32 @FD_SET(i32 0, i32* %3)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @select(i64 1, i32* %3, i32* null, i32* null, i32* null)
store i32 %12, i32* %4
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %43
18: ; preds = %8
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %41
23: ; preds = %18
%24 = call i64 @FD_ISSET(i32 0, i32* %3)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %40
26: ; preds = %23
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%28 = call i32 @read(i32 0, i8* %27, i32 255)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp sle i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %43
33: ; preds = %26
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %35
store i8 0, i8* %36
%37 = load i32, i32* %6
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %37, i8* %38)
br label %40
40: ; preds = %33, %23
br label %41
41: ; preds = %40, %21
br label %42
42: ; preds = %41
br label %8
43: ; preds = %31, %16
%44 = call i32 @reset_terminal(%struct.termios* %2)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str.2 = [13 x i8] c"select error\00"
@.str.4 = [11 x i8] c"read error\00"
@.str.5 = [21 x i8] c"read string(%d): %s\0A\00"
@str = [14 x i8] c"\0Aselect begin\00"
@str.6 = [15 x i8] c"\0Aselect return\00"
@str.7 = [15 x i8] c"select timeout\00"
define i32 @main() {
%1 = alloca %struct.termios
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = bitcast %struct.termios* %1 to i8*
%5 = call i32 @setup_terminal(%struct.termios* %1)
%6 = bitcast i32* %2 to i8*
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
br label %8
8: ; preds = %19, %0
%9 = call i32 @FD_ZERO(i32* %2)
%10 = call i32 @FD_SET(i32 0, i32* %2)
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%12 = call i32 @select(i64 1, i32* %2, i32* null, i32* null, i32* null)
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.6, i64 0, i64 0))
%14 = icmp slt i32 %12, 0
br i1 %14, label %30, label %15
15: ; preds = %8
%16 = icmp eq i32 %12, 0
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.7, i64 0, i64 0))
br label %19
19: ; preds = %17, %26, %20
br label %8
20: ; preds = %15
%21 = call i64 @FD_ISSET(i32 0, i32* %2)
%22 = icmp eq i64 %21, 0
br i1 %22, label %19, label %23
23: ; preds = %20
%24 = call i32 @read(i32 0, i8* %7, i32 255)
%25 = icmp slt i32 %24, 1
br i1 %25, label %30, label %26
26: ; preds = %23
%27 = zext i32 %24 to i64
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %27
store i8 0, i8* %28
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %24, i8* %7)
br label %19
30: ; preds = %23, %8
%31 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), %23 ]
%32 = call i32 @perror(i8* %31)
%33 = call i32 @reset_terminal(%struct.termios* %1)
ret i32 0
}