#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 */
int EXIT_FAILURE;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ write(int, char *, int);
void async_read(int s, int s1) {
  fd_set rfds;
  struct timeval tv;
  int max = s > s1 ? s : s1;
  int len, r;
  char buffer[1024];
  max++;
  while (1) {
    FD_ZERO(&rfds);
    FD_SET(s, &rfds);
    FD_SET(s1, &rfds);
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    if ((r = select(max, &rfds, NULL, NULL, &tv)) < 0) {
      perror("select:");
      exit(EXIT_FAILURE);
    } else if (r > 0) {
      if (FD_ISSET(s, &rfds)) {
        memset(buffer, 0, 1024);
        if ((len = read(s, buffer, 1024)) <= 0) {
          close(s);
          close(s1);
          exit(1);
        }
        write(s1, buffer, len);
      }
      if (FD_ISSET(s1, &rfds)) {
        memset(buffer, 0, 1024);
        if ((len = read(s1, buffer, 1024)) <= 0) {
          close(s);
          close(s1);
          exit(1);
        }
        write(s, buffer, len);
      }
    }
  }
}


x86 assembly (clang, -O0):
<async_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    11cb <async_read+0x2b>
mov    -0x4(%rbp),%eax
mov    %eax,-0x434(%rbp)
jmp    11d4 <async_read+0x34>
mov    -0x8(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
lea    -0xc(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10c0 <FD_SET@plt>
movl   $0x1,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x24(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1090 <select@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1253 <async_read+0xb3>
lea    0xdc0(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    134c <async_read+0x1ac>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12d2 <async_read+0x132>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jg     12c0 <async_read+0x120>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1347 <async_read+0x1a7>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jg     1335 <async_read+0x195>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
jmp    134c <async_read+0x1ac>
jmp    1351 <async_read+0x1b1>
jmp    11e6 <async_read+0x46>

x86 assembly (clang, -O2):
<async_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %esi,%r15d
mov    %edi,%ebp
cmp    %esi,%edi
mov    %esi,%r14d
cmovg  %edi,%r14d
add    $0x1,%r14d
lea    0xc(%rsp),%rbx
lea    0x10(%rsp),%r12
lea    0x20(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
movl   $0x1,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1090 <select@plt>
test   %eax,%eax
js     12b0 <async_read+0x110>
je     11e0 <async_read+0x40>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1268 <async_read+0xc8>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    12ca <async_read+0x12a>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11e0 <async_read+0x40>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    12ca <async_read+0x12a>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    11e0 <async_read+0x40>
lea    0xd49(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [8 x i8] c"select:\00"
@EXIT_FAILURE = global i32 0
define void @async_read(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%11 = load i32, i32* %3
%12 = load i32, i32* %4
%13 = icmp sgt i32 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %3
br label %18
16: ; preds = %2
%17 = load i32, i32* %4
br label %18
18: ; preds = %16, %14
%19 = phi i32 [ %15, %14 ], [ %17, %16 ]
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = add nsw i32 %20, 1
store i32 %21, i32* %7
br label %22
22: ; preds = %18, %86
%23 = call i32 @FD_ZERO(i32* %5)
%24 = load i32, i32* %3
%25 = call i32 @FD_SET(i32 %24, i32* %5)
%26 = load i32, i32* %4
%27 = call i32 @FD_SET(i32 %26, i32* %5)
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 1, i32* %28
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 0, i64* %29
%30 = load i32, i32* %7
%31 = call i32 @select(i32 %30, i32* %5, i32* null, i32* null, %struct.timeval* %6)
store i32 %31, i32* %9
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %22
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %22
%38 = load i32, i32* %9
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %85
40: ; preds = %37
%41 = load i32, i32* %3
%42 = call i64 @FD_ISSET(i32 %41, i32* %5)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %62
44: ; preds = %40
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%46 = call i32 @memset(i8* %45, i32 0, i32 1024)
%47 = load i32, i32* %3
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%49 = call i32 @read(i32 %47, i8* %48, i32 1024)
store i32 %49, i32* %8
%50 = icmp sle i32 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %44
%52 = load i32, i32* %3
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %4
%55 = call i32 @close(i32 %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %44
%58 = load i32, i32* %4
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%60 = load i32, i32* %8
%61 = call i32 @write(i32 %58, i8* %59, i32 %60)
br label %62
62: ; preds = %57, %40
%63 = load i32, i32* %4
%64 = call i64 @FD_ISSET(i32 %63, i32* %5)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %84
66: ; preds = %62
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%68 = call i32 @memset(i8* %67, i32 0, i32 1024)
%69 = load i32, i32* %4
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%71 = call i32 @read(i32 %69, i8* %70, i32 1024)
store i32 %71, i32* %8
%72 = icmp sle i32 %71, 0
br i1 %72, label %73, label %79
73: ; preds = %66
%74 = load i32, i32* %3
%75 = call i32 @close(i32 %74)
%76 = load i32, i32* %4
%77 = call i32 @close(i32 %76)
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %66
%80 = load i32, i32* %3
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%82 = load i32, i32* %8
%83 = call i32 @write(i32 %80, i8* %81, i32 %82)
br label %84
84: ; preds = %79, %62
br label %85
85: ; preds = %84, %37
br label %86
86: ; preds = %85
br label %22
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [8 x i8] c"select:\00"
@EXIT_FAILURE = global i32 0
define void @async_read(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca [1024 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = icmp sgt i32 %0, %1
%9 = select i1 %8, i32 %0, i32 %1
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = add nsw i32 %9, 1
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
br label %14
14: ; preds = %26, %2
%15 = call i32 @FD_ZERO(i32* %3)
%16 = call i32 @FD_SET(i32 %0, i32* %3)
%17 = call i32 @FD_SET(i32 %1, i32* %3)
store i32 1, i32* %12
store i64 0, i64* %13
%18 = call i32 @select(i32 %11, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
%25 = icmp eq i32 %18, 0
br i1 %25, label %26, label %27
26: ; preds = %24, %51, %40
br label %14
27: ; preds = %24
%28 = call i64 @FD_ISSET(i32 %0, i32* %3)
%29 = icmp eq i64 %28, 0
br i1 %29, label %40, label %30
30: ; preds = %27
%31 = call i32 @memset(i8* %10, i32 0, i32 1024)
%32 = call i32 @read(i32 %0, i8* %10, i32 1024)
%33 = icmp slt i32 %32, 1
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = call i32 @close(i32 %0)
%36 = call i32 @close(i32 %1)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = call i32 @write(i32 %1, i8* %10, i32 %32)
br label %40
40: ; preds = %38, %27
%41 = call i64 @FD_ISSET(i32 %1, i32* %3)
%42 = icmp eq i64 %41, 0
br i1 %42, label %26, label %43
43: ; preds = %40
%44 = call i32 @memset(i8* %10, i32 0, i32 1024)
%45 = call i32 @read(i32 %1, i8* %10, i32 1024)
%46 = icmp slt i32 %45, 1
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = call i32 @close(i32 %0)
%49 = call i32 @close(i32 %1)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %43
%52 = call i32 @write(i32 %0, i8* %10, i32 %45)
br label %26
}