#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ isprint(char);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char);
int read(int, void *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int receive_teststring(int fd) {
  fd_set rfds;
  struct timeval timeout;
  int nuf_fd;
  int nuf_read = 0;
  char buffer = 0;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  timeout.tv_sec = 1;
  timeout.tv_usec = 0;
  nuf_fd = select(fd + 1, &rfds, NULL, NULL, &timeout);
  if (nuf_fd == 1) {
  } else if (nuf_fd == 0) {
    return -2;
  } else if (nuf_fd == -1) {
    perror("select");
    return -1;
  } else {
    fprintf(stderr, "Unknown return value from read select.\n");
    return -1;
  }
  nuf_read = read(fd, (void *)&buffer, sizeof(buffer));
  if (nuf_read != sizeof(buffer)) {
    fprintf(stderr, "read uneven number of read data");
  }
  if (isprint(buffer)) {
    printf("%c", buffer);
  } else {
    printf("[%d]", buffer);
  }
  return 0;
}


x86 assembly (clang, -O0):
<receive_teststring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x28(%rbp)
movb   $0x0,-0x29(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10a0 <FD_SET@plt>
movl   $0x1,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    11e6 <receive_teststring+0x66>
jmp    123f <receive_teststring+0xbf>
cmpl   $0x0,-0x24(%rbp)
jne    11fc <receive_teststring+0x7c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12b5 <receive_teststring+0x135>
cmpl   $0xffffffff,-0x24(%rbp)
jne    121e <receive_teststring+0x9e>
lea    0xdf3(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <receive_teststring+0x135>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <receive_teststring+0x135>
mov    -0x8(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
movslq -0x28(%rbp),%rax
cmp    $0x1,%rax
je     1276 <receive_teststring+0xf6>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
call   1050 <fprintf@plt>
movsbl -0x29(%rbp),%edi
call   1080 <isprint@plt>
cmp    $0x0,%rax
je     129e <receive_teststring+0x11e>
lea    0xdbf(%rip),%rdi
movsbl -0x29(%rbp),%esi
call   1030 <printf@plt>
jmp    12ae <receive_teststring+0x12e>
lea    0xdad(%rip),%rdi
movsbl -0x29(%rbp),%esi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_teststring>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movb   $0x0,0x3(%rsp)
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10a0 <FD_SET@plt>
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x1(%rbx),%edi
lea    0x8(%rsp),%r8
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
test   %eax,%eax
je     11f8 <receive_teststring+0x78>
cmp    $0x1,%eax
je     11ff <receive_teststring+0x7f>
cmp    $0xffffffff,%eax
je     125c <receive_teststring+0xdc>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1254 <receive_teststring+0xd4>
mov    $0xfffffffe,%eax
jmp    1254 <receive_teststring+0xd4>
lea    0x3(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     122a <receive_teststring+0xaa>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1050 <fprintf@plt>
movsbl 0x3(%rsp),%ebx
mov    %ebx,%edi
call   1080 <isprint@plt>
test   %rax,%rax
je     1244 <receive_teststring+0xc4>
lea    0xe0d(%rip),%rdi
jmp    124b <receive_teststring+0xcb>
lea    0xe07(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd9d(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    1254 <receive_teststring+0xd4>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [7 x i8] c"select\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"Unknown return value from read select.\0A\00"
@.str.2 = [32 x i8] c"read uneven number of read data\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [5 x i8] c"[%d]\00"
define i32 @receive_teststring(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i32 %0, i32* %3
store i32 0, i32* %7
store i8 0, i8* %8
%9 = call i32 @FD_ZERO(i32* %4)
%10 = load i32, i32* %3
%11 = call i32 @FD_SET(i32 %10, i32* %4)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i32 1, i32* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i64 0, i64* %13
%14 = load i32, i32* %3
%15 = add nsw i32 %14, 1
%16 = call i32 @select(i32 %15, i32* %4, i32* null, i32* null, %struct.timeval* %5)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %20
19: ; preds = %1
br label %32
20: ; preds = %1
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 -2, i32* %2
br label %52
24: ; preds = %20
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %52
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %52
32: ; preds = %19
%33 = load i32, i32* %3
%34 = call i32 @read(i32 %33, i8* %8, i32 1)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = icmp ne i64 %36, 1
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %38, %32
%42 = load i8, i8* %8
%43 = call i64 @isprint(i8 signext %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i8, i8* %8
%47 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8 signext %46)
br label %51
48: ; preds = %41
%49 = load i8, i8* %8
%50 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8 signext %49)
br label %51
51: ; preds = %48, %45
store i32 0, i32* %2
br label %52
52: ; preds = %51, %29, %27, %23
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [7 x i8] c"select\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"Unknown return value from read select.\0A\00"
@.str.2 = [32 x i8] c"read uneven number of read data\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [5 x i8] c"[%d]\00"
define i32 @receive_teststring(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca i8
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.timeval* %3 to i8*
store i8 0, i8* %4
%7 = call i32 @FD_ZERO(i32* %2)
%8 = call i32 @FD_SET(i32 %0, i32* %2)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 1, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 0, i64* %10
%11 = add nsw i32 %0, 1
%12 = call i32 @select(i32 %11, i32* %2, i32* null, i32* null, %struct.timeval* %3)
switch i32 %12, label %15 [
i32 1, label %18
i32 0, label %32
i32 -1, label %13
]
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %32
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %32
18: ; preds = %1
%19 = call i32 @read(i32 %0, i8* %4, i32 1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %21, %18
%25 = load i8, i8* %4
%26 = call i64 @isprint(i8 signext %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8 signext %25)
br label %32
30: ; preds = %24
%31 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8 signext %25)
br label %32
32: ; preds = %28, %30, %1, %15, %13
%33 = phi i32 [ -1, %13 ], [ -1, %15 ], [ -2, %1 ], [ 0, %30 ], [ 0, %28 ]
ret i32 %33
}