#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 fd1, fd2;
  fd_set input, tset;
  int max_fd;
  char c;
  if ((fd1 = open("/dev/mychar0", O_RDWR)) == -1) {
    perror("Error Opening File: ");
    return -1;
  }
  if ((fd2 = open("/dev/gpio0", O_RDWR)) == -1) {
    perror("Error Opening File: ");
    return -1;
  }
  FD_ZERO(&input);
  FD_SET(fd1, &input);
  FD_SET(fd2, &input);
  tset = input;
  max_fd = ((fd1 > fd2) ? (fd1 + 1) : (fd2 + 1));
  while (select(max_fd, &tset, NULL, NULL, NULL) > 0) {
    if (FD_ISSET(fd1, &tset)) {
      printf("Got input on first device file\n");
      if (read(fd1, &c, 1) > 0) {
        printf("Value read is %c\n", c);
      }
    } else if (FD_ISSET(fd2, &tset)) {
      printf("Got input on second device file\n");
      if (read(fd2, &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    1335 <main+0x1b5>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    0xe4a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1201 <main+0x81>
lea    0xe1d(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <main+0x1b5>
lea    -0x10(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    1242 <main+0xc2>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    124b <main+0xcb>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <select@plt>
cmp    $0x0,%rax
jle    132e <main+0x1ae>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12c8 <main+0x148>
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jle    12c3 <main+0x143>
movsbl -0x19(%rbp),%esi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1323 <main+0x1a3>
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     131e <main+0x19e>
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jle    1319 <main+0x199>
movsbl -0x19(%rbp),%esi
lea    0xd3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    131e <main+0x19e>
jmp    1323 <main+0x1a3>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1251 <main+0xd1>
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),%rbp
mov    0x0(%rbp),%esi
lea    0xe51(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12dd <main+0x14d>
mov    %eax,%ebx
mov    0x0(%rbp),%esi
lea    0xe59(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12dd <main+0x14d>
mov    %eax,%r14d
lea    0x14(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10b0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   10b0 <FD_SET@plt>
mov    0x14(%rsp),%eax
mov    %eax,0x10(%rsp)
cmp    %r14d,%ebx
mov    %r14d,%r15d
cmovg  %ebx,%r15d
add    $0x1,%r15d
xor    %r12d,%r12d
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %rax,%rax
jle    12ef <main+0x15f>
lea    0x10(%rsp),%rbp
lea    0xf(%rsp),%r13
jmp    1266 <main+0xd6>
xchg   %ax,%ax
mov    0x14(%rsp),%eax
mov    %eax,0x10(%rsp)
xor    %r12d,%r12d
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %rax,%rax
jle    12ef <main+0x15f>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1290 <main+0x100>
lea    0xde3(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
jmp    12af <main+0x11f>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1240 <main+0xb0>
lea    0xd98(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    1240 <main+0xb0>
movsbl 0xf(%rsp),%esi
lea    0xd5c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1240 <main+0xb0>
lea    0xd29(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%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 = [11 x i8] c"/dev/gpio0\00"
@.str.3 = [32 x i8] c"Got input on first device file\0A\00"
@.str.4 = [18 x i8] c"Value read is %c\0A\00"
@.str.5 = [33 x i8] c"Got input on second device file\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i32 0, i32* %1
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = 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 %73
13: ; preds = %0
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %14)
store i32 %15, i32* %3
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = 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 %73
19: ; preds = %13
%20 = call i32 @FD_ZERO(i32* %4)
%21 = load i32, i32* %2
%22 = call i32 @FD_SET(i32 %21, i32* %4)
%23 = load i32, i32* %3
%24 = call i32 @FD_SET(i32 %23, i32* %4)
%25 = load i32, i32* %4
store i32 %25, i32* %5
%26 = load i32, i32* %2
%27 = load i32, i32* %3
%28 = icmp sgt i32 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = load i32, i32* %2
%31 = add nsw i32 %30, 1
br label %35
32: ; preds = %19
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
br label %35
35: ; preds = %32, %29
%36 = phi i32 [ %31, %29 ], [ %34, %32 ]
store i32 %36, i32* %6
br label %37
37: ; preds = %70, %35
%38 = load i32, i32* %6
%39 = call i64 @select(i32 %38, i32* %5, i32* null, i32* null, i32* null)
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %72
41: ; preds = %37
%42 = load i32, i32* %2
%43 = call i64 @FD_ISSET(i32 %42, i32* %5)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %41
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* %2
%48 = call i64 @read(i32 %47, i8* %7, i32 1)
%49 = icmp sgt i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = load i8, i8* %7
%52 = sext i8 %51 to i32
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %50, %45
br label %70
55: ; preds = %41
%56 = load i32, i32* %3
%57 = call i64 @FD_ISSET(i32 %56, i32* %5)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %69
59: ; preds = %55
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%61 = load i32, i32* %3
%62 = call i64 @read(i32 %61, i8* %7, i32 1)
%63 = icmp sgt i64 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %59
%65 = load i8, i8* %7
%66 = sext i8 %65 to i32
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %66)
br label %68
68: ; preds = %64, %59
br label %69
69: ; preds = %68, %55
br label %70
70: ; preds = %69, %54
%71 = load i32, i32* %4
store i32 %71, i32* %5
br label %37
72: ; preds = %37
store i32 0, i32* %1
br label %73
73: ; preds = %72, %17, %11
%74 = load i32, i32* %1
ret i32 %74
}

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.2 = [11 x i8] c"/dev/gpio0\00"
@.str.4 = [18 x i8] c"Value read is %c\0A\00"
@str = [32 x i8] c"Got input on second device file\00"
@str.6 = [31 x i8] c"Got input on first device file\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 %45, label %9
9: ; preds = %0
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %45, label %13
13: ; preds = %9
%14 = call i32 @FD_ZERO(i32* %1)
%15 = call i32 @FD_SET(i32 %7, i32* %1)
%16 = call i32 @FD_SET(i32 %11, i32* %1)
%17 = load i32, i32* %1
store i32 %17, i32* %2
%18 = icmp sgt i32 %7, %11
%19 = select i1 %18, i32 %7, i32 %11
%20 = add nuw nsw i32 %19, 1
%21 = call i64 @select(i32 %20, i32* %2, i32* null, i32* null, i32* null)
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %47
23: ; preds = %13, %41
%24 = call i64 @FD_ISSET(i32 %7, i32* %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %23
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.6, i64 0, i64 0))
%28 = call i64 @read(i32 %7, i8* %3, i32 1)
%29 = icmp sgt i64 %28, 0
br i1 %29, label %37, label %41
30: ; preds = %23
%31 = call i64 @FD_ISSET(i32 %11, i32* %2)
%32 = icmp eq i64 %31, 0
br i1 %32, label %41, label %33
33: ; preds = %30
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%35 = call i64 @read(i32 %11, i8* %3, i32 1)
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %33, %26
%38 = load i8, i8* %3
%39 = sext i8 %38 to i32
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %37, %30, %33, %26
%42 = load i32, i32* %1
store i32 %42, i32* %2
%43 = call i64 @select(i32 %20, i32* %2, i32* null, i32* null, i32* null)
%44 = icmp sgt i64 %43, 0
br i1 %44, label %23, label %47
45: ; preds = %9, %0
%46 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %47
47: ; preds = %41, %45, %13
%48 = phi i32 [ 0, %13 ], [ -1, %45 ], [ 0, %41 ]
ret i32 %48
}