#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__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ read_frame(int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int mainloop(int fd) {
  while (1) {
    for (;;) {
      fd_set fds;
      struct timeval tv;
      int r;
      FD_ZERO(&fds);
      FD_SET(fd, &fds);
      tv.tv_sec = 2;
      tv.tv_usec = 0;
      r = select(fd + 1, &fds, NULL, NULL, &tv);
      if (-1 == r) {
        if (EINTR == errno)
          continue;
        perror("Fail to select");
        exit(EXIT_FAILURE);
      }
      if (0 == r) {
        fprintf(stderr, "select Timeout\n");
        exit(EXIT_FAILURE);
      }
      if (read_frame(fd))
        break;
    }
    sleep(5);
  }
  return 0;
}


x86 assembly (clang, -O0):
<mainloop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
jmp    1190 <mainloop+0x10>
lea    -0x8(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1090 <FD_SET@plt>
movl   $0x2,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    1218 <mainloop+0x98>
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
jne    11fe <mainloop+0x7e>
jmp    1190 <mainloop+0x10>
lea    0xdfb(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jne    1246 <mainloop+0xc6>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
call   1060 <read_frame@plt>
cmp    $0x0,%rax
je     125d <mainloop+0xdd>
jmp    1262 <mainloop+0xe2>
jmp    1190 <mainloop+0x10>
mov    $0x5,%edi
call   10a0 <sleep@plt>
jmp    118b <mainloop+0xb>

x86 assembly (clang, -O2):
<mainloop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
lea    0x1(%rbx),%r14d
lea    0x4(%rsp),%rbp
lea    0x8(%rsp),%r15
mov    0x2e23(%rip),%r12
mov    0x2e2c(%rip),%r13
jmp    11ba <mainloop+0x3a>
xchg   %ax,%ax
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1210 <mainloop+0x90>
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
movl   $0x2,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1050 <select@plt>
cmp    $0xffffffff,%eax
je     11b0 <mainloop+0x30>
test   %eax,%eax
je     121e <mainloop+0x9e>
mov    %ebx,%edi
call   1060 <read_frame@plt>
test   %rax,%rax
je     11ba <mainloop+0x3a>
mov    $0x5,%edi
call   10a0 <sleep@plt>
jmp    11ba <mainloop+0x3a>
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
jmp    1233 <mainloop+0xb3>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@EINTR = global i64 0
@errno = global i64 0
@.str = [15 x i8] c"Fail to select\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"select Timeout\0A\00"
define i32 @mainloop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i32
store i32 %0, i32* %2
br label %6
6: ; preds = %1, %41
br label %7
7: ; preds = %40, %22, %6
%8 = call i32 @FD_ZERO(i32* %3)
%9 = load i32, i32* %2
%10 = call i32 @FD_SET(i32 %9, i32* %3)
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i32 2, i32* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i64 0, i64* %12
%13 = load i32, i32* %2
%14 = add nsw i32 %13, 1
%15 = call i32 @select(i32 %14, i32* %3, i32* null, i32* null, %struct.timeval* %4)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp eq i32 -1, %16
br i1 %17, label %18, label %27
18: ; preds = %7
%19 = load i64, i64* @EINTR
%20 = load i64, i64* @errno
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
br label %7
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %7
%28 = load i32, i32* %5
%29 = icmp eq i32 0, %28
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %27
%36 = load i32, i32* %2
%37 = call i64 @read_frame(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
br label %41
40: ; preds = %35
br label %7
41: ; preds = %39
%42 = call i32 @sleep(i32 5)
br label %6
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@EINTR = global i64 0
@errno = global i64 0
@.str = [15 x i8] c"Fail to select\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"select Timeout\0A\00"
define i32 @mainloop(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%8 = add nsw i32 %0, 1
br label %9
9: ; preds = %30, %1
%10 = call i32 @FD_ZERO(i32* %2)
%11 = call i32 @FD_SET(i32 %0, i32* %2)
store i32 2, i32* %6
store i64 0, i64* %7
%12 = call i32 @select(i32 %8, i32* %2, i32* null, i32* null, %struct.timeval* %3)
switch i32 %12, label %26 [
i32 -1, label %13
i32 0, label %21
]
13: ; preds = %9
%14 = load i64, i64* @EINTR
%15 = load i64, i64* @errno
%16 = icmp eq i64 %14, %15
br i1 %16, label %29, label %17
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %9
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %9
%27 = call i64 @read_frame(i32 %0)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26, %13
br label %30
30: ; preds = %29, %31
br label %9
31: ; preds = %26
%32 = call i32 @sleep(i32 5)
br label %30
}