#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;

/* 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_RDONLY;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ showdata(char *, int);
int /*<<< orphan*/ stderr;
int main(int ac, char *av[]) {
  int fd1, fd2;
  struct timeval timeout;
  fd_set readfds;
  int maxfd;
  int retval;
  if (ac != 4) {
    fprintf(stderr, "useage: %s file file timeout", *av);
    exit(1);
  }
  if ((fd1 = open(av[1], O_RDONLY)) == -1) {
    perror(av[1]);
    exit(2);
  };
  if ((fd2 = open(av[2], O_RDONLY)) == -1) {
    perror(av[2]);
    exit(3);
  };
  maxfd = 1 + (fd1 > fd2 ? fd1 : fd2);
  while (1) {
    FD_ZERO(&readfds);
    FD_SET(fd1, &readfds);
    FD_SET(fd2, &readfds);
    timeout.tv_sec = atoi(av[3]);
    timeout.tv_usec = 0;
    retval = select(maxfd, &readfds, NULL, NULL, &timeout);
    if (retval == -1) {
      perror("select");
      exit(4);
    };
    if (retval > 0) {
      if (FD_ISSET(fd1, &readfds))
        showdata(av[1], fd1);
      if (FD_ISSET(fd2, &readfds))
        showdata(av[2], fd2);
    } else {
      printf("no input after %d seconds\n", atoi(av[3]));
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11f6 <main+0x46>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe19(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    122f <main+0x7f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <perror@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1268 <main+0xb8>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <perror@plt>
mov    $0x3,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    127f <main+0xcf>
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    1285 <main+0xd5>
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
lea    -0x2c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x18(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1301 <main+0x151>
lea    0xd2b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x4,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x34(%rbp)
jle    135c <main+0x1ac>
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1331 <main+0x181>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x14(%rbp),%esi
call   1050 <showdata@plt>
mov    -0x18(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1357 <main+0x1a7>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x18(%rbp),%esi
call   1050 <showdata@plt>
jmp    1377 <main+0x1c7>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,%esi
lea    0xcb2(%rip),%rdi
call   1040 <printf@plt>
jmp    128e <main+0xde>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
cmp    $0x4,%edi
jne    11f0 <main+0x40>
mov    0x8(%r14),%rdi
mov    0x2e17(%rip),%rbx
mov    (%rbx),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
jne    1212 <main+0x62>
mov    0x8(%r14),%rdi
call   10a0 <perror@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdfd(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %eax,%r12d
mov    0x10(%r14),%rdi
mov    (%rbx),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
jne    1238 <main+0x88>
mov    0x10(%r14),%rdi
call   10a0 <perror@plt>
mov    $0x3,%edi
call   10c0 <exit@plt>
mov    %eax,%ebx
cmp    %eax,%r12d
mov    %eax,%r15d
cmovg  %r12d,%r15d
add    $0x1,%r15d
lea    0x4(%rsp),%rbp
lea    0x8(%rsp),%r13
jmp    1277 <main+0xc7>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x18(%r14),%rdi
call   10b0 <atoi@plt>
lea    0xdb4(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
mov    0x18(%r14),%rdi
call   10b0 <atoi@plt>
mov    %eax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1080 <select@plt>
cmp    $0xffffffff,%eax
je     1300 <main+0x150>
test   %eax,%eax
jle    1260 <main+0xb0>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12e1 <main+0x131>
mov    0x8(%r14),%rdi
mov    %r12d,%esi
call   1050 <showdata@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1277 <main+0xc7>
mov    0x10(%r14),%rdi
mov    %ebx,%esi
call   1050 <showdata@plt>
jmp    1277 <main+0xc7>
lea    0xd16(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x4,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@stderr = global i32 0
@.str = [29 x i8] c"useage: %s file file timeout\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [27 x i8] c"no input after %d seconds\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 4
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %20
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @perror(i8* %30)
%32 = call i32 @exit(i32 2)
unreachable
33: ; preds = %20
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = load i32, i32* @O_RDONLY
%38 = call i32 @open(i8* %36, i32 %37)
store i32 %38, i32* %7
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %46
40: ; preds = %33
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = load i8*, i8** %42
%44 = call i32 @perror(i8* %43)
%45 = call i32 @exit(i32 3)
unreachable
46: ; preds = %33
%47 = load i32, i32* %6
%48 = load i32, i32* %7
%49 = icmp sgt i32 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = load i32, i32* %6
br label %54
52: ; preds = %46
%53 = load i32, i32* %7
br label %54
54: ; preds = %52, %50
%55 = phi i32 [ %51, %50 ], [ %53, %52 ]
%56 = add nsw i32 1, %55
store i32 %56, i32* %10
br label %57
57: ; preds = %54, %106
%58 = call i32 @FD_ZERO(i32* %9)
%59 = load i32, i32* %6
%60 = call i32 @FD_SET(i32 %59, i32* %9)
%61 = load i32, i32* %7
%62 = call i32 @FD_SET(i32 %61, i32* %9)
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 3
%65 = load i8*, i8** %64
%66 = call i32 @atoi(i8* %65)
%67 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %66, i32* %67
%68 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %68
%69 = load i32, i32* %10
%70 = call i32 @select(i32 %69, i32* %9, i32* null, i32* null, %struct.timeval* %8)
store i32 %70, i32* %11
%71 = load i32, i32* %11
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %76
73: ; preds = %57
%74 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%75 = call i32 @exit(i32 4)
unreachable
76: ; preds = %57
%77 = load i32, i32* %11
%78 = icmp sgt i32 %77, 0
br i1 %78, label %79, label %100
79: ; preds = %76
%80 = load i32, i32* %6
%81 = call i64 @FD_ISSET(i32 %80, i32* %9)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %89
83: ; preds = %79
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 1
%86 = load i8*, i8** %85
%87 = load i32, i32* %6
%88 = call i32 @showdata(i8* %86, i32 %87)
br label %89
89: ; preds = %83, %79
%90 = load i32, i32* %7
%91 = call i64 @FD_ISSET(i32 %90, i32* %9)
%92 = icmp ne i64 %91, 0
br i1 %92, label %93, label %99
93: ; preds = %89
%94 = load i8**, i8*** %5
%95 = getelementptr inbounds i8*, i8** %94, i64 2
%96 = load i8*, i8** %95
%97 = load i32, i32* %7
%98 = call i32 @showdata(i8* %96, i32 %97)
br label %99
99: ; preds = %93, %89
br label %106
100: ; preds = %76
%101 = load i8**, i8*** %5
%102 = getelementptr inbounds i8*, i8** %101, i64 3
%103 = load i8*, i8** %102
%104 = call i32 @atoi(i8* %103)
%105 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %104)
br label %106
106: ; preds = %100, %99
br label %57
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@stderr = global i32 0
@.str = [29 x i8] c"useage: %s file file timeout\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [27 x i8] c"no input after %d seconds\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 4
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 @open(i8* %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = load i8*, i8** %14
%21 = tail call i32 @perror(i8* %20)
%22 = tail call i32 @exit(i32 2)
unreachable
23: ; preds = %13
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDONLY
%27 = tail call i32 @open(i8* %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i8*, i8** %24
%31 = tail call i32 @perror(i8* %30)
%32 = tail call i32 @exit(i32 3)
unreachable
33: ; preds = %23
%34 = icmp sgt i32 %17, %27
%35 = select i1 %34, i32 %17, i32 %27
%36 = add nuw nsw i32 %35, 1
%37 = getelementptr inbounds i8*, i8** %1, i64 3
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
br label %40
40: ; preds = %62, %33
%41 = call i32 @FD_ZERO(i32* %4)
%42 = call i32 @FD_SET(i32 %17, i32* %4)
%43 = call i32 @FD_SET(i32 %27, i32* %4)
%44 = load i8*, i8** %37
%45 = call i32 @atoi(i8* %44)
store i32 %45, i32* %38
store i64 0, i64* %39
%46 = call i32 @select(i32 %36, i32* %4, i32* null, i32* null, %struct.timeval* %3)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%50 = call i32 @exit(i32 4)
unreachable
51: ; preds = %40
%52 = icmp sgt i32 %46, 0
br i1 %52, label %53, label %66
53: ; preds = %51
%54 = call i64 @FD_ISSET(i32 %17, i32* %4)
%55 = icmp eq i64 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %53
%57 = load i8*, i8** %14
%58 = call i32 @showdata(i8* %57, i32 %17)
br label %59
59: ; preds = %56, %53
%60 = call i64 @FD_ISSET(i32 %27, i32* %4)
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %59, %63, %66
br label %40
63: ; preds = %59
%64 = load i8*, i8** %24
%65 = call i32 @showdata(i8* %64, i32 %27)
br label %62
66: ; preds = %51
%67 = load i8*, i8** %37
%68 = call i32 @atoi(i8* %67)
%69 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %68)
br label %62
}