#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;
  int /*<<< orphan*/ member_0;
};
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*/ MAP_SHARED;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ dump_mem(unsigned char *, int);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
unsigned char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                    int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(unsigned char *, int);
int /*<<< orphan*/ printf(char *, ...);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int lgx_test(int fd[], int num) {
  unsigned char *mem[4];
  int i;
  unsigned char *buf;
  fd_set read_set;
  struct timeval read_timeout = {0};
  int ret;
  buf = (unsigned char *)malloc(0x11000);
  memset(buf, 0, 0x11000);
  for (i = 0; i < num; i++)
    mem[i] = mmap(NULL, 0x10000, PROT_READ, MAP_SHARED, fd[i], 0);
  FD_ZERO(&read_set);
  for (i = 0; i < num; i++) {
    FD_SET(fd[i], &read_set);
  }
  read_timeout.tv_sec = 3;
  ret = select(fd[num - 1] + 1, &read_set, NULL, NULL, &read_timeout);
  if (ret < 0) {
    printf("select error!\n");
  } else if (ret == 0) {
    printf("select time out!\n");
  } else {
    for (i = 0; i < num; i++) {
      if (FD_ISSET(fd[i], &read_set)) {
        printf("lg %d select return!\n", i);
        dump_mem(mem[i], 20);
      }
    }
  }
  for (i = 0; i < num; i++)
    munmap(mem[i], 0x10000);
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<lgx_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    $0x11000,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x11000,%edx
call   1080 <memset@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1249 <lgx_test+0x99>
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rsi
mov    (%rax,%rsi,4),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x10000,%esi
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
mov    %rcx,-0x30(%rbp,%rax,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11f4 <lgx_test+0x44>
lea    -0x44(%rbp),%rdi
call   1090 <FD_ZERO@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1287 <lgx_test+0xd7>
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x44(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1259 <lgx_test+0xa9>
movl   $0x3,-0x50(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edi
add    $0x1,%edi
lea    -0x44(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x50(%rbp),%r8
mov    %rcx,%rdx
call   10a0 <select@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    12d5 <lgx_test+0x125>
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1364 <lgx_test+0x1b4>
cmpl   $0x0,-0x54(%rbp)
jne    12f2 <lgx_test+0x142>
lea    0xd29(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    135f <lgx_test+0x1af>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    135a <lgx_test+0x1aa>
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x44(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1347 <lgx_test+0x197>
mov    -0x34(%rbp),%esi
lea    0xcf4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movslq -0x34(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rdi
mov    $0x14,%esi
call   1050 <dump_mem@plt>
jmp    134c <lgx_test+0x19c>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12f9 <lgx_test+0x149>
jmp    135f <lgx_test+0x1af>
jmp    1364 <lgx_test+0x1b4>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1398 <lgx_test+0x1e8>
movslq -0x34(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rdi
mov    $0x10000,%esi
call   10c0 <munmap@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    136b <lgx_test+0x1bb>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lgx_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
movq   $0x3,0x18(%rsp)
mov    $0x11000,%edi
call   10c0 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x11000,%edx
call   1090 <memset@plt>
test   %r14d,%r14d
jle    1287 <lgx_test+0xc7>
mov    %r14d,0x10(%rsp)
mov    %r14d,%ebp
xor    %ebx,%ebx
mov    0x2de5(%rip),%r14
mov    0x2dce(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edx
mov    (%r12),%ecx
mov    (%r15,%rbx,4),%r8d
xor    %edi,%edi
mov    $0x10000,%esi
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,0x20(%rsp,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1220 <lgx_test+0x60>
lea    0xc(%rsp),%rdi
call   10a0 <FD_ZERO@plt>
mov    0x10(%rsp),%r14d
test   %r14d,%r14d
jle    1291 <lgx_test+0xd1>
xor    %ebx,%ebx
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r15,%rbx,4),%edi
mov    %r12,%rsi
call   10e0 <FD_SET@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1270 <lgx_test+0xb0>
jmp    1291 <lgx_test+0xd1>
lea    0xc(%rsp),%rdi
call   10a0 <FD_ZERO@plt>
movslq %r14d,%rax
mov    -0x4(%r15,%rax,4),%edi
add    $0x1,%edi
lea    0xc(%rsp),%rsi
lea    0x18(%rsp),%r8
xor    %edx,%edx
xor    %ecx,%ecx
call   10b0 <select@plt>
test   %eax,%eax
js     1317 <lgx_test+0x157>
je     1328 <lgx_test+0x168>
test   %r14d,%r14d
jle    1368 <lgx_test+0x1a8>
mov    %r13,0x10(%rsp)
mov    %r14d,%ebx
mov    %r14d,%r14d
lea    0xc(%rsp),%r13
lea    0xd2b(%rip),%r12
xor    %ebp,%ebp
jmp    12e9 <lgx_test+0x129>
nopl   0x0(%rax)
add    $0x1,%rbp
cmp    %rbp,%r14
je     133c <lgx_test+0x17c>
mov    (%r15,%rbp,4),%edi
mov    %r13,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     12e0 <lgx_test+0x120>
mov    %r12,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x20(%rsp,%rbp,8),%rdi
mov    $0x14,%esi
call   1060 <dump_mem@plt>
jmp    12e0 <lgx_test+0x120>
mov    %r14d,%ebx
mov    %r13,0x10(%rsp)
lea    0xd01(%rip),%rdi
jmp    1337 <lgx_test+0x177>
mov    %r14d,%ebx
mov    %r13,0x10(%rsp)
lea    0xcdf(%rip),%rdi
call   1040 <puts@plt>
test   %ebx,%ebx
mov    0x10(%rsp),%r13
jle    1368 <lgx_test+0x1a8>
mov    %ebx,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    0x20(%rsp,%rbp,8),%rdi
mov    $0x10000,%esi
call   10d0 <munmap@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1350 <lgx_test+0x190>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str = [15 x i8] c"select error!\0A\00"
@.str.1 = [18 x i8] c"select time out!\0A\00"
@.str.2 = [22 x i8] c"lg %d select return!\0A\00"
define i32 @lgx_test(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca [4 x i8*]
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%11 = bitcast %struct.timeval* %9 to i8*
%12 = call i64 @malloc(i32 69632)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = call i32 @memset(i8* %14, i32 0, i32 69632)
store i32 0, i32* %6
br label %16
16: ; preds = %32, %2
%17 = load i32, i32* %6
%18 = load i32, i32* %4
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %35
20: ; preds = %16
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @MAP_SHARED
%23 = load i32*, i32** %3
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
%28 = call i8* @mmap(i32* null, i32 65536, i32 %21, i32 %22, i32 %27, i32 0)
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 %30
store i8* %28, i8** %31
br label %32
32: ; preds = %20
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
br label %16
35: ; preds = %16
%36 = call i32 @FD_ZERO(i32* %8)
store i32 0, i32* %6
br label %37
37: ; preds = %48, %35
%38 = load i32, i32* %6
%39 = load i32, i32* %4
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %51
41: ; preds = %37
%42 = load i32*, i32** %3
%43 = load i32, i32* %6
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %42, i64 %44
%46 = load i32, i32* %45
%47 = call i32 @FD_SET(i32 %46, i32* %8)
br label %48
48: ; preds = %41
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
br label %37
51: ; preds = %37
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 3, i32* %52
%53 = load i32*, i32** %3
%54 = load i32, i32* %4
%55 = sub nsw i32 %54, 1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %53, i64 %56
%58 = load i32, i32* %57
%59 = add nsw i32 %58, 1
%60 = call i32 @select(i32 %59, i32* %8, i32* null, i32* null, %struct.timeval* %9)
store i32 %60, i32* %10
%61 = load i32, i32* %10
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %51
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %97
65: ; preds = %51
%66 = load i32, i32* %10
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %65
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %96
70: ; preds = %65
store i32 0, i32* %6
br label %71
71: ; preds = %92, %70
%72 = load i32, i32* %6
%73 = load i32, i32* %4
%74 = icmp slt i32 %72, %73
br i1 %74, label %75, label %95
75: ; preds = %71
%76 = load i32*, i32** %3
%77 = load i32, i32* %6
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i32, i32* %76, i64 %78
%80 = load i32, i32* %79
%81 = call i64 @FD_ISSET(i32 %80, i32* %8)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %91
83: ; preds = %75
%84 = load i32, i32* %6
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %84)
%86 = load i32, i32* %6
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 %87
%89 = load i8*, i8** %88
%90 = call i32 @dump_mem(i8* %89, i32 20)
br label %91
91: ; preds = %83, %75
br label %92
92: ; preds = %91
%93 = load i32, i32* %6
%94 = add nsw i32 %93, 1
store i32 %94, i32* %6
br label %71
95: ; preds = %71
br label %96
96: ; preds = %95, %68
br label %97
97: ; preds = %96, %63
store i32 0, i32* %6
br label %98
98: ; preds = %108, %97
%99 = load i32, i32* %6
%100 = load i32, i32* %4
%101 = icmp slt i32 %99, %100
br i1 %101, label %102, label %111
102: ; preds = %98
%103 = load i32, i32* %6
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 %104
%106 = load i8*, i8** %105
%107 = call i32 @munmap(i8* %106, i32 65536)
br label %108
108: ; preds = %102
%109 = load i32, i32* %6
%110 = add nsw i32 %109, 1
store i32 %110, i32* %6
br label %98
111: ; preds = %98
%112 = load i8*, i8** %7
%113 = call i32 @free(i8* %112)
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [22 x i8] c"lg %d select return!\0A\00"
@str = [17 x i8] c"select time out!\00"
@str.3 = [14 x i8] c"select error!\00"
define i32 @lgx_test(i32* nocapture %0, i32 %1) {
%3 = alloca [4 x i8*]
%4 = alloca i32
%5 = alloca i64
%6 = bitcast i64* %5 to %struct.timeval*
%7 = bitcast [4 x i8*]* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i64* %5 to i8*
store i64 3, i64* %5
%10 = tail call i64 @malloc(i32 69632)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @memset(i8* %11, i32 0, i32 69632)
%13 = icmp sgt i32 %1, 0
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = call i32 @FD_ZERO(i32* %4)
br label %39
16: ; preds = %2
%17 = zext i32 %1 to i64
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ 0, %16 ], [ %26, %18 ]
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @MAP_SHARED
%22 = getelementptr inbounds i32, i32* %0, i64 %19
%23 = load i32, i32* %22
%24 = tail call i8* @mmap(i32* null, i32 65536, i32 %20, i32 %21, i32 %23, i32 0)
%25 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 %19
store i8* %24, i8** %25
%26 = add nuw nsw i64 %19, 1
%27 = icmp eq i64 %26, %17
br i1 %27, label %28, label %18
28: ; preds = %18
%29 = call i32 @FD_ZERO(i32* %4)
br i1 %13, label %30, label %39
30: ; preds = %28
%31 = zext i32 %1 to i64
br label %32
32: ; preds = %30, %32
%33 = phi i64 [ 0, %30 ], [ %37, %32 ]
%34 = getelementptr inbounds i32, i32* %0, i64 %33
%35 = load i32, i32* %34
%36 = call i32 @FD_SET(i32 %35, i32* %4)
%37 = add nuw nsw i64 %33, 1
%38 = icmp eq i64 %37, %31
br i1 %38, label %39, label %32
39: ; preds = %32, %14, %28
%40 = add nsw i32 %1, -1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %0, i64 %41
%43 = load i32, i32* %42
%44 = add nsw i32 %43, 1
%45 = call i32 @select(i32 %44, i32* %4, i32* null, i32* null, %struct.timeval* %6)
%46 = icmp slt i32 %45, 0
br i1 %46, label %67, label %47
47: ; preds = %39
%48 = icmp eq i32 %45, 0
br i1 %48, label %67, label %49
49: ; preds = %47
br i1 %13, label %50, label %80
50: ; preds = %49
%51 = zext i32 %1 to i64
br label %52
52: ; preds = %50, %64
%53 = phi i64 [ 0, %50 ], [ %65, %64 ]
%54 = getelementptr inbounds i32, i32* %0, i64 %53
%55 = load i32, i32* %54
%56 = call i64 @FD_ISSET(i32 %55, i32* %4)
%57 = icmp eq i64 %56, 0
br i1 %57, label %64, label %58
58: ; preds = %52
%59 = trunc i64 %53 to i32
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %59)
%61 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 %53
%62 = load i8*, i8** %61
%63 = call i32 @dump_mem(i8* %62, i32 20)
br label %64
64: ; preds = %52, %58
%65 = add nuw nsw i64 %53, 1
%66 = icmp eq i64 %65, %51
br i1 %66, label %70, label %52
67: ; preds = %47, %39
%68 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.3, i64 0, i64 0), %39 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0), %47 ]
%69 = call i32 @puts(i8* dereferenceable(1) %68)
br label %70
70: ; preds = %64, %67
br i1 %13, label %71, label %80
71: ; preds = %70
%72 = zext i32 %1 to i64
br label %73
73: ; preds = %71, %73
%74 = phi i64 [ 0, %71 ], [ %78, %73 ]
%75 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 %74
%76 = load i8*, i8** %75
%77 = call i32 @munmap(i8* %76, i32 65536)
%78 = add nuw nsw i64 %74, 1
%79 = icmp eq i64 %78, %72
br i1 %79, label %80, label %73
80: ; preds = %73, %49, %70
%81 = call i32 @free(i8* %11)
ret i32 0
}