#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 /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ O_RDWR;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main() {
  int fd;
  fd_set input, tset;
  int max_fd;
  char c;
  if ((fd = open("/dev/mychar0", O_RDWR)) == -1) {
    perror("Error Opening File: ");
    return -1;
  }
  FD_ZERO(&input);
  FD_SET(fd, &input);
  tset = input;
  max_fd = fd + 1;
  while (select(max_fd, &tset, NULL, NULL, NULL) > 0) {
    if (FD_ISSET(fd, &tset)) {
      printf("Got Select\n");
      if (read(fd, &c, 1) > 0) {
        printf("Value read is %c\n", c);
      }
    }
    tset = input;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11c8 <main+0x48>
lea    0xe56(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1275 <main+0xf5>
lea    -0xc(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <select@plt>
cmp    $0x0,%rax
jle    126e <main+0xee>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1263 <main+0xe3>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jle    125e <main+0xde>
movsbl -0x15(%rbp),%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1263 <main+0xe3>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11ec <main+0x6c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1282 <main+0xf2>
mov    %eax,%ebx
lea    0x14(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10b0 <FD_SET@plt>
mov    0x14(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%r14d
add    $0x1,%r14d
xor    %r13d,%r13d
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %rax,%rax
jle    1294 <main+0x104>
lea    0x10(%rsp),%rbp
lea    0xe24(%rip),%r15
lea    0xf(%rsp),%r12
jmp    1242 <main+0xb2>
nopw   0x0(%rax,%rax,1)
mov    0x14(%rsp),%eax
mov    %eax,0x10(%rsp)
xor    %r13d,%r13d
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %rax,%rax
jle    1294 <main+0x104>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1220 <main+0x90>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    1220 <main+0x90>
movsbl 0xf(%rsp),%esi
lea    0xda9(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1220 <main+0x90>
lea    0xd84(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/mychar0\00"
@O_RDWR = global i32 0
@.str.1 = [21 x i8] c"Error Opening File: \00"
@.str.2 = [12 x i8] c"Got Select\0A\00"
@.str.3 = [18 x i8] c"Value read is %c\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i32 0, i32* %1
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %40
12: ; preds = %0
%13 = call i32 @FD_ZERO(i32* %3)
%14 = load i32, i32* %2
%15 = call i32 @FD_SET(i32 %14, i32* %3)
%16 = load i32, i32* %3
store i32 %16, i32* %4
%17 = load i32, i32* %2
%18 = add nsw i32 %17, 1
store i32 %18, i32* %5
br label %19
19: ; preds = %37, %12
%20 = load i32, i32* %5
%21 = call i64 @select(i32 %20, i32* %4, i32* null, i32* null, i32* null)
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %39
23: ; preds = %19
%24 = load i32, i32* %2
%25 = call i64 @FD_ISSET(i32 %24, i32* %4)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* %2
%30 = call i64 @read(i32 %29, i8* %6, i32 1)
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i8, i8* %6
%34 = sext i8 %33 to i32
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %32, %27
br label %37
37: ; preds = %36, %23
%38 = load i32, i32* %3
store i32 %38, i32* %4
br label %19
39: ; preds = %19
store i32 0, i32* %1
br label %40
40: ; preds = %39, %10
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/mychar0\00"
@O_RDWR = global i32 0
@.str.1 = [21 x i8] c"Error Opening File: \00"
@.str.3 = [18 x i8] c"Value read is %c\0A\00"
@str = [11 x i8] c"Got Select\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %33
11: ; preds = %0
%12 = call i32 @FD_ZERO(i32* %1)
%13 = call i32 @FD_SET(i32 %7, i32* %1)
%14 = load i32, i32* %1
store i32 %14, i32* %2
%15 = add nuw nsw i32 %7, 1
%16 = call i64 @select(i32 %15, i32* %2, i32* null, i32* null, i32* null)
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %33
18: ; preds = %11, %29
%19 = call i64 @FD_ISSET(i32 %7, i32* %2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %18
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%23 = call i64 @read(i32 %7, i8* %3, i32 1)
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i8, i8* %3
%27 = sext i8 %26 to i32
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %21, %25, %18
%30 = load i32, i32* %1
store i32 %30, i32* %2
%31 = call i64 @select(i32 %15, i32* %2, i32* null, i32* null, i32* null)
%32 = icmp sgt i64 %31, 0
br i1 %32, label %18, label %33
33: ; preds = %29, %11, %9
%34 = phi i32 [ -1, %9 ], [ 0, %11 ], [ 0, %29 ]
ret i32 %34
}