#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 ssize_t;
typedef int /*<<< orphan*/ readbuf;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int O_NONBLOCK;
int O_RDWR;
int errno;
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
char *strerror(int);
int main(int args, char *argv[]) {
  fd_set rfds;
  struct timeval tv;
  if (args != 2) {
    printf("Usage: ./dupSerial serialDevice (ie: ./dupSerial /dev/ttyUSB0\n");
    return -1;
  }
  int err, fd;
  fd = open(argv[1], O_RDWR | O_NONBLOCK);
  if (fd == -1) {
    err = errno;
    printf("open %s\n", strerror(err));
    return -1;
  }
  FD_SET(fd, &rfds);
  tv.tv_sec = 5;
  tv.tv_usec = 0;
  int selected = select(fd + 1, &rfds, NULL, NULL, &tv);
  if (selected == -1) {
    err = errno;
    printf("select %s\n", strerror(err));
    return -1;
  } else if (selected) {
    printf("Data is available now.\n");
    if (FD_ISSET(fd, &rfds)) {
      char readbuf[80];
      ssize_t readed = read(fd, readbuf, sizeof(readbuf));
      if (readed == -1) {
        err = errno;
        printf("read %s\n", strerror(err));
        return -1;
      }
      printf("size of read buf %ld\n", readed);
      readbuf[readed] = '\0';
      printf("%s", readbuf);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ad <main+0x3d>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134c <main+0x1dc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2e1b(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    120a <main+0x9a>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134c <main+0x1dc>
mov    -0x30(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1090 <FD_SET@plt>
movl   $0x5,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%edi
add    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    127d <main+0x10d>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134c <main+0x1dc>
cmpl   $0x0,-0x34(%rbp)
je     1340 <main+0x1d0>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     133b <main+0x1cb>
mov    -0x30(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x50,%edx
call   1050 <read@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
jne    1303 <main+0x193>
mov    0x2cf7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134c <main+0x1dc>
mov    -0x94(%rbp),%esi
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0x94(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x90(%rbp),%rsi
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1340 <main+0x1d0>
jmp    1345 <main+0x1d5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x78,%rsp
cmp    $0x2,%edi
jne    1205 <main+0x85>
mov    0x8(%rsi),%rdi
mov    0x2e52(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1213 <main+0x93>
mov    %eax,%ebx
lea    0xc(%rsp),%rbp
mov    %eax,%edi
mov    %rbp,%rsi
call   10a0 <FD_SET@plt>
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %ebx,%edi
add    $0x1,%edi
lea    0x10(%rsp),%r8
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1237 <main+0xb7>
cmp    $0xffffffff,%ebp
jne    1240 <main+0xc0>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe06(%rip),%rdi
jmp    1228 <main+0xa8>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
jmp    1232 <main+0xb2>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd8(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x78,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdef(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12a4 <main+0x124>
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0x50,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     12a8 <main+0x128>
mov    %eax,%ebx
lea    0xda1(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movslq %ebx,%rax
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xd9d(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1237 <main+0xb7>
xor    %ebp,%ebp
jmp    1237 <main+0xb7>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd57(%rip),%rdi
jmp    1228 <main+0xa8>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [63 x i8] c"Usage: ./dupSerial serialDevice (ie: ./dupSerial /dev/ttyUSB0\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@.str.1 = [9 x i8] c"open %s\0A\00"
@.str.2 = [11 x i8] c"select %s\0A\00"
@.str.3 = [24 x i8] c"Data is available now.\0A\00"
@.str.4 = [9 x i8] c"read %s\0A\00"
@.str.5 = [22 x i8] c"size of read buf %ld\0A\00"
@.str.6 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [80 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %77
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %20, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %17
%28 = load i32, i32* @errno
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %30)
store i32 -1, i32* %3
br label %77
32: ; preds = %17
%33 = load i32, i32* %9
%34 = call i32 @FD_SET(i32 %33, i32* %6)
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 5, i32* %35
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i64 0, i64* %36
%37 = load i32, i32* %9
%38 = add nsw i32 %37, 1
%39 = call i32 @select(i32 %38, i32* %6, i32* null, i32* null, %struct.timeval* %7)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %47
42: ; preds = %32
%43 = load i32, i32* @errno
store i32 %43, i32* %8
%44 = load i32, i32* %8
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %45)
store i32 -1, i32* %3
br label %77
47: ; preds = %32
%48 = load i32, i32* %10
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %75
50: ; preds = %47
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%52 = load i32, i32* %9
%53 = call i64 @FD_ISSET(i32 %52, i32* %6)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %74
55: ; preds = %50
%56 = load i32, i32* %9
%57 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8* %57, i32 80)
store i32 %58, i32* %12
%59 = load i32, i32* %12
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %66
61: ; preds = %55
%62 = load i32, i32* @errno
store i32 %62, i32* %8
%63 = load i32, i32* %8
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %64)
store i32 -1, i32* %3
br label %77
66: ; preds = %55
%67 = load i32, i32* %12
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %67)
%69 = load i32, i32* %12
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 %70
store i8 0, i8* %71
%72 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %72)
br label %74
74: ; preds = %66, %50
br label %75
75: ; preds = %74, %47
br label %76
76: ; preds = %75
store i32 0, i32* %3
br label %77
77: ; preds = %76, %61, %42, %27, %15
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@.str.1 = [9 x i8] c"open %s\0A\00"
@.str.2 = [11 x i8] c"select %s\0A\00"
@.str.4 = [9 x i8] c"read %s\0A\00"
@.str.5 = [22 x i8] c"size of read buf %ld\0A\00"
@.str.6 = [3 x i8] c"%s\00"
@str = [23 x i8] c"Data is available now.\00"
@str.7 = [62 x i8] c"Usage: ./dupSerial serialDevice (ie: ./dupSerial /dev/ttyUSB0\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca [80 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = icmp eq i32 %0, 2
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str.7, i64 0, i64 0))
br label %50
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_NONBLOCK
%16 = or i32 %15, %14
%17 = tail call i32 @open(i8* %13, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %11
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %50
23: ; preds = %11
%24 = call i32 @FD_SET(i32 %17, i32* %3)
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 5, i32* %25
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %26
%27 = add nuw nsw i32 %17, 1
%28 = call i32 @select(i32 %27, i32* %3, i32* null, i32* null, %struct.timeval* %4)
switch i32 %28, label %33 [
i32 -1, label %29
i32 0, label %50
]
29: ; preds = %23
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %50
33: ; preds = %23
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%35 = call i64 @FD_ISSET(i32 %17, i32* %3)
%36 = icmp eq i64 %35, 0
br i1 %36, label %50, label %37
37: ; preds = %33
%38 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%39 = call i32 @read(i32 %17, i8* %38, i32 80)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %43)
br label %50
45: ; preds = %37
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %39)
%47 = sext i32 %39 to i64
%48 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 %47
store i8 0, i8* %48
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %38)
br label %50
50: ; preds = %33, %23, %45, %41, %19, %29, %9
%51 = phi i32 [ -1, %9 ], [ -1, %19 ], [ -1, %29 ], [ -1, %41 ], [ 0, %45 ], [ %28, %23 ], [ 0, %33 ]
ret i32 %51
}