#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*/ exit(int);
int /*<<< orphan*/ memfrob(char *, 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)
          exit(1);
        memfrob(buffer, len);
        write(s1, buffer, len);
      }
      if (FD_ISSET(s1, &rfds)) {
        memset(buffer, 0, 1024);
        if ((len = read(s1, buffer, 1024)) <= 0)
          exit(1);
        memfrob(buffer, len);
        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    134a <async_read+0x1aa>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12d1 <async_read+0x131>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <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     12b0 <async_read+0x110>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1030 <memfrob@plt>
mov    -0x8(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1345 <async_read+0x1a5>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <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     1324 <async_read+0x184>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1030 <memfrob@plt>
mov    -0x4(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <write@plt>
jmp    134a <async_read+0x1aa>
jmp    134f <async_read+0x1af>
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,%r14d
cmp    %esi,%edi
mov    %esi,%eax
cmovg  %edi,%eax
add    $0x1,%eax
mov    %eax,0x8(%rsp)
lea    0xc(%rsp),%r13
lea    0x10(%rsp),%rbx
nop
mov    %r13,%rdi
call   1080 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %r13,%rsi
call   10c0 <FD_SET@plt>
mov    %r15d,%edi
mov    %r13,%rsi
call   10c0 <FD_SET@plt>
movl   $0x1,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x8(%rsp),%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1090 <select@plt>
test   %eax,%eax
js     12d3 <async_read+0x133>
je     11d0 <async_read+0x30>
mov    %r14d,%edi
mov    %r13,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1273 <async_read+0xd3>
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    12ed <async_read+0x14d>
mov    %eax,%r12d
mov    %rbp,%rdi
mov    %eax,%esi
call   1030 <memfrob@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1040 <write@plt>
mov    %r15d,%edi
mov    %r13,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     11d0 <async_read+0x30>
mov    %rbx,%r12
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    12ed <async_read+0x14d>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <memfrob@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %r12,%rbx
jmp    11d0 <async_read+0x30>
lea    0xd26(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@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, %84
%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 %83
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 %61
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 %53
51: ; preds = %44
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %44
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%55 = load i32, i32* %8
%56 = call i32 @memfrob(i8* %54, i32 %55)
%57 = load i32, i32* %4
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%59 = load i32, i32* %8
%60 = call i32 @write(i32 %57, i8* %58, i32 %59)
br label %61
61: ; preds = %53, %40
%62 = load i32, i32* %4
%63 = call i64 @FD_ISSET(i32 %62, i32* %5)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %82
65: ; preds = %61
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%67 = call i32 @memset(i8* %66, i32 0, i32 1024)
%68 = load i32, i32* %4
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%70 = call i32 @read(i32 %68, i8* %69, i32 1024)
store i32 %70, i32* %8
%71 = icmp sle i32 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %65
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %65
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%76 = load i32, i32* %8
%77 = call i32 @memfrob(i8* %75, i32 %76)
%78 = load i32, i32* %3
%79 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%80 = load i32, i32* %8
%81 = call i32 @write(i32 %78, i8* %79, i32 %80)
br label %82
82: ; preds = %74, %61
br label %83
83: ; preds = %82, %37
br label %84
84: ; preds = %83
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, %48, %39
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 %39, 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 %36
34: ; preds = %30
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %30
%37 = call i32 @memfrob(i8* %10, i32 %32)
%38 = call i32 @write(i32 %1, i8* %10, i32 %32)
br label %39
39: ; preds = %36, %27
%40 = call i64 @FD_ISSET(i32 %1, i32* %3)
%41 = icmp eq i64 %40, 0
br i1 %41, label %26, label %42
42: ; preds = %39
%43 = call i32 @memset(i8* %10, i32 0, i32 1024)
%44 = call i32 @read(i32 %1, i8* %10, i32 1024)
%45 = icmp slt i32 %44, 1
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %42
%49 = call i32 @memfrob(i8* %10, i32 %44)
%50 = call i32 @write(i32 %0, i8* %10, i32 %44)
br label %26
}